Files
Diabolo/Demon_v0.19/helper_roaming.gd
T

48 lines
2.1 KiB
GDScript

extends RefCounted
var w
var rng = RandomNumberGenerator.new()
func setup(workshop):
w = workshop
rng.seed = 291026
func spawn_point():
var entrance = w.g.GATE_EXIT + Vector3(1.1,0,0)
var candidates = Array(w.free_floor_points)
candidates.sort_custom(func(a,b): return a.distance_squared_to(entrance)<b.distance_squared_to(entrance))
for point in candidates:
if w.helpers.any(func(h): return h.root.position.distance_to(point)<.85): continue
if w.g.player.position.distance_to(point)<.8: continue
if w.nav.get_id_path(w.cell(entrance),w.cell(point)).is_empty(): continue
return point
return w.free_floor(entrance,.42)
func cancel(h):
h.roam_target = null; h.roam_wait = 0.0; h.roam_stuck = 0.0
h.path = PackedVector2Array(); h.destination = Vector2i(-1,-1); h.revision = -1
func update(h, dt):
if w.g.paused: return
if h.get("roam_revision",-1)!=w.revision:
cancel(h); h.roam_revision=w.revision
if h.get("roam_wait",0.0)>0:
h.roam_wait=maxf(0,h.roam_wait-dt); h.model.set_walking(false); return
if h.get("roam_target")==null:
# Bounded search and existing nav grid; never route through equipment.
for attempt in range(24):
if w.free_floor_points.is_empty(): break
var point=w.free_floor_points[rng.randi_range(0,w.free_floor_points.size()-1)]
var distance=h.root.position.distance_to(point)
if distance<2 or distance>7 or point.x<-4.2: continue
if w.g.player.position.distance_to(point)<1: continue
if w.helpers.any(func(other): return other!=h and (other.root.position.distance_to(point)<1 or (other.get("roam_target")!=null and other.roam_target.distance_to(point)<1))): continue
if w.nav.get_id_path(w.cell(h.root.position),w.cell(point)).is_empty(): continue
h.roam_target=point; h.revision=-1; break
if h.get("roam_target")==null:
h.roam_wait=1.0; h.model.set_walking(false); return
var before=h.root.position
var arrived=w.move_helper(h,h.roam_target,dt*.65)
h.roam_stuck=h.get("roam_stuck",0.0)+dt if before.distance_squared_to(h.root.position)<.000001 else 0.0
if arrived or h.roam_stuck>1.0:
cancel(h); h.roam_wait=rng.randf_range(1.0,3.0); h.model.set_walking(false)