| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package model.objects;
- import java.awt.Point;
- import java.awt.geom.Point2D;
- import java.awt.geom.Point2D.Double;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import main.Window;
- public class Player extends DrawObject {
- private int score, health, speed = 10;
- private ArrayList<PowerUp> powerups;
-
- private long lastMovement;
- private int lastDirection, frame;
- private Double beginlocation;
- private BufferedImage spriteimage;
- private String name;
-
- public Player(BufferedImage image, int x, int y, String name) {
- super(image.getSubimage(38, 0, 40, 54));
- super.setPosition(x, y);
- beginlocation = new Point2D.Double(x,y);
- this.name = name;
- spriteimage = image;
- health = 5;
- powerups = new ArrayList<PowerUp>();
- }
-
- public void update(){
- if(System.currentTimeMillis() - lastMovement >= 100){ //If the player didn't move for 200ms, look forward
- image = spriteimage.getSubimage(38, 0, 40, 54);
- frame = 0;
- }else{ //Look into the right walking direction
- frame++;
- frame %= 1000;
- image = spriteimage.getSubimage(2+(((frame / 4) % 4) * 38), 54*lastDirection, 38, 50);
- }
- }
-
- public void walkLeft(){
- if(getX() -30 - speed > 0){
- setX(getX()-speed);
- lastDirection = 1;
- lastMovement = System.currentTimeMillis();
- }
- }
-
- public void walkRight(){
- if(getX() + 30 + getWidth() + speed < Window.WIDTH){
- setX(getX()+speed);
- lastDirection = 2;
- lastMovement = System.currentTimeMillis();
- }
- }
-
- public void reset(){
- setPosition(beginlocation);
- speed = 10;
- }
-
- //** Getters and Setters **//
-
- public String getName(){
- return name;
- }
- public int getScore() {
- return score;
- }
- public int getHealth() {
- return health;
- }
-
- public void setHealth(int health){
- this.health = health;
- }
-
- }
|