fuzzyworld

godot-shooter / commit

Added a simple trail system for the player.

c0b42019738125730c27249638f0f6c24df0500c · Pavel Safronov · 2023-11-07 14:24:33 -0800 · parents 6ae9f305f9c1

Added a simple trail system for the player.
 Player.gd     |  1 +
 Player.tscn   | 10 +++++++++-
 Trails.gd     | 26 ++++++++++++++++++++++++++
 Trails.tscn   | 23 +++++++++++++++++++++++
 project.godot | 10 ++++++++--
 5 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/Player.gd b/Player.gd
index da5163b..0b3b80c 100644
--- a/Player.gd
+++ b/Player.gd
@@ -23,6 +23,7 @@ func _process(_delta: float) -> void:
 		return
 	$Graphics/Alive/Control/HealthLabel.text = "%s / %s" % [health, max_health]
 	$Graphics/Alive/Control.global_rotation = 0
+	$Graphics/Alive/Trails.add_position(global_position)
 
 func _physics_process(_delta: float) -> void:
 	if dead:
diff --git a/Player.tscn b/Player.tscn
index c06924c..07c50e0 100644
--- a/Player.tscn
+++ b/Player.tscn
@@ -1,10 +1,14 @@
-[gd_scene load_steps=6 format=2]
+[gd_scene load_steps=8 format=2]
 
 [ext_resource path="res://assets/kenney_simple-space/PNG/Default/ship_sidesA.png" type="Texture" id=1]
 [ext_resource path="res://Explosion.tscn" type="PackedScene" id=2]
 [ext_resource path="res://Player.gd" type="Script" id=3]
 [ext_resource path="res://Bullet.tscn" type="PackedScene" id=4]
 [ext_resource path="res://CharacterTheme.tres" type="Theme" id=5]
+[ext_resource path="res://Trails.tscn" type="PackedScene" id=6]
+
+[sub_resource type="Gradient" id=1]
+colors = PoolColorArray( 0, 0, 0, 0, 0, 1, 0, 1 )
 
 [node name="Player" type="KinematicBody2D"]
 rotation = -0.000165944
@@ -32,6 +36,10 @@ text = "HEALTH"
 align = 1
 uppercase = true
 
+[node name="Trails" parent="Graphics/Alive" instance=ExtResource( 6 )]
+modulate = Color( 1, 0, 0, 1 )
+gradient = SubResource( 1 )
+
 [node name="Dead" type="Node2D" parent="Graphics"]
 visible = false
 
diff --git a/Trails.gd b/Trails.gd
new file mode 100644
index 0000000..6097593
--- /dev/null
+++ b/Trails.gd
@@ -0,0 +1,26 @@
+extends Node2D
+class_name Trails
+
+export var trails_length = 400
+export var gradient: Gradient
+onready var trails = $CanvasLayer/Trails
+
+func _ready() -> void:
+	trails.gradient = gradient
+
+func _process(delta: float) -> void:
+	var points = trails.points
+	while points.size() > trails_length:
+		points.remove(0)
+
+func add_position(pos: Vector2) -> void:
+	var points = trails.points
+	var should_add = points.size() == 0
+	if points.size() > 0:
+		var last_added = points[-1]
+		var dist_to_last_added = pos.distance_to(last_added)
+		should_add = dist_to_last_added > 1
+
+	if should_add:
+		points.append(pos)
+		trails.points = points
diff --git a/Trails.tscn b/Trails.tscn
new file mode 100644
index 0000000..128d142
--- /dev/null
+++ b/Trails.tscn
@@ -0,0 +1,23 @@
+[gd_scene load_steps=4 format=2]
+
+[ext_resource path="res://Trails.gd" type="Script" id=1]
+
+[sub_resource type="Curve" id=1]
+_data = [ Vector2( 0, 0.220721 ), 0.0, -0.0409159, 0, 0, Vector2( 0.781651, 0.243243 ), 0.0, 0.0, 0, 0, Vector2( 1, 1 ), 0.0, 0.0, 0, 0 ]
+
+[sub_resource type="Gradient" id=2]
+colors = PoolColorArray( 1, 1, 1, 0, 1, 1, 1, 1 )
+
+[node name="Node2D" type="Node2D"]
+script = ExtResource( 1 )
+
+[node name="CanvasLayer" type="CanvasLayer" parent="."]
+layer = -100
+follow_viewport_enable = true
+
+[node name="Trails" type="Line2D" parent="CanvasLayer"]
+modulate = Color( 1, 1, 1, 0.670588 )
+rotation = -0.000165944
+width_curve = SubResource( 1 )
+default_color = Color( 0.4, 0.501961, 1, 1 )
+gradient = SubResource( 2 )
diff --git a/project.godot b/project.godot
index 2b97731..fab199b 100644
--- a/project.godot
+++ b/project.godot
@@ -23,11 +23,17 @@ _global_script_classes=[ {
 "class": "Player",
 "language": "GDScript",
 "path": "res://Player.gd"
+}, {
+"base": "Node2D",
+"class": "Trails",
+"language": "GDScript",
+"path": "res://Trails.gd"
 } ]
 _global_script_class_icons={
 "Bullet": "",
 "Enemy": "",
-"Player": ""
+"Player": "",
+"Trails": ""
 }
 
 [application]
@@ -40,10 +46,10 @@ config/icon="res://icon.png"
 [display]
 
 window/size/width=500
+window/size/height=800
 window/size/resizable=false
 window/stretch/mode="2d"
 window/stretch/aspect="keep"
-window/size/height=800
 
 [gui]