Skip to content

Commit

Permalink
Merge pull request #18 from SanderVanhove/main
Browse files Browse the repository at this point in the history
Add screenshake on jump
  • Loading branch information
TaylorAnderson authored Apr 8, 2022
2 parents 674f83a + 7ee67dc commit b729c55
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 11 deletions.
14 changes: 13 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 12 additions & 1 deletion scenes/Main.tscn
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
[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 )

[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 )

Expand All @@ -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"]
18 changes: 9 additions & 9 deletions scenes/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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",
Expand All @@ -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"]
Expand Down
15 changes: 15 additions & 0 deletions scenes/ScreenShake.tscn
Original file line number Diff line number Diff line change
@@ -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"]
16 changes: 16 additions & 0 deletions scripts/Camera.gd
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions scripts/Player.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extends KinematicBody2D

signal jumping

const UP = Vector2.UP
const GRAVITY = 100
const MAXFALLSPEED = 1000
Expand Down Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions scripts/ScreenShake.gd
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit b729c55

Please sign in to comment.