From 64e27f916d92d60f216513143e3c75cb77845cfd Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Fri, 13 Sep 2019 15:45:16 +0300 Subject: [PATCH] World impl --- src/village.cr | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/village.cr b/src/village.cr index b96cd78..e4eecfb 100644 --- a/src/village.cr +++ b/src/village.cr @@ -21,54 +21,59 @@ abstract class Command @ts end - abstract def run(queue : CommandQueue, res : Resources) + abstract def run(world : World) end class BuildMillCommand < Command - def run(queue : CommandQueue, res : Resources) + def run(world : World) puts "build mill" c = GetWoodCommand.new(@ts + 5) - queue.push(c) + world.push(c) end end class GetWoodCommand < Command - def run(queue : CommandQueue, res : Resources) + def run(world : World) + res = world.resources res.add_wood(10) puts "get wood" c = GetWoodCommand.new(@ts + 5) - queue.push(c) + world.push(c) end end -class CommandQueue +class World def initialize @resources = Resources.new - @data = Array(Command).new + @queue = Array(Command).new + end + + def resources + @resources end def push(command : Command) printf "push command %d\n", command.ts - @data.push(command) - @data.sort! do |c| + @queue.push(command) + @queue.sort! do |c| -c.ts end end def run(ts : Int32) - while @data.size != 0 - c = @data.pop + while @queue.size != 0 + c = @queue.pop printf "pop command %d\n", c.ts if c.ts > ts break end - c.run(self, @resources) + c.run(self) end printf "Wood: %d\n", @resources.wood end end -q = CommandQueue.new +q = World.new q.push(BuildMillCommand.new(0)) q.push(BuildMillCommand.new(0)) q.push(BuildMillCommand.new(0))