Player.java 12 KB

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