Entity.java 11 KB

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