Skip to main content
Ramblings of a Developer

Journal: A CRUD developer builds a game

What did I do today? #

It has reached a reasonable state. There is some UI, a life bar, score keeping, and even some fancy camera shaking effect. The camera shaking was interesting, I borrowed some code from online and tweaked it a bit. The effect is alright.

Here is the code (thanks to KidsCanCode):

extends Camera2D

export var decay = 0.7  # How quickly the shaking stops [0, 1].
export var max_offset = Vector2(50, 35)  # Maximum hor/ver shake in pixels.
export var max_roll = 0.1  # Maximum rotation in radians (use sparingly).

var trauma = 0.0  # Current shake strength.
var trauma_power = 3  # Trauma exponent. Use [2, 3].

onready var noise = OpenSimplexNoise.new()
var noise_y = 0

func _ready():
	randomize()
	noise.seed = randi()
	noise.period = 4
	noise.octaves = 2

func add_trauma(amount):
	trauma = min(trauma + amount, 1.0)

func _process(delta):
	if trauma:
		trauma = max(trauma - decay * delta, 0)
		shake()

func shake():
	var amount = pow(trauma, trauma_power)
	noise_y += 1
	rotation = max_roll * amount * noise.get_noise_2d(noise.seed, noise_y)
	offset.x = max_offset.x * amount * noise.get_noise_2d(noise.seed*2, noise_y)
	offset.y = max_offset.y * amount * noise.get_noise_2d(noise.seed*3, noise_y)

Use it by calling add_trauma with a value between 0.1 and 1.0. The higher the value, the more intense the shake.

Demo #

Click here to play.

WASD or arrows to move, or click/touch to move.