fuzzyworld

godot-shooter / commit

Added a player shield that prevents damage and takes a set time to recharge.

c37b0e755c51fa1e5b006a24c9cd0c33357e9c6e · Pavel Safronov · 2023-11-08 00:21:47 -0800 · parents f54990a124b6

Added a player shield that prevents damage and takes a set time to recharge.
 HUD.tscn    |  6 ++---
 Player.gd   | 18 +++++++++++++
 Player.tscn | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 World.tscn  |  1 -
 4 files changed, 104 insertions(+), 6 deletions(-)

diff --git a/HUD.tscn b/HUD.tscn
index 419374c..83be37e 100644
--- a/HUD.tscn
+++ b/HUD.tscn
@@ -46,7 +46,7 @@ margin_right = 40.0
 margin_bottom = 40.0
 
 [node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
-margin_right = 278.0
+margin_right = 282.0
 margin_bottom = 74.0
 
 [node name="ScoreText" type="Label" parent="VBoxContainer/HBoxContainer"]
@@ -55,8 +55,8 @@ margin_bottom = 74.0
 text = "Score: "
 
 [node name="Score" type="Label" parent="VBoxContainer/HBoxContainer"]
-margin_left = 196.0
-margin_right = 278.0
+margin_left = 200.0
+margin_right = 282.0
 margin_bottom = 74.0
 text = "100"
 uppercase = true
diff --git a/Player.gd b/Player.gd
index 6049a56..b32788d 100644
--- a/Player.gd
+++ b/Player.gd
@@ -9,6 +9,8 @@ export var bullet: PackedScene
 export var is_invincible = false
 export var turn_rate = 0.03
 export var shooting_range = 500
+export var shield_active = true
+export var shield_restart_period_seconds = 5
 
 var PopupControl = preload("res://Popup.tscn")
 
@@ -24,6 +26,8 @@ func _process(_delta: float) -> void:
 	$Graphics/Alive/Control/HealthLabel.text = "%s / %s" % [health, max_health]
 	$Graphics/Alive/Control.global_rotation = 0
 	$Graphics/Alive/Trails.add_position(global_position)
+	$ShieldCollisionShape.disabled = !shield_active
+	$ShipCollisionShape.disabled = shield_active
 
 func _physics_process(_delta: float) -> void:
 	if dead:
@@ -56,9 +60,23 @@ func speed_up(delta: int = 10):
 		return
 	move_speed += delta
 
+func enable_shield() -> void:
+	shield_active = true
+	$Graphics/Alive/Shield/Sprite/AnimationPlayer.play("fade in")
+
 func hurt(damage_amount: int = 1):
 	if dead or is_invincible:
 		return
+	if shield_active:
+		shield_active = false
+		$Graphics/Alive/Shield/Sprite/AnimationPlayer.play("fade out")
+		var timer = Timer.new()
+		add_child(timer)
+		timer.wait_time = shield_restart_period_seconds
+		timer.one_shot = true
+		timer.connect("timeout", self, "enable_shield")
+		timer.start()
+		return
 	health -= damage_amount
 	if health <= 0:
 		kill()
diff --git a/Player.tscn b/Player.tscn
index 415a8ae..62aa12d 100644
--- a/Player.tscn
+++ b/Player.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=9 format=2]
+[gd_scene load_steps=15 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]
@@ -6,6 +6,10 @@
 [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]
+[ext_resource path="res://assets/kenney_crosshair-pack/PNG/White Retina/crosshair120.png" type="Texture" id=7]
+
+[sub_resource type="CircleShape2D" id=7]
+radius = 61.0
 
 [sub_resource type="Gradient" id=2]
 colors = PoolColorArray( 1, 0, 0, 1, 1, 0, 0, 0 )
@@ -13,14 +17,78 @@ colors = PoolColorArray( 1, 0, 0, 1, 1, 0, 0, 0 )
 [sub_resource type="Gradient" id=1]
 colors = PoolColorArray( 0, 0, 0, 0, 0, 1, 0, 1 )
 
