mirror of
https://github.com/Relintai/godot-steering-ai-framework.git
synced 2024-11-14 04:57:19 +01:00
Add Cohesion behavior and RadiusProximity caching
This commit is contained in:
parent
ac99344633
commit
34b58a6bba
@ -20,10 +20,20 @@ _global_script_classes=[ {
|
|||||||
"path": "res://src/behaviors/GSTArrive.gd"
|
"path": "res://src/behaviors/GSTArrive.gd"
|
||||||
}, {
|
}, {
|
||||||
"base": "GSTSteeringBehavior",
|
"base": "GSTSteeringBehavior",
|
||||||
|
"class": "GSTAvoidCollisions",
|
||||||
|
"language": "GDScript",
|
||||||
|
"path": "res://src/behaviors/GSTAvoidCollisions.gd"
|
||||||
|
}, {
|
||||||
|
"base": "GSTSteeringBehavior",
|
||||||
"class": "GSTBlend",
|
"class": "GSTBlend",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://src/behaviors/GSTBlend.gd"
|
"path": "res://src/behaviors/GSTBlend.gd"
|
||||||
}, {
|
}, {
|
||||||
|
"base": "GSTSteeringBehavior",
|
||||||
|
"class": "GSTCohesion",
|
||||||
|
"language": "GDScript",
|
||||||
|
"path": "res://src/behaviors/GSTCohesion.gd"
|
||||||
|
}, {
|
||||||
"base": "GSTPursue",
|
"base": "GSTPursue",
|
||||||
"class": "GSTEvade",
|
"class": "GSTEvade",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
@ -39,6 +49,11 @@ _global_script_classes=[ {
|
|||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://src/behaviors/GSTFlee.gd"
|
"path": "res://src/behaviors/GSTFlee.gd"
|
||||||
}, {
|
}, {
|
||||||
|
"base": "GSTArrive",
|
||||||
|
"class": "GSTFollowPath",
|
||||||
|
"language": "GDScript",
|
||||||
|
"path": "res://src/behaviors/GSTFollowPath.gd"
|
||||||
|
}, {
|
||||||
"base": "GSTProximity",
|
"base": "GSTProximity",
|
||||||
"class": "GSTInfiniteProximity",
|
"class": "GSTInfiniteProximity",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
@ -54,6 +69,11 @@ _global_script_classes=[ {
|
|||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://src/behaviors/GSTMatchOrientation.gd"
|
"path": "res://src/behaviors/GSTMatchOrientation.gd"
|
||||||
}, {
|
}, {
|
||||||
|
"base": "Reference",
|
||||||
|
"class": "GSTPath",
|
||||||
|
"language": "GDScript",
|
||||||
|
"path": "res://src/GSTPath.gd"
|
||||||
|
}, {
|
||||||
"base": "GSTSteeringBehavior",
|
"base": "GSTSteeringBehavior",
|
||||||
"class": "GSTPriority",
|
"class": "GSTPriority",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
@ -107,13 +127,17 @@ _global_script_classes=[ {
|
|||||||
_global_script_class_icons={
|
_global_script_class_icons={
|
||||||
"GSTAgentLocation": "",
|
"GSTAgentLocation": "",
|
||||||
"GSTArrive": "",
|
"GSTArrive": "",
|
||||||
|
"GSTAvoidCollisions": "",
|
||||||
"GSTBlend": "",
|
"GSTBlend": "",
|
||||||
|
"GSTCohesion": "",
|
||||||
"GSTEvade": "",
|
"GSTEvade": "",
|
||||||
"GSTFace": "",
|
"GSTFace": "",
|
||||||
"GSTFlee": "",
|
"GSTFlee": "",
|
||||||
|
"GSTFollowPath": "",
|
||||||
"GSTInfiniteProximity": "",
|
"GSTInfiniteProximity": "",
|
||||||
"GSTLookWhereYouGo": "",
|
"GSTLookWhereYouGo": "",
|
||||||
"GSTMatchOrientation": "",
|
"GSTMatchOrientation": "",
|
||||||
|
"GSTPath": "",
|
||||||
"GSTPriority": "",
|
"GSTPriority": "",
|
||||||
"GSTProximity": "",
|
"GSTProximity": "",
|
||||||
"GSTPursue": "",
|
"GSTPursue": "",
|
||||||
|
@ -11,3 +11,4 @@ var max_angular_acceleration: = 0.0
|
|||||||
var linear_velocity: = Vector3.ZERO
|
var linear_velocity: = Vector3.ZERO
|
||||||
var angular_velocity: = 0.0
|
var angular_velocity: = 0.0
|
||||||
var bounding_radius: = 0.0
|
var bounding_radius: = 0.0
|
||||||
|
var tagged: = false
|
||||||
|
27
project/src/behaviors/GSTCohesion.gd
Normal file
27
project/src/behaviors/GSTCohesion.gd
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
extends GSTSteeringBehavior
|
||||||
|
class_name GSTCohesion
|
||||||
|
# Group behavior that produces linear acceleration that attempts to move the agent towards the
|
||||||
|
# center of mass of the agents in the area defined by the defined Proximity.
|
||||||
|
|
||||||
|
|
||||||
|
var center_of_mass: Vector3
|
||||||
|
var proximity: GSTProximity
|
||||||
|
|
||||||
|
|
||||||
|
func _init(agent: GSTSteeringAgent, proximity: GSTProximity).(agent) -> void:
|
||||||
|
self.proximity = proximity
|
||||||
|
|
||||||
|
|
||||||
|
func _calculate_steering(acceleration: GSTTargetAcceleration) -> GSTTargetAcceleration:
|
||||||
|
acceleration.set_zero()
|
||||||
|
center_of_mass = Vector3.ZERO
|
||||||
|
var neighbor_count = proximity.find_neighbors(funcref(self, "_report_neighbor"))
|
||||||
|
if neighbor_count > 0:
|
||||||
|
center_of_mass *= 1.0 / neighbor_count
|
||||||
|
acceleration.linear = (center_of_mass - agent.position).normalized() * agent.max_linear_acceleration
|
||||||
|
return acceleration
|
||||||
|
|
||||||
|
|
||||||
|
func _report_neighbor(neighbor: GSTSteeringAgent) -> bool:
|
||||||
|
center_of_mass += neighbor.position
|
||||||
|
return true
|
@ -1,16 +1,16 @@
|
|||||||
extends Reference
|
extends Reference
|
||||||
class_name GSTProximity
|
class_name GSTProximity
|
||||||
# Defines an area that is used by group behaviors to find and process the owner's neighbors.
|
# Defines an area that is used by group behaviors to find and process the owner's neighbors.
|
||||||
|
|
||||||
|
|
||||||
var agent: GSTSteeringAgent
|
var agent: GSTSteeringAgent
|
||||||
var agents: = []
|
var agents: = []
|
||||||
|
|
||||||
|
|
||||||
func _init(agent: GSTSteeringAgent, agents: Array) -> void:
|
func _init(agent: GSTSteeringAgent, agents: Array) -> void:
|
||||||
self.agent = agent
|
self.agent = agent
|
||||||
self.agents = agents
|
self.agents = agents
|
||||||
|
|
||||||
|
|
||||||
func find_neighbors(callback: FuncRef) -> int:
|
func find_neighbors(callback: FuncRef) -> int:
|
||||||
return 0
|
return 0
|
||||||
|
@ -6,28 +6,46 @@ class_name GSTRadiusProximity
|
|||||||
|
|
||||||
var radius: = 0.0
|
var radius: = 0.0
|
||||||
|
|
||||||
|
var _last_frame: = 0
|
||||||
|
var _scene_tree: SceneTree
|
||||||
|
|
||||||
|
|
||||||
func _init(agent: GSTSteeringAgent, agents: Array, radius: float).(agent, agents) -> void:
|
func _init(agent: GSTSteeringAgent, agents: Array, radius: float).(agent, agents) -> void:
|
||||||
self.radius = radius
|
self.radius = radius
|
||||||
|
_scene_tree = Engine.get_main_loop()
|
||||||
|
|
||||||
|
|
||||||
func find_neighbors(callback: FuncRef) -> int:
|
func find_neighbors(callback: FuncRef) -> int:
|
||||||
var agent_count: = agents.size()
|
var agent_count: = agents.size()
|
||||||
var neighbor_count: = 0
|
var neighbor_count: = 0
|
||||||
|
|
||||||
var owner_position: = agent.position
|
|
||||||
|
|
||||||
for i in range(agent_count):
|
var current_frame: = _scene_tree.get_frame() if _scene_tree else -_last_frame
|
||||||
var current_agent: = agents[i] as GSTSteeringAgent
|
if current_frame != _last_frame:
|
||||||
|
_last_frame = current_frame
|
||||||
if current_agent != agent:
|
|
||||||
var distance_squared: = owner_position.distance_squared_to(current_agent.position)
|
var owner_position: = agent.position
|
||||||
|
|
||||||
var range_to: = radius + current_agent.bounding_radius
|
for i in range(agent_count):
|
||||||
|
var current_agent: = agents[i] as GSTSteeringAgent
|
||||||
if distance_squared < range_to * range_to:
|
|
||||||
|
if current_agent != agent:
|
||||||
|
var distance_squared: = owner_position.distance_squared_to(current_agent.position)
|
||||||
|
|
||||||
|
var range_to: = radius + current_agent.bounding_radius
|
||||||
|
|
||||||
|
if distance_squared < range_to * range_to:
|
||||||
|
if callback.call_func(current_agent) == true:
|
||||||
|
current_agent.tagged = true
|
||||||
|
neighbor_count += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
current_agent.tagged = false
|
||||||
|
else:
|
||||||
|
for i in range(agent_count):
|
||||||
|
var current_agent = agents[i] as GSTSteeringAgent
|
||||||
|
|
||||||
|
if current_agent != agent and current_agent.tagged:
|
||||||
if callback.call_func(current_agent) == true:
|
if callback.call_func(current_agent) == true:
|
||||||
neighbor_count += 1
|
neighbor_count += 1
|
||||||
continue
|
|
||||||
|
|
||||||
return neighbor_count
|
return neighbor_count
|
||||||
|
Loading…
Reference in New Issue
Block a user