Use points for map

This commit is contained in:
Anton Vakhrushev 2019-09-15 12:17:33 +03:00
parent 7652adebc4
commit 40c8e0640f
2 changed files with 21 additions and 5 deletions

View File

@ -33,6 +33,12 @@ class WoodTile < Tile
end
end
class WoodMillTile < Tile
def letter : Char
'm'
end
end
struct Point
def initialize(@x : Int32, @y : Int32)
end
@ -59,10 +65,14 @@ class Map
@data[key(Point.new(2, 2))] = WoodTile.new(100)
end
def get(x : Int32, y : Int32) : Tile
def get(point : Point) : Tile
@data[key(Point.new(x, y))]
end
def set(point : Point, tile : Tile)
@data[key(point)] = tile
end
def print
(0...4).each do |x|
(0...4).each do |y|

View File

@ -1,4 +1,5 @@
require "./queue"
require "./map"
class Resources
def initialize
@ -19,14 +20,18 @@ abstract class Command
abstract def run(ts : Int32, world : World)
end
class BuildMillCommand < Command
class BuildWoodMillCommand < Command
def initialize(@point : Point)
end
def supports?(world : World) : Bool
return true
end
def run(ts : Int32, world : World)
puts "build mill"
printf "build mill at [%d,%d]\n", @point.x, @point.y
c = GetWoodCommand.new
world.map.set(@point, WoodMillTile.new)
world.push(ts + 5, c)
end
end
@ -89,7 +94,7 @@ class World
if !command.supports?(self)
return false
end
printf "push command %d\n", ts
printf "push command %s, %d\n", typeof(command), ts
@queue.push(ts, command)
true
end
@ -109,6 +114,7 @@ end
w = World.new
w.map.print
w.push(0, BuildMillCommand.new)
w.push(0, BuildWoodMillCommand.new(Point.new(0, 0)))
w.push(1, BuildForesterHouseCommand.new)
w.run(20)
w.map.print