Player.java 1.7 KB

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