66 lines
2.1 KiB
GDScript
66 lines
2.1 KiB
GDScript
extends BTAction
|
|
class_name BTBuildPath
|
|
|
|
func is_dynamic_target(npc):
|
|
var target_group = npc.has_meta("target_group")
|
|
if target_group in ["_player", "_away", "_enemy", "female", "male"]:
|
|
return true
|
|
return false
|
|
func tick(tick: Tick) -> int:
|
|
var npc = tick.actor
|
|
var bb = tick.blackboard
|
|
var npc_pos = npc.global_transform.origin
|
|
assert(npc.has_meta("target_loc"))
|
|
assert(npc.has_meta("target_group"))
|
|
var loc = npc.get_meta("target_loc")
|
|
var target_group = npc.get_meta("target_group")
|
|
if npc.has_meta("path_valid") && !is_dynamic_target(npc):
|
|
var valid = npc.get_meta("path_valid")
|
|
if valid > 0.0:
|
|
valid -= tick.blackboard.get("delta")
|
|
npc.set_meta("path_valid", valid)
|
|
return OK
|
|
# print("path no longer valid")
|
|
if loc:
|
|
var path = []
|
|
var target_result: Dictionary = bb.get("target_result").duplicate()
|
|
assert(typeof(target_result) == TYPE_DICTIONARY)
|
|
assert(target_result != null)
|
|
print(target_result)
|
|
var target_collider: PhysicsBody
|
|
var target_position: Vector3
|
|
if !target_result.has("collider"):
|
|
path = [npc_pos, loc]
|
|
if target_result.has("collider"):
|
|
target_collider = target_result.collider
|
|
target_position = target_result.position
|
|
if target_group == "_away":
|
|
if target_collider:
|
|
loc = target_position
|
|
path = [npc_pos, loc]
|
|
else:
|
|
var metaai = tick.blackboard.get("metaai")
|
|
var astar: AStar = metaai.astar
|
|
var start = astar.get_closest_point(npc_pos)
|
|
var end = astar.get_closest_point(loc)
|
|
path = astar.get_point_path(start, end)
|
|
if start == end && npc_pos.distance_to(loc) > 2.0:
|
|
path = []
|
|
if path.size() == 0:
|
|
path = [npc_pos, loc]
|
|
|
|
# assert(path.size() > 0)
|
|
# print("BTBuildPath: ", npc.name, " ", path.size(), ": ", path)
|
|
if path.size() == 0:
|
|
path = [npc_pos, loc]
|
|
npc.set_meta("path", path)
|
|
if !npc.get_meta("target_group") in ["_player", "_away", "female", "male"]:
|
|
npc.set_meta("path_valid", 1.0)
|
|
else:
|
|
npc.set_meta("path_valid", 0.3)
|
|
# if npc.get_meta("target_group") == "hide_spot":
|
|
# print("hide_spot: path: ", path)
|
|
return OK
|
|
# print("path failed")
|
|
return FAILED
|