Small fixes
This commit is contained in:
parent
51b36019f7
commit
6cf4759e5e
@ -34,18 +34,26 @@ module Game
|
|||||||
end
|
end
|
||||||
|
|
||||||
class MineCommand < Command
|
class MineCommand < Command
|
||||||
@holded : Resource | Nil = nil
|
@holded : Resource? = nil
|
||||||
|
|
||||||
def initialize(@point : Point)
|
def initialize(@point : Point)
|
||||||
end
|
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
|
def start(world : World) : TimeSpan
|
||||||
tile = world.map.get(@point).as(BuildingTile)
|
tile = world.map.get(@point).as(BuildingTile)
|
||||||
building = tile.building
|
building = tile.building
|
||||||
mining = building.mining.as(Mining)
|
mining = building.mining.as(Mining)
|
||||||
deposit_tile = nearest_deposit(world, mining.resource.type)
|
deposit_tile = nearest_deposit(world, mining.resource.type)
|
||||||
if deposit_tile
|
if deposit_tile
|
||||||
@holded = deposit_tile.as(DepositTile).dep.dec(mining.resource)
|
@holded = deposit_tile.dep.dec(mining.resource)
|
||||||
end
|
end
|
||||||
mining.ts
|
mining.ts
|
||||||
end
|
end
|
||||||
@ -53,23 +61,16 @@ module Game
|
|||||||
def finish(world : World)
|
def finish(world : World)
|
||||||
if @holded
|
if @holded
|
||||||
holded = @holded.as(Resource)
|
holded = @holded.as(Resource)
|
||||||
world.resources.inc(holded.type, holded.amount)
|
world.resources.inc(holded)
|
||||||
end
|
end
|
||||||
world.push(MineCommand.new(@point))
|
world.push(MineCommand.new(@point))
|
||||||
end
|
end
|
||||||
|
|
||||||
def desc : String
|
private def nearest_deposit(world : World, res_type : Resource::Type) : DepositTile?
|
||||||
if @holded
|
tile = world.map.nearest_tile @point do |tile|
|
||||||
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|
|
|
||||||
tile.is_a?(DepositTile) && tile.dep.type == res_type && tile.dep.cur > 0
|
tile.is_a?(DepositTile) && tile.dep.type == res_type && tile.dep.cur > 0
|
||||||
end
|
end
|
||||||
|
tile.as?(DepositTile)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
require "./exception"
|
require "./types"
|
||||||
|
|
||||||
struct Game::Resource
|
struct Game::Resource
|
||||||
enum Type
|
enum Type
|
||||||
@ -33,12 +33,16 @@ class Game::ResourceBag
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def [](t : Resource::Type)
|
def [](res_type : Resource::Type)
|
||||||
@values.fetch(t, 0)
|
@values.fetch(res_type, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
def has(t : Resource::Type, value : Capacity) : Bool
|
def has(res_type : Resource::Type, value : Capacity) : Bool
|
||||||
@values[t] >= value
|
@values[res_type] >= value
|
||||||
|
end
|
||||||
|
|
||||||
|
def has(res : Resource) : Bool
|
||||||
|
has(res.type, res.amount)
|
||||||
end
|
end
|
||||||
|
|
||||||
def has(vs : ResourceHash) : Bool
|
def has(vs : ResourceHash) : Bool
|
||||||
@ -53,24 +57,20 @@ class Game::ResourceBag
|
|||||||
has vs.to_hash
|
has vs.to_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def inc(t : Resource::Type, value : Capacity)
|
def inc(res_type : Resource::Type, value : Capacity)
|
||||||
new_value = @values.fetch(t, 0) + value
|
new_value = @values[res_type] + value
|
||||||
if new_value < 0
|
if new_value < 0
|
||||||
raise NotEnoughtResources.new
|
raise NotEnoughtResources.new
|
||||||
end
|
end
|
||||||
@values[t] = new_value
|
@values[res_type] = new_value
|
||||||
end
|
end
|
||||||
|
|
||||||
def inc?(vs : ResourceBag) : Bool
|
def inc(res : Resource)
|
||||||
false unless has(vs)
|
inc(res.type, res.amount)
|
||||||
vs.each do |t, c|
|
|
||||||
@values[t] = @values[t] + c
|
|
||||||
end
|
|
||||||
true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def dec(t : Resource::Type, value : Capacity)
|
def dec(res_type : Resource::Type, value : Capacity)
|
||||||
inc(t, -value)
|
inc(res_type, -value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_hash : ResourceHash
|
def to_hash : ResourceHash
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
require "./resources"
|
|
||||||
|
|
||||||
class Game::World
|
class Game::World
|
||||||
property ts : TimePoint
|
property ts : TimePoint
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user