LondonerController.java 7.0 KB

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