Resourses as a hash

This commit is contained in:
Anton Vakhrushev 2019-09-20 18:42:12 +03:00
parent c02903b49c
commit 97030f7c5b
2 changed files with 17 additions and 9 deletions

View File

@ -16,8 +16,7 @@ class BuildCrystalHarvesterCommand < Command
end end
def finish(world : World) def finish(world : World)
harverster = CrystalHarvesterTile.new(@point) world.map.set(CrystalHarvesterTile.new(@point))
world.map.set(harverster)
world.push(HarvestCrystalCommand.new(@point)) world.push(HarvestCrystalCommand.new(@point))
end end
end end
@ -45,7 +44,7 @@ class HarvestCrystalCommand < Command
end end
def finish(world : World) def finish(world : World)
world.resources.add_crystal(@value) world.resources.inc(ResourceType::Crystal, @value)
world.push(HarvestCrystalCommand.new(@point)) world.push(HarvestCrystalCommand.new(@point))
end end

View File

@ -3,16 +3,25 @@ require "./command"
require "./map" require "./map"
require "./queue" require "./queue"
enum ResourceType
Crystal
Terraformation
end
class Resources class Resources
def initialize(@crystal = 0) def initialize
@values = {} of ResourceType => Int32
ResourceType.each do |t|
@values[t] = 0
end
end end
def add_crystal(x) def [](t : ResourceType)
@crystal += x @values[t]
end end
def crystal def inc(t : ResourceType, value : Int32)
@crystal @values[t] = @values[t] + value
end end
end end
@ -64,4 +73,4 @@ w.push(BuildCrystalRestorerCommand.new(Point.new(1, 2)))
w.push(BuildCrystalRestorerCommand.new(Point.new(3, 2))) w.push(BuildCrystalRestorerCommand.new(Point.new(3, 2)))
w.run(60) w.run(60)
w.map.print w.map.print
printf "Wood: %d\n", w.resources.crystal pp w.resources