Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package engine.game.level;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import engine.game.entities.Entity;
- import engine.game.entities.Player;
- import engine.game.gfx.Colours;
- import engine.game.gfx.Screen;
- public class Level {
- private byte[] tiles;
- public int width;
- public int height;
- private Tile tile;
- Random random = new Random();
- public List<Entity> entities = new ArrayList<Entity>();
- public Level(int width, int height) {
- this.tiles = new byte[width * height];
- this.width = width;
- this.height = height;
- this.generateLevel();
- }
- /**
- * public void generateLevel() { for (int y=0; y<this.height;y++){ for(int
- * x=0; x<this.width;x++){ if(x*y %10 <5){
- * this.tiles[x+y*this.width]=Tile.GRASS.getId(); }else if(x*y %10 < 6){
- * this.tiles[x+y*this.width]=Tile.STONE.getId(); } else if (x*y %10 < 7){
- * this.tiles[x+y*this.width]=Tile.GRASS1.getId(); }else {
- * this.tiles[x+y*this.width]=Tile.GRASS2.getId(); } } } }
- **/
- public void generateLevel() {
- for (int y = 0; y < this.height; y++) {
- for (int x = 0; x < this.width; x++) {
- switch (this.random.nextInt(3)) {
- case 0:tempshit(x,y);
- break;
- case 1:
- if (x * y % 10 < 5) {
- this.tiles[x + y * this.width] = Tile.GRASS2.getId();
- } else {
- this.tiles[x + y * this.width] = Tile.STONE.getId();
- }
- break;
- case 2:
- if (x + y % 10 < 5) {
- this.tiles[x + y * this.width] = Tile.GRASS2.getId();
- } else {
- this.tiles[x + y * this.width] = Tile.GRASS1.getId();
- }
- break;
- case 3:
- this.tiles[x + y * this.width] = Tile.GRASS2.getId();
- break;
- default:
- this.tiles[x + y * this.width] = Tile.GRASS2.getId();
- break;
- }
- }
- }
- }
- private void tempshit(int x, int y){
- switch (this.random.nextInt(1)) {
- case 0: this.tiles[x + y * this.width] = Tile.GRASS.getId();
- break;
- case 1: this.tiles[x + y * this.width] = Tile.GRASS.getId();
- break;
- default: this.tiles[x + y * this.width] = Tile.GRASS1.getId();
- break;
- }
- }
- public void tick() {
- for (Entity e : this.entities) {
- e.tick();
- }
- }
- public void renderTiles(Screen screen, int xOffset, int yOffset) {
- if (xOffset < 0)
- xOffset = 0;
- if (xOffset > ((this.width << 3) - screen.width))
- xOffset = ((this.width << 3) - screen.width);
- if (yOffset < 0)
- yOffset = 0;
- if (yOffset > ((this.height << 3) - screen.height))
- yOffset = ((this.height << 3) - screen.height);
- screen.setOffset(xOffset, yOffset);
- for (int y = 0; y < this.height; y++) {
- for (int x = 0; x < this.width; x++) {
- getTile(x, y).render(screen, this, x << 3, y << 3);
- }
- }
- }
- public void renderEntities(Screen screen) {
- for (Entity e : this.entities) {
- e.render(screen);
- }
- }
- private Tile getTile(int x, int y) {
- if (x < 0 || x > this.width || y < 0 || y > this.height)
- return Tile.VOID;
- return Tile.tiles[this.tiles[x + y * this.width]];
- }
- public void addEntity(Entity entity) {
- this.entities.add(entity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement