Entity.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. energy.tick();
  163. preMotionX = motionX;
  164. preMotionY = motionY;
  165. if(preMotionX != 0.0f)
  166. {
  167. face = preMotionX > 0.0f ? Face.RIGHT : Face.LEFT;
  168. }
  169. if(move.hasGravity())
  170. {
  171. preMotionY += GRAVITY * move.getGravityFactor();
  172. }
  173. if(move.canMoveEverywhere())
  174. {
  175. motionX = preMotionX;
  176. motionY = preMotionY;
  177. }
  178. else
  179. {
  180. CollisionBox testBox = box.copy().expand(preMotionX, preMotionY);
  181. List<CollisionBox> boxes = level.getMovementBoxesAt(testBox, this);
  182. if(!boxes.isEmpty())
  183. {
  184. float mx = preMotionX;
  185. float my = preMotionY;
  186. testBox.reset();
  187. float oldX = testBox.getMinX();
  188. float oldY = testBox.getMinY();
  189. while(mx != 0.0 || my != 0.0)
  190. {
  191. testBox.save();
  192. if(mx < 0.0)
  193. {
  194. if(mx > -STEP)
  195. {
  196. testBox.offsetX(mx);
  197. mx = 0.0f;
  198. }
  199. else
  200. {
  201. testBox.offsetX(-STEP);
  202. mx += STEP;
  203. }
  204. }
  205. else if(mx > 0.0)
  206. {
  207. if(mx < STEP)
  208. {
  209. testBox.offsetX(mx);
  210. mx = 0.0f;
  211. }
  212. else
  213. {
  214. testBox.offsetX(STEP);
  215. mx -= STEP;
  216. }
  217. }
  218. for(CollisionBox cb : boxes)
  219. {
  220. if(cb.isColliding(testBox))
  221. {
  222. mx = 0.0f;
  223. testBox.reset();
  224. break;
  225. }
  226. }
  227. testBox.save();
  228. if(my < 0.0)
  229. {
  230. if(my > -STEP)
  231. {
  232. testBox.offsetY(my);
  233. my = 0.0f;
  234. }
  235. else
  236. {
  237. testBox.offsetY(-STEP);
  238. my += STEP;
  239. }
  240. }
  241. else if(my > 0.0)
  242. {
  243. if(my < STEP)
  244. {
  245. testBox.offsetY(my);
  246. my = 0.0f;
  247. }
  248. else
  249. {
  250. testBox.offsetY(STEP);
  251. my -= STEP;
  252. }
  253. }
  254. for(CollisionBox cb : boxes)
  255. {
  256. if(cb.isColliding(testBox))
  257. {
  258. my = 0.0f;
  259. testBox.reset();
  260. break;
  261. }
  262. }
  263. }
  264. motionX = testBox.getMinX() - oldX;
  265. motionY = testBox.getMinY() - oldY;
  266. }
  267. else
  268. {
  269. motionX = preMotionX;
  270. motionY = preMotionY;
  271. }
  272. }
  273. posX += motionX;
  274. posY += motionY;
  275. box.reset().offset(posX, posY);
  276. onGround = preMotionY > 0.0f && motionY == 0;
  277. move.setInWater(false);
  278. move.setFrictionFactor(0.6f);
  279. // apply collision
  280. CollisionBox cb = box.copy();
  281. for(Face f : Face.values())
  282. {
  283. cb.reset();
  284. cb.expand(f.getCollisionOffsetX(), f.getCollisionOffsetY());
  285. level.getEntitiesCollidingWith(this, box).forEach(ent -> ent.controller.onCollideWithEntity(ent, f));
  286. level.getCollisionBoxesAt(cb).forEach(loc ->
  287. {
  288. controller.onCollideWithTile(loc, f);
  289. loc.getTile().onEntityCollide(this, loc.getX(), loc.getY(), f.getOpposite());
  290. });
  291. }
  292. motionX *= move.getFrictionFactor();
  293. if(Math.abs(motionX) < 0.3)
  294. {
  295. motionX = 0.0f;
  296. }
  297. health.tick();
  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. }