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

Camera shake & fix: DamageResource report #62

Merged
merged 1 commit into from
Sep 18, 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,30 @@
class_name CameraShakeResource
extends Resource

@export var length:float = 0.5
@export var frequency:Curve
@export var amplitude:Curve
@export_range (0.0, 360.0) var angleFrom:float = 0.0
@export_range (0.0, 360.0) var angleto:float = 360.0
@export var camera_reference:ReferenceNodeResource
@export var tween_resource:TweenValueResource

var dir:Vector2

func play()->void:
if camera_reference.node == null:
return

var angle = deg_to_rad(lerp(angleFrom, angleto, randf())) * TAU
dir = Vector2(cos(angle), sin(angle))

if tween_resource.value != null:
tween_resource.value.kill()

tween_resource.value = camera_reference.node.create_tween().bind_node(camera_reference.node)
# warning-ignore:return_value_discarded
tween_resource.value.tween_method(sample, 0.0, 1.0, length)

func sample(t:float)->void:
var offset:Vector2 = sin(TAU * frequency.sample(t) * length * (1.0 - t)) * amplitude.sample(t) * dir
camera_reference.node.offset = offset
25 changes: 25 additions & 0 deletions resources/CameraShakeResource/enemy_damage_shake.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[gd_resource type="Resource" script_class="CameraShakeResource" load_steps=6 format=3 uid="uid://ddpkkj7j7ync6"]

[ext_resource type="Script" path="res://addons/nezvers_library/resources/CameraShakeResource/CameraShakeResource.gd" id="1_cdekx"]
[ext_resource type="Resource" uid="uid://c5igju2sl4cdg" path="res://resources/ReferenceNodeResources/camera_reference.tres" id="1_hal8d"]
[ext_resource type="Resource" uid="uid://blem3ysrttsws" path="res://resources/CameraShakeResource/tween_camera_shake.tres" id="2_ilewv"]

[sub_resource type="Curve" id="Curve_vv6d3"]
max_value = 2.0
_data = [Vector2(0.00887574, 1.95648), 0.0, -5.25372, 0, 0, Vector2(1, 0), -0.820407, 0.0, 0, 0]
point_count = 2

[sub_resource type="Curve" id="Curve_bfgph"]
max_value = 25.0
_data = [Vector2(0, 15.0444), 0.0, 18.5351, 0, 0, Vector2(0.994083, 25), 0.0, 0.0, 0, 0]
point_count = 2

[resource]
script = ExtResource("1_cdekx")
length = 0.2
frequency = SubResource("Curve_bfgph")
amplitude = SubResource("Curve_vv6d3")
angleFrom = 0.0
angleto = 360.0
camera_reference = ExtResource("1_hal8d")
tween_resource = ExtResource("2_ilewv")
25 changes: 25 additions & 0 deletions resources/CameraShakeResource/player_damage_shake.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[gd_resource type="Resource" script_class="CameraShakeResource" load_steps=6 format=3 uid="uid://dkb7r2v362yj"]

[ext_resource type="Resource" uid="uid://c5igju2sl4cdg" path="res://resources/ReferenceNodeResources/camera_reference.tres" id="1_iwsv1"]
[ext_resource type="Script" path="res://addons/nezvers_library/resources/CameraShakeResource/CameraShakeResource.gd" id="2_cayog"]
[ext_resource type="Resource" uid="uid://blem3ysrttsws" path="res://resources/CameraShakeResource/tween_camera_shake.tres" id="3_nmm4h"]

[sub_resource type="Curve" id="Curve_iasph"]
max_value = 4.0
_data = [Vector2(0, 4), 0.0, -9.15852, 0, 0, Vector2(1, 0), -1.27128, 0.0, 0, 0]
point_count = 2

[sub_resource type="Curve" id="Curve_l376i"]
max_value = 15.0
_data = [Vector2(0, 4.31993), 0.0, 15.2385, 0, 0, Vector2(1, 15), 0.0, 0.0, 0, 0]
point_count = 2

[resource]
script = ExtResource("2_cayog")
length = 0.3
frequency = SubResource("Curve_l376i")
amplitude = SubResource("Curve_iasph")
angleFrom = 0.0
angleto = 360.0
camera_reference = ExtResource("1_iwsv1")
tween_resource = ExtResource("3_nmm4h")
8 changes: 8 additions & 0 deletions resources/CameraShakeResource/tween_camera_shake.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="Resource" script_class="TweenValueResource" load_steps=2 format=3 uid="uid://blem3ysrttsws"]

