Coverage Summary for Class: CameraShake (com.mozarellabytes.kroy.Utilities)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
CameraShake | 0% (0/ 1) | 0% (0/ 4) | 0% (0/ 26) |
1 package com.mozarellabytes.kroy.Utilities;
2
3 import java.util.Random;
4 import com.badlogic.gdx.graphics.Camera;
5 import com.badlogic.gdx.math.Vector2;
6
7 /**
8 * Allows for Camera Shaking by calling this update() method every frame in the game screen
9 * and checking if shakeDuration is > 0, in which case the camera will "shake" for this duration.
10 */
11 public class CameraShake {
12
13 /** Array of floating point values used for X and Y axis offsetting. */
14 private static float[] samples;
15
16 /** Keeps track of time inside the update() method */
17 private static float internalTimer = 0;
18
19 /** Duration of camera shake in approximate seconds */
20 private static float shakeDuration = 0;
21
22 /** Used to calculate sampleCount */
23 private static final int amount = 5;
24
25 /** Used to calculate sampleCount and affect the smoothing of the camera shake */
26 private static final int frequency = 35;
27
28 /** Determines the size of "samples" */
29 private static int sampleCount;
30
31 /**
32 * Generates an array of random samples the size of duration * frequency used in the update() method
33 * to calculate new camera positions.
34 */
35 public CameraShake(){
36 sampleCount = amount * frequency;
37 samples = new float[sampleCount];
38
39 for (int i =0; i < sampleCount; i++){
40 Random rand = new Random();
41 samples[i] = rand.nextFloat() * 2f - 1f;
42 }
43 }
44
45 /**
46 * Calculates new camera positions using randomly generated samples (in the constructor)
47 * and rapidly offsets the camera by using those positions. The method is called every frame
48 * until shakeDuration reaches 0.
49 *
50 * Must be called every frame in the screen where camera shakes are supposed to happen.
51 *
52 * @param delta Time since last render
53 * @param camera Camera Object (To change its position)
54 * @param center Center of camera to allow it to return to its original position
55 */
56 public static void update(float delta, Camera camera, Vector2 center){
57 internalTimer += delta;
58
59 if (internalTimer > amount) {
60 internalTimer -= amount;
61 }
62
63 if (shakeDuration > 0){
64 shakeDuration -= delta;
65 float shakeTime = (internalTimer * frequency);
66 int first = (int)shakeTime;
67 int second = (first + 1)%sampleCount;
68 int third = (first + 2)%sampleCount;
69 float deltaT = shakeTime - (int)shakeTime;
70 float deltaX = samples[first] * deltaT + samples[second] * ( 1f - deltaT);
71 float deltaY = samples[second] * deltaT + samples[third] * ( 1f - deltaT);
72
73 boolean falloff = true;
74 float amplitude = 2;
75 camera.position.x = center.x + deltaX * amplitude * (falloff ? Math.min(shakeDuration, 1f) : 1f);
76 camera.position.y = center.y + deltaY * amplitude * (falloff ? Math.min(shakeDuration, 1f) : 1f);
77 camera.update();
78 }
79 }
80
81 /**
82 * Called in-game whenever a Camera Shake happens. Since duration > 0, the update()
83 * method begins shaking the camera
84 *
85 * @param seconds Duration of the Camera Shake
86 */
87 public void shakeIt(float seconds){
88 shakeDuration = seconds;
89 }
90 }