Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package level;
- import java.util.*;
- import java.awt.*;
- import util.*;
- import world.*;
- import world.objects.*;
- // Actually more like a builder or a factory for generic level stuff,
- // collect information about a level and later create actual useful stuff
- public class Level
- {
- // TODO change from public to private,
- // make some nice setters ;)
- public String name;
- public int levelNum;
- public int width, height;
- public int gridSize;
- public Vec2i playerPos;
- // Our current image sets
- public String tileset;
- public String propset;
- // Map set index with positions
- public Map<Vec2i, Integer> tiles;
- public Map<Vec2i, Integer> props;
- // Map objects with position
- public Map<Vec2i, ObjectBuilder> objects;
- public Level()
- {
- tiles = new HashMap<Vec2i, Integer>();
- props = new HashMap<Vec2i, Integer>();
- objects = new HashMap<Vec2i, ObjectBuilder>();
- }
- public Grid createGrid()
- {
- // Initialize tiles
- Map<Vec2i, Tile> realTiles = new HashMap<Vec2i, Tile>(width * height);
- for (Map.Entry<Vec2i, Integer> e : tiles.entrySet())
- {
- Vec2i pos = e.getKey();
- int index = e.getValue();
- realTiles.put(pos, Tile.createTile(tileset, index, gridSize));
- }
- // initialize border tiles, necessary to get correct amount, and direction, to "real" neighbours
- Map<Vec2i, Tile> borderTiles = new HashMap<Vec2i, Tile>(width * height / 2);
- for (int x = 0; x < width; ++x) {
- for (int y = 0; y < height; ++y) {
- final Vec2i pos = new Vec2i(x, y);
- if (!realTiles.containsKey(pos) && !borderTiles.containsKey(pos)) {
- // if we have actual neighbour tiles
- boolean up = realTiles.containsKey(new Vec2i(x, y - 1));
- boolean down = realTiles.containsKey(new Vec2i(x, y + 1));
- boolean left = realTiles.containsKey(new Vec2i(x - 1, y));
- boolean right = realTiles.containsKey(new Vec2i(x + 1, y));
- borderTiles.put(pos, Tile.createBorderTile(
- tileset, gridSize,
- up, down, left, right
- ));
- }
- }
- }
- realTiles.putAll(borderTiles);
- //Insert props to tiles
- for (Map.Entry<Vec2i, Integer> e : props.entrySet())
- {
- Vec2i pos = e.getKey();
- int index = e.getValue();
- Prop prop = new Prop(propset, index, gridSize);
- realTiles.get(pos).attach(prop);
- }
- for (Map.Entry<Vec2i, ObjectBuilder> e : objects.entrySet())
- {
- Vec2i pos = e.getKey();
- ObjectBuilder builder = e.getValue();
- InteractiveObject o = ObjectFactory.createObject(builder);
- if (o == null) {
- System.out.println("It is null");
- }
- else {
- realTiles.get(pos).attach(o);
- }
- }
- Grid grid = new Grid(width, height, gridSize, realTiles);
- return grid;
- }
- public Vec2i playerGridPos()
- {
- return playerPos;
- }
- }
Add Comment
Please, Sign In to add comment