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
|
|
|
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
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)
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
var player_agent: GSTAgentLocation
|
2020-01-16 09:44:44 +01:00
|
|
|
var velocity := Vector2.ZERO
|
2020-01-13 18:38:46 +01:00
|
|
|
var start_speed: float
|
|
|
|
var start_accel: float
|
2020-01-16 09:44:44 +01:00
|
|
|
var use_seek := true
|
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()
|
2020-01-15 20:15:31 +01:00
|
|
|
if use_seek:
|
|
|
|
accel = seek.calculate_steering(accel)
|
|
|
|
else:
|
|
|
|
accel = flee.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
|