Entity.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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
  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. public boolean isColliding(double minX, double minY, double maxX, double maxY)
  36. {
  37. return xPos + width > minX && maxX > xPos &&
  38. yPos + height > minY && maxY > yPos;
  39. }
  40. public void tick()
  41. {
  42. if(!(this instanceof Hero))
  43. {
  44. if(xPos < STEP)
  45. {
  46. vx = Math.abs(vx);
  47. }
  48. if(xPos + width > 400 - STEP)
  49. {
  50. vx = -Math.abs(vx);
  51. }
  52. if(yPos < STEP)
  53. {
  54. vy = Math.abs(vx);
  55. }
  56. if(yPos + height > 300 - STEP)
  57. {
  58. vy = -Math.abs(vx);
  59. }
  60. }
  61. lastXPos = xPos;
  62. lastYPos = yPos;
  63. if(vx == 0.0 && vy == 0.0)
  64. {
  65. return;
  66. }
  67. double lvx = vx;
  68. double lvy = vy;
  69. List<Entity> list = game.entities.getAllEntitiesAt(this,
  70. xPos - STEP + ((vx < 0) ? vx : 0),
  71. yPos - STEP + ((vy < 0) ? vy : 0),
  72. xPos + STEP + width + ((vx > 0) ? vx : 0),
  73. yPos + STEP + height + ((vy > 0) ? vy : 0));
  74. if(list.isEmpty())
  75. {
  76. xPos += vx;
  77. yPos += vy;
  78. game.entities.update(this);
  79. return;
  80. }
  81. while(lvx != 0.0 || lvy != 0.0)
  82. {
  83. double oldXPos = xPos;
  84. double oldYPos = yPos;
  85. if(lvx < 0.0)
  86. {
  87. if(lvx > -STEP)
  88. {
  89. xPos += lvx;
  90. lvx = 0.0;
  91. }
  92. else
  93. {
  94. xPos -= STEP;
  95. lvx += STEP;
  96. }
  97. }
  98. else if(lvx > 0.0)
  99. {
  100. if(lvx < STEP)
  101. {
  102. xPos += lvx;
  103. lvx = 0.0;
  104. }
  105. else
  106. {
  107. xPos += STEP;
  108. lvx -= STEP;
  109. }
  110. }
  111. for(Entity ent : list)
  112. {
  113. if(this.isColliding(ent))
  114. {
  115. lvx = 0.0f;
  116. xPos = oldXPos;
  117. break;
  118. }
  119. }
  120. if(lvy < 0.0)
  121. {
  122. if(lvy > -STEP)
  123. {
  124. yPos += lvy;
  125. lvy = 0.0;
  126. }
  127. else
  128. {
  129. yPos -= STEP;
  130. lvy += STEP;
  131. }
  132. }
  133. else if(lvy > 0.0)
  134. {
  135. if(lvy < STEP)
  136. {
  137. yPos += lvy;
  138. lvy = 0.0;
  139. }
  140. else
  141. {
  142. yPos += STEP;
  143. lvy -= STEP;
  144. }
  145. }
  146. for(Entity ent : list)
  147. {
  148. if(this.isColliding(ent))
  149. {
  150. lvy = 0.0f;
  151. yPos = oldYPos;
  152. break;
  153. }
  154. }
  155. }
  156. game.entities.update(this);
  157. for(Face f : Face.values())
  158. {
  159. double minX = xPos + STEP * f.getOffsetX();
  160. double minY = yPos + STEP * f.getOffsetY();
  161. double maxX = minX + width;
  162. double maxY = minY + height;
  163. list.stream().filter(ent -> ent.isColliding(minX, minY, maxX, maxY))
  164. .forEach(ent -> onCollide(f, ent));
  165. }
  166. }
  167. public void onCollide(Face f, Entity ent)
  168. {
  169. switch(f)
  170. {
  171. case UP:
  172. vy = Math.abs(vy);
  173. break;
  174. case DOWN:
  175. vy = -Math.abs(vy);
  176. break;
  177. case LEFT:
  178. vx = Math.abs(vx);
  179. break;
  180. case RIGHT:
  181. vx = -Math.abs(vx);
  182. break;
  183. }
  184. }
  185. public void renderTick(float lag)
  186. {
  187. Shader.getColorRenderer().setDepth((float) yPos);
  188. float x = (float) (lastXPos + (xPos - lastXPos) * lag);
  189. float y = (float) (lastYPos + (yPos - lastYPos) * lag);
  190. Shader.getColorRenderer().drawRectangle(x, y, (float) (x + width), (float) (y + height), 0x770000FF);
  191. }
  192. public double getX()
  193. {
  194. return xPos;
  195. }
  196. public double getY()
  197. {
  198. return yPos;
  199. }
  200. public void setPosition(double x, double y)
  201. {
  202. xPos = x;
  203. yPos = y;
  204. }
  205. }