Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. package engine.game.level;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import engine.game.entities.Entity;
  8. import engine.game.entities.Player;
  9. import engine.game.gfx.Colours;
  10. import engine.game.gfx.Screen;
  11.  
  12. public class Level {
  13. private byte[] tiles;
  14. public int width;
  15. public int height;
  16. private Tile tile;
  17. Random random = new Random();
  18.  
  19. public List<Entity> entities = new ArrayList<Entity>();
  20.  
  21. public Level(int width, int height) {
  22. this.tiles = new byte[width * height];
  23. this.width = width;
  24. this.height = height;
  25. this.generateLevel();
  26. }
  27.  
  28. /**
  29. * public void generateLevel() { for (int y=0; y<this.height;y++){ for(int
  30. * x=0; x<this.width;x++){ if(x*y %10 <5){
  31. * this.tiles[x+y*this.width]=Tile.GRASS.getId(); }else if(x*y %10 < 6){
  32. * this.tiles[x+y*this.width]=Tile.STONE.getId(); } else if (x*y %10 < 7){
  33. * this.tiles[x+y*this.width]=Tile.GRASS1.getId(); }else {
  34. * this.tiles[x+y*this.width]=Tile.GRASS2.getId(); } } } }
  35. **/
  36. public void generateLevel() {
  37. for (int y = 0; y < this.height; y++) {
  38. for (int x = 0; x < this.width; x++) {
  39. switch (this.random.nextInt(3)) {
  40. case 0:tempshit(x,y);
  41. break;
  42.  
  43. case 1:
  44. if (x * y % 10 < 5) {
  45. this.tiles[x + y * this.width] = Tile.GRASS2.getId();
  46. } else {
  47. this.tiles[x + y * this.width] = Tile.STONE.getId();
  48. }
  49. break;
  50.  
  51. case 2:
  52. if (x + y % 10 < 5) {
  53. this.tiles[x + y * this.width] = Tile.GRASS2.getId();
  54. } else {
  55. this.tiles[x + y * this.width] = Tile.GRASS1.getId();
  56. }
  57. break;
  58.  
  59. case 3:
  60. this.tiles[x + y * this.width] = Tile.GRASS2.getId();
  61. break;
  62. default:
  63. this.tiles[x + y * this.width] = Tile.GRASS2.getId();
  64. break;
  65. }
  66. }
  67. }
  68. }
  69.  
  70. private void tempshit(int x, int y){
  71. switch (this.random.nextInt(1)) {
  72. case 0: this.tiles[x + y * this.width] = Tile.GRASS.getId();
  73. break;
  74. case 1: this.tiles[x + y * this.width] = Tile.GRASS.getId();
  75. break;
  76. default: this.tiles[x + y * this.width] = Tile.GRASS1.getId();
  77. break;
  78. }
  79. }
  80.  
  81. public void tick() {
  82. for (Entity e : this.entities) {
  83. e.tick();
  84. }
  85. }
  86.  
  87. public void renderTiles(Screen screen, int xOffset, int yOffset) {
  88. if (xOffset < 0)
  89. xOffset = 0;
  90. if (xOffset > ((this.width << 3) - screen.width))
  91. xOffset = ((this.width << 3) - screen.width);
  92. if (yOffset < 0)
  93. yOffset = 0;
  94. if (yOffset > ((this.height << 3) - screen.height))
  95. yOffset = ((this.height << 3) - screen.height);
  96.  
  97. screen.setOffset(xOffset, yOffset);
  98.  
  99. for (int y = 0; y < this.height; y++) {
  100. for (int x = 0; x < this.width; x++) {
  101. getTile(x, y).render(screen, this, x << 3, y << 3);
  102. }
  103. }
  104. }
  105.  
  106. public void renderEntities(Screen screen) {
  107. for (Entity e : this.entities) {
  108. e.render(screen);
  109. }
  110. }
  111.  
  112. private Tile getTile(int x, int y) {
  113. if (x < 0 || x > this.width || y < 0 || y > this.height)
  114. return Tile.VOID;
  115. return Tile.tiles[this.tiles[x + y * this.width]];
  116. }
  117.  
  118. public void addEntity(Entity entity) {
  119. this.entities.add(entity);
  120.  
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement