Entity.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package me.hammerle.supersnuvi.entity;
  2. import java.util.List;
  3. import me.hammerle.supersnuvi.entity.components.animator.Renderer;
  4. import me.hammerle.supersnuvi.entity.components.ai.Controller;
  5. import me.hammerle.supersnuvi.entity.components.Energy;
  6. import me.hammerle.supersnuvi.entity.components.Health;
  7. import me.hammerle.supersnuvi.entity.components.ItemCollector;
  8. import me.hammerle.supersnuvi.entity.components.Movement;
  9. import me.hammerle.supersnuvi.gamelogic.Level;
  10. import me.hammerle.supersnuvi.util.CollisionBox;
  11. import me.hammerle.supersnuvi.util.Face;
  12. import me.hammerle.supersnuvi.util.Utils;
  13. public final class Entity
  14. {
  15. public static float GRAVITY = 8.0f;
  16. public static float STEP = 0.0625f;
  17. // the last position is used for interpolation during rendering
  18. private float lastPosX;
  19. private float lastPosY;
  20. // the current position of the entity
  21. private float posX;
  22. private float posY;
  23. // the collision box of the entity
  24. private final CollisionBox box;
  25. // the motion before the movement collision check
  26. private float preMotionX;
  27. private float preMotionY;
  28. // the motion after the movement collision check
  29. private float motionX;
  30. private float motionY;
  31. // a flag indicating that the entity is on the ground
  32. private boolean onGround = false;
  33. // the level of the entity
  34. private final Level level;
  35. // entity components
  36. protected Renderer renderer;
  37. protected Controller controller;
  38. protected Health health;
  39. protected Energy energy;
  40. protected Movement move;
  41. protected ItemCollector itemCollector;
  42. protected Entity(Level level, float x, float y, CollisionBox box)
  43. {
  44. lastPosX = x;
  45. lastPosY = y;
  46. posX = x;
  47. posY = y;
  48. // ensure the box cannot be modified from the outside
  49. this.box = box.copy().offset(x, y);
  50. preMotionX = 0.0f;
  51. preMotionY = 0.0f;
  52. motionX = 0.0f;
  53. motionY = 0.0f;
  54. this.level = level;
  55. // components
  56. this.renderer = Renderer.NULL;
  57. this.controller = Controller.NULL;
  58. this.health = Health.NULL;
  59. this.energy = Energy.NULL;
  60. this.move = Movement.NULL;
  61. this.itemCollector = ItemCollector.NULL;
  62. }
  63. public Level getLevel()
  64. {
  65. return level;
  66. }
  67. //--------------------------------------------------------------------------
  68. // components
  69. //--------------------------------------------------------------------------
  70. public boolean isAnimated()
  71. {
  72. return renderer.isAnimated();
  73. }
  74. public Health getHealth()
  75. {
  76. return health;
  77. }
  78. public Energy getEnergy()
  79. {
  80. return energy;
  81. }
  82. public Movement getMovement()
  83. {
  84. return move;
  85. }
  86. public ItemCollector getItemCollector()
  87. {
  88. return itemCollector;
  89. }
  90. //--------------------------------------------------------------------------
  91. // basic stuff
  92. //--------------------------------------------------------------------------
  93. public float getSquaredDistance(Entity e)
  94. {
  95. return Utils.getSquaredDistance(
  96. posX + box.getWidth() * 0.5f, posY + box.getHeight() * 0.5f,
  97. e.posX + e.box.getWidth() * 0.5f, e.posY + e.box.getHeight() * 0.5f);
  98. }
  99. public float getX()
  100. {
  101. return posX;
  102. }
  103. public float getY()
  104. {
  105. return posY;
  106. }
  107. public float getLastX()
  108. {
  109. return lastPosX;
  110. }
  111. public float getLastY()
  112. {
  113. return lastPosY;
  114. }
  115. public float getCenterX()
  116. {
  117. return posX + box.getWidth() * 0.5f;
  118. }
  119. public float getCenterY()
  120. {
  121. return posY + box.getHeight() * 0.5f;
  122. }
  123. public Face getFace()
  124. {
  125. return preMotionX > 0.0f ? Face.RIGHT : Face.LEFT;
  126. }
  127. public float getMotionX()
  128. {
  129. return motionX;
  130. }
  131. public void setMotionX(float motionX)
  132. {
  133. this.motionX = motionX;
  134. }
  135. public float getMotionY()
  136. {
  137. return motionY;
  138. }
  139. public void setMotionY(float motionY)
  140. {
  141. this.motionY = motionY;
  142. }
  143. public float getPreMotionX()
  144. {
  145. return preMotionX;
  146. }
  147. public float getPreMotionY()
  148. {
  149. return preMotionY;
  150. }
  151. //--------------------------------------------------------------------------
  152. // ticking
  153. //--------------------------------------------------------------------------
  154. public void tick()
  155. {
  156. lastPosX = posX;
  157. lastPosY = posY;
  158. controller.tick();
  159. health.tick();
  160. energy.tick();
  161. preMotionX = motionX;
  162. preMotionY = motionY;
  163. if(move.hasGravity())
  164. {
  165. preMotionY += GRAVITY * move.getGravityFactor();
  166. }
  167. if(move.canMoveEverywhere())
  168. {
  169. motionX = preMotionX;
  170. motionY = preMotionY;
  171. }
  172. else
  173. {
  174. CollisionBox testBox = box.copy().expand(preMotionX, preMotionY);
  175. List<CollisionBox> boxes = level.getMovementBoxesAt(testBox);
  176. if(!boxes.isEmpty())
  177. {
  178. float mx = preMotionX;
  179. float my = preMotionY;
  180. testBox.reset();
  181. float oldX = testBox.getMinX();
  182. float oldY = testBox.getMinY();
  183. while(mx != 0.0 || my != 0.0)
  184. {
  185. testBox.save();
  186. if(mx < 0.0)
  187. {
  188. if(mx > -STEP)
  189. {
  190. testBox.offsetX(mx);
  191. mx = 0.0f;
  192. }
  193. else
  194. {
  195. testBox.offsetX(-STEP);
  196. mx += STEP;
  197. }
  198. }
  199. else if(mx > 0.0)
  200. {
  201. if(mx < STEP)
  202. {
  203. testBox.offsetX(mx);
  204. mx = 0.0f;
  205. }
  206. else
  207. {
  208. testBox.offsetX(STEP);
  209. mx -= STEP;
  210. }
  211. }
  212. for(CollisionBox cb : boxes)
  213. {
  214. if(cb.isColliding(testBox))
  215. {
  216. mx = 0.0f;
  217. testBox.reset();
  218. break;
  219. }
  220. }
  221. testBox.save();
  222. if(my < 0.0)
  223. {
  224. if(my > -STEP)
  225. {
  226. testBox.offsetY(my);
  227. my = 0.0f;
  228. }
  229. else
  230. {
  231. testBox.offsetY(-STEP);
  232. my += STEP;
  233. }
  234. }
  235. else if(my > 0.0)
  236. {
  237. if(my < STEP)
  238. {
  239. testBox.offsetY(my);
  240. my = 0.0f;
  241. }
  242. else
  243. {
  244. testBox.offsetY(STEP);
  245. my -= STEP;
  246. }
  247. }
  248. for(CollisionBox cb : boxes)
  249. {
  250. if(cb.isColliding(testBox))
  251. {
  252. my = 0.0f;
  253. testBox.reset();
  254. break;
  255. }
  256. }
  257. }
  258. motionX = testBox.getMinX() - oldX;
  259. motionY = testBox.getMinY() - oldY;
  260. }
  261. else
  262. {
  263. motionX = preMotionX;
  264. motionY = preMotionY;
  265. }
  266. }
  267. posX += motionX;
  268. posY += motionY;
  269. box.reset().offset(posX, posY);
  270. onGround = preMotionY > 0.0f && motionY == 0;
  271. move.setInWater(false);
  272. move.setFrictionFactor(0.7f);
  273. // apply collision
  274. CollisionBox cb = box.copy();
  275. for(Face f : Face.values())
  276. {
  277. cb.reset();
  278. cb.expand(f.getCollisionOffsetX(), f.getCollisionOffsetY());
  279. level.getEntitiesCollidingWith(this, box).forEach(ent -> ent.controller.onCollideWithEntity(ent, f));
  280. level.getCollisionBoxesAt(cb).forEach(loc ->
  281. {
  282. controller.onCollideWithTile(loc, f);
  283. loc.getTile().onEntityCollide(this, loc.getX(), loc.getY(), f.getOpposite());
  284. });
  285. }
  286. }
  287. public void renderTick(float lag)
  288. {
  289. renderer.renderTick(lag);
  290. }
  291. //--------------------------------------------------------------------------
  292. // gravity, friction
  293. //--------------------------------------------------------------------------
  294. public boolean isOnGround()
  295. {
  296. return onGround;
  297. }
  298. }