[ext_resource type="Script" path="res://addons/nezvers_library/resources/ValueResource/TweenValueResource.gd" id="1_uk4tt"]

[resource]
script = ExtResource("1_uk4tt")
version = 0
not_saved = false
6 changes: 6 additions & 0 deletions resources/ReferenceNodeResources/camera_reference.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_resource type="Resource" script_class="ReferenceNodeResource" load_steps=2 format=3 uid="uid://c5igju2sl4cdg"]

[ext_resource type="Script" path="res://addons/nezvers_library/resources/ReferenceNodeResource/ReferenceNodeResource.gd" id="1_oo0pe"]

[resource]
script = ExtResource("1_oo0pe")
9 changes: 7 additions & 2 deletions scenes/actors/player.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=24 format=3 uid="uid://bmp0g88dtijqw"]
[gd_scene load_steps=26 format=3 uid="uid://bmp0g88dtijqw"]

[ext_resource type="PackedScene" uid="uid://botai66n8rwt3" path="res://scenes/actors/actor.tscn" id="1_mvls6"]
[ext_resource type="Resource" uid="uid://c61lm3lfv4u6h" path="res://resources/health_resource/player_health.tres" id="2_ck5gl"]
Expand All @@ -18,6 +18,8 @@
[ext_resource type="Script" path="res://scripts/actor/DashAbility.gd" id="16_vooxc"]
[ext_resource type="Script" path="res://scripts/actor/Player/PlayerJuice.gd" id="17_32qwh"]
[ext_resource type="Resource" uid="uid://diqvd4u0lbw7h" path="res://resources/CommandNodeResource/color_flash.tres" id="18_5jtne"]
[ext_resource type="Resource" uid="uid://ddpkkj7j7ync6" path="res://resources/CameraShakeResource/enemy_damage_shake.tres" id="18_7uk1v"]
[ext_resource type="Resource" uid="uid://dkb7r2v362yj" path="res://resources/CameraShakeResource/player_damage_shake.tres" id="19_d25rq"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_gfelk"]
resource_local_to_scene = true
Expand Down Expand Up @@ -159,7 +161,10 @@ reference_resource = ExtResource("12_l6ujm")
script = ExtResource("16_vooxc")
mover = NodePath("../MoverTopDown2D")

[node name="PlayerJuice" type="Node" parent="." index="12" node_paths=PackedStringArray("damage_receiver")]
[node name="PlayerJuice" type="Node" parent="." index="12" node_paths=PackedStringArray("damage_receiver", "weapon_manager")]
script = ExtResource("17_32qwh")
damage_receiver = NodePath("../DamageReceiver")
weapon_manager = NodePath("../WeaponManager")
enemy_damage_shake = ExtResource("18_7uk1v")
screen_flash_command = ExtResource("18_5jtne")
player_damage_shake = ExtResource("19_d25rq")
8 changes: 7 additions & 1 deletion scenes/levels/level_0.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=15 format=4 uid="uid://cpjckxc4g3s0w"]
[gd_scene load_steps=16 format=4 uid="uid://cpjckxc4g3s0w"]

[ext_resource type="PackedScene" uid="uid://7ixxtkmuui6p" path="res://scenes/main_camera.tscn" id="1_4dff6"]
[ext_resource type="PackedScene" uid="uid://bmp0g88dtijqw" path="res://scenes/actors/player.tscn" id="1_nn7va"]
Expand All @@ -11,6 +11,7 @@
[ext_resource type="Resource" uid="uid://ini50pn76erv" path="res://resources/pause_resource.tres" id="6_wjqwx"]
[ext_resource type="Script" path="res://scripts/game/RestartScene.gd" id="7_it1dq"]
[ext_resource type="PackedScene" uid="uid://bqh7iwpl0s1ha" path="res://scenes/screen_effects/screen_effects.tscn" id="8_lcfiq"]
[ext_resource type="Resource" uid="uid://c5igju2sl4cdg" path="res://resources/ReferenceNodeResources/camera_reference.tres" id="8_lllv4"]
[ext_resource type="Script" path="res://scripts/game/MusicSetter.gd" id="8_u3y66"]
[ext_resource type="Script" path="res://addons/nezvers_library/resources/ReferenceNodeResource/ReferenceNodeSetter.gd" id="10_26fnk"]
[ext_resource type="Resource" uid="uid://d1ck1axrd4kd4" path="res://resources/ReferenceNodeResources/ysort_reference.tres" id="11_u6wfl"]
Expand Down Expand Up @@ -66,6 +67,11 @@ reference_resource = ExtResource("6_uxajn")
[node name="MainCamera" parent="." node_paths=PackedStringArray("target") instance=ExtResource("1_4dff6")]
target = NodePath("../YSorted/Player")

