Add resource exception
This commit is contained in:
parent
6404a6e519
commit
446b1659de
@ -10,4 +10,13 @@ describe "World" do
|
||||
world.run(100)
|
||||
world.map.get(point).has_role(TileRole::CrystalHarvester)
|
||||
end
|
||||
|
||||
it "should fail when not enought resources" do
|
||||
world = World.new
|
||||
point = Point.new(2, 3)
|
||||
cmd = BuildCrystalRestorerCommand.new(point)
|
||||
expect_raises(NotEnoughtResources) do
|
||||
world.push(cmd)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -62,12 +62,14 @@ class HarvestCrystalCommand < Command
|
||||
end
|
||||
|
||||
class BuildCrystalRestorerCommand < Command
|
||||
BUILD_TIME = 50
|
||||
CRYSTALS_COST = 100
|
||||
BUILD_TIME = 50
|
||||
|
||||
def initialize(@point : Point)
|
||||
end
|
||||
|
||||
def start(world : World) : Int32
|
||||
world.resources.dec(ResourceType::Crystal, CRYSTALS_COST)
|
||||
BUILD_TIME
|
||||
end
|
||||
|
||||
@ -112,12 +114,14 @@ class RestoreCrystalCommand < Command
|
||||
end
|
||||
|
||||
class BuildTerraformerCommand < Command
|
||||
BUILD_TIME = 120
|
||||
CRYSTALS_COST = 100
|
||||
BUILD_TIME = 120
|
||||
|
||||
def initialize(@point : Point)
|
||||
end
|
||||
|
||||
def start(world : World) : Int32
|
||||
world.resources.dec(ResourceType::Crystal, CRYSTALS_COST)
|
||||
BUILD_TIME
|
||||
end
|
||||
|
||||
|
2
src/exception.cr
Normal file
2
src/exception.cr
Normal file
@ -0,0 +1,2 @@
|
||||
class NotEnoughtResources < Exception
|
||||
end
|
@ -1,3 +1,5 @@
|
||||
require "./exception"
|
||||
|
||||
enum ResourceType
|
||||
Crystal
|
||||
Terraformation
|
||||
@ -16,6 +18,14 @@ class Resources
|
||||
end
|
||||
|
||||
def inc(t : ResourceType, value : Int32)
|
||||
@values[t] = @values[t] + value
|
||||
new_value = @values[t] + value
|
||||
if new_value < 0
|
||||
raise NotEnoughtResources.new
|
||||
end
|
||||
@values[t] = new_value
|
||||
end
|
||||
|
||||
def dec(t : ResourceType, value : Int32)
|
||||
inc(t, -value)
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user