Level.java 7.3 KB

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