World impl
This commit is contained in:
parent
db72b838d3
commit
64e27f916d
@ -21,54 +21,59 @@ abstract class Command
|
|||||||
@ts
|
@ts
|
||||||
end
|
end
|
||||||
|
|
||||||
abstract def run(queue : CommandQueue, res : Resources)
|
abstract def run(world : World)
|
||||||
end
|
end
|
||||||
|
|
||||||
class BuildMillCommand < Command
|
class BuildMillCommand < Command
|
||||||
def run(queue : CommandQueue, res : Resources)
|
def run(world : World)
|
||||||
puts "build mill"
|
puts "build mill"
|
||||||
c = GetWoodCommand.new(@ts + 5)
|
c = GetWoodCommand.new(@ts + 5)
|
||||||
queue.push(c)
|
world.push(c)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class GetWoodCommand < Command
|
class GetWoodCommand < Command
|
||||||
def run(queue : CommandQueue, res : Resources)
|
def run(world : World)
|
||||||
|
res = world.resources
|
||||||
res.add_wood(10)
|
res.add_wood(10)
|
||||||
puts "get wood"
|
puts "get wood"
|
||||||
c = GetWoodCommand.new(@ts + 5)
|
c = GetWoodCommand.new(@ts + 5)
|
||||||
queue.push(c)
|
world.push(c)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class CommandQueue
|
class World
|
||||||
def initialize
|
def initialize
|
||||||
@resources = Resources.new
|
@resources = Resources.new
|
||||||
@data = Array(Command).new
|
@queue = Array(Command).new
|
||||||
|
end
|
||||||
|
|
||||||
|
def resources
|
||||||
|
@resources
|
||||||
end
|
end
|
||||||
|
|
||||||
def push(command : Command)
|
def push(command : Command)
|
||||||
printf "push command %d\n", command.ts
|
printf "push command %d\n", command.ts
|
||||||
@data.push(command)
|
@queue.push(command)
|
||||||
@data.sort! do |c|
|
@queue.sort! do |c|
|
||||||
-c.ts
|
-c.ts
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(ts : Int32)
|
def run(ts : Int32)
|
||||||
while @data.size != 0
|
while @queue.size != 0
|
||||||
c = @data.pop
|
c = @queue.pop
|
||||||
printf "pop command %d\n", c.ts
|
printf "pop command %d\n", c.ts
|
||||||
if c.ts > ts
|
if c.ts > ts
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
c.run(self, @resources)
|
c.run(self)
|
||||||
end
|
end
|
||||||
printf "Wood: %d\n", @resources.wood
|
printf "Wood: %d\n", @resources.wood
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
q = CommandQueue.new
|
q = World.new
|
||||||
q.push(BuildMillCommand.new(0))
|
q.push(BuildMillCommand.new(0))
|
||||||
q.push(BuildMillCommand.new(0))
|
q.push(BuildMillCommand.new(0))
|
||||||
q.push(BuildMillCommand.new(0))
|
q.push(BuildMillCommand.new(0))
|
||||||
|
Loading…
Reference in New Issue
Block a user