From 495fa2183fcb8bf8f387829540becd04407d0266 Mon Sep 17 00:00:00 2001 From: halotroop2288 Date: Mon, 24 Feb 2020 22:16:41 -0800 Subject: [PATCH] Added new timer. --- .../halotroop/litecraft/logic/Timer.java | 62 +++++++++++++++++++ .../com/github/hydos/ginger/Litecraft.java | 30 ++++----- 2 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/github/halotroop/litecraft/logic/Timer.java diff --git a/src/main/java/com/github/halotroop/litecraft/logic/Timer.java b/src/main/java/com/github/halotroop/litecraft/logic/Timer.java new file mode 100644 index 0000000..438da2b --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/logic/Timer.java @@ -0,0 +1,62 @@ +package com.github.halotroop.litecraft.logic; + +import java.util.*; + +/* + * @author Jack Wilsdon (Stack Exchange) + * https://codereview.stackexchange.com/questions/111855/ticker-for-game-timing + */ +public class Timer +{ + private double lastTick; + private double nextTick; + private int tickRate; + private Set tickListeners = new HashSet<>(); + + public Timer(int tickRate) + { this.tickRate = tickRate; } + + public void addTickListener(TickListener listener) + { tickListeners.add(listener); } + + public void removeTickListener(TickListener listener) + { tickListeners.remove(listener); } + + public void setTickRate(int tickRate) + { this.tickRate = tickRate; } + + public int getTickRate() + { return tickRate; } + + public void reset() + { + lastTick = 0; + nextTick = 0; + } + + public boolean tick() + { + long currentTime = System.currentTimeMillis(); + if (currentTime >= nextTick) + { + long targetTimeDelta = 1000L / tickRate; + if (lastTick == 0 || nextTick == 0) + { + lastTick = currentTime - targetTimeDelta; + nextTick = currentTime; + } + float deltaTime = (float) (currentTime - lastTick) / targetTimeDelta; + for (TickListener listener : tickListeners) + { listener.onTick(deltaTime); } + lastTick = currentTime; + nextTick = currentTime + targetTimeDelta; + return true; + } + return false; + } + + public interface TickListener + { + void onTick(float deltaTime); + } +} diff --git a/src/main/java/com/github/hydos/ginger/Litecraft.java b/src/main/java/com/github/hydos/ginger/Litecraft.java index 1d50728..27b3220 100644 --- a/src/main/java/com/github/hydos/ginger/Litecraft.java +++ b/src/main/java/com/github/hydos/ginger/Litecraft.java @@ -2,6 +2,8 @@ package com.github.hydos.ginger; import java.util.*; +import com.github.halotroop.litecraft.logic.Timer; +import com.github.halotroop.litecraft.logic.Timer.TickListener; import com.github.halotroop.litecraft.types.block.*; import com.github.hydos.ginger.engine.api.*; import com.github.hydos.ginger.engine.api.game.*; @@ -14,7 +16,6 @@ import com.github.hydos.ginger.engine.io.Window; import com.github.hydos.ginger.engine.math.vectors.*; import com.github.hydos.ginger.engine.obj.ModelLoader; import com.github.hydos.ginger.engine.obj.shapes.StaticCube; -import com.github.hydos.ginger.engine.particle.*; import com.github.hydos.ginger.engine.render.MasterRenderer; import com.github.hydos.ginger.engine.render.models.TexturedModel; import com.github.hydos.ginger.engine.utils.Loader; @@ -26,7 +27,14 @@ public class Litecraft extends Game{ private boolean isInWorld = false; - private ParticleSystem system; + Timer timer; + TickListener tickListener = new TickListener() + { + public void onTick(float deltaTime) + { + + }; + }; public Litecraft() { super(); @@ -88,10 +96,7 @@ public class Litecraft extends Game{ Light sun = new Light(new Vector3f(100,105,-100), new Vector3f(1.3f, 1.3f, 1.3f), new Vector3f(0.0001f, 0.0001f, 0.0001f)); data.lights.add(sun); - data.entities.add(player); - - system = setupParticles(); - + data.entities.add(player); TextureButton playButton = ginger3D.registerButton("/textures/guis/purpur.png", new Vector2f(0, 0), new Vector2f(0.25f, 0.1f)); playButton.show(data.guis); @@ -104,19 +109,6 @@ public class Litecraft extends Game{ ginger3D.startGame(); } - - private ParticleSystem setupParticles() { - ParticleTexture particleTexture = new ParticleTexture(Loader.loadTexture("particles/smoke.png"), 8); - - system = new ParticleSystem(particleTexture, 100, 10f, 0.3f, 4, 3f); - system.randomizeRotation(); - system.setDirection(new Vector3f(0,0.001f,0), 0.00001f); - system.setLifeError(0); - system.setSpeedError(0); - system.setScaleError(1f); - return system; - } - //temp stuff to test out fbo fixes int oldWindowWidth = Window.width; int oldWindowHeight = Window.height;