From ff266c199a99e051840c339079840857654d22c7 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Thu, 10 Oct 2019 17:34:31 +0300 Subject: [PATCH] Resource type move in Resources class --- spec/resources_spec.cr | 7 +++++++ src/expansion.cr | 4 ++-- src/game/command.cr | 12 ++++++------ src/game/resources.cr | 33 +++++++++++++++++---------------- src/game/world.cr | 2 +- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/spec/resources_spec.cr b/spec/resources_spec.cr index 0ccaf75..2770931 100644 --- a/spec/resources_spec.cr +++ b/spec/resources_spec.cr @@ -1 +1,8 @@ require "./spec_helper" + +describe Game::Resources do + it "should be created from hash" do + res = Game::Resources.new({Game::Resources::Type::Crystals => 100}) + res[Game::Resources::Type::Crystals].should eq 100 + end +end diff --git a/src/expansion.cr b/src/expansion.cr index b8e4cd4..0de3f69 100644 --- a/src/expansion.cr +++ b/src/expansion.cr @@ -103,8 +103,8 @@ end def render_resources(world) printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n", - world.resources[Game::ResourceType::Crystal], - world.resources[Game::ResourceType::Terraformation] + world.resources[Game::Resources::Type::Crystals], + world.resources[Game::Resources::Type::Terraformation] end def render_world(world) diff --git a/src/game/command.cr b/src/game/command.cr index 2ed6327..d264c48 100644 --- a/src/game/command.cr +++ b/src/game/command.cr @@ -55,7 +55,7 @@ module Game end def finish(world : World) - world.resources.inc(ResourceType::Crystal, @value) + world.resources.inc(Resources::Type::Crystals, @value) world.push(HarvestCrystalCommand.new(@point)) end @@ -88,7 +88,7 @@ module Game if !tile.can_build? raise InvalidPlaceForBuilding.new end - world.resources.dec(ResourceType::Crystal, CRYSTALS_COST) + world.resources.dec(Resources::Type::Crystals, CRYSTALS_COST) world.map.set(ConstructionSiteTile.new(@point)) BUILD_TIME end @@ -153,7 +153,7 @@ module Game if !tile.can_build? raise InvalidPlaceForBuilding.new end - world.resources.dec(ResourceType::Crystal, CRYSTALS_COST) + world.resources.dec(Resources::Type::Crystals, CRYSTALS_COST) world.map.set(ConstructionSiteTile.new(@point)) BUILD_TIME end @@ -179,8 +179,8 @@ module Game end def start(world : World) : Int32 - if world.resources.has(ResourceType::Crystal, CRYSTAL_REQUIRED) - world.resources.dec(ResourceType::Crystal, CRYSTAL_REQUIRED) + if world.resources.has(Resources::Type::Crystals, CRYSTAL_REQUIRED) + world.resources.dec(Resources::Type::Crystals, CRYSTAL_REQUIRED) @can_terr = true PRODUCTION_TIME else @@ -194,7 +194,7 @@ module Game def finish(world : World) if @can_terr - world.resources.inc(ResourceType::Terraformation, PRODUCTION_VALUE) + world.resources.inc(Resources::Type::Terraformation, PRODUCTION_VALUE) end world.push(TerraformCommand.new(@point)) end diff --git a/src/game/resources.cr b/src/game/resources.cr index 1e1bcc5..2156bc5 100644 --- a/src/game/resources.cr +++ b/src/game/resources.cr @@ -1,35 +1,36 @@ require "./exception" -enum Game::ResourceType - Crystal - Terraformation -end - class Game::Resources + enum Type + Crystals + Terraformation + end + def initialize - @values = {} of ResourceType => Int32 - ResourceType.each do |t| - @values[t] = 0 - end + @values = {} of Type => Int32 end - def [](t : ResourceType) - @values[t] + def initialize(vals : Hash(Type, Int32)) + @values = vals.clone end - def has(t : ResourceType, value : Int32) : Bool - @values[t] >= value + def [](t : Type) + @values.fetch(t, 0) end - def inc(t : ResourceType, value : Int32) - new_value = @values[t] + value + def has(t : Type, value : Int32) : Bool + @values.fetch(t, 0) >= value + end + + def inc(t : Type, value : Int32) + new_value = @values.fetch(t, 0) + value if new_value < 0 raise NotEnoughtResources.new end @values[t] = new_value end - def dec(t : ResourceType, value : Int32) + def dec(t : Type, value : Int32) inc(t, -value) end end diff --git a/src/game/world.cr b/src/game/world.cr index 266ccab..8da4a30 100644 --- a/src/game/world.cr +++ b/src/game/world.cr @@ -41,6 +41,6 @@ class Game::World end def win? - @resources[ResourceType::Terraformation] >= 100 + @resources[Resources::Type::Terraformation] >= 100 end end