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-23 17:38:27 +01:00
|
|
|
onready var collision_shape: = $CollisionShape2D
|
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
|
|
|
|
var speed: float
|
|
|
|
var color: Color
|
2019-12-23 17:38:27 +01:00
|
|
|
var radius: = 0.0
|
2019-12-16 22:14:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
agent.max_linear_acceleration = speed/10
|
|
|
|
agent.max_linear_speed = speed
|
2019-12-23 17:38:27 +01:00
|
|
|
radius = collision_shape.shape.radius
|
2019-12-16 22:14:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _draw() -> void:
|
|
|
|
draw_circle(Vector2.ZERO, radius, color)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
if velocity.length_squared() > 0:
|
|
|
|
update()
|
|
|
|
|
|
|
|
|
|
|
|
func _update_agent() -> void:
|
|
|
|
agent.position = Vector3(global_position.x, global_position.y, 0)
|
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
|