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
class MineCommand < Command
@holded : Resource? = nil
abstract class ResourceCommand < Command
def initialize(@point : Point, *, @once = false)
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
if @holded
sprintf "Mine %s from %d,%d", @holded.type, @point.x, @point.y
@ -66,7 +75,9 @@ module Game
if deposit_tile && stock_tile
mined_amount = deposit_tile.dep.dec(resource.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
mining.ts
end
@ -88,22 +99,12 @@ module Game
end
tile.as?(DepositTile)
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
class RestoreCommand < Command
class RestoreCommand < ResourceCommand
@holded : Resource? = nil
@deposit_tile : DepositTile? = nil
def initialize(@point : Point, *, @once = false)
end
def desc : String
if @holded
sprintf "Restore %s from %d,%d", @holded.type, @point.x, @point.y
@ -148,21 +149,11 @@ module Game
end
tile.as?(DepositTile)
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
class ProduceCommand < Command
class ProduceCommand < ResourceCommand
@holded : ResourceBag? = nil
def initialize(@point : Point, *, @once = false)
end
def desc : String
if @holded
sprintf "Produce at %d,%d", @point.x, @point.y
@ -196,12 +187,5 @@ module Game
world.push(ProduceCommand.new(@point))
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