From 6cf4759e5ee2c78500b071e61e691c84085b51b6 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 13 Oct 2019 15:38:27 +0300 Subject: [PATCH] Small fixes --- src/game/command.cr | 27 ++++++++++++++------------- src/game/resources.cr | 32 ++++++++++++++++---------------- src/game/world.cr | 2 -- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/game/command.cr b/src/game/command.cr index bc2fefc..22fe8b0 100644 --- a/src/game/command.cr +++ b/src/game/command.cr @@ -34,18 +34,26 @@ module Game end class MineCommand < Command - @holded : Resource | Nil = nil + @holded : Resource? = nil def initialize(@point : Point) end + def desc : String + if @holded + sprintf "Mine %s from %d,%d", @holded.type, @point.x, @point.y + else + sprintf "Wait for resources at %d,%d", @point.x, @point.y + end + end + def start(world : World) : TimeSpan tile = world.map.get(@point).as(BuildingTile) building = tile.building mining = building.mining.as(Mining) deposit_tile = nearest_deposit(world, mining.resource.type) if deposit_tile - @holded = deposit_tile.as(DepositTile).dep.dec(mining.resource) + @holded = deposit_tile.dep.dec(mining.resource) end mining.ts end @@ -53,23 +61,16 @@ module Game def finish(world : World) if @holded holded = @holded.as(Resource) - world.resources.inc(holded.type, holded.amount) + world.resources.inc(holded) end world.push(MineCommand.new(@point)) end - def desc : String - if @holded - sprintf "Mine %s from %d,%d", @holded.as(Resource).type, @point.x, @point.y - else - sprintf "Wait for resources at %d,%d", @point.x, @point.y - end - end - - private def nearest_deposit(world : World, res_type : Resource::Type) - world.map.nearest_tile @point do |tile| + private def nearest_deposit(world : World, res_type : Resource::Type) : DepositTile? + tile = world.map.nearest_tile @point do |tile| tile.is_a?(DepositTile) && tile.dep.type == res_type && tile.dep.cur > 0 end + tile.as?(DepositTile) end end diff --git a/src/game/resources.cr b/src/game/resources.cr index 7dae9c3..b20b077 100644 --- a/src/game/resources.cr +++ b/src/game/resources.cr @@ -1,4 +1,4 @@ -require "./exception" +require "./types" struct Game::Resource enum Type @@ -33,12 +33,16 @@ class Game::ResourceBag end end - def [](t : Resource::Type) - @values.fetch(t, 0) + def [](res_type : Resource::Type) + @values.fetch(res_type, 0) end - def has(t : Resource::Type, value : Capacity) : Bool - @values[t] >= value + def has(res_type : Resource::Type, value : Capacity) : Bool + @values[res_type] >= value + end + + def has(res : Resource) : Bool + has(res.type, res.amount) end def has(vs : ResourceHash) : Bool @@ -53,24 +57,20 @@ class Game::ResourceBag has vs.to_hash end - def inc(t : Resource::Type, value : Capacity) - new_value = @values.fetch(t, 0) + value + def inc(res_type : Resource::Type, value : Capacity) + new_value = @values[res_type] + value if new_value < 0 raise NotEnoughtResources.new end - @values[t] = new_value + @values[res_type] = new_value end - def inc?(vs : ResourceBag) : Bool - false unless has(vs) - vs.each do |t, c| - @values[t] = @values[t] + c - end - true + def inc(res : Resource) + inc(res.type, res.amount) end - def dec(t : Resource::Type, value : Capacity) - inc(t, -value) + def dec(res_type : Resource::Type, value : Capacity) + inc(res_type, -value) end def to_hash : ResourceHash diff --git a/src/game/world.cr b/src/game/world.cr index a0c8175..e441b9d 100644 --- a/src/game/world.cr +++ b/src/game/world.cr @@ -1,5 +1,3 @@ -require "./resources" - class Game::World property ts : TimePoint