Player.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package pathgame.gameplay;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import pathgame.tilemap.Tile;
  5. import pathgame.tilemap.TileMap;
  6. public class Player
  7. {
  8. private static final float SPEED = 0.25f;
  9. private PlayerAbilities abilities = PlayerAbilities.NORMAL;
  10. private float lastX = 0;
  11. private float lastY = 0;
  12. private float x = 0;
  13. private float y = 0;
  14. private float velX = 0;
  15. private float velY = 0;
  16. private boolean isMoving = false;
  17. private int currSpeedSlowdown = 1;
  18. private boolean isSailing = false;
  19. private boolean hasWon = false;
  20. private int energySupply;
  21. private int energyUsed = 0;
  22. private int objectivesAmount;
  23. private int objectivesVisited = 0;
  24. private final LinkedList<MinusStepsValues> steps = new LinkedList<>();
  25. private Tile currentTile;// = Tiles.GRASS;
  26. public Player()
  27. {
  28. }
  29. public float getLastX()
  30. {
  31. return lastX;
  32. }
  33. public float getLastY()
  34. {
  35. return lastY;
  36. }
  37. public float getX()
  38. {
  39. return x;
  40. }
  41. public float getY()
  42. {
  43. return y;
  44. }
  45. private void tickSteps()
  46. {
  47. Iterator<MinusStepsValues> iter = steps.iterator();
  48. while(iter.hasNext())
  49. {
  50. MinusStepsValues next = iter.next();
  51. if(next.tick())
  52. {
  53. iter.remove();
  54. }
  55. }
  56. }
  57. public void tick(TileMap map)
  58. {
  59. tickSteps();
  60. int currentTileX = Math.round(x);
  61. int currentTileY = Math.round(y);
  62. currentTile = map.getTile(currentTileX, currentTileY);
  63. currSpeedSlowdown = currentTile.getEnergyCost(this);
  64. lastX = x;
  65. lastY = y;
  66. if(isOnTile())
  67. {
  68. velX = 0.0f;
  69. velY = 0.0f;
  70. if(isMoving)
  71. {
  72. currentTile.onEnter(this, map, currentTileX, currentTileY);
  73. steps.addLast(new MinusStepsValues(currSpeedSlowdown));
  74. energyUsed += currSpeedSlowdown;
  75. }
  76. isMoving = false;
  77. currentTile.isStandingOn(this, map, currentTileX, currentTileY);
  78. }
  79. if(Keys.LEFT_KEY.isDown() && !isMoving && currentTileX > 0 && !map.getTile(currentTileX - 1, currentTileY).isBlockingMovement(this))
  80. {
  81. velX = -SPEED;
  82. isMoving = true;
  83. currentTile.onLeave(this, map, currentTileX, currentTileY);
  84. }
  85. else if(Keys.RIGHT_KEY.isDown() && !isMoving && currentTileX < map.getWidth() - 1 && !map.getTile(currentTileX + 1, currentTileY).isBlockingMovement(this))
  86. {
  87. velX = SPEED;
  88. isMoving = true;
  89. currentTile.onLeave(this, map, currentTileX, currentTileY);
  90. }
  91. else if(Keys.UP_KEY.isDown() && !isMoving && currentTileY > 0 && !map.getTile(currentTileX, currentTileY - 1).isBlockingMovement(this))
  92. {
  93. velY = -SPEED;
  94. isMoving = true;
  95. currentTile.onLeave(this, map, currentTileX, currentTileY);
  96. }
  97. else if(Keys.DOWN_KEY.isDown() && !isMoving && currentTileY < map.getHeight() - 1 && !map.getTile(currentTileX, currentTileY + 1).isBlockingMovement(this))
  98. {
  99. velY = SPEED;
  100. isMoving = true;
  101. currentTile.onLeave(this, map, currentTileX, currentTileY);
  102. }
  103. float moveX = Math.abs(velX / currSpeedSlowdown);
  104. float moveY = Math.abs(velY / currSpeedSlowdown);
  105. if(velX < 0.0f)
  106. {
  107. float d = x - (float) Math.floor(x);
  108. if(d < 0.01f)
  109. {
  110. d = 1.0f;
  111. }
  112. moveX = -Math.min(moveX, d);
  113. }
  114. else if(velX > 0.0f)
  115. {
  116. float d = (float) Math.ceil(x) - x;
  117. if(d < 0.01f)
  118. {
  119. d = 1.0f;
  120. }
  121. moveX = Math.min(moveX, d);
  122. }
  123. else if(velY < 0.0f)
  124. {
  125. float d = y - (float) Math.floor(y);
  126. if(d < 0.01f)
  127. {
  128. d = 1.0f;
  129. }
  130. moveY = -Math.min(moveY, d);
  131. }
  132. else if(velY > 0.0f)
  133. {
  134. float d = (float) Math.ceil(y) - y;
  135. if(d < 0.01f)
  136. {
  137. d = 1.0f;
  138. }
  139. moveY = Math.min(moveY, d);
  140. }
  141. x += moveX;
  142. y += moveY;
  143. }
  144. private boolean isOnTile()
  145. {
  146. return Math.abs(x - Math.round(x)) < 0.01f && Math.abs(y - Math.round(y)) < 0.01f;
  147. }
  148. public float getVelX()
  149. {
  150. return velX;
  151. }
  152. public float getVelY()
  153. {
  154. return velY;
  155. }
  156. public int getEnergySupply()
  157. {
  158. return energySupply;
  159. }
  160. public int getEnergyLeft()
  161. {
  162. return energySupply - energyUsed;
  163. }
  164. public int getEnergyUsed()
  165. {
  166. return energyUsed;
  167. }
  168. public int getObjectivesAmount()
  169. {
  170. return objectivesAmount;
  171. }
  172. public void setObjectivesAmount(int objectivesAmount)
  173. {
  174. this.objectivesAmount = objectivesAmount;
  175. }
  176. public int getObjectivesVisited()
  177. {
  178. return objectivesVisited;
  179. }
  180. public void visitTown()
  181. {
  182. objectivesVisited++;
  183. }
  184. public LinkedList<MinusStepsValues> getLastSteps()
  185. {
  186. return steps;
  187. }
  188. public void setAbilities(PlayerAbilities playerAbilities)
  189. {
  190. abilities = playerAbilities;
  191. }
  192. public PlayerAbilities getAbilities()
  193. {
  194. return abilities;
  195. }
  196. public void setEnergySupply(int energySupply)
  197. {
  198. this.energySupply = energySupply;
  199. }
  200. public boolean canWin()
  201. {
  202. return objectivesVisited >= objectivesAmount;
  203. }
  204. public boolean hasWon()
  205. {
  206. return hasWon;
  207. }
  208. public void win()
  209. {
  210. this.hasWon = true;
  211. }
  212. public boolean hasLost()
  213. {
  214. return energyUsed >= energySupply;
  215. }
  216. public void reset(int sx, int sy, int energySupply, int objectivesAmount)
  217. {
  218. lastX = sx;
  219. lastY = sy;
  220. x = sx;
  221. y = sy;
  222. velX = 0;
  223. velY = 0;
  224. isMoving = false;
  225. currSpeedSlowdown = 1;
  226. hasWon = false;
  227. energyUsed = 0;
  228. isSailing = false;
  229. objectivesVisited = 0;
  230. steps.clear();
  231. this.energySupply = energySupply;
  232. this.objectivesAmount = objectivesAmount;
  233. }
  234. public void reset(int sx, int sy)
  235. {
  236. reset(sx, sy, 1000, 10);
  237. }
  238. public boolean isMoving()
  239. {
  240. return isMoving;
  241. }
  242. public void switchSailing()
  243. {
  244. isSailing = !isSailing;
  245. }
  246. public boolean isSailing()
  247. {
  248. return isSailing;
  249. }
  250. public Tile getCurrTile()
  251. {
  252. return currentTile;
  253. }
  254. }