LondonerController.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.entity.Entity;
  5. import me.hammerle.supersnuvi.gamelogic.Level;
  6. import me.hammerle.supersnuvi.tiles.RampTile;
  7. import me.hammerle.supersnuvi.tiles.Tile;
  8. import me.hammerle.supersnuvi.util.Face;
  9. import me.hammerle.supersnuvi.util.SoundUtils;
  10. import me.hammerle.supersnuvi.util.Utils;
  11. public class LondonerController extends Controller
  12. {
  13. private final static Texture LONDONER = new Texture("resources/londoner.png");
  14. // for render offset: 0.21875f, 0.59375f, 0.65625f, 2.0f
  15. // render offset: -0.21875, -0.59375
  16. private final static float OFFSET_X = -Tile.SIZE * 0.21875f;
  17. private final static float OFFSET_Y = -Tile.SIZE * 0.59375f;
  18. private final boolean evil;
  19. private float ox = 0.0f;
  20. private float tx = 0.0f;
  21. private float ty = 0.0f;
  22. private int walkFrame = 0;
  23. private int transformFrame = -1;
  24. private int deathCounter = 0;
  25. private int deathFrame = 0;
  26. private Face oldFace = Face.LEFT;
  27. private Face direction = Face.LEFT;
  28. private boolean shouldJump = false;
  29. private int hurtTicks = 0;
  30. public LondonerController(boolean evil)
  31. {
  32. this.evil = evil;
  33. }
  34. private boolean hasRedEyes()
  35. {
  36. return transformFrame >= 3;
  37. }
  38. @Override
  39. public boolean isAnimated()
  40. {
  41. return deathFrame < 7;
  42. }
  43. @Override
  44. public void tick(Entity ent, Level level)
  45. {
  46. if(hurtTicks > 0)
  47. {
  48. hurtTicks--;
  49. }
  50. if(evil)
  51. {
  52. if(transformFrame != -1)
  53. {
  54. if(transformFrame < 3)
  55. {
  56. ox = direction == Face.RIGHT ? 0.0f: -1.125f * Tile.SIZE;
  57. tx = transformFrame * 0.125f + 0.125f;
  58. ty = 0.375f;
  59. transformFrame++;
  60. return;
  61. }
  62. }
  63. else if(level.getHero().getSquaredDistance(ent) <= 16384) // 4 Tiles
  64. {
  65. transformFrame = 0;
  66. }
  67. }
  68. if(ent.getHealth().isDead())
  69. {
  70. ox = direction == Face.RIGHT ? -Tile.SIZE: -0.125f * Tile.SIZE;
  71. tx = deathFrame * 0.125f;
  72. ty = hasRedEyes() ? 0.125f : 0.25f;
  73. walkFrame = 0;
  74. deathCounter++;
  75. if(deathCounter >= 3)
  76. {
  77. deathCounter = 0;
  78. deathFrame++;
  79. }
  80. return;
  81. }
  82. if(hasRedEyes())
  83. {
  84. float hx = level.getHero().getCenterX();
  85. if(hx < ent.getCenterX())
  86. {
  87. ent.applyOwnForce(-ent.getMovement().getVelocityX(), 0.0f);
  88. direction = Face.LEFT;
  89. }
  90. else
  91. {
  92. ent.applyOwnForce(ent.getMovement().getVelocityX(), 0.0f);
  93. direction = Face.RIGHT;
  94. }
  95. }
  96. else
  97. {
  98. if(direction == Face.LEFT)
  99. {
  100. ent.applyOwnForce(-ent.getMovement().getVelocityX(), 0.0f);
  101. }
  102. else
  103. {
  104. ent.applyOwnForce(ent.getMovement().getVelocityX(), 0.0f);
  105. }
  106. }
  107. if(shouldJump)
  108. {
  109. SoundUtils.playSound(SoundUtils.Sound.LONDONER_JUMP);
  110. ent.jump();
  111. shouldJump = false;
  112. }
  113. ox = direction == Face.RIGHT ? 0.0f: -1.125f * Tile.SIZE;
  114. if(ent.isOnGround())
  115. {
  116. if(ent.getOwnForceX() == 0.0f)
  117. {
  118. tx = 0.875f;
  119. ty = hasRedEyes() ? 0.25f : 0.375f;
  120. }
  121. else
  122. {
  123. deathFrame = 0;
  124. walkFrame = (walkFrame + 1) % 6;
  125. tx = walkFrame * 0.125f;
  126. ty = hasRedEyes() ? 0.5f : 0.625f;
  127. }
  128. }
  129. else
  130. {
  131. deathFrame = 0;
  132. walkFrame = 0;
  133. tx = 0.125f;
  134. ty = hasRedEyes() ? 0.5f : 0.625f;
  135. }
  136. oldFace = direction;
  137. }
  138. @Override
  139. public void renderTick(Entity ent, float lag)
  140. {
  141. if(ent.getHealth().wasHurt())
  142. {
  143. Shader.setColorEnabled(true);
  144. Shader.setMixColorEnabled(true);
  145. Shader.setMixColor(1.0f, 0.0f, 0.0f, 1.0f);
  146. }
  147. if(ent.getHealth().wasHealed())
  148. {
  149. Shader.setColorEnabled(true);
  150. Shader.setMixColorEnabled(true);
  151. Shader.setMixColor(0.0f, 1.0f, 0.0f, 1.0f);
  152. }
  153. LONDONER.bind();
  154. float x = Utils.interpolate(ent.getLastX(), ent.getX(), lag);
  155. float y = Utils.interpolate(ent.getLastY(), ent.getY(), lag);
  156. float m1;
  157. float m2;
  158. if(oldFace == Face.LEFT)
  159. {
  160. m1 = 2.0f * Tile.SIZE;
  161. m2 = 0.0f;
  162. }
  163. else
  164. {
  165. m1 = 0.0f;
  166. m2 = 2.0f * Tile.SIZE;
  167. }
  168. Shader.getTextureRenderer().drawRectangle(
  169. x + ox + m1 + OFFSET_X, y + OFFSET_Y,
  170. x + ox + m2 + OFFSET_X, y + 2.0f * Tile.SIZE + OFFSET_Y,
  171. tx, ty,
  172. tx + 0.125f, ty + 0.125f);
  173. if(ent.getHealth().wasHurt() || ent.getHealth().wasHealed())
  174. {
  175. Shader.setColorEnabled(false);
  176. Shader.setMixColorEnabled(false);
  177. }
  178. }
  179. @Override
  180. public void onCollideWithTile(Entity ent, int x, int y, Level l, Tile t, Face face)
  181. {
  182. if(ent.getHealth().isDead())
  183. {
  184. return;
  185. }
  186. if(hasRedEyes())
  187. {
  188. if(!(t instanceof RampTile) && face == direction && ent.getOwnForceX() == 0.0f)
  189. {
  190. shouldJump = true;
  191. }
  192. }
  193. else
  194. {
  195. if(t.shouldAiUseCollisionBox(x, y, l))
  196. {
  197. switch(face)
  198. {
  199. case LEFT:
  200. direction = Face.RIGHT;
  201. break;
  202. case RIGHT:
  203. direction = Face.LEFT;
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. @Override
  210. public void onCollideWithEntity(Entity ent, Entity other, Face face)
  211. {
  212. if(ent.getHealth().isDead())
  213. {
  214. return;
  215. }
  216. switch(face)
  217. {
  218. case LEFT:
  219. case RIGHT:
  220. if(hurtTicks > 0)
  221. {
  222. return;
  223. }
  224. if(other.getItemCollector().isHero() && hasRedEyes())
  225. {
  226. other.getHealth().addHealthPercent(-0.1f);
  227. other.applyForce(0.0f, -20.0f * Tile.SIZE_SCALE);
  228. if(ent.getCenterX() < other.getCenterX())
  229. {
  230. other.applyForce(20.0f * Tile.SIZE_SCALE, 0.0f);
  231. }
  232. else
  233. {
  234. other.applyForce(-20.0f * Tile.SIZE_SCALE, 0.0f);
  235. }
  236. }
  237. else
  238. {
  239. direction = direction.getOpposite();
  240. }
  241. break;
  242. case UP:
  243. ent.getHealth().addHealthPercent(-0.201f);
  244. other.applyForce(0.0f, -other.getMotionY() - other.getMovement().getJumpPower() * 0.5f);
  245. hurtTicks = 10;
  246. break;
  247. }
  248. }
  249. }