Advertisement
riking

Untitled

Oct 6th, 2012
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. public class Game {
  2.     private static Character character;
  3.     public void getInput() {
  4.         character = new Character();
  5.                 Vector2Float char_position = new Vector2Float(character.getSX(), character.getSY());
  6.         Vector2Float motion = new Vector2Float();
  7.        
  8.         if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
  9.             motion.add(new Vector2Float(0,1);
  10.         }
  11.         if (Keyboard.isKeyDown(Keyboard.KEY_S)){
  12.             motion.add(new Vector2Float(0,-1);
  13.         }
  14.         if (Keyboard.isKeyDown(Keyboard.KEY_A)){
  15.             motion.add(new Vector2Float(-1,0);
  16.         }
  17.         if (Keyboard.isKeyDown(Keyboard.KEY_D)){
  18.             motion.add(new Vector2Float(1,0);
  19.         }
  20.  
  21.         coll = new Collision(char_position, motion, this);
  22.         if (!coll.getResult())
  23.         {
  24.             character.setPosition(char_position.add(motion));
  25.         }
  26.     }
  27. }
  28.  
  29. public class Vector2Float
  30. {
  31.     public float X, Y;
  32.     public Vector2Float()
  33.     {
  34.       this.X = 0;
  35.       this.Y = 0;
  36.     }
  37.     public Vector2Float(float x, float y)
  38.     {
  39.       this.X = x;
  40.       this.Y = y;
  41.     }
  42.     public void setValues(float x, float y)
  43.     {
  44.       this.X = x;
  45.       this.Y = y;
  46.     }
  47.     public float add(Vector2Float other)
  48.     {
  49.       return new Vector2Float(this.X + other.X, this.Y + other.Y);
  50.     }
  51.     public float getX()
  52.     {
  53.       return X;
  54.     }
  55.     public float getY()
  56.     {
  57.       return Y;
  58.     }
  59. }
  60.  
  61. public class Collision {
  62.     private Character character;
  63.     private Game game;
  64.     private static Game default_game = new Game(); //may want to fix that
  65.     public static float BORDER_TOP = 600;
  66.     public static float BORDER_BOTTOM = 0;
  67.     public static float BORDER_LEFT = 0;
  68.     public static float BORDER_RIGHT = 800;
  69.     /**
  70.      * False if movement should be denied.
  71.      */
  72.     protected boolean result;
  73.     /**
  74.      * Change the boundaries of the arena.
  75.      * @param topy - Y-ceiling.
  76.      * @param boty - Y-floor.
  77.      * @param rigx - X-ceiling.
  78.      * @param lefx - X-floor.
  79.      */
  80.     public static void changeBounds(float topy, float boty, float lefx, float rigx)
  81.     {
  82.     BORDER_TOP = topy;
  83.     BORDER_BOTTOM = boty;
  84.     BORDER_LEFT = lefx;
  85.     BORDER_RIGHT = rigx;
  86.     }
  87.  
  88.     public Collision(Character player)
  89.     {
  90.         character = player;
  91.         game = default_game;
  92.         update();
  93.     }
  94.     public Collision(Vector2Float pos, Vector2Float motion, Game g)
  95.     {
  96.         game = g;
  97.         update(pos,motion);
  98.     }
  99.     public boolean update()
  100.     {
  101.         result = true;
  102.         pos_x = character.getSX();
  103.         pos_y = character.getSY();
  104.         if (pos_x < BORDER_LEFT) result = false;
  105.         if (pos_x > BORDER_RIGHT) result = false;
  106.         if (pos_y < BORDER_BOTTOM) result = false;
  107.         if (pos_y > BORDER_TOP) result = false;
  108.  
  109.         if (!result) {
  110.             return result;
  111.         }
  112.         /*
  113.         CollisionBoundary[] objects = game.getAllCollisionObjects();
  114.         for (CollisionBoundary bound : objects)
  115.         {
  116.             // ...
  117.         }
  118.         */
  119.         return result;
  120.     }
  121.     public boolean getResult()
  122.     {
  123.         return result;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement