Guest User

Untitled

a guest
Oct 4th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | Source Code | 0 0
  1. package core;
  2.  
  3. import controller.Controller;
  4.  
  5. public class Motion {
  6.    
  7.     private Vector2D vector;
  8.     private double speed;
  9.    
  10.     public Motion(double speed) {
  11.         this.vector = new Vector2D(0, 0);
  12.         this.speed = speed;
  13.     }
  14.    
  15.     public void update(Controller controller, Direction direction) {
  16.         movement(controller, direction);
  17.         vector.normalize();
  18.         vector.multiply(speed);
  19.     }
  20.  
  21.     private void movement(Controller controller, Direction direction) {
  22.         int deltaX = 0, deltaY = 0;
  23.        
  24.         if(controller.isRequestingUp()) {deltaY--; deltaX = 0;}
  25.         if(controller.isRequestingDown()) {deltaY++; deltaX = 0;}
  26.         if(controller.isRequestingLeft()) {deltaX--; deltaY = 0;}
  27.         if(controller.isRequestingRight()) {deltaX++; deltaY = 0;} 
  28.         //For Overriding moves
  29.         if(controller.getRequestedDirection() == Direction.N) {deltaY--; deltaX = 0;}
  30.         if(controller.getRequestedDirection() == Direction.S) {deltaY++; deltaX = 0;}
  31.         if(controller.getRequestedDirection() == Direction.W) {deltaX--; deltaY = 0;}
  32.         if(controller.getRequestedDirection() == Direction.E) {deltaX++; deltaY = 0;}
  33.        
  34.         vector = new Vector2D(deltaX, deltaY);
  35.     }
  36.    
  37.     public Vector2D getVector() {
  38.         return vector;
  39.     }
  40.  
  41.     public boolean isMoving() {
  42.         return vector.length() > 0;
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment