Advertisement
Guest User

Untitled

a guest
Dec 17th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1.  
  2. package learningAid.Entity;
  3.  
  4. import uwe.ac.uk.s2Vora.learningAid.TileMap.LoadGameMap.*;
  5. import java.awt.Graphics2D;
  6. import java.awt.image.BufferedImage;
  7. import java.io.InputStream;
  8. import java.util.ArrayList;
  9. import java.util.Properties;
  10. import javax.imageio.ImageIO;
  11.  
  12.  
  13. public class Player extends MapObject{
  14.  
  15. private int health;
  16. private int maxHealth = 5;
  17. private int fire;
  18. private int maxFire = 2500;
  19.  
  20.  
  21. //Scratch
  22. private boolean scratching;
  23.  
  24.  
  25.  
  26. //animations
  27. private ArrayList<BufferedImage[]> sprites;
  28. private final int[] numFrames = {2, 8, 1, 2, 4, 2, 5};
  29.  
  30. //animation indexes
  31. private static final int IDLE = 0;
  32. private static final int WALKING = 1;
  33. private static final int SCRATCHING = 6;
  34.  
  35.  
  36. public int getHealth(){ return health;}
  37. public int getMaxHealth(){ return maxHealth;}
  38. public int getFire(){return fire;}
  39. public int getMaxFire(){return maxFire;}
  40.  
  41. private double moveAmt = 40;
  42.  
  43.  
  44.  
  45. private static Player playerInstance;
  46.  
  47. public static Player getPlayerInstance(){
  48.  
  49. if(playerInstance == null){
  50. playerInstance = new Player();
  51. }
  52. return playerInstance;
  53. }
  54.  
  55.  
  56.  
  57.  
  58. private Player(){
  59.  
  60. width = 30;
  61. height = 30;
  62. cwidth = 20;
  63. cheight = 20;
  64.  
  65. moveSpeed = 1;
  66. maxSpeed = 10;
  67. stopSpeed = 0.4;
  68.  
  69. health = (maxHealth);
  70. fire = maxFire;
  71.  
  72.  
  73. //Load Sprites
  74. try{
  75.  
  76. Properties prop = new Properties();
  77. InputStream propertiesInputStream = getClass().getResourceAsStream("/Resources/config.properties");
  78. prop.load(propertiesInputStream);
  79.  
  80. BufferedImage spriteSheet = ImageIO.read(getClass().getResourceAsStream(prop.getProperty("playerSprites")));
  81.  
  82. sprites = new ArrayList<BufferedImage[]>();
  83.  
  84. for (int i = 0; i < 2; i++) {
  85. BufferedImage[] imageArray = new BufferedImage[numFrames[i]];
  86.  
  87. for (int j = 0; j < numFrames[i]; j++) {
  88. imageArray[j] = spriteSheet.getSubimage(j * width, i * height, width, height);
  89. }
  90.  
  91. sprites.add(imageArray);
  92. }
  93.  
  94. }catch(Exception e){
  95. e.printStackTrace();
  96. }
  97.  
  98. animation = new Animation();
  99.  
  100. //Start the player as idle
  101. currentAction = IDLE;
  102. animation.setFrames(sprites.get(IDLE));
  103. animation.setDelay(400);
  104.  
  105. playerMapPosision = new double[2];
  106. }
  107.  
  108. //Is this creating a memory Leak?
  109. private double[] playerMapPosision = null;
  110.  
  111. public void resetPlayer(){
  112. isColliding = false;
  113. playerReachedEnd = true;
  114. playerMapPosision[0] = 100;
  115. playerMapPosision[1] = 100;
  116. commandsHopper = new ArrayList<String>();
  117. moveCharacter(100, 100);
  118. }
  119.  
  120. private boolean pause = false;
  121.  
  122.  
  123. public void setGamePause(boolean pauseGame){
  124. pause = (pauseGame == true) ? true : false;
  125. }
  126.  
  127. private void moveCharacter(double x, double y){
  128. this.x = x;
  129. this.y = y;
  130. }
  131.  
  132. private boolean isMoving = false;
  133.  
  134. public boolean isPlayerMoving(){
  135. return isMoving;
  136. }
  137.  
  138. private boolean playerReachedEnd = true;
  139.  
  140. public boolean hasPlayerReachedTheEnd(){
  141. return playerReachedEnd;
  142. }
  143.  
  144. public void setPlayerReachedEnd(boolean reachedTheEnd){
  145. this.playerReachedEnd = reachedTheEnd;
  146. }
  147.  
  148. public void checkObstacleCollision(ArrayList<Obstacle> obs){
  149.  
  150. for (int i = 0; i < obs.size(); i++) {
  151.  
  152. Obstacle obstacle = obs.get(i);
  153.  
  154. if(intersects(obstacle)){
  155. System.out.println("hitting Obstacle in Player Class");
  156. }
  157.  
  158. }
  159. }
  160.  
  161.  
  162. public void addCommand(String command){
  163. commandsHopper.add(command); //commands added at the top of arraylist
  164. }
  165.  
  166. private void setMovementCommands(String moveDirection, int moveAmount){
  167.  
  168. for (int i = 0; i < moveAmount; i++) {
  169.  
  170. if(moveDirection.equals("up")){
  171.  
  172. addCommand("up");
  173.  
  174. } else if(moveDirection.equals("down")){
  175. addCommand("down");
  176.  
  177. } else if(moveDirection.equals("left")){
  178. addCommand("left");
  179.  
  180. } else if(moveDirection.equals("right")){
  181. addCommand("right");
  182. }
  183. }
  184.  
  185. getNextTarget();
  186.  
  187. }
  188.  
  189.  
  190. public void moveUp(int tilesToMove){
  191. setMovementCommands("up", tilesToMove);
  192. }
  193.  
  194. public void moveDown(int tilesToMove){
  195. setMovementCommands("down", tilesToMove);
  196. }
  197.  
  198. public void moveRight(int tilesToMove){
  199. setMovementCommands("right", tilesToMove);
  200. }
  201.  
  202. public void moveLeft(int tilesToMove){
  203. setMovementCommands("left", tilesToMove);
  204. }
  205.  
  206. public boolean isColliding(){
  207. return isColliding;
  208. }
  209.  
  210.  
  211. private double[] determineTargetFromCommand(String command){
  212.  
  213. double[] tempArray = new double[2];
  214.  
  215. if(command.equals("up")){
  216. tempArray[0] = x;
  217. tempArray[1] = y - moveAmt;
  218.  
  219. } else if(command.equals("down")){
  220.  
  221. tempArray[0] = x;
  222. tempArray[1] = y + moveAmt;
  223.  
  224. } else if(command.equals("left")){
  225.  
  226. tempArray[0] = x - moveAmt;
  227. tempArray[1] = y;
  228.  
  229. } else if(command.equals("right")){
  230.  
  231. tempArray[0] = x + moveAmt;
  232. tempArray[1] = y;
  233. }
  234.  
  235. return tempArray;
  236.  
  237. }
  238.  
  239.  
  240. ArrayList<String> commandsHopper = new ArrayList<String>();
  241.  
  242.  
  243. public void getNextTarget(){
  244. if (!commandsHopper.isEmpty()){
  245. playerMapPosision = determineTargetFromCommand(commandsHopper.get(0));
  246. }else{
  247. playerReachedEnd = true;
  248. }
  249. }
  250.  
  251. public void updatePosition(){
  252.  
  253. double targetX = playerMapPosision[0];
  254. double targetY = playerMapPosision[1];
  255.  
  256. if(targetX > 0 && targetY > 0 && !commandsHopper.isEmpty() && pause == false){
  257.  
  258. isMoving = true;
  259.  
  260. if(y > targetY) {
  261.  
  262. moveCharacter(x, y - moveSpeed);
  263.  
  264. }else if(y < targetY) {
  265.  
  266. moveCharacter(x, y + moveSpeed);
  267.  
  268. }else if(x > targetX) {
  269.  
  270. moveCharacter(x - moveSpeed, y);
  271.  
  272. }else if(x < targetX) {
  273. moveCharacter(x + moveSpeed, y);
  274. }
  275.  
  276. if(y <= targetY && y >= targetY && x >= targetX && x <= targetX && playerReachedEnd == false || isColliding == true){
  277. commandsHopper.remove(0);
  278. getNextTarget();
  279. }
  280. }else{
  281. isMoving = false;
  282. }
  283.  
  284. }
  285.  
  286.  
  287. public void update(){
  288.  
  289. //update position
  290. checkTileMapCollision();
  291. updatePosition();
  292.  
  293. //set Animations
  294. if(scratching){
  295. if(currentAction != SCRATCHING){
  296. currentAction = SCRATCHING;
  297. animation.setFrames(sprites.get(SCRATCHING));
  298. animation.setDelay(50);
  299. width = 60;
  300. }
  301. }else if(left || right){
  302. if(currentAction != WALKING){
  303. currentAction = WALKING;
  304. animation.setFrames(sprites.get(WALKING));
  305. animation.setDelay(40);
  306. width = 30;
  307. }
  308. }else{
  309. if(currentAction != IDLE){
  310. currentAction = IDLE;
  311. animation.setFrames(sprites.get(IDLE));
  312. animation.setDelay(400);
  313. width = 30;
  314. }
  315. }
  316.  
  317.  
  318. animation.update();
  319. }
  320.  
  321.  
  322.  
  323. public void draw(Graphics2D g){
  324. setMapPosition();
  325.  
  326. //draw Player
  327. g.drawImage(animation.getImage(),
  328. (int)(x + xmap - width / 2),
  329. (int)(y + ymap - height / 2),
  330. null);
  331.  
  332.  
  333. }
  334.  
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement