HumanController.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package me.hammerle.supersnuvi.entity.components.ai;
  2. import me.hammerle.supersnuvi.Keys;
  3. import me.hammerle.supersnuvi.entity.Entity;
  4. import me.hammerle.supersnuvi.tiles.Tile;
  5. import me.hammerle.supersnuvi.util.SoundUtils;
  6. public class HumanController extends Controller
  7. {
  8. private boolean combatMode = false;
  9. private int combatTimer = 0;
  10. private int blockTimer = -1;
  11. private int attackTimer = -1;
  12. private int dodgeTimer = -1;
  13. private int dashTimer = -1;
  14. public HumanController(Entity ent)
  15. {
  16. super(ent);
  17. }
  18. @Override
  19. public void tick()
  20. {
  21. float f = 12;
  22. if(Keys.RUN.isDown())
  23. {
  24. f = 24;
  25. }
  26. if(Keys.RIGHT.isDown())
  27. {
  28. ent.setMotionX(f);
  29. }
  30. else if(Keys.LEFT.isDown())
  31. {
  32. ent.setMotionX(-f);
  33. }
  34. else
  35. {
  36. ent.setMotionX(0.0f);
  37. }
  38. if(Keys.JUMP.isDown())
  39. {
  40. ent.getMovement().jump();
  41. }
  42. }
  43. /*
  44. @Override
  45. public boolean isRunning()
  46. {
  47. return Keys.RUN.isDown();
  48. }
  49. @Override
  50. public boolean isInCombatMode()
  51. {
  52. return combatMode || combatTimer < 0;
  53. }
  54. @Override
  55. public int getCombatTimer()
  56. {
  57. return combatTimer;
  58. }
  59. @Override
  60. public boolean isBlocking()
  61. {
  62. return blockTimer >= 0;
  63. }
  64. @Override
  65. public boolean isAttacking()
  66. {
  67. return attackTimer >= 0;
  68. }
  69. @Override
  70. public boolean isDashing()
  71. {
  72. return dashTimer != -1;
  73. }
  74. @Override
  75. public boolean isDodging()
  76. {
  77. return dodgeTimer != -1;
  78. }
  79. @Override
  80. public void tick()
  81. {
  82. if(ent.getHealth().isDead())
  83. {
  84. return;
  85. }
  86. if(Keys.COMBAT.isReleased())
  87. {
  88. if(combatMode)
  89. {
  90. combatMode = false;
  91. combatTimer = -ent.getAnimator().getCombatStartTicks();
  92. SoundUtils.playSound(SoundUtils.Sound.SWORD_SHEATH);
  93. }
  94. else
  95. {
  96. combatMode = true;
  97. combatTimer = ent.getAnimator().getCombatStartTicks();
  98. SoundUtils.playSound(SoundUtils.Sound.SWORD_PULL);
  99. }
  100. }
  101. if(combatTimer < 0)
  102. {
  103. combatTimer++;
  104. }
  105. else if(combatTimer > 0)
  106. {
  107. combatTimer--;
  108. }
  109. if(blockTimer != -1)
  110. {
  111. blockTimer++;
  112. if(blockTimer >= ent.getAnimator().getBlockTicks())
  113. {
  114. blockTimer = -80;
  115. }
  116. }
  117. if(attackTimer != -1)
  118. {
  119. attackTimer++;
  120. if(attackTimer >= ent.getAnimator().getAttackTicks())
  121. {
  122. attackTimer = -1;
  123. float sign = ent.getAnimator().drawImageFlipped() ? -1 : 1;
  124. float extend = sign * Tile.SIZE;
  125. boolean once = true;
  126. for(Entity e : ent.getLevel().getEntitiesCollidingWith(ent, ent.getBox().expand(extend, 0)))
  127. {
  128. if(e.getController().isAttacking() && once)
  129. {
  130. once = false;
  131. ent.setMotionX(-sign * 7);
  132. e.setMotionX(sign * 7);
  133. }
  134. else if(!e.getController().isBlocking())
  135. {
  136. e.getHealth().addHealth(-30);
  137. }
  138. }
  139. return;
  140. }
  141. }
  142. if(dodgeTimer != -1)
  143. {
  144. dodgeTimer++;
  145. if(dodgeTimer > 12)
  146. {
  147. dodgeTimer = -1;
  148. ent.setMotionX(0.0f);
  149. }
  150. else
  151. {
  152. ent.setMotionX(ent.getAnimator().drawImageFlipped() ? 10 : -10);
  153. }
  154. }
  155. if(dashTimer != -1)
  156. {
  157. dashTimer++;
  158. if(dashTimer > 15)
  159. {
  160. dashTimer = -1;
  161. ent.setMotionX(0.0f);
  162. }
  163. else
  164. {
  165. ent.setMotionX(ent.getAnimator().drawImageFlipped() ? -15 : 15);
  166. }
  167. }
  168. if(combatMode && combatTimer == 0)
  169. {
  170. if(Keys.COMBAT_BLOCK.isDown() && blockTimer == -1)
  171. {
  172. ent.getAnimator().resetFrames();
  173. blockTimer = 0;
  174. }
  175. else if(Keys.COMBAT_ATTACK.isReleased()&& attackTimer == -1)
  176. {
  177. SoundUtils.playSound(SoundUtils.Sound.SWORD_SLASH);
  178. attackTimer = 0;
  179. }
  180. else if(Keys.COMBAT_DASH.isReleased() && dashTimer == -1 && ent.getEnergy().getEnergyPercent() >= 0.3)
  181. {
  182. SoundUtils.playSound(SoundUtils.Sound.DASH);
  183. dashTimer = 0;
  184. ent.getEnergy().addEnergyPercent(-0.3);
  185. }
  186. else if(Keys.COMBAT_DODGE.isReleased() && dodgeTimer == -1 && ent.getEnergy().getEnergyPercent() >= 0.1)
  187. {
  188. SoundUtils.playSound(SoundUtils.Sound.DODGE);
  189. dodgeTimer = 0;
  190. ent.getEnergy().addEnergyPercent(-0.10);
  191. }
  192. }
  193. if(!isBlocking() && dodgeTimer == -1 && dashTimer == -1)
  194. {
  195. if(Keys.JUMP.isDown() && ent.getEnergy().getEnergyPercent() > 0.2 && ent.getMovement().jump())
  196. {
  197. ent.getEnergy().addEnergyPercent(-0.10);
  198. SoundUtils.playSound(SoundUtils.Sound.JUMP);
  199. }
  200. float speed;
  201. if(ent.getController().isInCombatMode())
  202. {
  203. speed = 3.0f;
  204. }
  205. else
  206. {
  207. speed = 6.0f;
  208. if(isRunning())
  209. {
  210. if(ent.getEnergy().getEnergyPercent() > 0.0)
  211. {
  212. speed = 9.0f;
  213. }
  214. if(ent.isMoving())
  215. {
  216. ent.getEnergy().addEnergy(-0.3);
  217. }
  218. }
  219. }
  220. if(dodgeTimer == -1 && dashTimer == -1)
  221. {
  222. if(Keys.LEFT.isDown())
  223. {
  224. ent.setMotionX(-speed * ent.getMovementPenalty().getFactor());
  225. }
  226. else if(Keys.RIGHT.isDown())
  227. {
  228. ent.setMotionX(speed * ent.getMovementPenalty().getFactor());
  229. }
  230. }
  231. if(ent.isOnGround() && ent.getPreviousMotionX() != 0)
  232. {
  233. if(ent.getMovementPenalty().isInWater())
  234. {
  235. SoundUtils.playSound(SoundUtils.Sound.WALK_WATER);
  236. }
  237. else
  238. {
  239. SoundUtils.playSound(SoundUtils.Sound.WALK);
  240. }
  241. }
  242. }
  243. }*/
  244. }