From 87aef4825128b611a29d7d7ace10f89263ec5aee Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Sun, 22 Dec 2019 14:08:53 -0500 Subject: [PATCH] Add GUI to Face demo --- project/demos/face/Face.tscn | 5 +- project/demos/face/FaceDemo.gd | 30 +++++++ project/demos/face/GUI.gd | 47 ++++++++++ project/demos/face/GUI.tscn | 153 +++++++++++++++++++++++++++++++++ project/demos/face/Turret.gd | 15 ++-- 5 files changed, 242 insertions(+), 8 deletions(-) create mode 100644 project/demos/face/GUI.gd create mode 100644 project/demos/face/GUI.tscn diff --git a/project/demos/face/Face.tscn b/project/demos/face/Face.tscn index 4099538..16f3a08 100644 --- a/project/demos/face/Face.tscn +++ b/project/demos/face/Face.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=7 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] +[ext_resource path="res://demos/face/GUI.tscn" type="PackedScene" id=4] [sub_resource type="CircleShape2D" id=1] radius = 20.0 @@ -16,6 +17,8 @@ __meta__ = { "_editor_description_": "A demo showing the usage of the Face steering behavior." } +[node name="GUI" parent="." instance=ExtResource( 4 )] + [node name="Player" type="KinematicBody2D" parent="."] position = Vector2( 512, 450 ) script = ExtResource( 1 ) diff --git a/project/demos/face/FaceDemo.gd b/project/demos/face/FaceDemo.gd index 28ac945..c85ff14 100644 --- a/project/demos/face/FaceDemo.gd +++ b/project/demos/face/FaceDemo.gd @@ -2,3 +2,33 @@ extends Node2D onready var player: = $Player +onready var _gui: = $GUI +onready var _turret: = $Turret + + +func _ready() -> void: + _gui.connect("align_tolerance_changed", self, "_on_GUI_align_tolerance_changed") + _gui.connect("decel_radius_changed", self, "_on_GUI_decel_radius_changed") + _gui.connect("max_accel_changed", self, "_on_GUI_max_accel_changed") + _gui.connect("max_speed_changed", self, "_on_GUI_max_speed_changed") + _turret.setup() + _gui.align_tolerance.text = str(int(rad2deg(_turret._face.alignment_tolerance))) + _gui.decel_radius.text = str(int(rad2deg(_turret._face.deceleration_radius))) + _gui.max_speed.text = str(int(rad2deg(_turret._agent.max_angular_speed))) + _gui.max_accel.text = str(int(rad2deg(_turret._agent.max_angular_acceleration))) + + +func _on_GUI_align_tolerance_changed(value: int) -> void: + _turret._face.alignment_tolerance = deg2rad(value) + + +func _on_GUI_decel_radius_changed(value: int) -> void: + _turret._face.deceleration_radius = deg2rad(value) + + +func _on_GUI_max_accel_changed(value: int) -> void: + _turret._agent.max_angular_acceleration = deg2rad(value) + + +func _on_GUI_max_speed_changed(value: int) -> void: + _turret._agent.max_angular_speed = deg2rad(value) diff --git a/project/demos/face/GUI.gd b/project/demos/face/GUI.gd new file mode 100644 index 0000000..4284c41 --- /dev/null +++ b/project/demos/face/GUI.gd @@ -0,0 +1,47 @@ +extends MarginContainer + + +signal max_speed_changed(value) +signal max_accel_changed(value) +signal align_tolerance_changed(value) +signal decel_radius_changed(value) + +onready var max_speed: = $Controls/MaxSpeed/LineEdit +onready var max_accel: = $Controls/MaxAccel/LineEdit +onready var align_tolerance: = $Controls/AlignmentTolerance/LineEdit +onready var decel_radius: = $Controls/DecelerationRadius/LineEdit + + +func _ready() -> void: + max_speed.connect("text_changed", self, "_on_MaxSpeed_text_changed") + max_accel.connect("text_changed", self, "_on_MaxAccel_text_changed") + align_tolerance.connect("text_changed", self, "_on_AlignTolerance_text_changed") + decel_radius.connect("text_changed", self, "_on_DecelRadius_text_changed") + + +func _unhandled_input(event: InputEvent) -> void: + if event is InputEventMouseButton: + max_speed.release_focus() + max_accel.release_focus() + align_tolerance.release_focus() + decel_radius.release_focus() + + +func _on_MaxSpeed_text_changed(new_text: String) -> void: + if new_text.is_valid_integer(): + emit_signal("max_speed_changed", int(float(new_text))) + + +func _on_MaxAccel_text_changed(new_text: String) -> void: + if new_text.is_valid_integer(): + emit_signal("max_accel_changed", int(float(new_text))) + + +func _on_AlignTolerance_text_changed(new_text: String) -> void: + if new_text.is_valid_integer(): + emit_signal("align_tolerance_changed", int(float(new_text))) + + +func _on_DecelRadius_text_changed(new_text: String) -> void: + if new_text.is_valid_integer(): + emit_signal("decel_radius_changed", int(float(new_text))) diff --git a/project/demos/face/GUI.tscn b/project/demos/face/GUI.tscn new file mode 100644 index 0000000..09eef08 --- /dev/null +++ b/project/demos/face/GUI.tscn @@ -0,0 +1,153 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://demos/face/GUI.gd" type="Script" id=1] + +[node name="GUI" type="MarginContainer"] +anchor_bottom = 1.0 +margin_right = 40.0 +custom_constants/margin_right = 20 +custom_constants/margin_top = 20 +custom_constants/margin_left = 20 +custom_constants/margin_bottom = 20 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Controls" type="VBoxContainer" parent="."] +margin_left = 20.0 +margin_top = 20.0 +margin_right = 212.0 +margin_bottom = 580.0 + +[node name="MaxSpeed" type="HBoxContainer" parent="Controls"] +margin_right = 192.0 +margin_bottom = 24.0 + +[node name="Label" type="Label" parent="Controls/MaxSpeed"] +margin_top = 4.0 +margin_right = 130.0 +margin_bottom = 19.0 +rect_min_size = Vector2( 130, 15 ) +text = "Max speed" + +[node name="LineEdit" type="LineEdit" parent="Controls/MaxSpeed"] +margin_left = 134.0 +margin_right = 192.0 +margin_bottom = 24.0 +focus_mode = 1 + +[node name="MaxAccel" type="HBoxContainer" parent="Controls"] +margin_top = 28.0 +margin_right = 192.0 +margin_bottom = 52.0 + +[node name="Label" type="Label" parent="Controls/MaxAccel"] +margin_top = 4.0 +margin_right = 130.0 +margin_bottom = 19.0 +rect_min_size = Vector2( 130, 15 ) +text = "Max acceleration" + +[node name="LineEdit" type="LineEdit" parent="Controls/MaxAccel"] +margin_left = 134.0 +margin_right = 192.0 +margin_bottom = 24.0 +focus_mode = 1 + +[node name="AlignmentTolerance" type="HBoxContainer" parent="Controls"] +margin_top = 56.0 +margin_right = 192.0 +margin_bottom = 80.0 + +[node name="Label" type="Label" parent="Controls/AlignmentTolerance"] +margin_top = 4.0 +margin_right = 130.0 +margin_bottom = 19.0 +rect_min_size = Vector2( 130, 15 ) +text = "Alignment tolerance" + +[node name="LineEdit" type="LineEdit" parent="Controls/AlignmentTolerance"] +margin_left = 134.0 +margin_right = 192.0 +margin_bottom = 24.0 +focus_mode = 1 + +[node name="DecelerationRadius" type="HBoxContainer" parent="Controls"] +margin_top = 84.0 +margin_right = 192.0 +margin_bottom = 108.0 + +[node name="Label" type="Label" parent="Controls/DecelerationRadius"] +margin_top = 4.0 +margin_right = 130.0 +margin_bottom = 19.0 +rect_min_size = Vector2( 130, 15 ) +text = "Deceleration radius" + +[node name="LineEdit" type="LineEdit" parent="Controls/DecelerationRadius"] +margin_left = 134.0 +margin_right = 192.0 +margin_bottom = 24.0 +focus_mode = 1 + +[node name="Help" type="VBoxContainer" parent="Controls"] +margin_top = 112.0 +margin_right = 192.0 +margin_bottom = 164.0 + +[node name="Controls" type="Label" parent="Controls/Help"] +margin_right = 192.0 +margin_bottom = 14.0 +text = "Controls" + +[node name="GridContainer" type="GridContainer" parent="Controls/Help"] +margin_top = 18.0 +margin_right = 192.0 +margin_bottom = 52.0 +columns = 3 + +[node name="Sep" type="Control" parent="Controls/Help/GridContainer"] +margin_right = 15.0 +margin_bottom = 15.0 +rect_min_size = Vector2( 15, 15 ) + +[node name="W" type="Label" parent="Controls/Help/GridContainer"] +margin_left = 19.0 +margin_right = 34.0 +margin_bottom = 15.0 +rect_min_size = Vector2( 15, 15 ) +text = "W" +align = 1 + +[node name="Sep2" type="Control" parent="Controls/Help/GridContainer"] +margin_left = 38.0 +margin_right = 53.0 +margin_bottom = 15.0 +rect_min_size = Vector2( 15, 15 ) + +[node name="A" type="Label" parent="Controls/Help/GridContainer"] +margin_top = 19.0 +margin_right = 15.0 +margin_bottom = 34.0 +rect_min_size = Vector2( 15, 15 ) +text = "A" +align = 1 + +[node name="S" type="Label" parent="Controls/Help/GridContainer"] +margin_left = 19.0 +margin_top = 19.0 +margin_right = 34.0 +margin_bottom = 34.0 +rect_min_size = Vector2( 15, 15 ) +text = "S" +align = 1 + +[node name="D" type="Label" parent="Controls/Help/GridContainer"] +margin_left = 38.0 +margin_top = 19.0 +margin_right = 53.0 +margin_bottom = 34.0 +rect_min_size = Vector2( 15, 15 ) +text = "D" +align = 1 diff --git a/project/demos/face/Turret.gd b/project/demos/face/Turret.gd index 9306eae..2573e6d 100644 --- a/project/demos/face/Turret.gd +++ b/project/demos/face/Turret.gd @@ -16,9 +16,10 @@ func _draw() -> void: draw_circle(Vector2.ZERO, _radius, Color.teal) + func _physics_process(delta: float) -> void: if not _face: - _setup() + return _accel = _face.calculate_steering(_accel) _angular_velocity += _accel.angular @@ -33,12 +34,7 @@ func _physics_process(delta: float) -> void: _update_agent() -func _update_agent() -> void: - _agent.angular_velocity = _angular_velocity - _agent.orientation = rotation - - -func _setup() -> void: +func setup() -> void: _face = GSTFace.new(_agent, owner.player.agent) _face.alignment_tolerance = 0.1 @@ -49,3 +45,8 @@ func _setup() -> void: _agent.position = Vector3(global_position.x, global_position.y, 0) _update_agent() + + +func _update_agent() -> void: + _agent.angular_velocity = _angular_velocity + _agent.orientation = rotation