From 546d12b15bfd92a3e58d4b85fd3cf2526e97f7ca Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Fri, 20 Dec 2019 13:17:27 -0500 Subject: [PATCH] Implement demo for Face behavior --- project/demos/face/Face.tscn | 31 +++++++++++ project/demos/face/FaceDemo.gd | 4 ++ project/demos/face/Player.gd | 26 ++++++++++ project/demos/face/Turret.gd | 52 +++++++++++++++++++ .../demos/pursue_vs_seek/PursueVSSeek.tscn | 2 + .../seek_and_flee/{ => Agents}/Coward.tscn | 0 .../seek_and_flee/{ => Agents}/Seeker.tscn | 0 project/demos/seek_and_flee/SeekFlee.tscn | 4 +- project/src/behaviors/GSTMatchOrientation.gd | 4 +- 9 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 project/demos/face/Face.tscn create mode 100644 project/demos/face/FaceDemo.gd create mode 100644 project/demos/face/Player.gd create mode 100644 project/demos/face/Turret.gd rename project/demos/seek_and_flee/{ => Agents}/Coward.tscn (100%) rename project/demos/seek_and_flee/{ => Agents}/Seeker.tscn (100%) diff --git a/project/demos/face/Face.tscn b/project/demos/face/Face.tscn new file mode 100644 index 0000000..4099538 --- /dev/null +++ b/project/demos/face/Face.tscn @@ -0,0 +1,31 @@ +[gd_scene load_steps=6 format=2] + +[ext_resource path="res://demos/face/Player.gd" type="Script" id=1] +[ext_resource path="res://demos/face/Turret.gd" type="Script" id=2] +[ext_resource path="res://demos/face/FaceDemo.gd" type="Script" id=3] + +[sub_resource type="CircleShape2D" id=1] +radius = 20.0 + +[sub_resource type="CircleShape2D" id=2] +radius = 30.0 + +[node name="Face" type="Node2D"] +script = ExtResource( 3 ) +__meta__ = { +"_editor_description_": "A demo showing the usage of the Face steering behavior." +} + +[node name="Player" type="KinematicBody2D" parent="."] +position = Vector2( 512, 450 ) +script = ExtResource( 1 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"] +shape = SubResource( 1 ) + +[node name="Turret" type="KinematicBody2D" parent="."] +position = Vector2( 512, 150 ) +script = ExtResource( 2 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Turret"] +shape = SubResource( 2 ) diff --git a/project/demos/face/FaceDemo.gd b/project/demos/face/FaceDemo.gd new file mode 100644 index 0000000..28ac945 --- /dev/null +++ b/project/demos/face/FaceDemo.gd @@ -0,0 +1,4 @@ +extends Node2D + + +onready var player: = $Player diff --git a/project/demos/face/Player.gd b/project/demos/face/Player.gd new file mode 100644 index 0000000..a453ebb --- /dev/null +++ b/project/demos/face/Player.gd @@ -0,0 +1,26 @@ +extends KinematicBody2D + + +onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius +onready var agent: = GSTAgentLocation.new() + +export var speed: = 125.0 + + +func _draw() -> void: + draw_circle(Vector2.ZERO, _radius, Color.red) + + +func _physics_process(delta: float) -> void: + var movement: = _get_movement() + move_and_slide(movement * speed) + _update_agent() + + +func _get_movement() -> Vector2: + return Vector2( Input.get_action_strength("sf_right") - Input.get_action_strength("sf_left"), + Input.get_action_strength("sf_down") - Input.get_action_strength("sf_up")) + + +func _update_agent() -> void: + agent.position = Vector3(global_position.x, global_position.y, 0) diff --git a/project/demos/face/Turret.gd b/project/demos/face/Turret.gd new file mode 100644 index 0000000..5c3760d --- /dev/null +++ b/project/demos/face/Turret.gd @@ -0,0 +1,52 @@ +extends KinematicBody2D + + +onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius +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 + + +func _draw() -> void: + draw_rect(_cannon, Color.blue) + draw_circle(Vector2.ZERO, _radius, Color.teal) + + +func _physics_process(delta: float) -> void: + if not _face: + _setup() + + _accel = _face.calculate_steering(_accel) + _angular_velocity += _accel.angular + + if _angular_velocity < 0: + _angular_velocity += _angular_drag * delta + elif _angular_velocity > 0: + _angular_velocity -= _angular_drag * delta + + rotation += _angular_velocity * delta + + _update_agent() + + +func _update_agent() -> void: + _agent.angular_velocity = _angular_velocity + _agent.orientation = rotation + + +func _setup() -> void: + _face = GSTFace.new(_agent, owner.player.agent) + + _face.alignment_tolerance = 0.1 + _face.deceleration_radius = PI/2 + + _agent.max_angular_acceleration = 0.5 + _agent.max_angular_speed = 5 + _agent.position = Vector3(global_position.x, global_position.y, 0) + + _update_agent() diff --git a/project/demos/pursue_vs_seek/PursueVSSeek.tscn b/project/demos/pursue_vs_seek/PursueVSSeek.tscn index 7f3109b..4942c38 100644 --- a/project/demos/pursue_vs_seek/PursueVSSeek.tscn +++ b/project/demos/pursue_vs_seek/PursueVSSeek.tscn @@ -29,6 +29,7 @@ script = ExtResource( 1 ) color = Color( 0.811765, 0.909804, 0.113725, 1 ) [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BoundaryManager/Pursuer"] +modulate = Color( 0.694118, 0.694118, 0.694118, 1 ) polygon = PoolVector2Array( 0, -30, -25, 25, 25, 25 ) [node name="Seeker" type="KinematicBody2D" parent="BoundaryManager"] @@ -39,4 +40,5 @@ color = Color( 0.113725, 0.909804, 0.219608, 1 ) use_seek = true [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="BoundaryManager/Seeker"] +modulate = Color( 0.317647, 0.317647, 0.317647, 1 ) polygon = PoolVector2Array( 0, -30, -25, 25, 25, 25 ) diff --git a/project/demos/seek_and_flee/Coward.tscn b/project/demos/seek_and_flee/Agents/Coward.tscn similarity index 100% rename from project/demos/seek_and_flee/Coward.tscn rename to project/demos/seek_and_flee/Agents/Coward.tscn diff --git a/project/demos/seek_and_flee/Seeker.tscn b/project/demos/seek_and_flee/Agents/Seeker.tscn similarity index 100% rename from project/demos/seek_and_flee/Seeker.tscn rename to project/demos/seek_and_flee/Agents/Seeker.tscn diff --git a/project/demos/seek_and_flee/SeekFlee.tscn b/project/demos/seek_and_flee/SeekFlee.tscn index 7b9c9b7..5439a90 100644 --- a/project/demos/seek_and_flee/SeekFlee.tscn +++ b/project/demos/seek_and_flee/SeekFlee.tscn @@ -3,9 +3,9 @@ [ext_resource path="res://demos/seek_and_flee/Player.gd" type="Script" id=1] [ext_resource path="res://demos/seek_and_flee/Spawner.gd" type="Script" id=2] [ext_resource path="res://demos/seek_and_flee/SeekFleeDemo.gd" type="Script" id=3] -[ext_resource path="res://demos/seek_and_flee/Seeker.tscn" type="PackedScene" id=4] +[ext_resource path="res://demos/seek_and_flee/Agents/Seeker.tscn" type="PackedScene" id=4] [ext_resource path="res://demos/seek_and_flee/Boundary.gd" type="Script" id=5] -[ext_resource path="res://demos/seek_and_flee/Coward.tscn" type="PackedScene" id=6] +[ext_resource path="res://demos/seek_and_flee/Agents/Coward.tscn" type="PackedScene" id=6] [sub_resource type="CircleShape2D" id=1] radius = 30.0 diff --git a/project/src/behaviors/GSTMatchOrientation.gd b/project/src/behaviors/GSTMatchOrientation.gd index 182eefe..bf653cf 100644 --- a/project/src/behaviors/GSTMatchOrientation.gd +++ b/project/src/behaviors/GSTMatchOrientation.gd @@ -36,8 +36,8 @@ func _match_orientation(acceleration: GSTTargetAcceleration, desired_orientation var limited_acceleration: = -acceleration.angular if acceleration.angular < 0 else acceleration.angular if limited_acceleration > agent.max_angular_acceleration: acceleration.angular *= agent.max_angular_acceleration / limited_acceleration - - acceleration.linear = Vector3.ZERO + + acceleration.linear = Vector3.ZERO return acceleration