Player.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. /**
  8. * A container for holding everything about the player
  9. *
  10. * @author julia
  11. */
  12. public class Player
  13. {
  14. private static final float SPEED = 0.25f;
  15. private PlayerAbilities abilities = PlayerAbilities.NORMAL;
  16. private float lastX = 0;
  17. private float lastY = 0;
  18. private float x = 0;
  19. private float y = 0;
  20. private float velX = 0;
  21. private float velY = 0;
  22. private boolean isMoving = false;
  23. private int currSpeedSlowdown = 1;
  24. private boolean isSailing = false;
  25. private boolean hasWon = false;
  26. private int energySupply;
  27. private int energyUsed = 0;
  28. private int objectivesAmount;
  29. private int objectivesVisited = 0;
  30. private final LinkedList<MinusStepsValues> steps = new LinkedList<>();
  31. private Tile currentTile;// = Tiles.GRASS;
  32. /**
  33. * Constructor of the player
  34. *
  35. */
  36. public Player()
  37. {
  38. }
  39. /**
  40. * Returns x-position of the player of the last frame
  41. *
  42. * @return x-position of the player of the last frame
  43. */
  44. public float getLastX()
  45. {
  46. return lastX;
  47. }
  48. /**
  49. * Returns y-position of the player of the last frame
  50. *
  51. * @return y-position of the player of the last frame
  52. */
  53. public float getLastY()
  54. {
  55. return lastY;
  56. }
  57. /**
  58. * Returns current x-position of the player
  59. *
  60. * @return current x-position of the player
  61. */
  62. public float getX()
  63. {
  64. return x;
  65. }
  66. /**
  67. * Returns current y-position of the player
  68. *
  69. * @return current y-position of the player
  70. */
  71. public float getY()
  72. {
  73. return y;
  74. }
  75. private void tickSteps()
  76. {
  77. Iterator<MinusStepsValues> iter = steps.iterator();
  78. while(iter.hasNext())
  79. {
  80. MinusStepsValues next = iter.next();
  81. if(next.tick())
  82. {
  83. iter.remove();
  84. }
  85. }
  86. }
  87. /**
  88. * Recalculates the player position every gametick based on user input
  89. *
  90. * @param map the current map
  91. */
  92. public void tick(TileMap map)
  93. {
  94. tickSteps();
  95. int currentTileX = Math.round(x);
  96. int currentTileY = Math.round(y);
  97. currentTile = map.getTile(currentTileX, currentTileY);
  98. currSpeedSlowdown = currentTile.getEnergyCost(this);
  99. lastX = x;
  100. lastY = y;
  101. if(isOnTile())
  102. {
  103. velX = 0.0f;
  104. velY = 0.0f;
  105. if(isMoving)
  106. {
  107. Logger.onTileEnter(this, map, currentTileX, currentTileY);
  108. currentTile.onEnter(this, map, currentTileX, currentTileY);
  109. steps.addLast(new MinusStepsValues(currSpeedSlowdown));
  110. energyUsed += currSpeedSlowdown;
  111. }
  112. isMoving = false;
  113. currentTile.isStandingOn(this, map, currentTileX, currentTileY);
  114. }
  115. if(Keys.LEFT_KEY.isDown() && !isMoving && currentTileX > 0 && !map.getTile(currentTileX - 1, currentTileY).isBlockingMovement(this))
  116. {
  117. velX = -SPEED;
  118. isMoving = true;
  119. Logger.onTileLeave(this, map, currentTileX, currentTileY);
  120. currentTile.onLeave(this, map, currentTileX, currentTileY);
  121. }
  122. else if(Keys.RIGHT_KEY.isDown() && !isMoving && currentTileX < map.getWidth() - 1 && !map.getTile(currentTileX + 1, currentTileY).isBlockingMovement(this))
  123. {
  124. velX = SPEED;
  125. isMoving = true;
  126. Logger.onTileLeave(this, map, currentTileX, currentTileY);
  127. currentTile.onLeave(this, map, currentTileX, currentTileY);
  128. }
  129. else if(Keys.UP_KEY.isDown() && !isMoving && currentTileY > 0 && !map.getTile(currentTileX, currentTileY - 1).isBlockingMovement(this))
  130. {
  131. velY = -SPEED;
  132. isMoving = true;
  133. Logger.onTileLeave(this, map, currentTileX, currentTileY);
  134. currentTile.onLeave(this, map, currentTileX, currentTileY);
  135. }
  136. else if(Keys.DOWN_KEY.isDown() && !isMoving && currentTileY < map.getHeight() - 1 && !map.getTile(currentTileX, currentTileY + 1).isBlockingMovement(this))
  137. {
  138. velY = SPEED;
  139. isMoving = true;
  140. Logger.onTileLeave(this, map, currentTileX, currentTileY);
  141. currentTile.onLeave(this, map, currentTileX, currentTileY);
  142. }
  143. float moveX = Math.abs(velX / currSpeedSlowdown);
  144. float moveY = Math.abs(velY / currSpeedSlowdown);
  145. if(velX < 0.0f)
  146. {
  147. float d = x - (float) Math.floor(x);
  148. if(d < 0.01f)
  149. {
  150. d = 1.0f;
  151. }
  152. moveX = -Math.min(moveX, d);
  153. }
  154. else if(velX > 0.0f)
  155. {
  156. float d = (float) Math.ceil(x) - x;
  157. if(d < 0.01f)
  158. {
  159. d = 1.0f;
  160. }
  161. moveX = Math.min(moveX, d);
  162. }
  163. else if(velY < 0.0f)
  164. {
  165. float d = y - (float) Math.floor(y);
  166. if(d < 0.01f)
  167. {
  168. d = 1.0f;
  169. }
  170. moveY = -Math.min(moveY, d);
  171. }
  172. else if(velY > 0.0f)
  173. {
  174. float d = (float) Math.ceil(y) - y;
  175. if(d < 0.01f)
  176. {
  177. d = 1.0f;
  178. }
  179. moveY = Math.min(moveY, d);
  180. }
  181. x += moveX;
  182. y += moveY;
  183. }
  184. private boolean isOnTile()
  185. {
  186. return Math.abs(x - Math.round(x)) < 0.01f && Math.abs(y - Math.round(y)) < 0.01f;
  187. }
  188. /**
  189. * Returns the current x-velocity of the player
  190. *
  191. * @return the current x-velocity of the player
  192. */
  193. public float getVelX()
  194. {
  195. return velX;
  196. }
  197. /**
  198. * Returns the current y-velocity of the player
  199. *
  200. * @return the current y-velocity of the player
  201. */
  202. public float getVelY()
  203. {
  204. return velY;
  205. }
  206. /**
  207. * Returns the overall energy supply of the player for the current map
  208. *
  209. * @return the overall energy supply of the player for the current map
  210. */
  211. public int getEnergySupply()
  212. {
  213. return energySupply;
  214. }
  215. /**
  216. * Returns the current energy of the player that is left for the current map
  217. *
  218. * @return the current energy of the player that is left for the current map
  219. */
  220. public int getEnergyLeft()
  221. {
  222. return energySupply - energyUsed;
  223. }
  224. /**
  225. * Returns the energy of the player that is already used for the current map
  226. *
  227. * @return the energy of the player that is already used for the current map
  228. */
  229. public int getEnergyUsed()
  230. {
  231. return energyUsed;
  232. }
  233. /**
  234. * Returns the overall amount of objectives that the player has to visit
  235. *
  236. * @return the overall amount of objectives that the player has to visit
  237. */
  238. public int getObjectivesAmount()
  239. {
  240. return objectivesAmount;
  241. }
  242. /**
  243. * Sets the overall amount of objectives that the player has to visit
  244. *
  245. * @param objectivesAmount sets the overall amount of objectives that the
  246. * player has to visit
  247. */
  248. public void setObjectivesAmount(int objectivesAmount)
  249. {
  250. this.objectivesAmount = objectivesAmount;
  251. }
  252. /**
  253. * Returns the amount of objectives that the player has already to visited
  254. *
  255. * @return the amount of objectives that the player has already to visited
  256. */
  257. public int getObjectivesVisited()
  258. {
  259. return objectivesVisited;
  260. }
  261. /**
  262. * Player visits a town and updates the player statistics
  263. */
  264. public void visitTown()
  265. {
  266. objectivesVisited++;
  267. }
  268. /**
  269. * Returns a list of the last energy costs of the last steps of the player
  270. *
  271. * @return a list of the last energy costs of the last steps of the player
  272. */
  273. public LinkedList<MinusStepsValues> getLastSteps()
  274. {
  275. return steps;
  276. }
  277. /**
  278. * Sets the abilities of the player
  279. *
  280. * @param playerAbilities sets the abilities of the player
  281. */
  282. public void setAbilities(PlayerAbilities playerAbilities)
  283. {
  284. abilities = playerAbilities;
  285. }
  286. /**
  287. * Returns the abilities of the player
  288. *
  289. * @return the abilities of the player
  290. */
  291. public PlayerAbilities getAbilities()
  292. {
  293. return abilities;
  294. }
  295. /**
  296. * Sets the overall energy supply for the player for the current map
  297. *
  298. * @param energySupply sets the overall energy supply for the player for the
  299. * current map
  300. */
  301. public void setEnergySupply(int energySupply)
  302. {
  303. this.energySupply = energySupply;
  304. }
  305. /**
  306. * Returns if player has already visited all towns and is allowed win when
  307. * going to the start field
  308. *
  309. * @return if player has already visited all towns and is allowed win when
  310. * going to the start field
  311. */
  312. public boolean canWin()
  313. {
  314. return objectivesVisited >= objectivesAmount;
  315. }
  316. /**
  317. * Returns if player has already won the current map
  318. *
  319. * @return if player has already won the current map
  320. */
  321. public boolean hasWon()
  322. {
  323. return hasWon;
  324. }
  325. /**
  326. * Player wins and updates the player statistics
  327. */
  328. public void win(TileMap map)
  329. {
  330. this.hasWon = true;
  331. Logger.onWin(this, map);
  332. }
  333. /**
  334. * Returns if player has lost the current map
  335. *
  336. * @return if player has lost the current map
  337. */
  338. public boolean hasLost()
  339. {
  340. return energyUsed > energySupply;
  341. }
  342. /**
  343. * Player is resetted
  344. *
  345. * @param sx start x-position
  346. * @param sy start y-position
  347. * @param energySupply overall energy supply for current map
  348. * @param objectivesAmount objectives amount on current map
  349. */
  350. public void reset(int sx, int sy, int energySupply, int objectivesAmount)
  351. {
  352. lastX = sx;
  353. lastY = sy;
  354. x = sx;
  355. y = sy;
  356. velX = 0;
  357. velY = 0;
  358. isMoving = false;
  359. currSpeedSlowdown = 1;
  360. hasWon = false;
  361. energyUsed = 0;
  362. isSailing = false;
  363. objectivesVisited = 0;
  364. steps.clear();
  365. this.energySupply = energySupply;
  366. this.objectivesAmount = objectivesAmount;
  367. }
  368. /**
  369. * Player is resetted, energy supply is set to standard of 1000, objectives
  370. * amount is set to standard of 10
  371. *
  372. * @param sx start x-position
  373. * @param sy start y-position
  374. */
  375. public void reset(int sx, int sy)
  376. {
  377. reset(sx, sy, 1000, 10);
  378. }
  379. /**
  380. * Returns if player is currently moving
  381. *
  382. * @return if player is currently moving
  383. */
  384. public boolean isMoving()
  385. {
  386. return isMoving;
  387. }
  388. /**
  389. * Changes Player state to sailing
  390. */
  391. public void switchSailing()
  392. {
  393. isSailing = !isSailing;
  394. }
  395. /**
  396. * Returns if player is currently sailing
  397. *
  398. * @return if player is currently sailing
  399. */
  400. public boolean isSailing()
  401. {
  402. return isSailing;
  403. }
  404. /**
  405. * Returns the tile the player is currently standing on
  406. *
  407. * @return the tile the player is currently standing on
  408. */
  409. public Tile getCurrTile()
  410. {
  411. return currentTile;
  412. }
  413. }