Improve wood mill command

This commit is contained in:
Anton Vakhrushev 2019-09-15 14:17:38 +03:00
parent 05801dd983
commit fb139b2334
2 changed files with 42 additions and 32 deletions

View File

@ -9,6 +9,10 @@ struct Point
def y def y
@y @y
end end
def distance(p : Point) : Int32
return (p.x - @x).abs + (p.y - @y).abs
end
end end
abstract class Tile abstract class Tile
@ -79,7 +83,7 @@ class Map
end end
def get(point : Point) : Tile def get(point : Point) : Tile
@data[key(Point.new(x, y))] @data[key(point)]
end end
def set(tile : Tile) def set(tile : Tile)
@ -90,6 +94,24 @@ class Map
@data[key(point)] = tile @data[key(point)] = tile
end end
def nearest_wood(point : Point) : Point | Nil
p = nil
d = 99999
(0...4).each do |x|
(0...4).each do |y|
tile = self.get(Point.new(x, y))
if tile.letter == 'f'
td = Point.new(x, y).distance(point)
if td < d
d = td
p = Point.new(x, y)
end
end
end
end
p
end
def print def print
(0...4).each do |x| (0...4).each do |x|
(0...4).each do |y| (0...4).each do |y|

View File

@ -30,12 +30,20 @@ class BuildWoodMillCommand < Command
def run(ts : Int32, world : World) def run(ts : Int32, world : World)
printf "build mill at [%d,%d]\n", @point.x, @point.y printf "build mill at [%d,%d]\n", @point.x, @point.y
world.map.set(WoodMillTile.new(@point)) mill = WoodMillTile.new(@point)
world.push(ts + 5, GetWoodCommand.new) world.map.set(mill)
wood_point = world.map.nearest_wood(@point)
if !wood_point.nil?
printf "cut down wood at [%d,%d]\n", wood_point.x, wood_point.y
dist = @point.distance(wood_point)
world.push(ts + dist + 5, GetWoodCommand.new(@point))
else
printf "no wood tile\n"
end
end end
end end
class BuildForesterHouseCommand < Command class GetWoodCommand < Command
def initialize(@point : Point) def initialize(@point : Point)
end end
@ -43,36 +51,17 @@ class BuildForesterHouseCommand < Command
return true return true
end end
def run(ts : Int32, world : World)
printf "build forester house at [%d,%d]\n", @point.x, @point.y
world.map.set(ForesterHouseTile.new(@point))
world.push(ts + 10, GrowWoodCommand.new)
end
end
class GetWoodCommand < Command
def supports?(world : World) : Bool
return true
end
def run(ts : Int32, world : World) def run(ts : Int32, world : World)
res = world.resources res = world.resources
res.add_wood(10) res.add_wood(10)
puts "get wood" wood_point = world.map.nearest_wood(@point)
c = GetWoodCommand.new if !wood_point.nil?
world.push(ts + 5, c) printf "cut down wood at [%d,%d]\n", wood_point.x, wood_point.y
end dist = @point.distance(wood_point)
end world.push(ts + dist + 5, GetWoodCommand.new(@point))
else
class GrowWoodCommand < Command printf "no wood tile\n"
def supports?(world : World) : Bool end
return true
end
def run(ts : Int32, world : World)
res = world.resources
puts "grow wood"
world.push(ts + 5, GetWoodCommand.new)
end end
end end
@ -116,6 +105,5 @@ end
w = World.new w = World.new
w.map.print w.map.print
w.push(0, BuildWoodMillCommand.new(Point.new(0, 0))) w.push(0, BuildWoodMillCommand.new(Point.new(0, 0)))
w.push(0, BuildForesterHouseCommand.new(Point.new(2, 0)))
w.run(20) w.run(20)
w.map.print w.map.print