Implement demo for Face behavior

This commit is contained in:
Francois Belair 2019-12-20 13:17:27 -05:00
parent f531e835fd
commit 546d12b15b
9 changed files with 119 additions and 4 deletions

View File

@ -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 )

View File

@ -0,0 +1,4 @@
extends Node2D
onready var player: = $Player

View File

@ -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)

View File

@ -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()

View File

@ -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 )

View File

@ -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

View File

@ -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