From 7ee67dc1d9e67eeacfb9f498b945044f9ef123ba Mon Sep 17 00:00:00 2001 From: Sander Vanhove Date: Fri, 8 Apr 2022 22:42:53 +0200 Subject: [PATCH] Add screenshake on jump --- project.godot | 14 ++++++++++- scenes/Main.tscn | 13 +++++++++- scenes/Player.tscn | 18 +++++++------- scenes/ScreenShake.tscn | 15 ++++++++++++ scripts/Camera.gd | 16 ++++++++++++ scripts/Player.gd | 3 +++ scripts/ScreenShake.gd | 54 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 scenes/ScreenShake.tscn create mode 100644 scripts/Camera.gd create mode 100644 scripts/ScreenShake.gd diff --git a/project.godot b/project.godot index ccdc096ea..157565e59 100644 --- a/project.godot +++ b/project.godot @@ -13,9 +13,21 @@ _global_script_classes=[ { "class": "EndPortal", "language": "GDScript", "path": "res://scripts/EndPortal.gd" +}, { +"base": "Node", +"class": "ScreenShake", +"language": "GDScript", +"path": "res://scripts/ScreenShake.gd" +}, { +"base": "Camera2D", +"class": "ShakingCamera", +"language": "GDScript", +"path": "res://scripts/Camera.gd" } ] _global_script_class_icons={ -"EndPortal": "" +"EndPortal": "", +"ScreenShake": "", +"ShakingCamera": "" } [application] diff --git a/scenes/Main.tscn b/scenes/Main.tscn index de30b9ecf..4e182dcdd 100644 --- a/scenes/Main.tscn +++ b/scenes/Main.tscn @@ -1,9 +1,11 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://scenes/Player.tscn" type="PackedScene" id=1] [ext_resource path="res://scripts/Main.gd" type="Script" id=2] [ext_resource path="res://fonts/ALittleNameCalle.ttf" type="DynamicFontData" id=3] [ext_resource path="res://scenes/levels/Level00.tscn" type="PackedScene" id=4] +[ext_resource path="res://scenes/ScreenShake.tscn" type="PackedScene" id=5] +[ext_resource path="res://scripts/Camera.gd" type="Script" id=6] [sub_resource type="DynamicFont" id=1] font_data = ExtResource( 3 ) @@ -11,6 +13,13 @@ font_data = ExtResource( 3 ) [node name="Main" type="Node2D"] script = ExtResource( 2 ) +[node name="Camera" type="Camera2D" parent="."] +anchor_mode = 0 +current = true +script = ExtResource( 6 ) + +[node name="ScreenShake" parent="Camera" instance=ExtResource( 5 )] + [node name="Player" parent="." instance=ExtResource( 1 )] position = Vector2( 159, 443 ) @@ -32,3 +41,5 @@ rect_scale = Vector2( 2, 2 ) custom_fonts/normal_font = SubResource( 1 ) text = "ARROW KEYS TO MOVE SPACE TO JUMP" + +[connection signal="jumping" from="Player" to="Camera" method="trigger_small_shake"] diff --git a/scenes/Player.tscn b/scenes/Player.tscn index 30cb00a1f..228d0bc2c 100644 --- a/scenes/Player.tscn +++ b/scenes/Player.tscn @@ -7,6 +7,10 @@ [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 23.5, 27 ) +[sub_resource type="AtlasTexture" id=2] +atlas = ExtResource( 2 ) +region = Rect2( 0, 0, 32, 32 ) + [sub_resource type="AtlasTexture" id=3] atlas = ExtResource( 2 ) region = Rect2( 0, 32, 32, 32 ) @@ -35,12 +39,13 @@ region = Rect2( 160, 32, 32, 32 ) atlas = ExtResource( 2 ) region = Rect2( 32, 0, 32, 32 ) -[sub_resource type="AtlasTexture" id=2] -atlas = ExtResource( 2 ) -region = Rect2( 0, 0, 32, 32 ) - [sub_resource type="SpriteFrames" id=10] animations = [ { +"frames": [ SubResource( 2 ) ], +"loop": true, +"name": "idle", +"speed": 5.0 +}, { "frames": [ SubResource( 3 ), SubResource( 4 ), SubResource( 5 ), SubResource( 6 ), SubResource( 7 ), SubResource( 8 ) ], "loop": true, "name": "run", @@ -50,11 +55,6 @@ animations = [ { "loop": true, "name": "jump", "speed": 5.0 -}, { -"frames": [ SubResource( 2 ) ], -"loop": true, -"name": "idle", -"speed": 5.0 } ] [node name="Player" type="KinematicBody2D"] diff --git a/scenes/ScreenShake.tscn b/scenes/ScreenShake.tscn new file mode 100644 index 000000000..ed0a3977b --- /dev/null +++ b/scenes/ScreenShake.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://scripts/ScreenShake.gd" type="Script" id=1] + +[node name="ScreenShake" type="Node"] +script = ExtResource( 1 ) + +[node name="Tween" type="Tween" parent="."] + +[node name="Frequency" type="Timer" parent="."] + +[node name="Duration" type="Timer" parent="."] + +[connection signal="timeout" from="Frequency" to="." method="_on_Frequency_timeout"] +[connection signal="timeout" from="Duration" to="." method="_on_Duration_timeout"] diff --git a/scripts/Camera.gd b/scripts/Camera.gd new file mode 100644 index 000000000..2a22ebb8c --- /dev/null +++ b/scripts/Camera.gd @@ -0,0 +1,16 @@ +extends Camera2D +class_name ShakingCamera + +onready var _screen_shake: ScreenShake = $ScreenShake + + +func trigger_small_shake() -> void: + _screen_shake.start(.1, 8, 10, 7) + + +func trigger_medium_shake() -> void: + _screen_shake.start(.1, 15, 20, 5) + + +func trigger_large_shake() -> void: + _screen_shake.start(.1, 20, 50, 10) diff --git a/scripts/Player.gd b/scripts/Player.gd index fc3df7cd7..fc5de172f 100644 --- a/scripts/Player.gd +++ b/scripts/Player.gd @@ -1,5 +1,7 @@ extends KinematicBody2D +signal jumping + const UP = Vector2.UP const GRAVITY = 100 const MAXFALLSPEED = 1000 @@ -48,6 +50,7 @@ func _physics_process(delta : float) -> void: coyote_timer = 0 motion.y = -JUMPFORCE $JumpSFX.play() + emit_signal("jumping") if is_on_floor(): coyote_timer = COYOTE_TIME diff --git a/scripts/ScreenShake.gd b/scripts/ScreenShake.gd new file mode 100644 index 000000000..50e62314f --- /dev/null +++ b/scripts/ScreenShake.gd @@ -0,0 +1,54 @@ +extends Node +class_name ScreenShake + +const TRANS = Tween.TRANS_SINE +const EASE = Tween.EASE_IN_OUT + +var amplitude = 0 +var priority = 0 + +onready var camera = get_parent() +onready var _tween: Tween = $Tween +onready var _freq_timer: Timer = $Frequency +onready var _duration_timer: Timer = $Duration + + +func start(duration = 0.2, frequency = 15, amplitude = 16, priority = 0): + if (priority < self.priority): + return + + self.priority = priority + self.amplitude = amplitude + + _duration_timer.wait_time = duration + _freq_timer.wait_time = 1 / float(frequency) + _duration_timer.start() + _freq_timer.start() + + _new_shake() + + +func _new_shake(): + var rand = Vector2() + rand.x = rand_range(-amplitude, amplitude) + rand.y = rand_range(-amplitude, amplitude) + + _tween.interpolate_property(camera, "offset", camera.offset, rand, _freq_timer.wait_time, TRANS, EASE) + _tween.start() + + +func _reset(): + _tween.interpolate_property(camera, "offset", camera.offset, Vector2.ZERO, _freq_timer.wait_time, TRANS, EASE) + _tween.start() + + priority = 0 + + +func _on_Frequency_timeout() -> void: + _new_shake() + + +func _on_Duration_timeout() -> void: + _reset() + _freq_timer.stop() + _duration_timer.stop()