[node name="ReferenceNodeSetter" type="Node" parent="MainCamera" node_paths=PackedStringArray("reference_node")]
script = ExtResource("10_26fnk")
reference_node = NodePath("..")
reference_resource = ExtResource("8_lllv4")

[node name="ScreenEffects" parent="." instance=ExtResource("8_lcfiq")]

[node name="UI" parent="." instance=ExtResource("2_xaqoc")]
Expand Down
8 changes: 7 additions & 1 deletion scenes/projectiles/zombie_slash.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
[gd_scene load_steps=6 format=3 uid="uid://cm7673sbsnyya"]
[gd_scene load_steps=7 format=3 uid="uid://cm7673sbsnyya"]

[ext_resource type="PackedScene" uid="uid://beifde6xst23w" path="res://scenes/projectiles/projectile.tscn" id="1_5ra58"]
[ext_resource type="Texture2D" uid="uid://brjp0173qoawo" path="res://assets/images/vfx/slash_2_strip5.png" id="2_eg08h"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_7uwdv"]
size = Vector2(8, 4)

[sub_resource type="Animation" id="Animation_3u71e"]
length = 0.001
tracks/0/type = "value"
Expand Down Expand Up @@ -57,6 +60,9 @@ _data = {
[node name="ZombieSlash" instance=ExtResource("1_5ra58")]
speed = 0.0

[node name="CollisionShape2D" parent="DamageSource" index="0"]
shape = SubResource("RectangleShape2D_7uwdv")

[node name="Sprite2D" parent="RotatedNode" index="0"]
texture = ExtResource("2_eg08h")
hframes = 5
Expand Down
1 change: 0 additions & 1 deletion scenes/weapons/shotgun.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interval = 0.75
[node name="ProjectileSpawner" parent="." index="4"]
projectile_scene = ExtResource("2_6w2s8")
projectile_angles = Array[float]([0.0, -20.0, 20.0, -10.0, 10.0])
new_damage = false

[node name="SpreadShot" parent="ProjectileSpawner" index="0"]
random_angle_offset = 2.5
4 changes: 3 additions & 1 deletion scenes/weapons/sword.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ kickback_strength = -40.0
[node name="ProjectileInterval" parent="WeaponTrigger" index="0"]
interval = 0.3

[node name="AnimationTrigger" parent="WeaponTrigger" index="1"]
animation_name = &""

[node name="ProjectileSpawner" parent="." index="4"]
axis_multiplication = Vector2(1, 1)
initial_distance = 16.0
projectile_scene = ExtResource("2_x78ie")
new_damage = false

[node name="SpreadShot" parent="ProjectileSpawner" index="0"]
random_angle_offset = 0.0
10 changes: 10 additions & 0 deletions scripts/actor/Player/PlayerJuice.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ class_name PlayerJuice
extends Node

@export var damage_receiver:DamageReceiver
@export var weapon_manager:WeaponManager
@export var enemy_damage_shake:CameraShakeResource
@export var screen_flash_command:CommandNodeResource
@export var player_damage_shake:CameraShakeResource

func _ready()->void:
damage_receiver.health_resource.damaged.connect(on_damaged)
weapon_manager.damage_report.connect(on_damage_report)

func on_damaged()->void:
assert(screen_flash_command.node != null, "reference is not set")
screen_flash_command.command("play", ["white_flash"])
player_damage_shake.play()

## Receives data for every damage dealt
## Use for screenshake
func on_damage_report(_damage:DamageResource)->void:
enemy_damage_shake.play()
8 changes: 5 additions & 3 deletions scripts/damage/DamageResource.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ signal damage_report(damage:DamageResource)
@export var total_damage:float
## TODO: include information from source character

var report_callback:Callable

## final value applied in HealthResource
## Projectiles can influence resulting value
func get_total_damage()->float:
Expand All @@ -40,13 +42,13 @@ func initialize_generation()->void:
## TODO: include more receiving end information
func report_damage_data(receiver:Node2D)->void:
hit_list.append(receiver)
damage_report.emit(self)
report_callback.call(self)

## Create a new generation for a new attack action.
## Do it from root DamageResource
func new_generation()->DamageResource:
var data:DamageResource = self.duplicate()
data.damage_report.connect(on_damage_report)
data.report_callback = on_damage_report
data.initialize_generation()
# create unique array
data.hit_list = []
Expand All @@ -55,7 +57,7 @@ func new_generation()->DamageResource:
## Create new splitsh of the same generation, like shrapnels from a granade
func new_split()->DamageResource:
var data:DamageResource = self.duplicate()
data.damage_report.connect(on_damage_report)
data.report_callback = report_callback
return data

## Mainly used for receiving information from duplicates
Expand Down
4 changes: 0 additions & 4 deletions scripts/weapon_system/Weapon.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ signal enabled_changed

func _ready()->void:
set_enabled(enabled)
#damage_resource.damage_report.connect(on_damage_report)

#func on_damage_report(damage:DamageResource)->void:
#pass

## Toggle connections to the action input and controls visibility
func set_enabled(value:bool)->void:
Expand Down
34 changes: 31 additions & 3 deletions scripts/weapon_system/WeaponManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends Node2D

## Emitted when weapon is changed
signal weapon_changed
signal damage_report(damage:DamageResource)

## Relative to this manager
## Will be modified to correct for instanced wepons
Expand All @@ -14,6 +15,9 @@ signal weapon_changed
## Passed to instantiated weapons, that pass it to projectiles
## TODO: Need to remove passing chain
@export_flags_2d_physics var collision_mask:int
## In case users share the same weapons it's better to make damage_resource unique
## That removes opion to tweak values from Godot Editor while a game is running
@export var make_unique_damage:bool = true

## List of instanced weapons available to the user
## Weapons that are already in the scene tree are added on _ready
Expand All @@ -23,15 +27,28 @@ var weapon_index:int = 0
## Currently activated weapon
var current_weapon:Weapon = null

func _init()->void:
for weapon:Node in get_children():
if !(weapon is Weapon):
continue
if make_unique_damage:
weapon.damage_resource = weapon.damage_resource.duplicate()
weapon.damage_resource.resource_name += "dup_"
if !weapon.damage_resource.damage_report.is_connected(on_damage_report):
weapon.damage_resource.damage_report.connect(on_damage_report)

func _ready()->void:
mover.input_resource.switch_weapon.connect(on_switch_weapon)


for weapon:Node in get_children():
if !(weapon is Weapon):
continue
# every weapon are disabled by default
weapon.set_enabled(false)
weapon_list.append(weapon)
var damage_resource_dup:DamageResource = weapon.damage_resource.duplicate()
var packed_scene:PackedScene = ScenePacker.create_package(weapon)
auto_instance_weapons.append(packed_scene)
remove_child(weapon)
weapon.queue_free()

for scene:PackedScene in auto_instance_weapons:
add_new_weapon_from_scene(scene)
Expand All @@ -45,6 +62,10 @@ func add_new_weapon_from_scene(scene:PackedScene)->void:
weapon.enabled = false
weapon.mover = mover
weapon.collision_mask = collision_mask
if make_unique_damage:
weapon.damage_resource = weapon.damage_resource.duplicate()
if !weapon.damage_resource.damage_report.is_connected(on_damage_report):
weapon.damage_resource.damage_report.connect(on_damage_report)
add_child(weapon)
weapon_list.append(weapon)

Expand All @@ -69,3 +90,10 @@ func set_weapon_index(value:int)->void:
current_weapon = weapon_list[weapon_index]
current_weapon.set_enabled(true)
weapon_changed.emit()

func get_current_damage()->DamageResource:
return current_weapon.damage_resource

## Collects all damage reports into one signal
func on_damage_report(damage:DamageResource)->void:
damage_report.emit(damage)