+[sub_resource type="Animation" id=3]
+length = 0.001
+tracks/0/type = "value"
+tracks/0/path = NodePath(".:modulate")
+tracks/0/interp = 1
+tracks/0/loop_wrap = true
+tracks/0/imported = false
+tracks/0/enabled = true
+tracks/0/keys = {
+"times": PoolRealArray( 0 ),
+"transitions": PoolRealArray( 1 ),
+"update": 0,
+"values": [ Color( 0, 0, 1, 1 ) ]
+}
+
+[sub_resource type="Animation" id=4]
+resource_name = "blinking"
+loop = true
+tracks/0/type = "value"
+tracks/0/path = NodePath(".:modulate")
+tracks/0/interp = 1
+tracks/0/loop_wrap = true
+tracks/0/imported = false
+tracks/0/enabled = true
+tracks/0/keys = {
+"times": PoolRealArray( 0, 0.7, 1 ),
+"transitions": PoolRealArray( 1, 1, 1 ),
+"update": 0,
+"values": [ Color( 0, 0, 1, 1 ), Color( 0, 0, 1, 0.509804 ), Color( 0, 0, 1, 1 ) ]
+}
+
+[sub_resource type="Animation" id=5]
+resource_name = "fade in"
+tracks/0/type = "value"
+tracks/0/path = NodePath(".:modulate")
+tracks/0/interp = 1
+tracks/0/loop_wrap = true
+tracks/0/imported = false
+tracks/0/enabled = true
+tracks/0/keys = {
+"times": PoolRealArray( 0, 1 ),
+"transitions": PoolRealArray( 1, 1 ),
+"update": 0,
+"values": [ Color( 0, 0, 1, 0 ), Color( 0, 0, 1, 1 ) ]
+}
+
+[sub_resource type="Animation" id=6]
+resource_name = "fade out"
+tracks/0/type = "value"
+tracks/0/path = NodePath(".:modulate")
+tracks/0/interp = 1
+tracks/0/loop_wrap = true
+tracks/0/imported = false
+tracks/0/enabled = true
+tracks/0/keys = {
+"times": PoolRealArray( 0, 1 ),
+"transitions": PoolRealArray( 1, 1 ),
+"update": 0,
+"values": [ Color( 0, 0, 1, 1 ), Color( 0, 0, 1, 0 ) ]
+}
+
 [node name="Player" type="KinematicBody2D" groups=["player"]]
 rotation = -0.000165944
 script = ExtResource( 3 )
 bullet = ExtResource( 4 )
 
-[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
+[node name="ShipCollisionShape" type="CollisionPolygon2D" parent="."]
 polygon = PoolVector2Array( -0.996349, -22.0002, -8.00133, 7.99867, -7.00166, 9.99884, -11.002, 11.9982, -14.0007, 3.99768, -20.002, 11.9967, -17.0038, 22.9972, -9.00465, 27.9985, -8.00448, 26.9987, -10.0027, 15.9983, -6.00232, 13.999, -0.00398266, 24, 4.99784, 13.0008, 9.99734, 16.0017, 6.99552, 27.0012, 7.99519, 29.0013, 10.9952, 29.0018, 17.996, 24.003, 20.998, 12.0035, 15.9993, 4.00266, 12.9993, 4.00216, 10.9982, 11.0018, 6.99834, 10.0012, 0.00381671, -23 )
 
+[node name="ShieldCollisionShape" type="CollisionShape2D" parent="."]
+shape = SubResource( 7 )
+
 [node name="Graphics" type="Node2D" parent="."]
 
 [node name="Alive" type="Node2D" parent="Graphics"]
@@ -51,6 +119,19 @@ uppercase = true
 modulate = Color( 1, 0, 0, 1 )
 gradient = SubResource( 1 )
 
+[node name="Shield" type="Node2D" parent="Graphics/Alive"]
+
+[node name="Sprite" type="Sprite" parent="Graphics/Alive/Shield"]
+modulate = Color( 0, 0, 1, 1 )
+texture = ExtResource( 7 )
+
+[node name="AnimationPlayer" type="AnimationPlayer" parent="Graphics/Alive/Shield/Sprite"]
+autoplay = "blinking"
+anims/RESET = SubResource( 3 )
+anims/blinking = SubResource( 4 )
+"anims/fade in" = SubResource( 5 )
+"anims/fade out" = SubResource( 6 )
+
 [node name="Dead" type="Node2D" parent="Graphics"]
 visible = false
 
diff --git a/World.tscn b/World.tscn
index 698b737..e15ce44 100644
--- a/World.tscn
+++ b/World.tscn
@@ -46,7 +46,6 @@ __meta__ = {
 visible = false
 
 [node name="Player" parent="." instance=ExtResource( 2 )]
-position = Vector2( 248, 410 )
 
 [node name="Environment" type="Node2D" parent="."]