Skip to content

Commit

Permalink
feat: camera shake
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Sep 25, 2023
1 parent 40d433f commit a03e3a7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
42 changes: 42 additions & 0 deletions core/src/me/vinceh121/wanderer/Wanderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.bullet.dynamics.btDiscreteDynamicsWorld;
import com.badlogic.gdx.utils.Align;
Expand Down Expand Up @@ -46,6 +47,7 @@
import me.vinceh121.wanderer.i18n.I18N;
import me.vinceh121.wanderer.input.Input;
import me.vinceh121.wanderer.input.InputListenerAdapter;
import me.vinceh121.wanderer.math.EllipsePath;
import me.vinceh121.wanderer.modding.ModManager;
import me.vinceh121.wanderer.platform.audio.Sound3D;
import me.vinceh121.wanderer.script.JsGame;
Expand Down Expand Up @@ -99,6 +101,7 @@ public class Wanderer extends ApplicationDelegate {
private Subtitle subtitle;

private float timeOfDay, elapsedTimeOfDay, dayDuration = 15800f;
private float cameraShakeIntensity, cameraShakeTime, cameraShakeRevolutionTime;

public Wanderer(ApplicationMultiplexer applicationMultiplexer) {
super(applicationMultiplexer);
Expand Down Expand Up @@ -337,6 +340,8 @@ public void render() {
}
}

this.processCameraShake(delta);

this.graphicsManager.clear();
this.graphicsManager.renderSkybox(this.timeOfDay);

Expand Down Expand Up @@ -386,24 +391,61 @@ public void render() {

protected void flushEntityQueue() {
this.entities.removeAll(this.toRemove, true);

for (final AbstractEntity e : this.toRemove) {
this.toAdd.removeValue(e, true);
e.leaveBtWorld(this.physicsManager.getBtWorld());

if (e instanceof IClanMember) {
for (final Clan c : this.clans) {
c.removeMember((IClanMember) e);
}
}
}

this.toRemove.clear();

this.entities.addAll(this.toAdd);

for (final AbstractEntity e : this.toAdd) {
e.enterBtWorld(this.physicsManager.getBtWorld());
}

this.toAdd.clear();
}

public void shakeCamera(float intensity, float time) {
this.shakeCamera(intensity, time, 0.25f);
}

public void shakeCamera(float intensity, float time, float revolutionTime) {
this.cameraShakeIntensity = intensity;
this.cameraShakeTime = time;
this.cameraShakeRevolutionTime = revolutionTime;
}

private void processCameraShake(float delta) {
// should be a percentage
float cameraShakeModifier = Preferences.getPreferences().getOrElse("a11y.cameraShake", 1.0).floatValue();

if (cameraShakeModifier == 0 || this.cameraShakeTime == 0) {
return;
}

final PerspectiveCamera cam = this.graphicsManager.getCamera();

EllipsePath path = new EllipsePath(0f, 0f, 0.5f * this.cameraShakeIntensity, 0.2f * this.cameraShakeIntensity);
path.y = path.height / 2;
Vector2 shakeVec2 = path.valueAt(new Vector2(), this.cameraShakeTime / this.cameraShakeRevolutionTime);
Vector3 shakeVec3 = new Vector3(shakeVec2, 0);
shakeVec3.rot(cam.combined);
cam.position.add(shakeVec3);

cam.update();

this.cameraShakeTime = Math.max(0, this.cameraShakeTime - delta);
}

public void addEntity(final AbstractEntity e) {
this.toAdd.add(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void fire() {

this.fireSoundEmitter.play();
this.fireTimeout = 0.085f;

this.game.shakeCamera(0.5f, 0.25f);
}

private void fireTurret(final MachineGunTurret turret) {
Expand Down
69 changes: 69 additions & 0 deletions core/src/me/vinceh121/wanderer/math/EllipsePath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.vinceh121.wanderer.math;

import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Ellipse;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Path;
import com.badlogic.gdx.math.Vector2;

public class EllipsePath extends Ellipse implements Path<Vector2> {
private static final long serialVersionUID = 9035881773211510268L;

public EllipsePath() {
}

public EllipsePath(Circle circle) {
super(circle);
}

public EllipsePath(Ellipse ellipse) {
super(ellipse);
}

public EllipsePath(float x, float y, float width, float height) {
super(x, y, width, height);
}

public EllipsePath(Vector2 position, float width, float height) {
super(position, width, height);
}

public EllipsePath(Vector2 position, Vector2 size) {
super(position, size);
}

@Override
public Vector2 derivativeAt(Vector2 out, float t) {
throw new UnsupportedOperationException("EllipsePath#derivativeAt(out, t) is not implemented");
}

@Override
public Vector2 valueAt(Vector2 out, float t) {
final float rad = t * MathUtils.PI2;

return this.valueAtRad(out, rad);
}

public Vector2 valueAtRad(Vector2 out, float rad) {
out.set(this.width * MathUtils.cos(rad), this.height * MathUtils.sin(rad));
out.add(this.x, this.y);

return out;
}

@Override
public float approximate(Vector2 v) {
throw new UnsupportedOperationException("EllipsePath#approximate(v) is not implemented");
}

@Override
public float locate(Vector2 v) {
throw new UnsupportedOperationException("EllipsePath#locate(v) is not implemented");
}

@Override
public float approxLength(int samples) {
throw new UnsupportedOperationException("EllipsePath#approxLength(samples) is not implemented");
}

}
2 changes: 1 addition & 1 deletion core/src/me/vinceh121/wanderer/util/MathUtilsW.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static float getSphericalPolar(float z) {
}

public static float getSphericalAzimuth(float x, float y) {
return Math.signum(y) * MathUtils.acos(x / (float)(Math.sqrt(x * x + y * y)));
return Math.signum(y) * MathUtils.acos(x / (float) (Math.sqrt(x * x + y * y)));
}

public static Vector3 fixNaN(final Vector3 v, final float val) {
Expand Down

0 comments on commit a03e3a7

Please sign in to comment.