1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
extends Node
var calcs_per_tick: int = 15
var request_queue: Array = [] #array of dictionaries
var cur_req: Dictionary
var cur_req_args: Dictionary
var WORLD_MAP_RID: RID
enum Task {PATHFIND, FINDITEM}
#tasks: find path, find object
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func set_rid(id: RID) -> void:
WORLD_MAP_RID = id
func _physics_process(_delta):
for i in calcs_per_tick:
if len(request_queue) == 0:
return
cur_req = request_queue.pop_front()
cur_req_args = cur_req["args"]
if cur_req["type"] == Task.PATHFIND:
var dest_vec: Vector3 = cur_req_args["dest"] if cur_req_args["obj"] == null else cur_req_args["obj"].global_transform.origin
var path: PoolVector3Array = NavigationServer.map_get_path(WORLD_MAP_RID, cur_req["char"].global_transform.origin, dest_vec, cur_req_args["optimize"])
cur_req["char"].ai_set_path_array(path)
elif cur_req["type"] == Task.FINDITEM:
var bodies = cur_req["char"].get_node("AISearchArea").get_overlapping_bodies()
for body in bodies:
if body.name.begins_with(cur_req_args["objtype"]):
#var path: PoolVector3Array = NavigationServer.map_get_path(WORLD_MAP_RID, cur_req["char"].global_transform.origin, body.global_transform.origin, cur_req_args["optimize"])
cur_req["char"].ai_set_path_target(body.global_transform.origin)
#cur_req["char"].ai_set_path_array(path)
cur_req["char"].ai_set_look_status(body, "TRACK")
return
#will request a path from character to dest_g, or to the global origin of obj if passed
func request_find_path(character: NetChar, dest_g: Vector3, precise: bool, obj: Spatial = null) -> void:
request_queue.append({"char" : character, "type" : Task.PATHFIND, "args" : {"dest" : dest_g, "obj" : obj, "optimize" : !precise}})
func request_find_object(character: NetChar, object: String, radius: float, precise: bool):
request_queue.append({"char" : character, "type" : Task.FINDITEM, "args" : {"objtype" : object, "radius" : radius, "optimize" : !precise}})
|