Small fixes

This commit is contained in:
Anton Vakhrushev 2019-10-13 15:38:27 +03:00
parent 51b36019f7
commit 6cf4759e5e
3 changed files with 30 additions and 31 deletions

View File

@ -34,18 +34,26 @@ module Game
end
class MineCommand < Command
@holded : Resource | Nil = nil
@holded : Resource? = nil
def initialize(@point : Point)
end
def desc : String
if @holded
sprintf "Mine %s from %d,%d", @holded.type, @point.x, @point.y
else
sprintf "Wait for resources at %d,%d", @point.x, @point.y
end
end
def start(world : World) : TimeSpan
tile = world.map.get(@point).as(BuildingTile)
building = tile.building
mining = building.mining.as(Mining)
deposit_tile = nearest_deposit(world, mining.resource.type)
if deposit_tile
@holded = deposit_tile.as(DepositTile).dep.dec(mining.resource)
@holded = deposit_tile.dep.dec(mining.resource)
end
mining.ts
end
@ -53,23 +61,16 @@ module Game
def finish(world : World)
if @holded
holded = @holded.as(Resource)
world.resources.inc(holded.type, holded.amount)
world.resources.inc(holded)
end
world.push(MineCommand.new(@point))
end
def desc : String
if @holded
sprintf "Mine %s from %d,%d", @holded.as(Resource).type, @point.x, @point.y
else
sprintf "Wait for resources at %d,%d", @point.x, @point.y
end
end
private def nearest_deposit(world : World, res_type : Resource::Type)
world.map.nearest_tile @point do |tile|
private def nearest_deposit(world : World, res_type : Resource::Type) : DepositTile?
tile = world.map.nearest_tile @point do |tile|
tile.is_a?(DepositTile) && tile.dep.type == res_type && tile.dep.cur > 0
end
tile.as?(DepositTile)
end
end

View File

@ -1,4 +1,4 @@
require "./exception"
require "./types"
struct Game::Resource
enum Type
@ -33,12 +33,16 @@ class Game::ResourceBag
end
end
def [](t : Resource::Type)
@values.fetch(t, 0)
def [](res_type : Resource::Type)
@values.fetch(res_type, 0)
end
def has(t : Resource::Type, value : Capacity) : Bool
@values[t] >= value
def has(res_type : Resource::Type, value : Capacity) : Bool
@values[res_type] >= value
end
def has(res : Resource) : Bool
has(res.type, res.amount)
end
def has(vs : ResourceHash) : Bool
@ -53,24 +57,20 @@ class Game::ResourceBag
has vs.to_hash
end
def inc(t : Resource::Type, value : Capacity)
new_value = @values.fetch(t, 0) + value
def inc(res_type : Resource::Type, value : Capacity)
new_value = @values[res_type] + value
if new_value < 0
raise NotEnoughtResources.new
end
@values[t] = new_value
@values[res_type] = new_value
end
def inc?(vs : ResourceBag) : Bool
false unless has(vs)
vs.each do |t, c|
@values[t] = @values[t] + c
end
true
def inc(res : Resource)
inc(res.type, res.amount)
end
def dec(t : Resource::Type, value : Capacity)
inc(t, -value)
def dec(res_type : Resource::Type, value : Capacity)
inc(res_type, -value)
end
def to_hash : ResourceHash

View File

@ -1,5 +1,3 @@
require "./resources"
class Game::World
property ts : TimePoint