Added new timer.

pull/7/head
Caroline Bell 2020-02-24 22:16:41 -08:00
parent 1fe45349ce
commit 495fa2183f
2 changed files with 73 additions and 19 deletions

View File

@ -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<TickListener> 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);
}
}

View File

@ -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;