diff --git a/player/camera_noise_shake_effect.gd b/player/camera_noise_shake_effect.gd new file mode 100644 index 00000000..e4ec5fff --- /dev/null +++ b/player/camera_noise_shake_effect.gd @@ -0,0 +1,63 @@ +extends Camera + +# Constant values of the effect. +const SPEED = 1.0 +const DECAY_RATE = 1.5 +const MAX_YAW = 0.05 +const MAX_PITCH = 0.05 +const MAX_ROLL = 0.1 +const MAX_TRAUMA = 0.6 + +# Default values. +var start_rotation = rotation +var trauma = 0.0 +var time = 0.0 +var noise = OpenSimplexNoise.new() +var noise_seed = randi() + + +func _ready(): + noise.seed = noise_seed + noise.octaves = 1 + noise.period = 256.0 + noise.persistence = 0.5 + noise.lacunarity = 1.0 + + # This variable is reset if the camera position is changed by other scripts, + # such as when zooming in/out or focusing on a different position. + # This should NOT be done when the camera shake is happening. + start_rotation = rotation + + +func _process(delta): + if trauma > 0.0: + decay_trauma(delta) + apply_shake(delta) + + +# Add trauma to start/continue the shake. +func add_trauma(amount): + trauma = min(trauma + amount, MAX_TRAUMA) + + +# Decay the trauma effect over time. +func decay_trauma(delta): + var change = DECAY_RATE * delta + trauma = max(trauma - change, 0.0) + + +# Apply the random shake accoring to delta time. +func apply_shake(delta): + # Using a magic number here to get a pleasing effect at SPEED 1.0. + time += delta * SPEED * 5000.0 + var shake = trauma * trauma + var yaw = MAX_YAW * shake * get_noise_value(noise_seed, time) + var pitch = MAX_PITCH * shake * get_noise_value(noise_seed + 1, time) + var roll = MAX_ROLL * shake * get_noise_value(noise_seed + 2, time) + rotation = start_rotation + Vector3(pitch, yaw, roll) + + +# Return a random float in range(-1, 1) using OpenSimplex noise. +func get_noise_value(seed_value, t): + noise.seed = seed_value + return noise.get_noise_1d(t) diff --git a/player/player.gd b/player/player.gd index 6217fc4b..6c1c2b54 100644 --- a/player/player.gd +++ b/player/player.gd @@ -159,6 +159,8 @@ func _physics_process(delta): bullet.add_collision_exception_with(self) fire_cooldown.start() sound_effect_shoot.play() + camera_camera.add_trauma(0.35) + else: # Not in air or aiming, idle. # Convert orientation to quaternions for interpolating rotation. var target = camera_x * motion.x + camera_z * motion.y diff --git a/player/player.tscn b/player/player.tscn index 3f6aefc2..e70db4a9 100644 --- a/player/player.tscn +++ b/player/player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=35 format=2] +[gd_scene load_steps=36 format=2] [ext_resource path="res://player/player.gd" type="Script" id=1] [ext_resource path="res://player/model/player.dae" type="PackedScene" id=2] @@ -7,6 +7,7 @@ [ext_resource path="res://player/audio/jump.wav" type="AudioStream" id=5] [ext_resource path="res://player/audio/land.wav" type="AudioStream" id=6] [ext_resource path="res://player/audio/shoot.wav" type="AudioStream" id=7] +[ext_resource path="res://player/camera_noise_shake_effect.gd" type="Script" id=8] [sub_resource type="AnimationNodeAdd3" id=1] resource_local_to_scene = true @@ -152,7 +153,7 @@ nodes/strafe/node = SubResource( 16 ) nodes/strafe/position = Vector2( -400, -120 ) nodes/walk/node = SubResource( 21 ) nodes/walk/position = Vector2( -400, 40 ) -node_connections = [ "output", 0, "eye_blend", "state", 0, "strafe", "state", 1, "walk", "state", 2, "jumpup", "state", 3, "jumpdown", "land", 0, "state", "land", 1, "hardland", "aim", 0, "aimdown", "aim", 1, "land", "aim", 2, "aimup", "eye_blend", 0, "aim", "eye_blend", 1, "eyes" ] +node_connections = [ "output", 0, "eye_blend", "state", 0, "strafe", "state", 1, "walk", "state", 2, "jumpup", "state", 3, "jumpdown", "aim", 0, "aimdown", "aim", 1, "land", "aim", 2, "aimup", "eye_blend", 0, "aim", "eye_blend", 1, "eyes", "land", 0, "state", "land", 1, "hardland" ] [sub_resource type="CapsuleShape" id=23] radius = 0.5 @@ -229,7 +230,7 @@ transform = Transform( 0.803991, 0, 0, 0, 0.803991, 0, 0, 0, 0.803991, 0, 0, 0 ) bones/46/bound_children = [ NodePath("GunBone") ] [node name="GunBone" type="BoneAttachment" parent="PlayerModel/Robot_Skeleton/Skeleton" index="5"] -transform = Transform( 0.916368, -0.24167, 0.319464, -0.240673, -0.969661, -0.0435164, 0.320323, -0.0369838, -0.946598, -0.207126, 1.38922, 0.483314 ) +transform = Transform( 0.904321, -0.247813, 0.347821, -0.25677, -0.966275, -0.0211712, 0.341372, -0.0701338, -0.937322, -0.207136, 1.3677, 0.535182 ) bone_name = "hand.R" [node name="ShootFrom" type="Position3D" parent="PlayerModel/Robot_Skeleton/Skeleton/GunBone"] @@ -278,6 +279,7 @@ spring_length = 2.4 transform = Transform( 1, 1.42109e-14, -2.84217e-14, 0, 1, 0, 0, 0, 1, 0, 0, 0 ) current = true far = 300.0 +script = ExtResource( 8 ) __meta__ = { "_editor_description_": "Translated when aiming." }