HumanController.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package me.hammerle.supersnuvi.entity.components.ai;
  2. import me.hammerle.snuviengine.api.Shader;
  3. import me.hammerle.snuviengine.api.Texture;
  4. import me.hammerle.supersnuvi.Keys;
  5. import me.hammerle.supersnuvi.entity.Entity;
  6. import me.hammerle.supersnuvi.util.Face;
  7. import me.hammerle.supersnuvi.util.Utils;
  8. public class HumanController extends Controller
  9. {
  10. private final static int SIZE = 1024;
  11. private final static Texture HERO = new Texture("resources/hero.png");
  12. private class TexturePart
  13. {
  14. private boolean draw = false;
  15. private float ox = 0.0f;
  16. private float oy = 0.0f;
  17. private float w = 0.0f;
  18. private float h = 0.0f;
  19. private float tx = 0.0f;
  20. private float ty = 0.0f;
  21. }
  22. private TexturePart body = new TexturePart();
  23. private TexturePart arms = new TexturePart();
  24. private TexturePart head = new TexturePart();
  25. private TexturePart[] parts = new TexturePart[]
  26. {
  27. body, arms, head
  28. };
  29. private int walkFrame = 0;
  30. private int idleCounter = 0;
  31. private int idleFrame = 0;
  32. private int deathFrame = 0;
  33. public HumanController(Entity ent)
  34. {
  35. super(ent);
  36. }
  37. private void nextWalkFrame()
  38. {
  39. deathFrame = 0;
  40. idleFrame = 0;
  41. walkFrame = (walkFrame + 1) % 9;
  42. }
  43. private void nextIdleFrame()
  44. {
  45. deathFrame = 0;
  46. walkFrame = 0;
  47. idleCounter++;
  48. if(idleCounter >= 3)
  49. {
  50. idleCounter = 0;
  51. idleFrame = (idleFrame + 1) % 14;
  52. }
  53. }
  54. private void nextDeathFrame()
  55. {
  56. walkFrame = 0;
  57. idleFrame = 0;
  58. deathFrame++;
  59. }
  60. private void resetFrames()
  61. {
  62. deathFrame = 0;
  63. walkFrame = 0;
  64. idleFrame = 0;
  65. }
  66. @Override
  67. public boolean isAnimated()
  68. {
  69. return deathFrame < 17;
  70. }
  71. @Override
  72. public void tick()
  73. {
  74. for(TexturePart tp : parts)
  75. {
  76. tp.draw = false;
  77. }
  78. if(ent.getHealth().isDead())
  79. {
  80. body.draw = true;
  81. body.ox = ent.getFace() == Face.RIGHT ? 0.0f: -32.0f;
  82. body.oy = 0.0f;
  83. body.h = 64.0f;
  84. body.w = 64.0f;
  85. body.tx = (deathFrame * 64.0f) / SIZE;
  86. if(deathFrame < 16)
  87. {
  88. body.ty = 192.0f / SIZE;
  89. }
  90. else
  91. {
  92. body.ty = 256.0f / SIZE;
  93. }
  94. nextDeathFrame();
  95. return;
  96. }
  97. float speed = ent.getMovement().getVelocityX();
  98. if(Keys.RUN.isDown())
  99. {
  100. speed *= 1.5f;
  101. }
  102. if(Keys.LEFT.isDown())
  103. {
  104. ent.setMotionX(-speed);
  105. }
  106. if(Keys.RIGHT.isDown())
  107. {
  108. ent.setMotionX(speed);
  109. }
  110. if(Keys.JUMP.isDown())
  111. {
  112. ent.getMovement().jump();
  113. }
  114. body.draw = true;
  115. body.ox = 0.0f;
  116. body.oy = 0.0f;
  117. body.h = 64.0f;
  118. body.w = 32.0f;
  119. if(ent.isOnGround())
  120. {
  121. if(ent.getMotionX() == 0.0f)
  122. {
  123. nextIdleFrame();
  124. body.tx = (idleFrame * 32.0f) / SIZE;
  125. body.ty = 128.0f / SIZE;
  126. }
  127. else
  128. {
  129. nextWalkFrame();
  130. body.tx = (96.0f + walkFrame * 32.0f) / SIZE;
  131. body.ty = 0.0f;
  132. }
  133. }
  134. else
  135. {
  136. resetFrames();
  137. body.tx = 0.0f;
  138. body.ty = 0.0f;
  139. }
  140. }
  141. @Override
  142. public void renderTick(float lag)
  143. {
  144. if(ent.getHealth().wasHurt())
  145. {
  146. Shader.setColorEnabled(true);
  147. Shader.setMixColorEnabled(true);
  148. Shader.setMixColor(1.0f, 0.0f, 0.0f, 1.0f);
  149. }
  150. if(ent.getHealth().wasHealed())
  151. {
  152. Shader.setColorEnabled(true);
  153. Shader.setMixColorEnabled(true);
  154. Shader.setMixColor(0.0f, 1.0f, 0.0f, 1.0f);
  155. }
  156. HERO.bind();
  157. float x = Utils.interpolate(ent.getLastX(), ent.getX(), lag);
  158. float y = Utils.interpolate(ent.getLastY(), ent.getY(), lag);
  159. for(TexturePart tp : parts)
  160. {
  161. if(tp.draw)
  162. {
  163. float m1;
  164. float m2;
  165. if(ent.getFace() == Face.LEFT)
  166. {
  167. m1 = tp.w;
  168. m2 = 0.0f;
  169. }
  170. else
  171. {
  172. m1 = 0.0f;
  173. m2 = tp.w;
  174. }
  175. Shader.getTextureRenderer().drawRectangle(
  176. x + tp.ox + m1, y + tp.oy,
  177. x + tp.ox + m2, y + tp.oy + tp.h,
  178. tp.tx, tp.ty,
  179. tp.tx + (tp.w / SIZE), tp.ty + (tp.h / SIZE));
  180. }
  181. }
  182. if(ent.getHealth().wasHurt() || ent.getHealth().wasHealed())
  183. {
  184. Shader.setColorEnabled(false);
  185. Shader.setMixColorEnabled(false);
  186. }
  187. }
  188. }