Add resource exception
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user