Entity.java 9.5 KB

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