Entity.java 11 KB

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