Resource type move in Resources class

This commit is contained in:
Anton Vakhrushev 2019-10-10 17:34:31 +03:00
parent e6f4e5ee29
commit ff266c199a
5 changed files with 33 additions and 25 deletions

View File

@ -1 +1,8 @@
require "./spec_helper" 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

View File

@ -103,8 +103,8 @@ end
def render_resources(world) def render_resources(world)
printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n", printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n",
world.resources[Game::ResourceType::Crystal], world.resources[Game::Resources::Type::Crystals],
world.resources[Game::ResourceType::Terraformation] world.resources[Game::Resources::Type::Terraformation]
end end
def render_world(world) def render_world(world)

View File

@ -55,7 +55,7 @@ module Game
end end
def finish(world : World) def finish(world : World)
world.resources.inc(ResourceType::Crystal, @value) world.resources.inc(Resources::Type::Crystals, @value)
world.push(HarvestCrystalCommand.new(@point)) world.push(HarvestCrystalCommand.new(@point))
end end
@ -88,7 +88,7 @@ module Game
if !tile.can_build? if !tile.can_build?
raise InvalidPlaceForBuilding.new raise InvalidPlaceForBuilding.new
end end
world.resources.dec(ResourceType::Crystal, CRYSTALS_COST) world.resources.dec(Resources::Type::Crystals, CRYSTALS_COST)
world.map.set(ConstructionSiteTile.new(@point)) world.map.set(ConstructionSiteTile.new(@point))
BUILD_TIME BUILD_TIME
end end
@ -153,7 +153,7 @@ module Game
if !tile.can_build? if !tile.can_build?
raise InvalidPlaceForBuilding.new raise InvalidPlaceForBuilding.new
end end
world.resources.dec(ResourceType::Crystal, CRYSTALS_COST) world.resources.dec(Resources::Type::Crystals, CRYSTALS_COST)
world.map.set(ConstructionSiteTile.new(@point)) world.map.set(ConstructionSiteTile.new(@point))
BUILD_TIME BUILD_TIME
end end
@ -179,8 +179,8 @@ module Game
end end
def start(world : World) : Int32 def start(world : World) : Int32
if world.resources.has(ResourceType::Crystal, CRYSTAL_REQUIRED) if world.resources.has(Resources::Type::Crystals, CRYSTAL_REQUIRED)
world.resources.dec(ResourceType::Crystal, CRYSTAL_REQUIRED) world.resources.dec(Resources::Type::Crystals, CRYSTAL_REQUIRED)
@can_terr = true @can_terr = true
PRODUCTION_TIME PRODUCTION_TIME
else else
@ -194,7 +194,7 @@ module Game
def finish(world : World) def finish(world : World)
if @can_terr if @can_terr
world.resources.inc(ResourceType::Terraformation, PRODUCTION_VALUE) world.resources.inc(Resources::Type::Terraformation, PRODUCTION_VALUE)
end end
world.push(TerraformCommand.new(@point)) world.push(TerraformCommand.new(@point))
end end

View File

@ -1,35 +1,36 @@
require "./exception" require "./exception"
enum Game::ResourceType
Crystal
Terraformation
end
class Game::Resources class Game::Resources
enum Type
Crystals
Terraformation
end
def initialize def initialize
@values = {} of ResourceType => Int32 @values = {} of Type => Int32
ResourceType.each do |t|
@values[t] = 0
end
end end
def [](t : ResourceType) def initialize(vals : Hash(Type, Int32))
@values[t] @values = vals.clone
end end
def has(t : ResourceType, value : Int32) : Bool def [](t : Type)
@values[t] >= value @values.fetch(t, 0)
end end
def inc(t : ResourceType, value : Int32) def has(t : Type, value : Int32) : Bool
new_value = @values[t] + value @values.fetch(t, 0) >= value
end
def inc(t : Type, value : Int32)
new_value = @values.fetch(t, 0) + value
if new_value < 0 if new_value < 0
raise NotEnoughtResources.new raise NotEnoughtResources.new
end end
@values[t] = new_value @values[t] = new_value
end end
def dec(t : ResourceType, value : Int32) def dec(t : Type, value : Int32)
inc(t, -value) inc(t, -value)
end end
end end

View File

@ -41,6 +41,6 @@ class Game::World
end end
def win? def win?
@resources[ResourceType::Terraformation] >= 100 @resources[Resources::Type::Terraformation] >= 100
end end
end end