Added new timer.
parent
1fe45349ce
commit
495fa2183f
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
@ -90,9 +98,6 @@ public class Litecraft extends Game{
|
|||
|
||||
data.entities.add(player);
|
||||
|
||||
system = setupParticles();
|
||||
|
||||
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue