Add build command

This commit is contained in:
Anton Vakhrushev
2019-10-11 18:36:36 +03:00
parent 94caa93d7c
commit dacecf2aa8
4 changed files with 37 additions and 6 deletions

View File

@ -1,5 +1,5 @@
module Game
struct Production
class Production
def initialize(@ts : TimeSpan, @input : Resources, @output : Resources)
end
@ -8,7 +8,7 @@ module Game
getter output
end
struct Restoration
class Restoration
def initialize(@ts : TimeSpan, @type : Resources::Type, @cap : Capacity)
end
@ -17,7 +17,7 @@ module Game
getter cap
end
struct Construction
class Construction
def initialize(@ts : TimeSpan, @cost : Resources, @requirements : Array(Building::Type))
end

View File

@ -16,8 +16,13 @@ module Game
end
def start(world : World) : TimeSpan
construction = @building.construction
if !world.resources.has(construction.cost)
raise NotEnoughtResources.new
end
# @todo check requirements
world.map.set(ConstructionSiteTile.new(@point))
@building.construction.ts
construction.ts
end
def finish(world : World)

View File

@ -6,10 +6,10 @@ class Game::Resources
Terraformation
end
alias Capacity = Int32
alias ResourceBag = Hash(Type, Capacity)
@values : ResourceBag
def initialize(vals : ResourceBag | Nil = nil)
@values = {} of Type => Capacity
Type.each { |t, v| @values[t] = 0 }
@ -37,6 +37,10 @@ class Game::Resources
end
end
def has(vs : self) : Bool
has vs.to_hash
end
def inc(t : Type, value : Capacity)
new_value = @values.fetch(t, 0) + value
if new_value < 0
@ -56,4 +60,8 @@ class Game::Resources
def dec(t : Type, value : Capacity)
inc(t, -value)
end
def to_hash : ResourceBag
@values.clone
end
end