From 7241bc754b99a928eb6626557fcdeed8513a4708 Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Sat, 8 Feb 2020 18:17:39 -0500 Subject: [PATCH] Separate ArriveDemo's drawing from root node Closes #26 --- project/demos/Arrive/ArriveDemo.gd | 16 +++------------- project/demos/Arrive/ArriveDemo.tscn | 10 ++++++++-- project/demos/Arrive/TargetDrawer.gd | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 project/demos/Arrive/TargetDrawer.gd diff --git a/project/demos/Arrive/ArriveDemo.gd b/project/demos/Arrive/ArriveDemo.gd index 6a8e86e..f88f728 100644 --- a/project/demos/Arrive/ArriveDemo.gd +++ b/project/demos/Arrive/ArriveDemo.gd @@ -1,4 +1,4 @@ -extends Node2D +extends Node export(float, 0, 2000, 40) var linear_speed_max := 800.0 setget set_linear_speed_max @@ -6,12 +6,8 @@ export(float, 0, 200, 2.0) var linear_acceleration_max := 80.0 setget set_linear export(float, 0, 100, 0.1) var arrival_tolerance := 25.0 setget set_arrival_tolerance export(float, 0, 500, 10) var deceleration_radius := 125.0 setget set_deceleration_radius -const COLORS := { - deceleration_radius = Color(1.0, 0.419, 0.592, 0.5), - arrival_tolerance = Color(0.278, 0.231, 0.47, 0.3) -} - onready var arriver := $Arriver +onready var target_drawer := $TargetDrawer func _ready() -> void: @@ -26,13 +22,7 @@ func _ready() -> void: func _unhandled_input(event: InputEvent) -> void: if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.is_pressed(): arriver.target.position = Vector3(event.position.x, event.position.y, 0) - update() - - -func _draw(): - var target_position := Vector2(arriver.target.position.x, arriver.target.position.y) - draw_circle(target_position, deceleration_radius, COLORS.deceleration_radius) - draw_circle(target_position, arrival_tolerance, COLORS.arrival_tolerance) + target_drawer.update() func set_arrival_tolerance(value: float) -> void: diff --git a/project/demos/Arrive/ArriveDemo.tscn b/project/demos/Arrive/ArriveDemo.tscn index f54d1c4..513d82f 100644 --- a/project/demos/Arrive/ArriveDemo.tscn +++ b/project/demos/Arrive/ArriveDemo.tscn @@ -1,15 +1,16 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://demos/Arrive/Arriver.gd" type="Script" id=1] [ext_resource path="res://demos/Utils/DemoInterface.tscn" type="PackedScene" id=2] [ext_resource path="res://demos/Arrive/ArriveDemo.gd" type="Script" id=3] [ext_resource path="res://demos/Utils/BackgroudLayer.tscn" type="PackedScene" id=4] +[ext_resource path="res://demos/Arrive/TargetDrawer.gd" type="Script" id=5] [ext_resource path="res://demos/Utils/CircleDraw.gd" type="Script" id=6] [sub_resource type="CircleShape2D" id=1] radius = 16.0 -[node name="ArriveDemo" type="Node2D"] +[node name="ArriveDemo" type="Node"] script = ExtResource( 3 ) [node name="BackgroudLayer" parent="." instance=ExtResource( 4 )] @@ -26,4 +27,9 @@ inner_color = Color( 0.235294, 0.639216, 0.439216, 1 ) outer_color = Color( 0.560784, 0.870588, 0.364706, 1 ) stroke = 4.0 +[node name="TargetDrawer" type="Node2D" parent="."] +script = ExtResource( 5 ) + [node name="DemoInterface" parent="." instance=ExtResource( 2 )] +text_bbcode = "Arrive Demo +Mouse click to make the [color=lime]green \"Player\"[/color] move to the [color=fuchsia]purple target[/color]" diff --git a/project/demos/Arrive/TargetDrawer.gd b/project/demos/Arrive/TargetDrawer.gd new file mode 100644 index 0000000..48322d0 --- /dev/null +++ b/project/demos/Arrive/TargetDrawer.gd @@ -0,0 +1,20 @@ +extends Node2D + + +const COLORS := { + deceleration_radius = Color(1.0, 0.419, 0.592, 0.5), + arrival_tolerance = Color(0.278, 0.231, 0.47, 0.3) +} + +var arriver: Node2D + + +func _ready() -> void: + yield(owner, "ready") + arriver = owner.arriver + + +func _draw(): + var target_position := GSTUtils.to_vector2(arriver.target.position) + draw_circle(target_position, owner.deceleration_radius, COLORS.deceleration_radius) + draw_circle(target_position, owner.arrival_tolerance, COLORS.arrival_tolerance)