Resourses as a hash
This commit is contained in:
parent
c02903b49c
commit
97030f7c5b
@ -16,8 +16,7 @@ class BuildCrystalHarvesterCommand < Command
|
||||
end
|
||||
|
||||
def finish(world : World)
|
||||
harverster = CrystalHarvesterTile.new(@point)
|
||||
world.map.set(harverster)
|
||||
world.map.set(CrystalHarvesterTile.new(@point))
|
||||
world.push(HarvestCrystalCommand.new(@point))
|
||||
end
|
||||
end
|
||||
@ -45,7 +44,7 @@ class HarvestCrystalCommand < Command
|
||||
end
|
||||
|
||||
def finish(world : World)
|
||||
world.resources.add_crystal(@value)
|
||||
world.resources.inc(ResourceType::Crystal, @value)
|
||||
world.push(HarvestCrystalCommand.new(@point))
|
||||
end
|
||||
|
||||
|
@ -3,16 +3,25 @@ require "./command"
|
||||
require "./map"
|
||||
require "./queue"
|
||||
|
||||
enum ResourceType
|
||||
Crystal
|
||||
Terraformation
|
||||
end
|
||||
|
||||
class Resources
|
||||
def initialize(@crystal = 0)
|
||||
def initialize
|
||||
@values = {} of ResourceType => Int32
|
||||
ResourceType.each do |t|
|
||||
@values[t] = 0
|
||||
end
|
||||
end
|
||||
|
||||
def add_crystal(x)
|
||||
@crystal += x
|
||||
def [](t : ResourceType)
|
||||
@values[t]
|
||||
end
|
||||
|
||||
def crystal
|
||||
@crystal
|
||||
def inc(t : ResourceType, value : Int32)
|
||||
@values[t] = @values[t] + value
|
||||
end
|
||||
end
|
||||
|
||||
@ -64,4 +73,4 @@ w.push(BuildCrystalRestorerCommand.new(Point.new(1, 2)))
|
||||
w.push(BuildCrystalRestorerCommand.new(Point.new(3, 2)))
|
||||
w.run(60)
|
||||
w.map.print
|
||||
printf "Wood: %d\n", w.resources.crystal
|
||||
pp w.resources
|
||||
|
Loading…
Reference in New Issue
Block a user