Use two dimensional array in map
This commit is contained in:
parent
34cf40a099
commit
55f9bad188
@ -4,7 +4,7 @@ require "../src/game/world"
|
|||||||
describe "World" do
|
describe "World" do
|
||||||
it "should build crystal harvester" do
|
it "should build crystal harvester" do
|
||||||
world = Game::World.new
|
world = Game::World.new
|
||||||
point = Game::Point.new(2, 3)
|
point = Game::Point.new 2, 3
|
||||||
cmd = Game::BuildCrystalHarvesterCommand.new(point)
|
cmd = Game::BuildCrystalHarvesterCommand.new(point)
|
||||||
world.push(cmd)
|
world.push(cmd)
|
||||||
world.run(100)
|
world.run(100)
|
||||||
@ -13,7 +13,7 @@ describe "World" do
|
|||||||
|
|
||||||
it "should fail when not enought resources" do
|
it "should fail when not enought resources" do
|
||||||
world = Game::World.new
|
world = Game::World.new
|
||||||
point = Game::Point.new(2, 3)
|
point = Game::Point.new 2, 3
|
||||||
cmd = Game::BuildCrystalRestorerCommand.new(point)
|
cmd = Game::BuildCrystalRestorerCommand.new(point)
|
||||||
expect_raises(Game::NotEnoughtResources) do
|
expect_raises(Game::NotEnoughtResources) do
|
||||||
world.push(cmd)
|
world.push(cmd)
|
||||||
|
@ -44,28 +44,29 @@ def render_time(ts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_map(world)
|
def render_map(world)
|
||||||
size = world.map.size
|
rows = world.map.rows
|
||||||
(0...size).each do |x|
|
cols = world.map.cols
|
||||||
|
(0...rows).each do |x|
|
||||||
if x == 0
|
if x == 0
|
||||||
printf "+"
|
printf "+"
|
||||||
(0...size).each do |y|
|
(0...cols).each do |y|
|
||||||
printf "------+"
|
printf "------+"
|
||||||
end
|
end
|
||||||
print "\n"
|
print "\n"
|
||||||
end
|
end
|
||||||
printf "|"
|
printf "|"
|
||||||
(0...size).each do |y|
|
(0...cols).each do |y|
|
||||||
tile = world.map.get(x, y)
|
tile = world.map.get(x, y)
|
||||||
printf "%s %d%d|", tile.letter.colorize(:green), x, y
|
printf "%s %d%d|", tile.letter.colorize(:green), x, y
|
||||||
end
|
end
|
||||||
print "\n"
|
print "\n"
|
||||||
printf "|"
|
printf "|"
|
||||||
(0...size).each do |y|
|
(0...cols).each do |y|
|
||||||
printf " |"
|
printf " |"
|
||||||
end
|
end
|
||||||
print "\n"
|
print "\n"
|
||||||
printf "|"
|
printf "|"
|
||||||
(0...size).each do |y|
|
(0...cols).each do |y|
|
||||||
tile = world.map.get(x, y)
|
tile = world.map.get(x, y)
|
||||||
if tile.letter == 'f'
|
if tile.letter == 'f'
|
||||||
printf "%6d|", world.map.get(x, y).cur
|
printf "%6d|", world.map.get(x, y).cur
|
||||||
@ -75,7 +76,7 @@ def render_map(world)
|
|||||||
end
|
end
|
||||||
print "\n"
|
print "\n"
|
||||||
printf "+"
|
printf "+"
|
||||||
(0...size).each do |y|
|
(0...cols).each do |y|
|
||||||
printf "------+"
|
printf "------+"
|
||||||
end
|
end
|
||||||
print "\n"
|
print "\n"
|
||||||
|
@ -15,14 +15,21 @@ module Game
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Map
|
class Map
|
||||||
SIZE = 5
|
alias TileRow = Array(Tile)
|
||||||
|
alias DataArray = Array(TileRow)
|
||||||
|
|
||||||
def initialize
|
property data : DataArray
|
||||||
@data = {} of String => Tile
|
|
||||||
(0...SIZE).each do |x|
|
def initialize(rows : Int32, cols : Int32)
|
||||||
(0...SIZE).each do |y|
|
@rows = rows
|
||||||
self.set(PlateauTile.new(Point.new(x, y)))
|
@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
|
end
|
||||||
|
@data << tile_row
|
||||||
end
|
end
|
||||||
self.set(MainBaseTile.new(Point.new(0, 0)))
|
self.set(MainBaseTile.new(Point.new(0, 0)))
|
||||||
self.set(CrystalTile.new(Point.new(1, 2), 100))
|
self.set(CrystalTile.new(Point.new(1, 2), 100))
|
||||||
@ -30,29 +37,28 @@ module Game
|
|||||||
self.set(CrystalTile.new(Point.new(3, 3), 100))
|
self.set(CrystalTile.new(Point.new(3, 3), 100))
|
||||||
end
|
end
|
||||||
|
|
||||||
def size
|
getter rows
|
||||||
SIZE
|
getter cols
|
||||||
end
|
|
||||||
|
|
||||||
def get(point : Point) : Tile
|
def get(point : Point) : Tile
|
||||||
@data[key(point)]
|
@data[point.x][point.y]
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(x : Int32, y : Int32) : Tile
|
def get(x : Int32, y : Int32) : Tile
|
||||||
get(Point.new(x, y))
|
get Point.new(x, y)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(tile : Tile)
|
def set(tile : Tile)
|
||||||
@data[key(tile.point)] = tile
|
set tile.point, tile
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(point : Point, tile : Tile)
|
def set(point : Point, tile : Tile)
|
||||||
@data[key(point)] = tile
|
@data[point.x][point.y] = tile
|
||||||
end
|
end
|
||||||
|
|
||||||
def tiles
|
def tiles
|
||||||
(0...SIZE).each do |x|
|
(0...@rows).each do |x|
|
||||||
(0...SIZE).each do |y|
|
(0...@cols).each do |y|
|
||||||
point = Point.new(x, y)
|
point = Point.new(x, y)
|
||||||
tile = self.get(point)
|
tile = self.get(point)
|
||||||
yield point, tile
|
yield point, tile
|
||||||
@ -74,9 +80,8 @@ module Game
|
|||||||
end
|
end
|
||||||
seek_tile
|
seek_tile
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private def key(p : Point) : String
|
class MapGenerator
|
||||||
return sprintf "%d:%d", p.x, p.y
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,7 +5,7 @@ class Game::World
|
|||||||
|
|
||||||
def initialize(@ts = 0_i64)
|
def initialize(@ts = 0_i64)
|
||||||
@start_ts = @ts
|
@start_ts = @ts
|
||||||
@map = Map.new
|
@map = Map.new 5, 5
|
||||||
@resources = Resources.new
|
@resources = Resources.new
|
||||||
@queue = Queue.new
|
@queue = Queue.new
|
||||||
@finished = false
|
@finished = false
|
||||||
|
Loading…
Reference in New Issue
Block a user