HumanController.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 idleFrame = 0;
  31. public HumanController(Entity ent)
  32. {
  33. super(ent);
  34. }
  35. private void nextWalkFrame()
  36. {
  37. walkFrame = (walkFrame + 1) % 9;
  38. idleFrame = 0;
  39. }
  40. private void nextIdleFrame()
  41. {
  42. walkFrame = 0;
  43. idleFrame = (idleFrame + 1) % 14;
  44. }
  45. private void resetFrames()
  46. {
  47. walkFrame = 0;
  48. idleFrame = 0;
  49. }
  50. @Override
  51. public void tick()
  52. {
  53. for(TexturePart tp : parts)
  54. {
  55. tp.draw = false;
  56. }
  57. float speed = ent.getMovement().getVelocityX();
  58. if(Keys.RUN.isDown())
  59. {
  60. speed *= 1.5f;
  61. }
  62. if(Keys.LEFT.isDown())
  63. {
  64. ent.setMotionX(-speed);
  65. }
  66. if(Keys.RIGHT.isDown())
  67. {
  68. ent.setMotionX(speed);
  69. }
  70. if(Keys.JUMP.isDown())
  71. {
  72. ent.getMovement().jump();
  73. }
  74. body.draw = true;
  75. body.ox = 0.0f;
  76. body.oy = 0.0f;
  77. body.h = 64.0f;
  78. body.w = 32.0f;
  79. if(ent.isOnGround())
  80. {
  81. if(ent.getMotionX() == 0.0f)
  82. {
  83. nextIdleFrame();
  84. body.tx = (idleFrame * 32.0f) / SIZE;
  85. body.ty = 128.0f / SIZE;
  86. }
  87. else
  88. {
  89. nextWalkFrame();
  90. body.tx = (96.0f + walkFrame * 32.0f) / SIZE;
  91. body.ty = 0.0f;
  92. }
  93. }
  94. else
  95. {
  96. resetFrames();
  97. body.tx = 0.0f;
  98. body.ty = 0.0f;
  99. }
  100. }
  101. @Override
  102. public void renderTick(float lag)
  103. {
  104. HERO.bind();
  105. float x = Utils.interpolate(ent.getLastX(), ent.getX(), lag);
  106. float y = Utils.interpolate(ent.getLastY(), ent.getY(), lag);
  107. for(TexturePart tp : parts)
  108. {
  109. if(tp.draw)
  110. {
  111. float m1;
  112. float m2;
  113. if(ent.getFace() == Face.LEFT)
  114. {
  115. m1 = tp.w;
  116. m2 = 0.0f;
  117. }
  118. else
  119. {
  120. m1 = 0.0f;
  121. m2 = tp.w;
  122. }
  123. Shader.getTextureRenderer().drawRectangle(
  124. x + tp.ox + m1, y + tp.oy,
  125. x + tp.ox + m2, y + tp.oy + tp.h,
  126. tp.tx, tp.ty,
  127. tp.tx + (tp.w / SIZE), tp.ty + (tp.h / SIZE));
  128. }
  129. }
  130. }
  131. }