From 189f6dd96b58cbde6561867ea9f3db9a40c1252d Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Tue, 15 Oct 2019 15:04:27 +0300 Subject: [PATCH] Calc paths when restore --- spec/command_spec.cr | 6 ++++-- src/game/command.cr | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/spec/command_spec.cr b/spec/command_spec.cr index e9238a3..38f5f1f 100644 --- a/spec/command_spec.cr +++ b/spec/command_spec.cr @@ -94,8 +94,10 @@ module Game::TestCommand world = World.new create_map_with_resource world.resources.inc(Resource::Type::Crystals, 20) command = RestoreCommand.new Point.new(0, 1), once: true - world.push command - world.run 20 + time_point = (20 + 2 * 2 + 1 * 2).to_i64 + done_at = world.push command + done_at.should eq time_point + world.run time_point # Check world resources world.resources[Resource::Type::Crystals].should eq 15 # Check tile deposit diff --git a/src/game/command.cr b/src/game/command.cr index 49a88f5..a5c8d3a 100644 --- a/src/game/command.cr +++ b/src/game/command.cr @@ -118,11 +118,16 @@ module Game end resource = restoration.resource @deposit_tile = nearest_deposit(world, resource.type) - if @deposit_tile + stock_tile = nearest_stock(world) + if @deposit_tile && stock_tile world.resources.dec restoration.input @holded = resource + restoration.ts + + 2 * tile.point.distance(stock_tile.point) + + 2 * tile.point.distance(@deposit_tile.as(DepositTile).point) + else + restoration.ts end - restoration.ts end def finish(world : World) @@ -140,6 +145,13 @@ 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 BuildCrystalHarvesterCommand < Command