Invulnerability component
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
[gd_scene load_steps=16 format=3 uid="uid://bqi5s710xb1ju"]
|
[gd_scene load_steps=17 format=3 uid="uid://bqi5s710xb1ju"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://scripts/player.gd" id="1_8j4h4"]
|
[ext_resource type="Script" path="res://scripts/player.gd" id="1_8j4h4"]
|
||||||
[ext_resource type="Texture2D" uid="uid://b7gp0gqvkv8j4" path="res://sprites/MrBrick_base.png" id="2_bc55y"]
|
[ext_resource type="Texture2D" uid="uid://b7gp0gqvkv8j4" path="res://sprites/MrBrick_base.png" id="2_bc55y"]
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
[ext_resource type="PackedScene" uid="uid://bymro4t7angv5" path="res://objects/brick.tscn" id="11_qutq2"]
|
[ext_resource type="PackedScene" uid="uid://bymro4t7angv5" path="res://objects/brick.tscn" id="11_qutq2"]
|
||||||
[ext_resource type="Script" path="res://scripts/components/stomp_damage_component.gd" id="12_payr4"]
|
[ext_resource type="Script" path="res://scripts/components/stomp_damage_component.gd" id="12_payr4"]
|
||||||
[ext_resource type="Script" path="res://scripts/components/flashing_component.gd" id="13_hrtyn"]
|
[ext_resource type="Script" path="res://scripts/components/flashing_component.gd" id="13_hrtyn"]
|
||||||
|
[ext_resource type="Script" path="res://scripts/components/invulnerability_component.gd" id="14_jopig"]
|
||||||
|
|
||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_hdsg1"]
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_hdsg1"]
|
||||||
size = Vector2(16, 31)
|
size = Vector2(16, 31)
|
||||||
@@ -110,5 +111,9 @@ sprite = NodePath("../Root/Right Eye")
|
|||||||
flash_duration = 1.0
|
flash_duration = 1.0
|
||||||
health_component = NodePath("../HealthComponent")
|
health_component = NodePath("../HealthComponent")
|
||||||
|
|
||||||
|
[node name="InvulnerabilityComponent" type="Node" parent="." node_paths=PackedStringArray("flashing_component")]
|
||||||
|
script = ExtResource("14_jopig")
|
||||||
|
flashing_component = NodePath("../FlashingComponent Base")
|
||||||
|
|
||||||
[connection signal="on_death" from="HealthComponent" to="PlayerDeathComponent" method="_on_health_component_on_death"]
|
[connection signal="on_death" from="HealthComponent" to="PlayerDeathComponent" method="_on_health_component_on_death"]
|
||||||
[connection signal="on_health_change" from="HealthComponent" to="KnockbackComponent" method="_on_health_component_on_health_change"]
|
[connection signal="on_health_change" from="HealthComponent" to="KnockbackComponent" method="_on_health_component_on_health_change"]
|
||||||
|
@@ -4,6 +4,7 @@ extends Node
|
|||||||
@export var damage: float = 0.25
|
@export var damage: float = 0.25
|
||||||
@export var area2d: Area2D
|
@export var area2d: Area2D
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
if not area2d:
|
if not area2d:
|
||||||
printerr("No area2d assigned!")
|
printerr("No area2d assigned!")
|
||||||
@@ -11,10 +12,21 @@ func _ready() -> void:
|
|||||||
|
|
||||||
area2d.body_entered.connect(on_area2d_body_entered)
|
area2d.body_entered.connect(on_area2d_body_entered)
|
||||||
|
|
||||||
func deal_damage(target: HealthComponent):
|
|
||||||
target.decrease_health(damage)
|
|
||||||
|
|
||||||
func on_area2d_body_entered(body: Node2D):
|
func deal_damage(target: HealthComponent) -> void:
|
||||||
|
target.decrease_health(damage)
|
||||||
|
print("Dealt damage to target!")
|
||||||
|
|
||||||
|
|
||||||
|
func on_area2d_body_entered(body: Node2D) -> void:
|
||||||
if body.has_node("HealthComponent"):
|
if body.has_node("HealthComponent"):
|
||||||
var health_component: HealthComponent = body.get_node("HealthComponent")
|
var health_component: HealthComponent = body.get_node("HealthComponent")
|
||||||
|
var invulnerability_component: InvulnerabilityComponent = body.get_node_or_null("InvulnerabilityComponent")
|
||||||
|
|
||||||
|
if invulnerability_component and invulnerability_component.is_invulnerable():
|
||||||
|
return
|
||||||
|
|
||||||
deal_damage(health_component)
|
deal_damage(health_component)
|
||||||
|
|
||||||
|
if invulnerability_component:
|
||||||
|
invulnerability_component.activate()
|
||||||
|
30
scripts/components/invulnerability_component.gd
Normal file
30
scripts/components/invulnerability_component.gd
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
class_name InvulnerabilityComponent
|
||||||
|
extends Node
|
||||||
|
|
||||||
|
@export var duration: float = 1.0
|
||||||
|
@export var flashing_component: FlashingComponent
|
||||||
|
|
||||||
|
var invulnerable: bool = false
|
||||||
|
|
||||||
|
|
||||||
|
func activate() -> void:
|
||||||
|
if invulnerable:
|
||||||
|
return
|
||||||
|
|
||||||
|
invulnerable = true
|
||||||
|
if flashing_component:
|
||||||
|
flashing_component.start_flashing()
|
||||||
|
|
||||||
|
var timer = get_tree().create_timer(duration)
|
||||||
|
timer.timeout.connect(deactivate)
|
||||||
|
|
||||||
|
|
||||||
|
func deactivate() -> void:
|
||||||
|
invulnerable = false
|
||||||
|
if flashing_component:
|
||||||
|
flashing_component.stop_flashing()
|
||||||
|
|
||||||
|
|
||||||
|
func is_invulnerable() -> bool:
|
||||||
|
return invulnerable
|
||||||
|
|
Reference in New Issue
Block a user