Advertisement
Guest User

Untitled

a guest
Oct 4th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | Source Code | 0 0
  1. package entity;
  2.  
  3.  
  4. import java.awt.Image;
  5.  
  6. import controller.Controller;
  7. import controller.PlayerController;
  8. import core.Direction;
  9. import core.Motion;
  10. import core.Position;
  11. import game.state.State;
  12. import gfx.AnimationManager;
  13. import gfx.SpriteLibrary;
  14.  
  15. public abstract class MovingEntity extends GameObject {
  16.    
  17.     public static int PROXIMITY_RANGE = 5;
  18.    
  19.     protected Controller controller;
  20.     protected Motion motion;
  21.     protected AnimationManager animationManager;
  22.     protected Direction direction;
  23.  
  24.     public MovingEntity(Controller controller, SpriteLibrary spriteLibrary, Position startingPosition) {
  25.         super();
  26.         super.position = startingPosition;
  27.         homePosition = startingPosition;
  28.         this.controller = controller;
  29.         this.motion = new Motion(3);
  30.         this.direction = Direction.S;
  31.         this.animationManager = new AnimationManager(spriteLibrary.getEntity("bouly"));
  32.     }
  33.    
  34.     @Override
  35.     public void update(State state) {
  36.         motion.update(controller, direction);
  37.         position.apply(motion);
  38.         manageDirection();
  39.         decideAnimation();
  40.         animationManager.update(direction);
  41.     }
  42.    
  43.     private void decideAnimation() {
  44.         if(motion.isMoving() && controller.getClass().getName() == PlayerController.class.getName()) {
  45.             animationManager.playAnimation("walking", 3, 4);
  46.         } else if(motion.isMoving()){
  47.             animationManager.playAnimation("walking", 5, 0);
  48.         } else
  49.             animationManager.playAnimation("idle", 10, 0);
  50.     }
  51.  
  52.     private void manageDirection() {
  53.         if(motion.isMoving())
  54.             this.direction = Direction.fromMotion(motion);
  55.     }
  56.  
  57.     @Override
  58.     public Image getSprite() {
  59.         return animationManager.getSprite();
  60.     }
  61.    
  62.     public Controller getController() {
  63.         return controller;
  64.     }
  65.    
  66.     public Position getHomePosition() {
  67.         return homePosition;
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement