Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package learningAid.Entity;
- import uwe.ac.uk.s2Vora.learningAid.TileMap.LoadGameMap.*;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Properties;
- import javax.imageio.ImageIO;
- public class Player extends MapObject{
- private int health;
- private int maxHealth = 5;
- private int fire;
- private int maxFire = 2500;
- //Scratch
- private boolean scratching;
- //animations
- private ArrayList<BufferedImage[]> sprites;
- private final int[] numFrames = {2, 8, 1, 2, 4, 2, 5};
- //animation indexes
- private static final int IDLE = 0;
- private static final int WALKING = 1;
- private static final int SCRATCHING = 6;
- public int getHealth(){ return health;}
- public int getMaxHealth(){ return maxHealth;}
- public int getFire(){return fire;}
- public int getMaxFire(){return maxFire;}
- private double moveAmt = 40;
- private static Player playerInstance;
- public static Player getPlayerInstance(){
- if(playerInstance == null){
- playerInstance = new Player();
- }
- return playerInstance;
- }
- private Player(){
- width = 30;
- height = 30;
- cwidth = 20;
- cheight = 20;
- moveSpeed = 1;
- maxSpeed = 10;
- stopSpeed = 0.4;
- health = (maxHealth);
- fire = maxFire;
- //Load Sprites
- try{
- Properties prop = new Properties();
- InputStream propertiesInputStream = getClass().getResourceAsStream("/Resources/config.properties");
- prop.load(propertiesInputStream);
- BufferedImage spriteSheet = ImageIO.read(getClass().getResourceAsStream(prop.getProperty("playerSprites")));
- sprites = new ArrayList<BufferedImage[]>();
- for (int i = 0; i < 2; i++) {
- BufferedImage[] imageArray = new BufferedImage[numFrames[i]];
- for (int j = 0; j < numFrames[i]; j++) {
- imageArray[j] = spriteSheet.getSubimage(j * width, i * height, width, height);
- }
- sprites.add(imageArray);
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- animation = new Animation();
- //Start the player as idle
- currentAction = IDLE;
- animation.setFrames(sprites.get(IDLE));
- animation.setDelay(400);
- playerMapPosision = new double[2];
- }
- //Is this creating a memory Leak?
- private double[] playerMapPosision = null;
- public void resetPlayer(){
- isColliding = false;
- playerReachedEnd = true;
- playerMapPosision[0] = 100;
- playerMapPosision[1] = 100;
- commandsHopper = new ArrayList<String>();
- moveCharacter(100, 100);
- }
- private boolean pause = false;
- public void setGamePause(boolean pauseGame){
- pause = (pauseGame == true) ? true : false;
- }
- private void moveCharacter(double x, double y){
- this.x = x;
- this.y = y;
- }
- private boolean isMoving = false;
- public boolean isPlayerMoving(){
- return isMoving;
- }
- private boolean playerReachedEnd = true;
- public boolean hasPlayerReachedTheEnd(){
- return playerReachedEnd;
- }
- public void setPlayerReachedEnd(boolean reachedTheEnd){
- this.playerReachedEnd = reachedTheEnd;
- }
- public void checkObstacleCollision(ArrayList<Obstacle> obs){
- for (int i = 0; i < obs.size(); i++) {
- Obstacle obstacle = obs.get(i);
- if(intersects(obstacle)){
- System.out.println("hitting Obstacle in Player Class");
- }
- }
- }
- public void addCommand(String command){
- commandsHopper.add(command); //commands added at the top of arraylist
- }
- private void setMovementCommands(String moveDirection, int moveAmount){
- for (int i = 0; i < moveAmount; i++) {
- if(moveDirection.equals("up")){
- addCommand("up");
- } else if(moveDirection.equals("down")){
- addCommand("down");
- } else if(moveDirection.equals("left")){
- addCommand("left");
- } else if(moveDirection.equals("right")){
- addCommand("right");
- }
- }
- getNextTarget();
- }
- public void moveUp(int tilesToMove){
- setMovementCommands("up", tilesToMove);
- }
- public void moveDown(int tilesToMove){
- setMovementCommands("down", tilesToMove);
- }
- public void moveRight(int tilesToMove){
- setMovementCommands("right", tilesToMove);
- }
- public void moveLeft(int tilesToMove){
- setMovementCommands("left", tilesToMove);
- }
- public boolean isColliding(){
- return isColliding;
- }
- private double[] determineTargetFromCommand(String command){
- double[] tempArray = new double[2];
- if(command.equals("up")){
- tempArray[0] = x;
- tempArray[1] = y - moveAmt;
- } else if(command.equals("down")){
- tempArray[0] = x;
- tempArray[1] = y + moveAmt;
- } else if(command.equals("left")){
- tempArray[0] = x - moveAmt;
- tempArray[1] = y;
- } else if(command.equals("right")){
- tempArray[0] = x + moveAmt;
- tempArray[1] = y;
- }
- return tempArray;
- }
- ArrayList<String> commandsHopper = new ArrayList<String>();
- public void getNextTarget(){
- if (!commandsHopper.isEmpty()){
- playerMapPosision = determineTargetFromCommand(commandsHopper.get(0));
- }else{
- playerReachedEnd = true;
- }
- }
- public void updatePosition(){
- double targetX = playerMapPosision[0];
- double targetY = playerMapPosision[1];
- if(targetX > 0 && targetY > 0 && !commandsHopper.isEmpty() && pause == false){
- isMoving = true;
- if(y > targetY) {
- moveCharacter(x, y - moveSpeed);
- }else if(y < targetY) {
- moveCharacter(x, y + moveSpeed);
- }else if(x > targetX) {
- moveCharacter(x - moveSpeed, y);
- }else if(x < targetX) {
- moveCharacter(x + moveSpeed, y);
- }
- if(y <= targetY && y >= targetY && x >= targetX && x <= targetX && playerReachedEnd == false || isColliding == true){
- commandsHopper.remove(0);
- getNextTarget();
- }
- }else{
- isMoving = false;
- }
- }
- public void update(){
- //update position
- checkTileMapCollision();
- updatePosition();
- //set Animations
- if(scratching){
- if(currentAction != SCRATCHING){
- currentAction = SCRATCHING;
- animation.setFrames(sprites.get(SCRATCHING));
- animation.setDelay(50);
- width = 60;
- }
- }else if(left || right){
- if(currentAction != WALKING){
- currentAction = WALKING;
- animation.setFrames(sprites.get(WALKING));
- animation.setDelay(40);
- width = 30;
- }
- }else{
- if(currentAction != IDLE){
- currentAction = IDLE;
- animation.setFrames(sprites.get(IDLE));
- animation.setDelay(400);
- width = 30;
- }
- }
- animation.update();
- }
- public void draw(Graphics2D g){
- setMapPosition();
- //draw Player
- g.drawImage(animation.getImage(),
- (int)(x + xmap - width / 2),
- (int)(y + ymap - height / 2),
- null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement