Refactoring

This commit is contained in:
Anton Vakhrushev 2019-10-15 15:33:02 +03:00
parent 52607c48dd
commit e0050b920e

View File

@ -39,12 +39,21 @@ module Game
end end
end end
class MineCommand < Command abstract class ResourceCommand < Command
@holded : Resource? = nil
def initialize(@point : Point, *, @once = false) def initialize(@point : Point, *, @once = false)
end end
protected def nearest_stock(world : World) : BuildingTile?
tile = world.map.nearest_tile @point do |t|
t.is_a?(BuildingTile) && t.building.has_role Building::Role::Storehouse
end
tile.as?(BuildingTile)
end
end
class MineCommand < ResourceCommand
@holded : Resource? = nil
def desc : String def desc : String
if @holded if @holded
sprintf "Mine %s from %d,%d", @holded.type, @point.x, @point.y sprintf "Mine %s from %d,%d", @holded.type, @point.x, @point.y
@ -66,7 +75,9 @@ module Game
if deposit_tile && stock_tile if deposit_tile && stock_tile
mined_amount = deposit_tile.dep.dec(resource.amount) mined_amount = deposit_tile.dep.dec(resource.amount)
@holded = resource.type.to_res mined_amount @holded = resource.type.to_res mined_amount
mining.ts + 2 * tile.point.distance(stock_tile.point) + 2 * tile.point.distance(deposit_tile.point) mining.ts +
2 * tile.point.distance(stock_tile.point) +
2 * tile.point.distance(deposit_tile.point)
else else
mining.ts mining.ts
end end
@ -88,22 +99,12 @@ module Game
end end
tile.as?(DepositTile) tile.as?(DepositTile)
end end
private def nearest_stock(world : World) : BuildingTile?
tile = world.map.nearest_tile @point do |t|
t.is_a?(BuildingTile) && t.building.has_role Building::Role::Storehouse
end
tile.as?(BuildingTile)
end
end end
class RestoreCommand < Command class RestoreCommand < ResourceCommand
@holded : Resource? = nil @holded : Resource? = nil
@deposit_tile : DepositTile? = nil @deposit_tile : DepositTile? = nil
def initialize(@point : Point, *, @once = false)
end
def desc : String def desc : String
if @holded if @holded
sprintf "Restore %s from %d,%d", @holded.type, @point.x, @point.y sprintf "Restore %s from %d,%d", @holded.type, @point.x, @point.y
@ -148,21 +149,11 @@ module Game
end end
tile.as?(DepositTile) tile.as?(DepositTile)
end end
private def nearest_stock(world : World) : BuildingTile?
tile = world.map.nearest_tile @point do |t|
t.is_a?(BuildingTile) && t.building.has_role Building::Role::Storehouse
end
tile.as?(BuildingTile)
end
end end
class ProduceCommand < Command class ProduceCommand < ResourceCommand
@holded : ResourceBag? = nil @holded : ResourceBag? = nil
def initialize(@point : Point, *, @once = false)
end
def desc : String def desc : String
if @holded if @holded
sprintf "Produce at %d,%d", @point.x, @point.y sprintf "Produce at %d,%d", @point.x, @point.y
@ -196,12 +187,5 @@ module Game
world.push(ProduceCommand.new(@point)) world.push(ProduceCommand.new(@point))
end end
end end
private def nearest_stock(world : World) : BuildingTile?
tile = world.map.nearest_tile @point do |t|
t.is_a?(BuildingTile) && t.building.has_role Building::Role::Storehouse
end
tile.as?(BuildingTile)
end
end end
end end