Player.java 7.1 KB

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