From a2a313698d163c9fd623449bb56cc253853a2151 Mon Sep 17 00:00:00 2001 From: Scott Wadden Date: Sun, 16 Apr 2023 23:40:24 -0300 Subject: [PATCH] Fixed bot movement --- src/controllers/script_controllers.nim | 11 +++++++++-- src/models/units.nim | 1 - src/nodes/bot_node.nim | 6 ++++-- src/types.nim | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/controllers/script_controllers.nim b/src/controllers/script_controllers.nim index c7a1dc7b..3ddeab82 100644 --- a/src/controllers/script_controllers.nim +++ b/src/controllers/script_controllers.nim @@ -135,6 +135,7 @@ proc begin_turn(self: Worker, unit: Unit, direction: Vector3, degrees: float, var degrees = floor_mod(degrees, 360) let ctx = self.active_unit.script_ctx ctx.callback = unit.on_begin_turn(direction, degrees, lean, move_mode) + ctx.last_ran = MonoTime.default if not ctx.callback.is_nil: ctx.pause() @@ -148,6 +149,7 @@ proc begin_move(self: Worker, unit: Unit, direction: Vector3, steps: float, steps = steps * -1 direction = direction * -1 ctx.callback = unit.on_begin_move(direction, steps, move_mode) + ctx.last_ran = MonoTime.default if not ctx.callback.is_nil: ctx.pause() @@ -161,6 +163,7 @@ proc sleep_impl(ctx: ScriptCtx, seconds: float) = Running else: Done + ctx.last_ran = MonoTime.default ctx.pause() proc hit(unit_a: Unit, unit_b: Unit): Vector3 = @@ -508,8 +511,12 @@ proc advance_unit(self: Worker, unit: Unit, timeout: MonoTime): bool = let now = get_mono_time() - let delta = (now - unit.last_ran).in_microseconds.float / 1000000.0 - unit.last_ran = now + let delta = if ?ctx.last_ran: + (now - ctx.last_ran).in_microseconds.float / 1000000.0 + else: + 0.0 + + ctx.last_ran = now if ctx.callback == nil or (task_state = ctx.callback(delta, timeout); task_state in {Done, NextTask}): diff --git a/src/models/units.nim b/src/models/units.nim index 5f1181a9..b915d900 100644 --- a/src/models/units.nim +++ b/src/models/units.nim @@ -14,7 +14,6 @@ proc init_unit*[T: Unit](self: T) = scale = Zen.init(1.0) glow = ZenValue[float].init color = Zen.init(self.start_color) - last_ran = get_mono_time() if ?self.parent: self.shared = self.parent.shared diff --git a/src/nodes/bot_node.nim b/src/nodes/bot_node.nim index f697599c..01b695bc 100644 --- a/src/nodes/bot_node.nim +++ b/src/nodes/bot_node.nim @@ -104,8 +104,6 @@ gdobj BotNode of KinematicBody: let bot = Bot(self.model) velocity_zid = bot.velocity.watch: if touched: - bot.velocity.pause velocity_zid: - bot.velocity.value = self.move_and_slide(change.item, UP) if bot.animation.value == "auto": self.set_walk_animation(change.item.length, false) bot.animation.watch: @@ -150,6 +148,10 @@ gdobj BotNode of KinematicBody: self.model.transform.pause self.transform_zid: self.model.transform.value = self.transform self.model.global_transform.value = self.global_transform + if self.model of Bot: + let bot = Bot(self.model) + if bot.velocity.value.length > 0: + discard self.move_and_slide(self.model.velocity.value, UP) var bot_scene {.threadvar.}: PackedScene proc init*(_: type BotNode): BotNode = diff --git a/src/types.nim b/src/types.nim index 1b86f4a0..f82004c6 100644 --- a/src/types.nim +++ b/src/types.nim @@ -94,7 +94,6 @@ type sight_ray*: RayCast frame_created*: int zids*: seq[ZID] - last_ran*: MonoTime Player* = ref object of Unit colliders*: HashSet[Model] @@ -177,6 +176,7 @@ type code*: string dependents*: HashSet[string] pass_context*: TPassContextArray + last_ran*: MonoTime VMError* = object of CatchableError QuitKind* = enum