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
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

View File

@ -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