Player.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package model.objects;
  2. import java.awt.image.BufferedImage;
  3. import java.util.ArrayList;
  4. import main.Window;
  5. public class Player extends DrawObject {
  6. private int score, health, speed = 10;
  7. private ArrayList<PowerUp> powerups;
  8. private long lastMovement;
  9. private int lastDirection, frame;
  10. private BufferedImage spriteimage;
  11. private String name;
  12. public Player(BufferedImage image, int x, int y, String name) {
  13. super(image.getSubimage(38, 0, 40, 54));
  14. super.setPosition(x, y);
  15. this.name = name;
  16. spriteimage = image;
  17. health = 5;
  18. powerups = new ArrayList<PowerUp>();
  19. }
  20. public void update(){
  21. if(System.currentTimeMillis() - lastMovement >= 100){ //If the player didn't move for 200ms, look forward
  22. image = spriteimage.getSubimage(38, 0, 40, 54);
  23. frame = 0;
  24. }else{ //Look into the right walking direction
  25. frame++;
  26. frame %= 1000;
  27. image = spriteimage.getSubimage(2+(((frame / 4) % 4) * 38), 54*lastDirection, 38, 50);
  28. }
  29. }
  30. public void walkLeft(){
  31. if(getX() -30 - speed > 0){
  32. setX(getX()-speed);
  33. lastDirection = 1;
  34. lastMovement = System.currentTimeMillis();
  35. }
  36. }
  37. public void walkRight(){
  38. if(getX() + 30 + getWidth() + speed < Window.WIDTH){
  39. setX(getX()+speed);
  40. lastDirection = 2;
  41. lastMovement = System.currentTimeMillis();
  42. }
  43. }
  44. //** Getters and Setters **//
  45. public String getName(){
  46. return name;
  47. }
  48. public int getScore() {
  49. return score;
  50. }
  51. public int getHealth() {
  52. return health;
  53. }
  54. }