Level.java 9.3 KB

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