Skip to content

Commit

Permalink
Fixed bot movement
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Apr 17, 2023
1 parent 718084f commit a2a3136
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/controllers/script_controllers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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 =
Expand Down Expand Up @@ -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}):
Expand Down
1 change: 0 additions & 1 deletion src/models/units.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/nodes/bot_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -177,6 +176,7 @@ type
code*: string
dependents*: HashSet[string]
pass_context*: TPassContextArray
last_ran*: MonoTime

VMError* = object of CatchableError
QuitKind* = enum
Expand Down

0 comments on commit a2a3136

Please sign in to comment.