Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Game {
- private static Character character;
- public void getInput() {
- character = new Character();
- Vector2Float char_position = new Vector2Float(character.getSX(), character.getSY());
- Vector2Float motion = new Vector2Float();
- if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
- motion.add(new Vector2Float(0,1);
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_S)){
- motion.add(new Vector2Float(0,-1);
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_A)){
- motion.add(new Vector2Float(-1,0);
- }
- if (Keyboard.isKeyDown(Keyboard.KEY_D)){
- motion.add(new Vector2Float(1,0);
- }
- coll = new Collision(char_position, motion, this);
- if (!coll.getResult())
- {
- character.setPosition(char_position.add(motion));
- }
- }
- }
- public class Vector2Float
- {
- public float X, Y;
- public Vector2Float()
- {
- this.X = 0;
- this.Y = 0;
- }
- public Vector2Float(float x, float y)
- {
- this.X = x;
- this.Y = y;
- }
- public void setValues(float x, float y)
- {
- this.X = x;
- this.Y = y;
- }
- public float add(Vector2Float other)
- {
- return new Vector2Float(this.X + other.X, this.Y + other.Y);
- }
- public float getX()
- {
- return X;
- }
- public float getY()
- {
- return Y;
- }
- }
- public class Collision {
- private Character character;
- private Game game;
- private static Game default_game = new Game(); //may want to fix that
- public static float BORDER_TOP = 600;
- public static float BORDER_BOTTOM = 0;
- public static float BORDER_LEFT = 0;
- public static float BORDER_RIGHT = 800;
- /**
- * False if movement should be denied.
- */
- protected boolean result;
- /**
- * Change the boundaries of the arena.
- * @param topy - Y-ceiling.
- * @param boty - Y-floor.
- * @param rigx - X-ceiling.
- * @param lefx - X-floor.
- */
- public static void changeBounds(float topy, float boty, float lefx, float rigx)
- {
- BORDER_TOP = topy;
- BORDER_BOTTOM = boty;
- BORDER_LEFT = lefx;
- BORDER_RIGHT = rigx;
- }
- public Collision(Character player)
- {
- character = player;
- game = default_game;
- update();
- }
- public Collision(Vector2Float pos, Vector2Float motion, Game g)
- {
- game = g;
- update(pos,motion);
- }
- public boolean update()
- {
- result = true;
- pos_x = character.getSX();
- pos_y = character.getSY();
- if (pos_x < BORDER_LEFT) result = false;
- if (pos_x > BORDER_RIGHT) result = false;
- if (pos_y < BORDER_BOTTOM) result = false;
- if (pos_y > BORDER_TOP) result = false;
- if (!result) {
- return result;
- }
- /*
- CollisionBoundary[] objects = game.getAllCollisionObjects();
- for (CollisionBoundary bound : objects)
- {
- // ...
- }
- */
- return result;
- }
- public boolean getResult()
- {
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement