Level.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package me.hammerle.supersnuvi.gamelogic;
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.function.Consumer;
  6. import java.util.stream.Collectors;
  7. import me.hammerle.snuviscript.code.Script;
  8. import me.hammerle.supersnuvi.Game;
  9. import me.hammerle.supersnuvi.entity.Entity;
  10. import me.hammerle.supersnuvi.entity.EntityBuilder;
  11. import me.hammerle.supersnuvi.rendering.TileUpdater;
  12. import me.hammerle.supersnuvi.tiles.Location;
  13. import me.hammerle.supersnuvi.tiles.Tile;
  14. import me.hammerle.supersnuvi.util.CollisionObject;
  15. import me.hammerle.supersnuvi.util.Utils;
  16. public abstract class Level
  17. {
  18. // -------------------------------------------------------------------------
  19. // level data
  20. // -------------------------------------------------------------------------
  21. private final TileUpdater tileUpdater = new TileUpdater();
  22. public abstract LevelData getData();
  23. public String getName()
  24. {
  25. return "Unknown";
  26. }
  27. public String getFileName()
  28. {
  29. return "Unknown";
  30. }
  31. public final TileUpdater getTileUpdater()
  32. {
  33. return tileUpdater;
  34. }
  35. // -------------------------------------------------------------------------
  36. // level stats
  37. // -------------------------------------------------------------------------
  38. public int getCurrentBottles()
  39. {
  40. return 0;
  41. }
  42. public int getMaxBottles()
  43. {
  44. return 0;
  45. }
  46. public void addBottles(int bottles)
  47. {
  48. }
  49. public float getTime()
  50. {
  51. return 0;
  52. }
  53. public void addMessage(String message)
  54. {
  55. }
  56. public String getMessage()
  57. {
  58. return null;
  59. }
  60. // -------------------------------------------------------------------------
  61. // entity related
  62. // -------------------------------------------------------------------------
  63. private final HashMap<Integer, Entity> entities = new HashMap<>();
  64. private final LinkedList<Entity> spawnQueue = new LinkedList<>();
  65. private Entity hero = null;
  66. private int entityCounter = 0;
  67. public final Entity getHero()
  68. {
  69. return hero;
  70. }
  71. public final void setHero(Entity hero)
  72. {
  73. this.hero = hero;
  74. }
  75. public final void removeEntities()
  76. {
  77. entities.clear();
  78. }
  79. public final void spawnEntity(Entity ent)
  80. {
  81. spawnQueue.add(ent);
  82. }
  83. public final void tickEntities()
  84. {
  85. entities.values().removeIf(ent ->
  86. {
  87. ent.tick(this);
  88. if(ent.getHealth().shouldDespawn() || (ent.getY() > getData().getHeight() * Tile.SIZE))
  89. {
  90. ent.getHealth().onDespawn();
  91. callEvent("entity_despawn", (sc) ->
  92. {
  93. sc.setVar("entity", ent);
  94. }, null);
  95. return true;
  96. }
  97. return false;
  98. });
  99. if(!spawnQueue.isEmpty())
  100. {
  101. spawnQueue.forEach(ent ->
  102. {
  103. entities.put(entityCounter++, ent);
  104. callEvent("entity_spawn", (sc) ->
  105. {
  106. sc.setVar("entity", ent);
  107. }, null);
  108. });
  109. spawnQueue.clear();
  110. }
  111. }
  112. public final List<Entity> getEntitiesCollidingWith(Entity not, CollisionObject cb)
  113. {
  114. return entities.values().stream().filter(ent -> ent != not && ent.getBox().isColliding(cb)).collect(Collectors.toList());
  115. }
  116. public final void forEachEntity(Consumer<Entity> c)
  117. {
  118. entities.values().forEach(c);
  119. }
  120. // -------------------------------------------------------------------------
  121. // controlling
  122. // -------------------------------------------------------------------------
  123. private boolean shouldReset = false;
  124. private boolean done = false;
  125. public abstract void tick();
  126. public final void finishLevel()
  127. {
  128. shouldReset = true;
  129. done = true;
  130. }
  131. public final boolean isFinished()
  132. {
  133. return done;
  134. }
  135. public final boolean shouldReset()
  136. {
  137. return shouldReset;
  138. }
  139. public final void scheduleReset()
  140. {
  141. shouldReset = true;
  142. }
  143. public final void reset()
  144. {
  145. onReset();
  146. shouldReset = false;
  147. done = false;
  148. // spawn entities
  149. getData().forEachEntity((x, y, tile) ->
  150. {
  151. if(tile > 0)
  152. {
  153. Entity ent = EntityBuilder.fromId(tile, this, Utils.toCoord(x), Utils.toCoord(y));
  154. if(ent != null)
  155. {
  156. entities.put(entityCounter++, ent);
  157. }
  158. }
  159. }, 0, getData().getWidth(), 0, getData().getHeight());
  160. }
  161. protected void onReset()
  162. {
  163. };
  164. // -------------------------------------------------------------------------
  165. // collision
  166. // -------------------------------------------------------------------------
  167. private Tile getInteractionTile(int x, int y)
  168. {
  169. int i = getData().getInteractionTile(x, y);
  170. if(i == -1)
  171. {
  172. return Game.FALLBACK_TILE;
  173. }
  174. return Game.get().getTile(i);
  175. }
  176. private CollisionObject getMovementBox(int x, int y)
  177. {
  178. int i = getData().getInteractionTile(x, y);
  179. if(i == -1)
  180. {
  181. return CollisionObject.NULL_BOX;
  182. }
  183. return Game.get().getTile(i).getMovementBox(x, y, this).reset().offset(Utils.toCoord(x), Utils.toCoord(y));
  184. }
  185. public final List<CollisionObject> getMovementBoxesAt(CollisionObject box, Entity not)
  186. {
  187. List<CollisionObject> boxes;
  188. if(not != null)
  189. {
  190. boxes = getEntitiesCollidingWith(not, box).stream().map(ent -> ent.getBox()).collect(Collectors.toList());
  191. }
  192. else
  193. {
  194. boxes = new LinkedList<>();
  195. }
  196. int startX = Utils.toBlock(box.getMinX());
  197. int endX = Utils.toBlock(box.getMaxX());
  198. int startY = Utils.toBlock(box.getMinY());
  199. int endY = Utils.toBlock(box.getMaxY());
  200. for(int x = startX; x <= endX; x++)
  201. {
  202. for(int y = startY; y <= endY; y++)
  203. {
  204. CollisionObject cb = getMovementBox(x, y);
  205. if(cb.mayCollide(box) && cb != CollisionObject.NULL_BOX)
  206. {
  207. boxes.add(cb.copy());
  208. }
  209. }
  210. }
  211. return boxes;
  212. }
  213. private CollisionObject getCollisionBox(int x, int y)
  214. {
  215. int i = getData().getInteractionTile(x, y);
  216. if(i == -1)
  217. {
  218. return CollisionObject.NULL_BOX;
  219. }
  220. return Game.get().getTile(i).getCollisionBox(x, y, this).reset().offset(Utils.toCoord(x), Utils.toCoord(y));
  221. }
  222. public final List<Location> getCollisionBoxesAt(CollisionObject cb)
  223. {
  224. LinkedList<Location> boxes = new LinkedList<>();
  225. int startX = Utils.toBlock(cb.getMinX());
  226. int endX = Utils.toBlock(cb.getMaxX());
  227. int startY = Utils.toBlock(cb.getMinY());
  228. int endY = Utils.toBlock(cb.getMaxY());
  229. for(int x = startX; x <= endX; x++)
  230. {
  231. for(int y = startY; y <= endY; y++)
  232. {
  233. if(getCollisionBox(x, y).isColliding(cb))
  234. {
  235. boxes.add(new Location(getInteractionTile(x, y), this, x, y));
  236. }
  237. }
  238. }
  239. return boxes;
  240. }
  241. // -------------------------------------------------------------------------
  242. // scripting
  243. // -------------------------------------------------------------------------
  244. public void callEvent(String name, Consumer<Script> before, Consumer<Script> after)
  245. {
  246. }
  247. public final void callEvent(String name)
  248. {
  249. callEvent(name, null, null);
  250. }
  251. }