From b32446217d939204cddf335402a21d406273dbee Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Fri, 20 Dec 2019 21:32:02 -0500 Subject: [PATCH] Implement demo for Arrive behavior --- project/demos/arrive/Arrive.tscn | 20 ++++++++++++++ project/demos/arrive/ArriveDemo.gd | 8 ++++++ project/demos/arrive/Arriver.gd | 44 ++++++++++++++++++++++++++++++ project/demos/arrive/Target.gd | 14 ++++++++++ project/demos/face/Turret.gd | 1 - project/project.godot | 12 ++++++++ 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 project/demos/arrive/Arrive.tscn create mode 100644 project/demos/arrive/ArriveDemo.gd create mode 100644 project/demos/arrive/Arriver.gd create mode 100644 project/demos/arrive/Target.gd diff --git a/project/demos/arrive/Arrive.tscn b/project/demos/arrive/Arrive.tscn new file mode 100644 index 0000000..1b5d194 --- /dev/null +++ b/project/demos/arrive/Arrive.tscn @@ -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 ) diff --git a/project/demos/arrive/ArriveDemo.gd b/project/demos/arrive/ArriveDemo.gd new file mode 100644 index 0000000..ab3fe71 --- /dev/null +++ b/project/demos/arrive/ArriveDemo.gd @@ -0,0 +1,8 @@ +extends Node2D + + +onready var _target: = $Target + + +func draw(location: Vector2) -> void: + _target.draw(location) diff --git a/project/demos/arrive/Arriver.gd b/project/demos/arrive/Arriver.gd new file mode 100644 index 0000000..b38fee7 --- /dev/null +++ b/project/demos/arrive/Arriver.gd @@ -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) diff --git a/project/demos/arrive/Target.gd b/project/demos/arrive/Target.gd new file mode 100644 index 0000000..831df6c --- /dev/null +++ b/project/demos/arrive/Target.gd @@ -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() diff --git a/project/demos/face/Turret.gd b/project/demos/face/Turret.gd index 5c3760d..9306eae 100644 --- a/project/demos/face/Turret.gd +++ b/project/demos/face/Turret.gd @@ -6,7 +6,6 @@ onready var _cannon: = Rect2(Vector2(-5, 0), Vector2(10, -_radius*2)) onready var _agent: = GSTSteeringAgent.new() onready var _accel: = GSTTargetAcceleration.new() - var _angular_velocity: = 0.0 var _angular_drag: = 1.0 var _face: GSTFace diff --git a/project/project.godot b/project/project.godot index 8531524..45baf3a 100644 --- a/project/project.godot +++ b/project/project.godot @@ -19,6 +19,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://src/behaviors/GSTArrive.gd" }, { +"base": "GSTSteeringBehavior", +"class": "GSTBlend", +"language": "GDScript", +"path": "res://src/behaviors/GSTBlend.gd" +}, { "base": "GSTPursue", "class": "GSTEvade", "language": "GDScript", @@ -45,6 +50,11 @@ _global_script_classes=[ { "path": "res://src/behaviors/GSTMatchOrientation.gd" }, { "base": "GSTSteeringBehavior", +"class": "GSTPriority", +"language": "GDScript", +"path": "res://src/behaviors/GSTPriority.gd" +}, { +"base": "GSTSteeringBehavior", "class": "GSTPursue", "language": "GDScript", "path": "res://src/behaviors/GSTPursue.gd" @@ -77,11 +87,13 @@ _global_script_classes=[ { _global_script_class_icons={ "GSTAgentLocation": "", "GSTArrive": "", +"GSTBlend": "", "GSTEvade": "", "GSTFace": "", "GSTFlee": "", "GSTLookWhereYouGo": "", "GSTMatchOrientation": "", +"GSTPriority": "", "GSTPursue": "", "GSTSeek": "", "GSTSteeringAgent": "",