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

@ -15,4 +15,22 @@ describe Game::Command do
tile = world.map.get point tile = world.map.get point
tile.should be_a(Game::BuildingTile) tile.should be_a(Game::BuildingTile)
end end
it "should restrict build if not enought resources" do
world = Game::World.new create_map_2x2
point = Game::Point.new 1, 0
building = Game::Building.new Game::Building::Type::StartPoint, "Dummy", **{
construction: Game::Construction.new(
ts: 10,
cost: Game::Resources.new({
Game::Resources::Type::Crystals => 100,
}),
requirements: [] of Game::Building::Type
),
}
command = Game::BuildCommand.new point, building
expect_raises(Game::NotEnoughtResources) do
world.push(command)
end
end
end end

View File

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

View File

@ -16,8 +16,13 @@ module Game
end end
def start(world : World) : TimeSpan 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)) world.map.set(ConstructionSiteTile.new(@point))
@building.construction.ts construction.ts
end end
def finish(world : World) def finish(world : World)

View File

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