HeroAnimator.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. package me.hammerle.supersnuvi.entity.components.animator;
  2. import me.hammerle.supersnuvi.Keys;
  3. import me.hammerle.supersnuvi.entity.Entity;
  4. import me.hammerle.supersnuvi.entity.components.Health;
  5. import me.hammerle.supersnuvi.javafx.Image;
  6. import me.hammerle.supersnuvi.rendering.OldGame;
  7. import me.hammerle.supersnuvi.rendering.IRenderer;
  8. import me.hammerle.supersnuvi.util.Utils;
  9. public class HeroAnimator extends EntityAnimator
  10. {
  11. private final static Image[] WALK = new Image[]
  12. {
  13. Utils.getImage("snuvi/walk/walk_f1", true),
  14. Utils.getImage("snuvi/walk/walk_f2", true),
  15. Utils.getImage("snuvi/walk/walk_f3", true),
  16. Utils.getImage("snuvi/walk/walk_f4", true),
  17. Utils.getImage("snuvi/walk/walk_f5", true),
  18. Utils.getImage("snuvi/walk/walk_f6", true),
  19. Utils.getImage("snuvi/walk/walk_f7", true),
  20. Utils.getImage("snuvi/walk/walk_f8", true),
  21. Utils.getImage("snuvi/walk/walk_f9", true)
  22. };
  23. private final static Image JUMP = Utils.getImage("snuvi/jump", true);
  24. static
  25. {
  26. Image block3 = Utils.getImage("snuvi/block/block_f3", true);
  27. BLOCK = new Image[]
  28. {
  29. Utils.getImage("snuvi/block/block_f1", true),
  30. Utils.getImage("snuvi/block/block_f2", true),
  31. block3,
  32. block3,
  33. block3,
  34. block3,
  35. block3,
  36. block3,
  37. block3,
  38. block3
  39. };
  40. Image headHit3 = Utils.getImage("snuvi/combat/cbat_hit_f3", true);
  41. COMBAT_HEAD_HIT = new Image[]
  42. {
  43. Utils.getImage("snuvi/combat/cbat_hit_f1", true),
  44. Utils.getImage("snuvi/combat/cbat_hit_f2", true),
  45. headHit3,
  46. headHit3
  47. };
  48. }
  49. private final static Image[] BLOCK;
  50. private final static Image[] DEATH = new Image[]
  51. {
  52. Utils.getImage("snuvi/death/cbat_death_f1"),
  53. Utils.getImage("snuvi/death/cbat_death_f2"),
  54. Utils.getImage("snuvi/death/cbat_death_f3"),
  55. Utils.getImage("snuvi/death/cbat_death_f4"),
  56. Utils.getImage("snuvi/death/cbat_death_f5"),
  57. Utils.getImage("snuvi/death/cbat_death_f6"),
  58. Utils.getImage("snuvi/death/cbat_death_f7"),
  59. Utils.getImage("snuvi/death/cbat_death_f8"),
  60. Utils.getImage("snuvi/death/cbat_death_f9"),
  61. Utils.getImage("snuvi/death/cbat_death_f10"),
  62. Utils.getImage("snuvi/death/cbat_death_f11"),
  63. Utils.getImage("snuvi/death/cbat_death_f12"),
  64. Utils.getImage("snuvi/death/cbat_death_f13"),
  65. Utils.getImage("snuvi/death/cbat_death_f14"),
  66. Utils.getImage("snuvi/death/cbat_death_f15"),
  67. Utils.getImage("snuvi/death/cbat_death_f16"),
  68. Utils.getImage("snuvi/death/cbat_death_f17")
  69. };
  70. private final static Image[] COMBAT_DEATH = new Image[]
  71. {
  72. Utils.getImage("snuvi/combat_death/death_f1"),
  73. Utils.getImage("snuvi/combat_death/death_f2"),
  74. Utils.getImage("snuvi/combat_death/death_f3"),
  75. Utils.getImage("snuvi/combat_death/death_f4"),
  76. Utils.getImage("snuvi/combat_death/death_f5"),
  77. Utils.getImage("snuvi/combat_death/death_f6"),
  78. Utils.getImage("snuvi/combat_death/death_f7"),
  79. Utils.getImage("snuvi/combat_death/death_f8"),
  80. Utils.getImage("snuvi/combat_death/death_f9"),
  81. Utils.getImage("snuvi/combat_death/death_f10"),
  82. Utils.getImage("snuvi/combat_death/death_f11"),
  83. Utils.getImage("snuvi/combat_death/death_f12"),
  84. Utils.getImage("snuvi/combat_death/death_f13"),
  85. Utils.getImage("snuvi/combat_death/death_f14"),
  86. Utils.getImage("snuvi/combat_death/death_f15"),
  87. Utils.getImage("snuvi/combat_death/death_f16"),
  88. Utils.getImage("snuvi/combat_death/death_f17")
  89. };
  90. private final static Image[] IDLE = new Image[]
  91. {
  92. Utils.getImage("snuvi/idle/idle_f1", true),
  93. Utils.getImage("snuvi/idle/idle_f2", true),
  94. Utils.getImage("snuvi/idle/idle_f3", true),
  95. Utils.getImage("snuvi/idle/idle_f4", true),
  96. Utils.getImage("snuvi/idle/idle_f5", true),
  97. Utils.getImage("snuvi/idle/idle_f6", true),
  98. Utils.getImage("snuvi/idle/idle_f7", true),
  99. Utils.getImage("snuvi/idle/idle_f8", true),
  100. Utils.getImage("snuvi/idle/idle_f9", true),
  101. Utils.getImage("snuvi/idle/idle_f10", true),
  102. Utils.getImage("snuvi/idle/idle_f11", true),
  103. Utils.getImage("snuvi/idle/idle_f12", true),
  104. Utils.getImage("snuvi/idle/idle_f13", true),
  105. Utils.getImage("snuvi/idle/idle_f14", true)
  106. };
  107. private final static Image[] COMBAT_HEAD = new Image[]
  108. {
  109. Utils.getImage("snuvi/combat/cbat_head_f1", true),
  110. Utils.getImage("snuvi/combat/cbat_head_f2", true),
  111. Utils.getImage("snuvi/combat/cbat_head_f3", true),
  112. Utils.getImage("snuvi/combat/cbat_head_f4", true),
  113. Utils.getImage("snuvi/combat/cbat_head_f5", true),
  114. Utils.getImage("snuvi/combat/cbat_head_f6", true),
  115. Utils.getImage("snuvi/combat/cbat_head_f7", true),
  116. Utils.getImage("snuvi/combat/cbat_head_f8", true),
  117. Utils.getImage("snuvi/combat/cbat_head_f9", true),
  118. Utils.getImage("snuvi/combat/cbat_head_f10", true),
  119. Utils.getImage("snuvi/combat/cbat_head_f11", true),
  120. Utils.getImage("snuvi/combat/cbat_head_f12", true),
  121. Utils.getImage("snuvi/combat/cbat_head_f13", true),
  122. Utils.getImage("snuvi/combat/cbat_head_f14", true)
  123. };
  124. private final static Image[] COMBAT_HEAD_HIT;
  125. private final static Image COMBAT_ARMS_IDLE = Utils.getImage("snuvi/combat/cbat_arms_idle", true);
  126. private final static Image[][] COMBAT_ARMS_SWING = new Image[][]
  127. {
  128. {
  129. Utils.getImage("snuvi/combat/cbat_swing1_f1", true),
  130. Utils.getImage("snuvi/combat/cbat_swing1_f2", true),
  131. Utils.getImage("snuvi/combat/cbat_swing1_f3", true)
  132. },
  133. {
  134. Utils.getImage("snuvi/combat/cbat_swing2_f1", true),
  135. Utils.getImage("snuvi/combat/cbat_swing2_f2", true),
  136. Utils.getImage("snuvi/combat/cbat_swing2_f3", true)
  137. },
  138. {
  139. Utils.getImage("snuvi/combat/cbat_swing3_f1", true),
  140. Utils.getImage("snuvi/combat/cbat_swing3_f2", true),
  141. Utils.getImage("snuvi/combat/cbat_swing3_f3", true)
  142. }
  143. };
  144. private final static Image COMBAT_BODY_IDLE = Utils.getImage("snuvi/combat/cbat_idle_body", true);
  145. private final static Image[] COMBAT_BODY_WALK = new Image[]
  146. {
  147. Utils.getImage("snuvi/combat/cbat_walk_f1", true),
  148. Utils.getImage("snuvi/combat/cbat_walk_f2", true),
  149. Utils.getImage("snuvi/combat/cbat_walk_f3", true),
  150. Utils.getImage("snuvi/combat/cbat_walk_f4", true)
  151. };
  152. private final static Image[] COMBAT_SWITCH = new Image[]
  153. {
  154. Utils.getImage("snuvi/combat/switch_f1", true),
  155. Utils.getImage("snuvi/combat/switch_f2", true),
  156. Utils.getImage("snuvi/combat/switch_f3", true),
  157. Utils.getImage("snuvi/combat/switch_f4", true),
  158. Utils.getImage("snuvi/combat/switch_f5", true),
  159. Utils.getImage("snuvi/combat/switch_f6", true),
  160. Utils.getImage("snuvi/combat/switch_f7", true),
  161. Utils.getImage("snuvi/combat/switch_f8", true)
  162. };
  163. private final static Image COMBAT_BODY_DODGE = Utils.getImage("snuvi/dodge", true);
  164. private final static Image COMBAT_BODY_DASH = Utils.getImage("snuvi/dash", true);
  165. private final static Image COMBAT_BODY_JUMP = Utils.getImage("snuvi/cbat_jump", true);
  166. private int counter = 0;
  167. private int counterIdle = 0;
  168. private int counterBody = 0;
  169. private int counterHead = 0;
  170. private int counterArms = 0;
  171. private int frame = 0;
  172. private int frameIdle = 0;
  173. private int frameBody = 0;
  174. private int frameHead = 0;
  175. private int frameHeadHit = 0;
  176. private int frameArms = 0;
  177. private int attackCounter = 0;
  178. private boolean flipped = false;
  179. public HeroAnimator(Entity ent)
  180. {
  181. super(ent);
  182. }
  183. @Override
  184. public int getCombatStartTicks()
  185. {
  186. return COMBAT_SWITCH.length * 5;
  187. }
  188. @Override
  189. public int getAttackTicks()
  190. {
  191. return COMBAT_ARMS_SWING[0].length * 5;
  192. }
  193. @Override
  194. public int getBlockTicks()
  195. {
  196. return BLOCK.length * 5;
  197. }
  198. @Override
  199. public int getDeathTicks()
  200. {
  201. return DEATH.length * 5;
  202. }
  203. @Override
  204. public void resetDefaultFrames()
  205. {
  206. counter = 0;
  207. frame = 0;
  208. }
  209. @Override
  210. public void resetIdleFrames()
  211. {
  212. counterIdle = 0;
  213. frameIdle = 0;
  214. }
  215. @Override
  216. public void resetHeadFrames()
  217. {
  218. counterHead = 0;
  219. frameHead = 0;
  220. frameHeadHit = 0;
  221. }
  222. @Override
  223. public void resetBodyFrames()
  224. {
  225. counterBody = 0;
  226. frameBody = 0;
  227. }
  228. @Override
  229. public void resetArmsFrames()
  230. {
  231. counterArms = 0;
  232. frameArms = 0;
  233. }
  234. @Override
  235. public void render(IRenderer renderer)
  236. {
  237. double x = ent.getX() - 2 * OldGame.SCALE;
  238. double y = ent.getY() + ent.getHeight() - 2 * OldGame.SCALE;
  239. if(ent.getHealth().isDead())
  240. {
  241. x -= (drawImageFlipped() ? ent.getWidth() : 0);
  242. if(frame >= DEATH.length)
  243. {
  244. frame = 0;
  245. }
  246. if(ent.getController().isInCombatMode())
  247. {
  248. renderer.drawImage(COMBAT_DEATH[frame], x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  249. }
  250. else
  251. {
  252. renderer.drawImage(DEATH[frame], x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  253. }
  254. }
  255. else if(ent.getController().isInCombatMode())
  256. {
  257. x -= (drawImageFlipped() ? ent.getWidth() : 0);
  258. if(ent.getController().isBlocking())
  259. {
  260. if(frame >= BLOCK.length)
  261. {
  262. frame = 0;
  263. }
  264. bindSlot(BLOCK[frame]);
  265. renderer.drawImage(BLOCK[frame], x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  266. }
  267. else
  268. {
  269. // body rendering
  270. if(!ent.isOnGround())
  271. {
  272. bindSlot(COMBAT_BODY_JUMP);
  273. renderer.drawImage(COMBAT_BODY_JUMP, x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  274. }
  275. else if(ent.getController().isDashing())
  276. {
  277. bindSlot(COMBAT_BODY_DASH);
  278. renderer.drawImage(COMBAT_BODY_DASH, x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  279. }
  280. else if(ent.getController().isDodging())
  281. {
  282. bindSlot(COMBAT_BODY_DODGE);
  283. renderer.drawImage(COMBAT_BODY_DODGE, x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  284. }
  285. else if(ent.getPreviousMotionX() != 0)
  286. {
  287. if(frameBody >= COMBAT_BODY_WALK.length)
  288. {
  289. frameBody = 0;
  290. }
  291. bindSlot(COMBAT_BODY_WALK[frameBody]);
  292. renderer.drawImage(COMBAT_BODY_WALK[frameBody], x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  293. }
  294. else
  295. {
  296. bindSlot(COMBAT_BODY_IDLE);
  297. renderer.drawImage(COMBAT_BODY_IDLE, x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  298. }
  299. // arms rendering
  300. int combatStart = ent.getController().getCombatTimer();
  301. if(combatStart != 0)
  302. {
  303. combatStart /= 5;
  304. if(combatStart < 0)
  305. {
  306. combatStart += COMBAT_SWITCH.length;
  307. }
  308. bindSlot(COMBAT_SWITCH[combatStart]);
  309. renderer.drawImage(COMBAT_SWITCH[combatStart], x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  310. }
  311. else if(ent.getController().isAttacking())
  312. {
  313. Image[] attack = COMBAT_ARMS_SWING[attackCounter];
  314. if(frameArms >= attack.length)
  315. {
  316. frameArms = 0;
  317. attackCounter++;
  318. if(attackCounter >= COMBAT_ARMS_SWING.length)
  319. {
  320. attackCounter = 0;
  321. }
  322. }
  323. bindSlot(attack[frameArms]);
  324. renderer.drawImage(attack[frameArms], x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  325. }
  326. else
  327. {
  328. bindSlot(COMBAT_ARMS_IDLE);
  329. renderer.drawImage(COMBAT_ARMS_IDLE, x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  330. }
  331. // head rendering
  332. if(ent.getHealth().hasInvincibility() && frameHeadHit < COMBAT_HEAD_HIT.length)
  333. {
  334. bindSlot(COMBAT_HEAD_HIT[frameHead]);
  335. renderer.drawImage(COMBAT_HEAD_HIT[frameHead], x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  336. }
  337. else
  338. {
  339. if(frameHead >= COMBAT_HEAD.length)
  340. {
  341. frameHead = 0;
  342. }
  343. bindSlot(COMBAT_HEAD[frameHead]);
  344. renderer.drawImage(COMBAT_HEAD[frameHead], x, y, ent.getWidth() * 2, ent.getHeight(), drawImageFlipped());
  345. }
  346. }
  347. }
  348. else if(ent.isOnGround())
  349. {
  350. if(ent.getPreviousMotionX() != 0)
  351. {
  352. if(frame >= WALK.length)
  353. {
  354. frame = 0;
  355. }
  356. bindSlot(WALK[frame]);
  357. renderer.drawImage(WALK[frame], x, y, ent.getWidth(), ent.getHeight(), drawImageFlipped());
  358. }
  359. else
  360. {
  361. if(frameIdle >= IDLE.length)
  362. {
  363. frameIdle = 0;
  364. }
  365. bindSlot(IDLE[frameIdle]);
  366. renderer.drawImage(IDLE[frameIdle], x, y, ent.getWidth(), ent.getHeight(), drawImageFlipped());
  367. }
  368. }
  369. else
  370. {
  371. bindSlot(JUMP);
  372. renderer.drawImage(JUMP, x, y, ent.getWidth(), ent.getHeight(), drawImageFlipped());
  373. }
  374. }
  375. @Override
  376. public boolean drawImageFlipped()
  377. {
  378. return flipped;
  379. }
  380. @Override
  381. public void tick()
  382. {
  383. counter++;
  384. counterIdle++;
  385. counterBody++;
  386. counterHead++;
  387. counterArms++;
  388. if(counter >= 6)
  389. {
  390. counter = 0;
  391. frame++;
  392. }
  393. if(counterIdle >= 12)
  394. {
  395. counterIdle = 0;
  396. frameIdle++;
  397. }
  398. if(counterBody >= 9)
  399. {
  400. counterBody = 0;
  401. frameBody++;
  402. }
  403. if(counterArms >= 9)
  404. {
  405. counterArms = 0;
  406. frameArms++;
  407. }
  408. if(counterHead >= 12)
  409. {
  410. counterHead = 0;
  411. frameHead++;
  412. frameHeadHit++;
  413. }
  414. if(frame < 0)
  415. {
  416. frame = 0;
  417. }
  418. if(frameIdle < 0)
  419. {
  420. frameIdle = 0;
  421. }
  422. if(frameBody < 0)
  423. {
  424. frameBody = 0;
  425. }
  426. if(frameHead < 0)
  427. {
  428. frameHead = 0;
  429. }
  430. if(frameArms < 0)
  431. {
  432. frameArms = 0;
  433. }
  434. if(ent.getController().isInCombatMode())
  435. {
  436. if(Keys.COMBAT_SWITCH_FACE.isReleased())
  437. {
  438. flipped = !flipped;
  439. }
  440. }
  441. else
  442. {
  443. if(ent.getMotionX() < 0)
  444. {
  445. flipped = true;
  446. }
  447. else if(ent.getMotionX() > 0)
  448. {
  449. flipped = false;
  450. }
  451. }
  452. }
  453. private void bindSlot(Image image)
  454. {
  455. if(image != null)
  456. {
  457. Health h = ent.getHealth();
  458. if(h.wasHealed())
  459. {
  460. image.bindSlot(1);
  461. }
  462. else if(h.wasHurt())
  463. {
  464. image.bindSlot(0);
  465. }
  466. else
  467. {
  468. image.bindSlot(-1);
  469. }
  470. }
  471. }
  472. }