Improve wood mill command
This commit is contained in:
parent
05801dd983
commit
fb139b2334
24
src/map.cr
24
src/map.cr
@ -9,6 +9,10 @@ struct Point
|
||||
def y
|
||||
@y
|
||||
end
|
||||
|
||||
def distance(p : Point) : Int32
|
||||
return (p.x - @x).abs + (p.y - @y).abs
|
||||
end
|
||||
end
|
||||
|
||||
abstract class Tile
|
||||
@ -79,7 +83,7 @@ class Map
|
||||
end
|
||||
|
||||
def get(point : Point) : Tile
|
||||
@data[key(Point.new(x, y))]
|
||||
@data[key(point)]
|
||||
end
|
||||
|
||||
def set(tile : Tile)
|
||||
@ -90,6 +94,24 @@ class Map
|
||||
@data[key(point)] = tile
|
||||
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
|
||||
(0...4).each do |x|
|
||||
(0...4).each do |y|
|
||||
|
@ -30,12 +30,20 @@ class BuildWoodMillCommand < Command
|
||||
|
||||
def run(ts : Int32, world : World)
|
||||
printf "build mill at [%d,%d]\n", @point.x, @point.y
|
||||
world.map.set(WoodMillTile.new(@point))
|
||||
world.push(ts + 5, GetWoodCommand.new)
|
||||
mill = WoodMillTile.new(@point)
|
||||
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
|
||||
|
||||
class BuildForesterHouseCommand < Command
|
||||
class GetWoodCommand < Command
|
||||
def initialize(@point : Point)
|
||||
end
|
||||
|
||||
@ -43,37 +51,18 @@ class BuildForesterHouseCommand < Command
|
||||
return true
|
||||
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)
|
||||
res = world.resources
|
||||
res.add_wood(10)
|
||||
puts "get wood"
|
||||
c = GetWoodCommand.new
|
||||
world.push(ts + 5, c)
|
||||
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
|
||||
|
||||
class GrowWoodCommand < Command
|
||||
def supports?(world : World) : Bool
|
||||
return true
|
||||
end
|
||||
|
||||
def run(ts : Int32, world : World)
|
||||
res = world.resources
|
||||
puts "grow wood"
|
||||
world.push(ts + 5, GetWoodCommand.new)
|
||||
end
|
||||
end
|
||||
|
||||
class World
|
||||
@ -116,6 +105,5 @@ end
|
||||
w = World.new
|
||||
w.map.print
|
||||
w.push(0, BuildWoodMillCommand.new(Point.new(0, 0)))
|
||||
w.push(0, BuildForesterHouseCommand.new(Point.new(2, 0)))
|
||||
w.run(20)
|
||||
w.map.print
|
||||
|
Loading…
Reference in New Issue
Block a user