Skip to content

Commit 575d662

Browse files
authored
Update Camera Shake 2D with feature requested in #2429 (#2448)
Thank you, @bunny-therapist, for the feature request! Also adds the context manager interface, allowing for the following neat code: ```py with self.shake, self.camera.activate(): # draw commands that want to be drawn with screen shake, # outside of the with block the shake has been removed, and the camera has been deactivated. ``` closes #2429 and #2446 (Thank you for the PR, but due to the extra feature I've made my own PR)
1 parent d12f5d9 commit 575d662

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

arcade/camera/grips/screen_shake_2d.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from __future__ import annotations
77

88
from math import exp, floor, log, pi, sin
9-
from random import uniform
9+
from random import randint, uniform
1010

1111
from arcade.camera.data_types import CameraData
1212
from arcade.math import quaternion_rotation
@@ -40,6 +40,13 @@ class ScreenShake2D:
4040
shake_frequency:
4141
The number of peaks per second. Avoid making it a multiple of half
4242
the target frame-rate. (e.g. at 60 fps avoid 30, 60, 90, 120, etc.)
43+
shake_angle:
44+
The angle around which the shake will focus. Defaults to 0 (horizontal)
45+
This is actuall 0 or 180 degress as the shake angle is mirrored.
46+
shake_range:
47+
The range in degrees from the shake angle that the shake may fall within.
48+
defaults to 90 degrees. This gives a range of +/- 90 degrees which covers the
49+
full circle because the shake angle is mirrored 180 degrees.
4350
"""
4451

4552
def __init__(
@@ -50,6 +57,8 @@ def __init__(
5057
falloff_time: float = 1.0,
5158
acceleration_duration: float = 1.0,
5259
shake_frequency: float = 15.0,
60+
shake_angle: float = 90.0,
61+
shake_range: float = 90.0,
5362
):
5463
self._data: CameraData = camera_data
5564

@@ -70,6 +79,9 @@ def __init__(
7079

7180
self._acceleration_duration: float = acceleration_duration
7281

82+
self.shake_angle: float = shake_angle
83+
self.shake_range: float = shake_range
84+
7385
self._shaking: bool = False
7486
self._length_shaking: float = 0.0
7587

@@ -275,7 +287,13 @@ def update_camera(self) -> None:
275287
floor(self._last_update_time * 2 * self.shake_frequency)
276288
< floor(self._length_shaking * 2.0 * self.shake_frequency)
277289
) or self._last_update_time == 0.0:
278-
self._current_dir = uniform(-180, 180)
290+
# Start from the shake angle and then offset by the reflected shake range,
291+
# then randomly pick whether to mirror to the other side.
292+
self._current_dir = (
293+
self.shake_angle
294+
+ uniform(-self.shake_range, self.shake_range)
295+
+ (180 * randint(0, 1))
296+
)
279297

280298
_amp = self._calc_amplitude() * self.max_amplitude
281299
_vec = quaternion_rotation(self._data.forward, self._data.up, self._current_dir)
@@ -305,3 +323,10 @@ def readjust_camera(self) -> None:
305323
self._data.position[2] - self._last_vector[2],
306324
)
307325
self._last_vector = (0.0, 0.0, 0.0)
326+
327+
def __enter__(self):
328+
self.update_camera()
329+
330+
def __exit__(self, exc_type, exc_val, exc_tb):
331+
self.readjust_camera()
332+
return False

0 commit comments

Comments
 (0)