mirror of
https://github.com/Relintai/godot-steering-ai-framework.git
synced 2024-11-14 04:57:19 +01:00
9f945cbf85
Razvan reviewed the code and suggested a number of changes to improve readability and make facets of the code more explicit and consistent.
54 lines
1.3 KiB
GDScript
54 lines
1.3 KiB
GDScript
extends KinematicBody2D
|
|
"""
|
|
AI agent that uses the Seek behavior to hone in on the player's location as directly as possible.
|
|
"""
|
|
|
|
|
|
onready var collision_shape: = $CollisionShape2D
|
|
|
|
onready var agent: = GSTSteeringAgent.new()
|
|
onready var accel: = GSTTargetAcceleration.new()
|
|
onready var seek: = GSTSeek.new(agent, player_agent)
|
|
onready var flee: = GSTFlee.new(agent, player_agent)
|
|
onready var _active_behavior: = seek
|
|
|
|
var player_agent: GSTAgentLocation
|
|
var velocity: = Vector2.ZERO
|
|
var speed: float
|
|
var color: Color
|
|
var radius: = 0.0
|
|
|
|
|
|
func _ready() -> void:
|
|
agent.max_linear_acceleration = speed/10
|
|
agent.max_linear_speed = speed
|
|
radius = collision_shape.shape.radius
|
|
|
|
|
|
func _draw() -> void:
|
|
draw_circle(Vector2.ZERO, radius, color)
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if not player_agent:
|
|
return
|
|
|
|
_update_agent()
|
|
accel = _active_behavior.calculate_steering(accel)
|
|
|
|
velocity = (velocity + Vector2(accel.linear.x, accel.linear.y)).clamped(agent.max_linear_speed)
|
|
velocity = move_and_slide(velocity)
|
|
if velocity.length_squared() > 0:
|
|
update()
|
|
|
|
|
|
func _update_agent() -> void:
|
|
agent.position = Vector3(global_position.x, global_position.y, 0)
|
|
|
|
|
|
func _on_GUI_mode_changed(mode: int) -> void:
|
|
if mode == 0:
|
|
_active_behavior = seek
|
|
else:
|
|
_active_behavior = flee
|