diff --git a/spec/world_spec.cr b/spec/world_spec.cr index 81139eb..d41f049 100644 --- a/spec/world_spec.cr +++ b/spec/world_spec.cr @@ -4,7 +4,7 @@ require "../src/game/world" describe "World" do it "should build crystal harvester" do world = Game::World.new - point = Game::Point.new(2, 3) + point = Game::Point.new 2, 3 cmd = Game::BuildCrystalHarvesterCommand.new(point) world.push(cmd) world.run(100) @@ -13,7 +13,7 @@ describe "World" do it "should fail when not enought resources" do world = Game::World.new - point = Game::Point.new(2, 3) + point = Game::Point.new 2, 3 cmd = Game::BuildCrystalRestorerCommand.new(point) expect_raises(Game::NotEnoughtResources) do world.push(cmd) diff --git a/src/expansion.cr b/src/expansion.cr index 7874078..bfc0c10 100644 --- a/src/expansion.cr +++ b/src/expansion.cr @@ -44,28 +44,29 @@ def render_time(ts) end def render_map(world) - size = world.map.size - (0...size).each do |x| + rows = world.map.rows + cols = world.map.cols + (0...rows).each do |x| if x == 0 printf "+" - (0...size).each do |y| + (0...cols).each do |y| printf "------+" end print "\n" end printf "|" - (0...size).each do |y| + (0...cols).each do |y| tile = world.map.get(x, y) printf "%s %d%d|", tile.letter.colorize(:green), x, y end print "\n" printf "|" - (0...size).each do |y| + (0...cols).each do |y| printf " |" end print "\n" printf "|" - (0...size).each do |y| + (0...cols).each do |y| tile = world.map.get(x, y) if tile.letter == 'f' printf "%6d|", world.map.get(x, y).cur @@ -75,7 +76,7 @@ def render_map(world) end print "\n" printf "+" - (0...size).each do |y| + (0...cols).each do |y| printf "------+" end print "\n" diff --git a/src/game/map.cr b/src/game/map.cr index 356587c..5b3bb9a 100644 --- a/src/game/map.cr +++ b/src/game/map.cr @@ -15,14 +15,21 @@ module Game end class Map - SIZE = 5 + alias TileRow = Array(Tile) + alias DataArray = Array(TileRow) - def initialize - @data = {} of String => Tile - (0...SIZE).each do |x| - (0...SIZE).each do |y| - self.set(PlateauTile.new(Point.new(x, y))) + property data : DataArray + + def initialize(rows : Int32, cols : Int32) + @rows = rows + @cols = cols + @data = DataArray.new @rows + @rows.times do |row_index| + tile_row = TileRow.new @cols + @cols.times do |col_index| + tile_row << PlateauTile.new(Point.new(row_index, col_index)) end + @data << tile_row end self.set(MainBaseTile.new(Point.new(0, 0))) self.set(CrystalTile.new(Point.new(1, 2), 100)) @@ -30,29 +37,28 @@ module Game self.set(CrystalTile.new(Point.new(3, 3), 100)) end - def size - SIZE - end + getter rows + getter cols def get(point : Point) : Tile - @data[key(point)] + @data[point.x][point.y] end def get(x : Int32, y : Int32) : Tile - get(Point.new(x, y)) + get Point.new(x, y) end def set(tile : Tile) - @data[key(tile.point)] = tile + set tile.point, tile end def set(point : Point, tile : Tile) - @data[key(point)] = tile + @data[point.x][point.y] = tile end def tiles - (0...SIZE).each do |x| - (0...SIZE).each do |y| + (0...@rows).each do |x| + (0...@cols).each do |y| point = Point.new(x, y) tile = self.get(point) yield point, tile @@ -74,9 +80,8 @@ module Game end seek_tile end + end - private def key(p : Point) : String - return sprintf "%d:%d", p.x, p.y - end + class MapGenerator end end diff --git a/src/game/world.cr b/src/game/world.cr index a87c697..9c51882 100644 --- a/src/game/world.cr +++ b/src/game/world.cr @@ -5,7 +5,7 @@ class Game::World def initialize(@ts = 0_i64) @start_ts = @ts - @map = Map.new + @map = Map.new 5, 5 @resources = Resources.new @queue = Queue.new @finished = false