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