Entity.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package me.hammerle.snuviengine.game;
  2. import java.util.List;
  3. import me.hammerle.snuviengine.api.Shader;
  4. import me.hammerle.snuviengine.game.BoxList.Node;
  5. import me.hammerle.snuviengine.util.Face;
  6. public class Entity implements IBoxListEntry
  7. {
  8. private static final double STEP = 0.0625;
  9. public Game game;
  10. public double lastXPos;
  11. public double lastYPos;
  12. public double xPos;
  13. public double yPos;
  14. public double width;
  15. public double height;
  16. public double vx = 0.0f;
  17. public double vy = 0.0f;
  18. public Node node;
  19. public int counter = 0;
  20. public Entity(Game game, double width, double height)
  21. {
  22. this.game = game;
  23. xPos = 0.0f;
  24. yPos = 0.0f;
  25. this.width = width;
  26. this.height = height;
  27. vx = 1; //Math.random() * 8 + 5;
  28. vy = 1; //Math.random() * 8 + 5;
  29. }
  30. private boolean isColliding(Entity ent)
  31. {
  32. return xPos + width > ent.xPos && ent.xPos + ent.width > xPos &&
  33. yPos + height > ent.yPos && ent.yPos + ent.height > yPos;
  34. }
  35. @Override
  36. public boolean isColliding(double minX, double minY, double maxX, double maxY)
  37. {
  38. return xPos + width > minX && maxX > xPos &&
  39. yPos + height > minY && maxY > yPos;
  40. }
  41. public void tick()
  42. {
  43. if(!(this instanceof Hero))
  44. {
  45. if(xPos < STEP)
  46. {
  47. vx = Math.abs(vx);
  48. }
  49. if(xPos + width > 400 - STEP)
  50. {
  51. vx = -Math.abs(vx);
  52. }
  53. if(yPos < STEP)
  54. {
  55. vy = Math.abs(vx);
  56. }
  57. if(yPos + height > 300 - STEP)
  58. {
  59. vy = -Math.abs(vx);
  60. }
  61. }
  62. lastXPos = xPos;
  63. lastYPos = yPos;
  64. if(vx == 0.0 && vy == 0.0)
  65. {
  66. return;
  67. }
  68. double lvx = vx;
  69. double lvy = vy;
  70. List<Entity> list = game.entities.getAllEntitiesAt(this,
  71. xPos - STEP + ((vx < 0) ? vx : 0),
  72. yPos - STEP + ((vy < 0) ? vy : 0),
  73. xPos + STEP + width + ((vx > 0) ? vx : 0),
  74. yPos + STEP + height + ((vy > 0) ? vy : 0));
  75. if(list.isEmpty())
  76. {
  77. xPos += vx;
  78. yPos += vy;
  79. game.entities.update(this);
  80. return;
  81. }
  82. while(lvx != 0.0 || lvy != 0.0)
  83. {
  84. double oldXPos = xPos;
  85. double oldYPos = yPos;
  86. if(lvx < 0.0)
  87. {
  88. if(lvx > -STEP)
  89. {
  90. xPos += lvx;
  91. lvx = 0.0;
  92. }
  93. else
  94. {
  95. xPos -= STEP;
  96. lvx += STEP;
  97. }
  98. }
  99. else if(lvx > 0.0)
  100. {
  101. if(lvx < STEP)
  102. {
  103. xPos += lvx;
  104. lvx = 0.0;
  105. }
  106. else
  107. {
  108. xPos += STEP;
  109. lvx -= STEP;
  110. }
  111. }
  112. for(Entity ent : list)
  113. {
  114. if(this.isColliding(ent))
  115. {
  116. lvx = 0.0f;
  117. xPos = oldXPos;
  118. break;
  119. }
  120. }
  121. if(lvy < 0.0)
  122. {
  123. if(lvy > -STEP)
  124. {
  125. yPos += lvy;
  126. lvy = 0.0;
  127. }
  128. else
  129. {
  130. yPos -= STEP;
  131. lvy += STEP;
  132. }
  133. }
  134. else if(lvy > 0.0)
  135. {
  136. if(lvy < STEP)
  137. {
  138. yPos += lvy;
  139. lvy = 0.0;
  140. }
  141. else
  142. {
  143. yPos += STEP;
  144. lvy -= STEP;
  145. }
  146. }
  147. for(Entity ent : list)
  148. {
  149. if(this.isColliding(ent))
  150. {
  151. lvy = 0.0f;
  152. yPos = oldYPos;
  153. break;
  154. }
  155. }
  156. }
  157. game.entities.update(this);
  158. for(Face f : Face.values())
  159. {
  160. double minX = xPos + STEP * f.getOffsetX();
  161. double minY = yPos + STEP * f.getOffsetY();
  162. double maxX = minX + width;
  163. double maxY = minY + height;
  164. list.stream().filter(ent -> ent.isColliding(minX, minY, maxX, maxY))
  165. .forEach(ent -> onCollide(f, ent));
  166. }
  167. }
  168. public void onCollide(Face f, Entity ent)
  169. {
  170. switch(f)
  171. {
  172. case UP:
  173. vy = Math.abs(vy);
  174. break;
  175. case DOWN:
  176. vy = -Math.abs(vy);
  177. break;
  178. case LEFT:
  179. vx = Math.abs(vx);
  180. break;
  181. case RIGHT:
  182. vx = -Math.abs(vx);
  183. break;
  184. }
  185. }
  186. public void renderTick(float lag)
  187. {
  188. Shader.getColorRenderer().setDepth((float) yPos);
  189. float x = (float) (lastXPos + (xPos - lastXPos) * lag);
  190. float y = (float) (lastYPos + (yPos - lastYPos) * lag);
  191. Shader.getColorRenderer().drawRectangle(x, y, (float) (x + width), (float) (y + height), 0x770000FF);
  192. }
  193. public double getX()
  194. {
  195. return xPos;
  196. }
  197. public double getY()
  198. {
  199. return yPos;
  200. }
  201. public void setPosition(double x, double y)
  202. {
  203. xPos = x;
  204. yPos = y;
  205. }
  206. @Override
  207. public double getCenterX()
  208. {
  209. return xPos + width / 2;
  210. }
  211. @Override
  212. public double getCenterY()
  213. {
  214. return yPos + height / 2;
  215. }
  216. @Override
  217. public double getWidth()
  218. {
  219. return width;
  220. }
  221. @Override
  222. public double getHeight()
  223. {
  224. return height;
  225. }
  226. @Override
  227. public Node getNode()
  228. {
  229. return node;
  230. }
  231. }