| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package model.objects;
- 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 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);
- 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();
- }
- }
-
- //** Getters and Setters **//
-
- public String getName(){
- return name;
- }
- public int getScore() {
- return score;
- }
- public int getHealth() {
- return health;
- }
-
- }
|