Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1.     float speed = 4f;
  2.     Vec2 velocity = new Vec2(0, 0);
  3.  
  4.         public void move(double d, double e){
  5.                 this.x += d;
  6.                 this.y += e;
  7.         }
  8.  
  9.         public void checkPlayerMove(GameKeyboard keyboard){
  10.  
  11.                 /*if(keyboard.pressedKeys.contains((Integer) Keyboard.KEY_W)
  12.                 && keyboard.pressedKeys.contains((Integer) Keyboard.KEY_A)){
  13.                         this.move(-speed / Math.sqrt(2), speed / Math.sqrt(2));
  14.                 }
  15.  
  16.                 else if(keyboard.pressedKeys.contains((Integer) Keyboard.KEY_W)
  17.                 && keyboard.pressedKeys.contains((Integer)      Keyboard.KEY_D)){
  18.                         this.move(speed / Math.sqrt(2), speed / Math.sqrt(2));
  19.                 }
  20.  
  21.                 else if(keyboard.pressedKeys.contains((Integer) Keyboard.KEY_S)
  22.                 && keyboard.pressedKeys.contains((Integer) Keyboard.KEY_A)){
  23.                         this.move(-speed / Math.sqrt(2), -speed / Math.sqrt(2));
  24.                 }
  25.  
  26.                 else if(keyboard.pressedKeys.contains((Integer) Keyboard.KEY_S)
  27.                 && keyboard.pressedKeys.contains((Integer) Keyboard.KEY_D)){
  28.                         this.move(speed / Math.sqrt(2), -speed / Math.sqrt(2));
  29.                 }
  30.  
  31.                 else if(keyboard.pressedKeys.contains((Integer) Keyboard.KEY_W)){
  32.                         this.move(0, speed);
  33.                 }
  34.  
  35.                 else if(keyboard.pressedKeys.contains((Integer) Keyboard.KEY_A)){
  36.                         this.move(-speed, 0);
  37.                 }
  38.  
  39.                 else if(keyboard.pressedKeys.contains((Integer) Keyboard.KEY_S)){
  40.                         this.move(0, -speed);
  41.                 }
  42.  
  43.                 else if(keyboard.pressedKeys.contains((Integer) Keyboard.KEY_D)){
  44.                         this.move(speed, 0);
  45.                 }*/
  46.         //These two commented and uncommented code blocks yield the same result
  47.         velocity = new Vec2(0, 0);
  48.         if (/*pressed left*/) {
  49.              velocity = new Vec2(velocity.x - speed, velocity.y);
  50.         }
  51.         if (/*pressed right*/) {
  52.             velocity = new Vec2(velocity.x + speed, velocity.y);
  53.         }
  54.         if (/*pressed up*/) {
  55.              velocity = new Vec2(velocity.x, velocity.y - speed);
  56.         }
  57.         if (/*pressed down*/) {
  58.              velocity = new Vec2(velocity.x, velocity.y + speed);
  59.         }
  60.         velocity = velocity.normalize();
  61.         //Slick2D's library didn't have the .mul() method, so I instead increased the velocity by speed
  62.         x += velocity.x;
  63.         y += velocity.y;
  64.  
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement