screen_shake: trembler l'écran quand:

- on tremble depuis longtemps
- onse paye un mur en dashant (ou le plafond)
This commit is contained in:
Thomas Lavocat
2023-10-15 16:52:34 +02:00
parent 37b25f1d19
commit cbcd61161d
3 changed files with 80 additions and 2 deletions

48
ShakingCamera2D.gd Normal file
View File

@@ -0,0 +1,48 @@
class_name ShakingCamera extends Camera2D
@export var decay = 0.8 # How quickly the shaking stops [0, 1].
@export var max_offset = Vector2(100, 75) # Maximum hor/ver shake in pixels.
@export var max_roll = 0.1 # Maximum rotation in radians (use sparingly).
@export var target = NodePath()
@onready var noise = FastNoiseLite.new()
var noise_y = 0
var traumax = 0.0 # Current shake strength.
var traumay = 0.0 # Current shake strength.
var trauma_power = 2 # Trauma exponent. Use [2, 3].
func _ready():
randomize()
noise.noise_type = FastNoiseLite.TYPE_PERLIN
noise.seed = 0982309586286872365
func add_trauma(amount):
traumax = min(traumax + amount, 1.0)
traumay = min(traumay + amount, 1.0)
func add_trauma_x(amount):
traumax = min(traumax + amount, 1.0)
func add_trauma_y(amount):
traumay = min(traumay + amount, 1.0)
func _process(delta):
if target:
global_position = get_node(target).global_position
var goshake = traumax or traumay
if traumax:
traumax = max(traumax - decay * delta, 0)
if traumay:
traumay = max(traumay - decay * delta, 0)
if goshake:
shake()
func shake():
noise_y += 1
var amountx = pow(traumax, trauma_power)
var amounty = pow(traumay, trauma_power)
if traumax:
rotation = max_roll * amountx * noise.get_noise_2d(noise.seed, noise_y)
offset.x = max_offset.x * amountx * noise.get_noise_2d(noise.seed*2, noise_y)
if traumay:
offset.y = max_offset.y * amounty * noise.get_noise_2d(noise.seed*3, noise_y)

View File

