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"
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)
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)

View File

@ -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

View File

@ -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

View File

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