Advertisement
Guest User

Level.java

a guest
Sep 8th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. package main;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import main.Entity.particle.Particle;
  7. import main.Entity.Entity;
  8. import main.Entity.projectile.Projectile;
  9. import main.Entity.spawner.Spawner;
  10. import main.tile.Tile;
  11.  
  12. public class Level {
  13.  
  14.  
  15.  
  16. protected int width, height;
  17. protected int[] tilesInt;
  18. protected int[] tiles;
  19. protected int tile_size;
  20.  
  21. private List<Entity> entities = new ArrayList<Entity>();
  22. private List<Projectile> projectiles = new ArrayList<Projectile>();
  23. private List<Particle> particles = new ArrayList<Particle>();
  24.  
  25. public static Level spawnLevel = new Level ("/spawn.png");
  26.  
  27. public Level(int width, int height) {
  28. this.width = width;
  29. this.height = height;
  30. tilesInt = new int[width * height];
  31. generateLevel();
  32. }
  33.  
  34. public Level(String path) {
  35. loadLevel(path);
  36. generateLevel();
  37.  
  38. add(new Spawner(392, 1695, Spawner.Type.PARTICLE, 50, this));
  39. }
  40.  
  41. protected void loadLevel(String path) {
  42. }
  43.  
  44. protected void generateLevel() {
  45.  
  46.  
  47. }
  48.  
  49. public void update() {
  50. for (int i = 0; i < particles.size(); i++) {
  51. particles.get(i).update();
  52. }
  53. for (int i = 0; i < entities.size(); i++) {
  54. entities.get(i).update();
  55. }
  56. for (int i = 0; i < projectiles.size(); i++) {
  57. projectiles.get(i).update();
  58. }
  59.  
  60. }
  61. public void remove() {
  62. for (int i = 0; i < particles.size(); i++) {
  63. if (particles.get(i).isRemoved()) particles.remove(i);;
  64. }
  65. for (int i = 0; i < entities.size(); i++) {
  66. if (entities.get(i).isRemoved()) entities.remove(i);
  67. }
  68. for (int i = 0; i < projectiles.size(); i++) {
  69. if (projectiles.get(i).isRemoved()) projectiles.remove(i);
  70. }
  71.  
  72. }
  73.  
  74.  
  75. public List<Projectile> getProjectiles() {
  76. return projectiles;
  77. }
  78.  
  79. private void time() {
  80.  
  81. }
  82.  
  83. public boolean solidCollision(double x, double y, double xa, double ya, int size) {
  84. boolean solid = false;
  85. for (int c = 0; c < 4; c++) {
  86. int xt = (((int) x + (int) xa) + c % 2 * size / 5) >> 4;
  87. int yt = (((int) y + (int) ya) + c / 2 * size / 5) >> 4;
  88. if (getTile(xt, yt).solid()) solid = true;
  89. }
  90. return solid;
  91. }
  92.  
  93. public void render(int xScroll, int yScroll, Screen screen) {
  94. screen.setOffset(xScroll, yScroll);
  95. int x0 = xScroll >> 4;
  96. int x1 = (xScroll + screen.width + 16) >> 4;
  97. int y0 = yScroll >> 4;
  98. int y1 = (yScroll + screen.height + 16) >> 4;
  99.  
  100. for (int y = y0; y < y1; y++) {
  101. for(int x = x0; x < x1; x++) {
  102. getTile(x, y).render(x, y, screen);
  103. }
  104. }
  105.  
  106. for (int i = 0; i < particles.size(); i++) {
  107. particles.get(i).render(screen);
  108. }
  109. for (int i = 0; i < entities.size(); i++) {
  110. entities.get(i).render(screen);
  111. }
  112. for (int i = 0; i < projectiles.size(); i++) {
  113. projectiles.get(i).render(screen);
  114. }
  115.  
  116. }
  117.  
  118. public void add(Entity e) {
  119. e.init(this);
  120. if (e instanceof Particle) {
  121. particles.add((Particle) e);
  122. } else if (e instanceof Projectile) {
  123. projectiles.add((Projectile) e);
  124. } else {
  125. entities.add(e);
  126. }
  127. }
  128.  
  129. //Grass = 0xFF00FF00
  130. //Rock = 0xFFb4b4b4
  131. //Flower = 0xFFFFFF00
  132.  
  133. public Tile getTile(int x, int y) {
  134. if (x < 0 || y < 0 || x >= width || y >= height) return Tile.voidTile;
  135. if (tiles [x + y * width] == Tile.colour_grass) return Tile.grass;
  136. if (tiles [x + y * width] == Tile.colour_flower) return Tile.flower;
  137. if (tiles [x + y * width] == Tile.colour_woodFloor) return Tile.woodFloor;
  138. if (tiles [x + y * width] == Tile.colour_water) return Tile.water;
  139. if (tiles [x + y * width] == Tile.colour_rockWall) return Tile.rockWall;
  140. if (tiles [x + y * width] == Tile.colour_rockPath) return Tile.rockPath;
  141. return Tile.voidTile;
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement