2019-12-16 22:14:46 +01:00
|
|
|
extends KinematicBody2D
|
2020-01-02 23:42:41 +01:00
|
|
|
# AI agent that uses the Seek behavior to hone in on the player's location as directly as possible.
|
2019-12-16 22:14:46 +01:00
|
|
|
|
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
onready var agent: = GSTSteeringAgent.new()
|
|
|
|
onready var accel: = GSTTargetAcceleration.new()
|
|
|
|
onready var seek: = GSTSeek.new(agent, player_agent)
|
2019-12-21 19:07:43 +01:00
|
|
|
onready var flee: = GSTFlee.new(agent, player_agent)
|
|
|
|
onready var _active_behavior: = seek
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
var player_agent: GSTAgentLocation
|
2019-12-16 22:14:46 +01:00
|
|
|
var velocity: = Vector2.ZERO
|
2020-01-13 18:38:46 +01:00
|
|
|
var start_speed: float
|
|
|
|
var start_accel: float
|
2019-12-16 22:14:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2020-01-13 18:38:46 +01:00
|
|
|
agent.max_linear_acceleration = start_accel
|
|
|
|
agent.max_linear_speed = start_speed
|
2019-12-16 22:14:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2019-12-17 05:12:18 +01:00
|
|
|
if not player_agent:
|
|
|
|
return
|
|
|
|
|
2019-12-16 22:14:46 +01:00
|
|
|
_update_agent()
|
2019-12-21 19:07:43 +01:00
|
|
|
accel = _active_behavior.calculate_steering(accel)
|
2019-12-16 22:14:46 +01:00
|
|
|
|
|
|
|
velocity = (velocity + Vector2(accel.linear.x, accel.linear.y)).clamped(agent.max_linear_speed)
|
|
|
|
velocity = move_and_slide(velocity)
|
|
|
|
|
|
|
|
|
|
|
|
func _update_agent() -> void:
|
2020-01-13 18:38:46 +01:00
|
|
|
agent.position.x = global_position.x
|
|
|
|
agent.position.y = global_position.y
|
|
|
|
agent.linear_velocity.x = velocity.x
|
|
|
|
agent.linear_velocity.y = velocity.y
|
2019-12-21 19:07:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _on_GUI_mode_changed(mode: int) -> void:
|
|
|
|
if mode == 0:
|
|
|
|
_active_behavior = seek
|
|
|
|
else:
|
|
|
|
_active_behavior = flee
|
2020-01-13 18:38:46 +01:00
|
|
|
|
|
|
|
|
2020-01-13 22:15:44 +01:00
|
|
|
func _on_GUI_accel_changed(value: float) -> void:
|
2020-01-13 18:38:46 +01:00
|
|
|
agent.max_linear_acceleration = value
|
|
|
|
|
|
|
|
|
|
|
|
func _on_GUI_speed_changed(value: float) -> void:
|
|
|
|
agent.max_linear_speed = value
|