fuzzyworld

godot-shooter / commit

Added trails to enemies.

04130db8f48e4d32c3fbd93def0881483d010522 · Pavel Safronov · 2023-11-07 15:20:18 -0800 · parents 4c8c9e02b00e

Added trails to enemies.
For trails to work with enemies, it was important to get the fading to work. Since we're using a CanvasLayer to place the trails, alpha needs to be passed into the Trails object.
 Enemy.gd    |  1 +
 Enemy.tscn  | 28 ++++++++++++++++++++++++----
 Trails.gd   | 13 +++++++------
 Trails.tscn |  3 +++
 World.gd    |  5 ++---
 5 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/Enemy.gd b/Enemy.gd
index a16e4a2..0ab9ecc 100644
--- a/Enemy.gd
+++ b/Enemy.gd
@@ -33,6 +33,7 @@ func _process(_delta: float) -> void:
 	$Graphics/Alive/Container.global_rotation = 0
 	$Graphics/Alive/Target.visible = is_targeted	
 	$Graphics/Alive/Target.global_rotation = 0
+	$Graphics/Alive/Trails.add_position(global_position)
 
 func _physics_process(_delta: float) -> void:
 	if dead:
diff --git a/Enemy.tscn b/Enemy.tscn
index 5953df8..d9770a2 100644
--- a/Enemy.tscn
+++ b/Enemy.tscn
@@ -1,9 +1,13 @@
-[gd_scene load_steps=8 format=2]
+[gd_scene load_steps=10 format=2]
 
 [ext_resource path="res://assets/kenney_simple-space/PNG/Default/enemy_A.png" type="Texture" id=1]
 [ext_resource path="res://Enemy.gd" type="Script" id=2]
 [ext_resource path="res://assets/kenney_crosshair-pack/PNG/White Retina/crosshair041.png" type="Texture" id=3]
 [ext_resource path="res://CharacterTheme.tres" type="Theme" id=4]
+[ext_resource path="res://Trails.tscn" type="PackedScene" id=5]
+
+[sub_resource type="Gradient" id=4]
+colors = PoolColorArray( 0, 0, 0, 0, 1, 0, 0, 1 )
 
 [sub_resource type="Animation" id=1]
 length = 3.0
@@ -38,6 +42,7 @@ tracks/1/keys = {
 
 [sub_resource type="Animation" id=2]
 resource_name = "fade_out"
+length = 3.0
 tracks/0/type = "value"
 tracks/0/path = NodePath(".:modulate")
 tracks/0/interp = 1
@@ -45,7 +50,7 @@ tracks/0/loop_wrap = true
 tracks/0/imported = false
 tracks/0/enabled = true
 tracks/0/keys = {
-"times": PoolRealArray( 0, 1 ),
+"times": PoolRealArray( 0, 3 ),
 "transitions": PoolRealArray( 1, 1 ),
 "update": 0,
 "values": [ Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 0 ) ]
@@ -57,17 +62,29 @@ tracks/1/loop_wrap = true
 tracks/1/imported = false
 tracks/1/enabled = true
 tracks/1/keys = {
-"times": PoolRealArray( 1 ),
+"times": PoolRealArray( 3 ),
 "transitions": PoolRealArray( 1 ),
 "values": [ {
 "args": [  ],
 "method": "remove_thyself"
 } ]
 }
+tracks/2/type = "value"
+tracks/2/path = NodePath("Graphics/Alive/Trails:alpha")
+tracks/2/interp = 1
+tracks/2/loop_wrap = true
+tracks/2/imported = false
+tracks/2/enabled = true
+tracks/2/keys = {
+"times": PoolRealArray( 0, 3 ),
+"transitions": PoolRealArray( 1, 1 ),
+"update": 0,
+"values": [ 1.0, 0.0 ]
+}
 
 [node name="Enemy" type="KinematicBody2D"]
+modulate = Color( 1, 1, 1, 0 )
 script = ExtResource( 2 )
-max_health = 5
 
 [node name="Graphics" type="Node2D" parent="."]
 
@@ -89,6 +106,9 @@ text = "HEALTH"
 modulate = Color( 1, 0, 0, 1 )
 texture = ExtResource( 3 )
 
+[node name="Trails" parent="Graphics/Alive" instance=ExtResource( 5 )]
+gradient = SubResource( 4 )
+
 [node name="AnimationPlayer" type="AnimationPlayer" parent="."]
 anims/RESET = SubResource( 1 )
 anims/fade_in = SubResource( 3 )
diff --git a/Trails.gd b/Trails.gd
index 6097593..eecf198 100644
--- a/Trails.gd
+++ b/Trails.gd
@@ -3,15 +3,15 @@ class_name Trails
 
 export var trails_length = 400
 export var gradient: Gradient
+export var alpha = 1.0
 onready var trails = $CanvasLayer/Trails
 
 func _ready() -> void:
 	trails.gradient = gradient
+	trails.points = PoolVector2Array([])
 
-func _process(delta: float) -> void:
-	var points = trails.points
-	while points.size() > trails_length:
-		points.remove(0)
+func _process(_delta: float) -> void:
+	trails.modulate = Color(1,1,1,alpha)
 
 func add_position(pos: Vector2) -> void:
 	var points = trails.points
@@ -20,7 +20,8 @@ func add_position(pos: Vector2) -> void:
 		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
+	while points.size() > trails_length:
+		points.remove(0)
+	trails.points = points
diff --git a/Trails.tscn b/Trails.tscn
index 9439c91..1618389 100644
--- a/Trails.tscn
+++ b/Trails.tscn
@@ -15,5 +15,8 @@ follow_viewport_enable = true
 [node name="Trails" type="Line2D" parent="CanvasLayer"]
 modulate = Color( 1, 1, 1, 0.670588 )
 rotation = -0.000165944
+points = PoolVector2Array( 13.8699, 784.002, 261.935, 390.043, 480.997, 20.0798 )
 width_curve = SubResource( 1 )
 default_color = Color( 0.4, 0.501961, 1, 1 )
+joint_mode = 2
+end_cap_mode = 2
diff --git a/World.gd b/World.gd
index a4b7efc..ca490dc 100644
--- a/World.gd
+++ b/World.gd
@@ -72,7 +72,7 @@ func _input(event: InputEvent) -> void:
 func _process(_delta: float) -> void:
 	if Input.is_action_just_pressed("pause"):
 		get_tree().paused = true
-		$CanvasLayer/PauseMenu.show()
+		$UI/PauseMenu.show()
 
 	if is_destination_set:
 		$Player.turn_towards(destination)
@@ -90,7 +90,6 @@ func _process(_delta: float) -> void:
 	# as they are removed, they should replace themselves
 	var screen_radius = get_viewport().get_visible_rect().size.length() / 2 + 10
 	var env_items = $Environment.get_children()
-	Utils.log("env items", env_items.size())
 	for i in env_items:
 		var item: Node2D = i
 		var diff = $Player.global_position - item.global_position
@@ -141,4 +140,4 @@ func _on_ShootTimer_timeout() -> void:
 
 func _on_Button_pressed() -> void:
 	get_tree().paused = false
-	$CanvasLayer/PauseMenu.hide()
+	$UI/PauseMenu.hide()