@@ -21,7 +21,7 @@ var gravity: int = ProjectSettings.get("physics/2d/default_gravity")
@onready var ground_far_detect := $ground_far_detect as RayCast2D @onready var ground_far_detect := $ground_far_detect as RayCast2D
@onready var ground_far_detect2 := $ground_far_detect2 as RayCast2D @onready var ground_far_detect2 := $ground_far_detect2 as RayCast2D
@onready var animation := $AnimatedSprite2D as AnimatedSprite2D @onready var animation := $AnimatedSprite2D as AnimatedSprite2D
@onready var camera := $Camera2D as Camera2D @onready var camera := $Camera2D as ShakingCamera
@onready var death_animation := $"Death player" as AnimationPlayer @onready var death_animation := $"Death player" as AnimationPlayer
@onready var nuage_prout := $"GPUParticles2D" as GPUParticles2D @onready var nuage_prout := $"GPUParticles2D" as GPUParticles2D
@@ -108,6 +108,8 @@ var coyote_grab = []
# variable d'état relative à la gravité # variable d'état relative à la gravité
var falling_step : int = -1 # Où en est la princess dans son acceleration de chute ? var falling_step : int = -1 # Où en est la princess dans son acceleration de chute ?
var character_falling: bool = false
var airtime: int = 0
# Variables d'état relative à l'accroche au mur # Variables d'état relative à l'accroche au mur
var pressing_wall_left = false # Princesse est elle en contact avec un mur à gauche? var pressing_wall_left = false # Princesse est elle en contact avec un mur à gauche?
@@ -120,6 +122,8 @@ var dashing : bool = false
var dash_step : int = -1 var dash_step : int = -1
var dash_direction_x : int = 1 var dash_direction_x : int = 1
var dash_direction_y : int = 1 var dash_direction_y : int = 1
var dashing_x_stuck : bool = false
var dashing_y_stuck : bool = false
# Variables d'état relative au crouch # Variables d'état relative au crouch
var crouch : bool = false var crouch : bool = false
@@ -151,6 +155,7 @@ func copy_from(other : Princess):
dash_direction_x = other.dash_direction_x dash_direction_x = other.dash_direction_x
dash_direction_y = other.dash_direction_y dash_direction_y = other.dash_direction_y
near_cliff = other.near_cliff near_cliff = other.near_cliff
character_falling = other.character_falling
################################################################################ ################################################################################
# #
@@ -282,6 +287,11 @@ func fall() -> float:
var intertia = 0 var intertia = 0
if velocity.y < 0: if velocity.y < 0:
intertia = max(velocity.y, -FALLING_SPEED) intertia = max(velocity.y, -FALLING_SPEED)
if is_on_floor():
airtime = 0
else:
if falling_step > 0:
airtime +=1
if is_on_floor_only(): if is_on_floor_only():
if get_floor_normal()[0] < 0: # pente à gauche if get_floor_normal()[0] < 0: # pente à gauche
if direction >= 0:# on va à droite, désactive la gravité if direction >= 0:# on va à droite, désactive la gravité
@@ -306,6 +316,7 @@ func fall() -> float:
func stop_fall() -> void: func stop_fall() -> void:
falling_step = -1 falling_step = -1
airtime = 0
func jump() -> float: func jump() -> float:
# fait sauter princesse # fait sauter princesse
@@ -590,6 +601,22 @@ func compute_state() -> void:
(wall_detect_right3.is_colliding()) (wall_detect_right3.is_colliding())
) )
pressing_wall = pressing_wall_left or pressing_wall_right pressing_wall = pressing_wall_left or pressing_wall_right
if character_falling:
if is_on_floor():
if airtime/100 > 0.10:
camera.add_trauma(min(airtime/100, 0.30))
Input.start_joy_vibration(0, 0, 1, min(airtime/100, 0.30))
dashing_y_stuck = dashing and is_zero_approx(velocity.y) and (is_on_ceiling() or is_on_floor())
dashing_x_stuck = dashing and is_zero_approx(velocity.x) and grab_wall
if dashing_x_stuck:
camera.add_trauma_x(0.05)
if dashing_y_stuck:
camera.add_trauma_y(0.05)
if dashing_x_stuck or dashing_y_stuck:
Input.stop_joy_vibration(0)
Input.start_joy_vibration(0, 0, 0.5, 0.05)
character_falling = !is_on_floor()
func get_new_animation() -> String: func get_new_animation() -> String:
# Renvoie la bonne annimation en fonction de l'état de la princesse # Renvoie la bonne annimation en fonction de l'état de la princesse

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=80 format=3 uid="uid://dv0mokf4eogm7"] [gd_scene load_steps=81 format=3 uid="uid://dv0mokf4eogm7"]
[ext_resource type="Script" path="res://princesse.gd" id="1_dkp7s"] [ext_resource type="Script" path="res://princesse.gd" id="1_dkp7s"]
[ext_resource type="Texture2D" uid="uid://dr7fyh2rufsyj" path="res://sprite/princess_falling_direction.png" id="2_hholp"] [ext_resource type="Texture2D" uid="uid://dr7fyh2rufsyj" path="res://sprite/princess_falling_direction.png" id="2_hholp"]
@@ -12,6 +12,7 @@
[ext_resource type="Texture2D" uid="uid://dgsn3ixn46anc" path="res://sprite/light.png" id="8_8c83t"] [ext_resource type="Texture2D" uid="uid://dgsn3ixn46anc" path="res://sprite/light.png" id="8_8c83t"]
[ext_resource type="Texture2D" uid="uid://vb6uaygswp4w" path="res://sprite/princess_edging.png" id="9_hgh77"] [ext_resource type="Texture2D" uid="uid://vb6uaygswp4w" path="res://sprite/princess_edging.png" id="9_hgh77"]
[ext_resource type="Texture2D" uid="uid://cfptwn1c03k1b" path="res://masks/smoke_particle_6.png" id="11_4uqsf"] [ext_resource type="Texture2D" uid="uid://cfptwn1c03k1b" path="res://masks/smoke_particle_6.png" id="11_4uqsf"]
[ext_resource type="Script" path="res://ShakingCamera2D.gd" id="12_oy87r"]
[sub_resource type="Gradient" id="Gradient_anx4x"] [sub_resource type="Gradient" id="Gradient_anx4x"]
offsets = PackedFloat32Array(0, 0.99893) offsets = PackedFloat32Array(0, 0.99893)
@@ -566,6 +567,8 @@ position_smoothing_enabled = true
position_smoothing_speed = 10.0 position_smoothing_speed = 10.0
drag_horizontal_enabled = true drag_horizontal_enabled = true
drag_vertical_enabled = true drag_vertical_enabled = true
script = ExtResource("12_oy87r")
target = NodePath("..")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, 4) position = Vector2(0, 4)