Make auto world run

This commit is contained in:
Anton Vakhrushev 2019-10-04 17:51:41 +03:00
parent de5a36d6d6
commit 123a025ce5
3 changed files with 19 additions and 18 deletions

View File

@ -5,13 +5,14 @@ require "./game/resources"
require "./game/world"
require "./cli/command_router"
world = Game::World.new
ts = Time.local.to_unix
world = Game::World.new(ts)
router = CLI::CommandRouter.new
router.add "st" do
printf "Stat:\n\tTime: %d\n\tCrystals: %d\n\tTarraform: %d\n",
world.ts,
printf "Stat:\n\tTime: %s\n\tCrystals: %d\n\tTarraform: %d\n",
Time.unix(world.ts).to_local.to_s,
world.resources[Game::ResourceType::Crystal],
world.resources[Game::ResourceType::Terraformation]
end
@ -20,29 +21,26 @@ router.add "m" do
world.map.print
end
router.add "run {ts}" do |p|
ts = p["ts"].to_i32
world.run ts
printf "Run to %d\n", ts
end
router.add "harv {x} {y}" do |p|
x = p["x"].to_i32
y = p["y"].to_i32
world.push(Game::BuildCrystalHarvesterCommand.new(Game::Point.new(x, y)))
point = Game::Point.new(x, y)
world.push(Game::BuildCrystalHarvesterCommand.new(point))
printf "Build harvester at %d %d\n", x, y
end
router.add "rest {x} {y}" do |p|
x = p["x"].to_i32
y = p["y"].to_i32
world.push(Game::BuildCrystalRestorerCommand.new(Game::Point.new(x, y)))
point = Game::Point.new(x, y)
world.push(Game::BuildCrystalRestorerCommand.new(point))
end
router.add "terr {x} {y}" do |p|
x = p["x"].to_i32
y = p["y"].to_i32
world.push(Game::BuildTerraformerCommand.new(Game::Point.new(x, y)))
point = Game::Point.new(x, y)
world.push(Game::BuildTerraformerCommand.new(point))
end
def normalize_command(cmd)
@ -56,6 +54,8 @@ loop do
if norm == "exit"
break
end
current_time = Time.local.to_unix
world.run current_time
router.handle cmd
printf "\n"
end

View File

@ -1,6 +1,6 @@
class Game::Queue
struct Item
def initialize(@ts : Int32, @command : Game::Command)
def initialize(@ts : Int64, @command : Game::Command)
end
getter ts
@ -11,7 +11,7 @@ class Game::Queue
@data = [] of Item
end
def push(ts : Int32, value : Game::Command)
def push(ts : Int64, value : Game::Command)
# very unoptimal algo
@data.push(Item.new(ts, value))
@data.sort! do |a, b|
@ -19,7 +19,7 @@ class Game::Queue
end
end
def pop(ts : Int32) : Item | Nil
def pop(ts : Int64) : Item | Nil
if @data.size == 0
return nil
end

View File

@ -1,7 +1,7 @@
require "./resources"
class Game::World
property ts : Int32
property ts : Int64
def initialize(@ts = 0)
@map = Map.new
@ -15,11 +15,11 @@ class Game::World
def push(command : Command)
dur = command.start(self)
done_at = @ts + dur
done_at = @ts + dur.to_i64
@tasks.push(done_at, command)
end
def run(ts : Int32)
def run(ts : Int64)
loop do
item = @tasks.pop(ts)
if item.nil?
@ -29,5 +29,6 @@ class Game::World
@ts = item.ts
command.finish(self)
end
@ts = ts
end
end