Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial version for damage points VFX #125

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[gd_resource type="Resource" script_class="InstanceResource" load_steps=3 format=3 uid="uid://xbek2osd7l4w"]

[ext_resource type="Resource" uid="uid://d16p36img8x6j" path="res://addons/top_down/resources/RoomResources/front_reference.tres" id="1_adrs0"]
[ext_resource type="Script" path="res://addons/great_games_library/resources/InstanceResource/InstanceResource.gd" id="1_y73ce"]

[resource]
script = ExtResource("1_y73ce")
scene_path = "res://addons/top_down/scenes/vfx/damage_points.tscn"
parent_reference_resource = ExtResource("1_adrs0")
9 changes: 8 additions & 1 deletion addons/top_down/scenes/actors/actor.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=44 format=3 uid="uid://botai66n8rwt3"]
[gd_scene load_steps=46 format=3 uid="uid://botai66n8rwt3"]

[ext_resource type="Texture2D" uid="uid://b3gj1j73p318" path="res://addons/top_down/assets/images/shadow.png" id="1_1xdx7"]
[ext_resource type="Script" path="res://addons/great_games_library/nodes/ResourceNode/ResourceNode.gd" id="1_atd7g"]
Expand All @@ -23,7 +23,9 @@
[ext_resource type="Script" path="res://addons/great_games_library/resources/ValueResource/StringArrayResource.gd" id="15_loiow"]
[ext_resource type="Script" path="res://addons/top_down/scripts/damage/HoleTrigger.gd" id="16_f6cga"]
[ext_resource type="Script" path="res://addons/top_down/scripts/damage/HoleDeath.gd" id="18_634cs"]
[ext_resource type="Resource" uid="uid://xbek2osd7l4w" path="res://addons/top_down/resources/InstanceResources/vfx/damage_points_instance_resource.tres" id="22_nljy0"]
[ext_resource type="Resource" uid="uid://d1ck1axrd4kd4" path="res://addons/top_down/resources/RoomResources/ysort_reference.tres" id="22_yjx3l"]
[ext_resource type="Script" path="res://addons/top_down/scripts/actor/DamageDisplay.gd" id="26_oudld"]

[sub_resource type="Resource" id="Resource_u7p0r"]
resource_name = "movement properties"
Expand Down Expand Up @@ -312,3 +314,8 @@ root_node = NodePath("..")
drop_parent_reference = ExtResource("22_yjx3l")
resource_node = NodePath("../ResourceNode")
asset_list = SubResource("Resource_mslt1")

[node name="DamageDisplay" type="Node" parent="." node_paths=PackedStringArray("resource_node")]
script = ExtResource("26_oudld")
resource_node = NodePath("../ResourceNode")
damage_points_instance_resource = ExtResource("22_nljy0")
30 changes: 30 additions & 0 deletions addons/top_down/scenes/vfx/damage_points.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[gd_scene load_steps=3 format=3 uid="uid://cub8dbj1w1gk"]

[ext_resource type="Script" path="res://addons/top_down/scripts/damage/DamagePoints.gd" id="1_tj76g"]
[ext_resource type="Script" path="res://addons/great_games_library/resources/InstanceResource/PoolNode.gd" id="2_hgvc1"]

[node name="Damagepoints" type="Node2D" node_paths=PackedStringArray("label", "pool_node")]
script = ExtResource("1_tj76g")
label = NodePath("Label")
pool_node = NodePath("PoolNode")

[node name="Label" type="Label" parent="."]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -3.5
offset_top = -4.5
offset_right = 3.5
offset_bottom = 4.5
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
theme_override_constants/outline_size = 2
theme_override_font_sizes/font_size = 6
text = "69"

[node name="PoolNode" type="Node" parent="." node_paths=PackedStringArray("ready_nodes")]
script = ExtResource("2_hgvc1")
ready_nodes = [NodePath(".."), NodePath("../Label")]
31 changes: 31 additions & 0 deletions addons/top_down/scripts/actor/DamageDisplay.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class_name DamageDisplay
extends Node

@export var resource_node:ResourceNode
@export var damage_points_instance_resource:InstanceResource

const UPDATE_INTERVAL:float = 0.3
var last_time:float
var last_points:DamagePoints
var total_points:float

func _ready()->void:
var _health_resource:HealthResource = resource_node.get_resource("health")
_health_resource.damage_data.connect(on_damage_data)

func on_damage_data(damage_resource:DamageResource)->void:
var _time:float = Time.get_ticks_msec() * 0.001
if last_points && _time < last_time + UPDATE_INTERVAL:
last_time = _time
total_points += damage_resource.get_total_damage()
last_points.set_displayed_points(total_points, damage_resource.is_critical)
return

last_time = _time
total_points = damage_resource.get_total_damage()
var _config_callback:Callable = func (inst:Node2D)->void:
# give offset to appear on body position
inst.global_position = owner.global_position + Vector2(0.0, -8.0)
(inst as DamagePoints).set_displayed_points(total_points, damage_resource.is_critical)

last_points = damage_points_instance_resource.instance(_config_callback)
3 changes: 1 addition & 2 deletions addons/top_down/scripts/damage/ActorDamage.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ extends Node
@export var dead_vfx_instance_resource:InstanceResource

func _ready()->void:

var _health_resource:HealthResource = resource_node.get_resource("health")
_health_resource.damaged.connect(flash_animation_player.stop)
_health_resource.damaged.connect(flash_animation_player.play.bind(flash_animation))
Expand All @@ -30,5 +29,5 @@ func play_dead()->void:
inst.global_position = owner.global_position
inst.scale.x = sprite_flip.dir

dead_vfx_instance_resource.instance(_config_callback)
dead_vfx_instance_resource.instance.call_deferred(_config_callback)
owner.queue_free()
22 changes: 22 additions & 0 deletions addons/top_down/scripts/damage/DamagePoints.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class_name DamagePoints
extends Node2D

@export var label:Label
@export var tween_time:float = 1.0
@export var distance:float = 16.0
@export var pool_node:PoolNode


func set_displayed_points(points:int, is_critical:bool)->void:
label.text = str(points)
label.set_anchors_and_offsets_preset(Control.PRESET_CENTER,Control.PRESET_MODE_MINSIZE)

func _ready()->void:
var _angle:float = randf_range(0.6, 0.9) * TAU
var _offset:Vector2 = Vector2.RIGHT.rotated(_angle) * distance
var tween:Tween = create_tween()
tween.tween_method(tween_move.bind(global_position, global_position + _offset), 0.0, 1.0, tween_time).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_CUBIC)
tween.finished.connect(pool_node.pool_return)

func tween_move(t:float, from:Vector2, to:Vector2)->void:
global_position = from.lerp(to, t)
4 changes: 4 additions & 0 deletions addons/top_down/scripts/damage/DamageResource.gd
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func process(resource_node:ResourceNode)->void:
hit_list.append(resource_node.owner)
_health_resource.add_hp( -get_total_damage() )

# TODO: need a dedicated receiver data exchange
# Used for showing received damage points
_health_resource.damage_data.emit(self)

var _push_resource:PushResource = resource_node.get_resource("push")
if _push_resource != null:
_push_resource.add_impulse(direction * kickback_strength)
Expand Down
1 change: 1 addition & 0 deletions addons/top_down/scripts/damage/HealthResource.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ signal damaged
signal dead
signal hp_changed
signal full
signal damage_data(damage_resource:DamageResource)

@export var hp:float = 5
@export var max_hp:float = 5
Expand Down