Implement demo for Arrive behavior

This commit is contained in:
Francois Belair 2019-12-20 21:32:02 -05:00
parent 546d12b15b
commit b32446217d
6 changed files with 98 additions and 1 deletions

View File

@ -0,0 +1,20 @@
[gd_scene load_steps=5 format=2]
[ext_resource path="res://demos/arrive/Arriver.gd" type="Script" id=1]
[ext_resource path="res://demos/arrive/ArriveDemo.gd" type="Script" id=2]
[ext_resource path="res://demos/arrive/Target.gd" type="Script" id=3]
[sub_resource type="CircleShape2D" id=1]
radius = 15.0
[node name="Arrive" type="Node2D"]
script = ExtResource( 2 )
[node name="Arriver" type="KinematicBody2D" parent="."]
script = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="Arriver"]
shape = SubResource( 1 )
[node name="Target" type="Node2D" parent="."]
script = ExtResource( 3 )

View File

@ -0,0 +1,8 @@
extends Node2D
onready var _target: = $Target
func draw(location: Vector2) -> void:
_target.draw(location)

View File

@ -0,0 +1,44 @@
extends KinematicBody2D
onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius
onready var _agent: = GSTSteeringAgent.new()
onready var _target: = GSTAgentLocation.new()
onready var _arrive: = GSTArrive.new(_agent, _target)
onready var _accel: = GSTTargetAcceleration.new()
var _velocity: = Vector2()
var _drag: = 1.0
func _ready() -> void:
_agent.max_linear_acceleration = 10
_agent.max_linear_speed = 200
_arrive.arrival_tolerance = 25
_arrive.deceleration_radius = 225
func _draw() -> void:
draw_circle(Vector2.ZERO, _radius, Color.red)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
var mb: InputEventMouseButton = event
if mb.button_index == BUTTON_LEFT and mb.pressed:
_target.position = Vector3(mb.position.x, mb.position.y, 0)
owner.draw(Vector2(_target.position.x, _target.position.y))
func _physics_process(delta: float) -> void:
_accel = _arrive.calculate_steering(_accel)
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
_velocity -= _velocity * _drag * delta
_velocity = move_and_slide(_velocity)
_update_agent()
func _update_agent() -> void:
_agent.position = Vector3(global_position.x, global_position.y, 0)
_agent.linear_velocity = Vector3(_velocity.x, _velocity.y, 0)

View File

@ -0,0 +1,14 @@
extends Node2D
var target: = Vector2.ZERO
func _draw() -> void:
draw_circle(target, 20, Color(1, 1, 0, 0.25))
draw_circle(target, 5, Color.yellow)
func draw(location: Vector2) -> void:
target = location
update()

View File

@ -6,7 +6,6 @@ onready var _cannon: = Rect2(Vector2(-5, 0), Vector2(10, -_radius*2))
onready var _agent: = GSTSteeringAgent.new() onready var _agent: = GSTSteeringAgent.new()
onready var _accel: = GSTTargetAcceleration.new() onready var _accel: = GSTTargetAcceleration.new()
var _angular_velocity: = 0.0 var _angular_velocity: = 0.0
var _angular_drag: = 1.0 var _angular_drag: = 1.0
var _face: GSTFace var _face: GSTFace

View File

@ -19,6 +19,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://src/behaviors/GSTArrive.gd" "path": "res://src/behaviors/GSTArrive.gd"
}, { }, {
"base": "GSTSteeringBehavior",
"class": "GSTBlend",
"language": "GDScript",
"path": "res://src/behaviors/GSTBlend.gd"
}, {
"base": "GSTPursue", "base": "GSTPursue",
"class": "GSTEvade", "class": "GSTEvade",
"language": "GDScript", "language": "GDScript",
@ -45,6 +50,11 @@ _global_script_classes=[ {
"path": "res://src/behaviors/GSTMatchOrientation.gd" "path": "res://src/behaviors/GSTMatchOrientation.gd"
}, { }, {
"base": "GSTSteeringBehavior", "base": "GSTSteeringBehavior",
"class": "GSTPriority",
"language": "GDScript",
"path": "res://src/behaviors/GSTPriority.gd"
}, {
"base": "GSTSteeringBehavior",
"class": "GSTPursue", "class": "GSTPursue",
"language": "GDScript", "language": "GDScript",
"path": "res://src/behaviors/GSTPursue.gd" "path": "res://src/behaviors/GSTPursue.gd"
@ -77,11 +87,13 @@ _global_script_classes=[ {
_global_script_class_icons={ _global_script_class_icons={
"GSTAgentLocation": "", "GSTAgentLocation": "",
"GSTArrive": "", "GSTArrive": "",
"GSTBlend": "",
"GSTEvade": "", "GSTEvade": "",
"GSTFace": "", "GSTFace": "",
"GSTFlee": "", "GSTFlee": "",
"GSTLookWhereYouGo": "", "GSTLookWhereYouGo": "",
"GSTMatchOrientation": "", "GSTMatchOrientation": "",
"GSTPriority": "",
"GSTPursue": "", "GSTPursue": "",
"GSTSeek": "", "GSTSeek": "",
"GSTSteeringAgent": "", "GSTSteeringAgent": "",