daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package level;
  2.  
  3. import java.util.*;
  4. import java.awt.*;
  5.  
  6. import util.*;
  7. import world.*;
  8. import world.objects.*;
  9.  
  10. // Actually more like a builder or a factory for generic level stuff,
  11. // collect information about a level and later create actual useful stuff
  12. public class Level
  13. {
  14.     // TODO change from public to private,
  15.     // make some nice setters ;)
  16.     public String name;
  17.     public int levelNum;
  18.  
  19.     public int width, height;
  20.     public int gridSize;
  21.  
  22.     public Vec2i playerPos;
  23.  
  24.     // Our current image sets
  25.     public String tileset;
  26.     public String propset;
  27.  
  28.     // Map set index with positions
  29.     public Map<Vec2i, Integer> tiles;
  30.     public Map<Vec2i, Integer> props;
  31.  
  32.     // Map objects with position
  33.     public Map<Vec2i, ObjectBuilder> objects;
  34.  
  35.     public Level()
  36.     {
  37.         tiles = new HashMap<Vec2i, Integer>();
  38.         props = new HashMap<Vec2i, Integer>();
  39.         objects = new HashMap<Vec2i, ObjectBuilder>();
  40.     }
  41.  
  42.     public Grid createGrid()
  43.     {
  44.         // Initialize tiles
  45.         Map<Vec2i, Tile> realTiles = new HashMap<Vec2i, Tile>(width * height);
  46.  
  47.         for (Map.Entry<Vec2i, Integer> e : tiles.entrySet())
  48.         {
  49.             Vec2i pos = e.getKey();
  50.             int index = e.getValue();
  51.  
  52.             realTiles.put(pos, Tile.createTile(tileset, index, gridSize));
  53.         }
  54.  
  55.         // initialize border tiles, necessary to get correct amount, and direction, to "real" neighbours
  56.         Map<Vec2i, Tile> borderTiles = new HashMap<Vec2i, Tile>(width * height / 2);
  57.  
  58.         for (int x = 0; x < width; ++x) {
  59.             for (int y = 0; y < height; ++y) {
  60.                 final Vec2i pos = new Vec2i(x, y);
  61.  
  62.                 if (!realTiles.containsKey(pos) && !borderTiles.containsKey(pos)) {
  63.                     // if we have actual neighbour tiles
  64.                     boolean up = realTiles.containsKey(new Vec2i(x, y - 1));
  65.                     boolean down = realTiles.containsKey(new Vec2i(x, y + 1));
  66.                     boolean left = realTiles.containsKey(new Vec2i(x - 1, y));
  67.                     boolean right = realTiles.containsKey(new Vec2i(x + 1, y));
  68.  
  69.                     borderTiles.put(pos, Tile.createBorderTile(
  70.                         tileset, gridSize,
  71.                         up, down, left, right
  72.                     ));
  73.                 }
  74.             }
  75.         }
  76.  
  77.         realTiles.putAll(borderTiles);
  78.  
  79.         //Insert props to tiles
  80.         for (Map.Entry<Vec2i, Integer> e : props.entrySet())
  81.         {
  82.             Vec2i pos = e.getKey();
  83.             int index = e.getValue();
  84.  
  85.             Prop prop = new Prop(propset, index, gridSize);
  86.             realTiles.get(pos).attach(prop);
  87.         }
  88.  
  89.         for (Map.Entry<Vec2i, ObjectBuilder> e : objects.entrySet())
  90.         {
  91.             Vec2i pos = e.getKey();
  92.             ObjectBuilder builder = e.getValue();
  93.  
  94.             InteractiveObject o = ObjectFactory.createObject(builder);
  95.             if (o == null) {
  96.                 System.out.println("It is null");
  97.             }
  98.             else {
  99.                 realTiles.get(pos).attach(o);
  100.             }
  101.         }
  102.  
  103.         Grid grid = new Grid(width, height, gridSize, realTiles);
  104.  
  105.         return grid;
  106.     }
  107.  
  108.     public Vec2i playerGridPos()
  109.     {
  110.         return playerPos;
  111.     }
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top