Add alias for resource capacity

This commit is contained in:
Anton Vakhrushev 2019-10-10 17:39:54 +03:00
parent ff266c199a
commit 28c51ba518

View File

@ -6,11 +6,13 @@ class Game::Resources
Terraformation Terraformation
end end
alias Capacity = Int32
def initialize def initialize
@values = {} of Type => Int32 @values = {} of Type => Capacity
end end
def initialize(vals : Hash(Type, Int32)) def initialize(vals : Hash(Type, Capacity))
@values = vals.clone @values = vals.clone
end end
@ -18,11 +20,11 @@ class Game::Resources
@values.fetch(t, 0) @values.fetch(t, 0)
end end
def has(t : Type, value : Int32) : Bool def has(t : Type, value : Capacity) : Bool
@values.fetch(t, 0) >= value @values.fetch(t, 0) >= value
end end
def inc(t : Type, value : Int32) def inc(t : Type, value : Capacity)
new_value = @values.fetch(t, 0) + value new_value = @values.fetch(t, 0) + value
if new_value < 0 if new_value < 0
raise NotEnoughtResources.new raise NotEnoughtResources.new
@ -30,7 +32,7 @@ class Game::Resources
@values[t] = new_value @values[t] = new_value
end end
def dec(t : Type, value : Int32) def dec(t : Type, value : Capacity)
inc(t, -value) inc(t, -value)
end end
end end