create constants to reduce magic numbers
Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
d5bb96b29a
commit
2e9be3bf1b
@ -26,6 +26,9 @@ import java.awt.event.KeyEvent;
|
|||||||
|
|
||||||
public class RabiClone {
|
public class RabiClone {
|
||||||
public static final String PROGRAM_NAME = "RabiClone";
|
public static final String PROGRAM_NAME = "RabiClone";
|
||||||
|
public static final int UPDATE_LOOP_FRAMERATE = 244;
|
||||||
|
public static final long UPDATE_LOOP_NANOTIME = (long)((1d/UPDATE_LOOP_FRAMERATE)*1000000000l);
|
||||||
|
public static final double UPDATE_MULT = 1d / UPDATE_LOOP_FRAMERATE;
|
||||||
public static String BLANK = "\0";
|
public static String BLANK = "\0";
|
||||||
|
|
||||||
public static int UPCOUNT = 0;
|
public static int UPCOUNT = 0;
|
||||||
@ -92,8 +95,7 @@ public class RabiClone {
|
|||||||
dt += System.nanoTime() - lastGameTime;
|
dt += System.nanoTime() - lastGameTime;
|
||||||
lastGameTime = System.nanoTime();
|
lastGameTime = System.nanoTime();
|
||||||
|
|
||||||
while (dt >= (1 / 244d) * 1000000000l) {
|
while (dt >= UPDATE_LOOP_NANOTIME) {
|
||||||
final double updateMult = 1 / 244d;
|
|
||||||
handleGameControllers();
|
handleGameControllers();
|
||||||
|
|
||||||
KeyBind.poll();
|
KeyBind.poll();
|
||||||
@ -124,21 +126,25 @@ public class RabiClone {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < OBJ.size(); i++) {
|
for (int i = 0; i < OBJ.size(); i++) {
|
||||||
OBJ.get(i).update(updateMult);
|
OBJ.get(i).update(UPDATE_MULT);
|
||||||
if (OBJ.get(i).isMarkedForDeletion()) {
|
if (OBJ.get(i).isMarkedForDeletion()) {
|
||||||
OBJ.remove(i--);
|
OBJ.remove(i--);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dt -= (1 / 244d) * 1000000000l;
|
dt -= UPDATE_LOOP_NANOTIME;
|
||||||
TIME += (1 / 244d) * 1000000000l;
|
TIME += UPDATE_LOOP_NANOTIME;
|
||||||
// System.out.println(TIME);
|
// System.out.println(TIME);
|
||||||
}
|
}
|
||||||
if (dt < (1 / 244d) * 1000000000l) {
|
gameUpdateLoopStabilizer(dt); //This is hackish. Removing this slows down the game by about 30%. The timer runs slower. ???
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void gameUpdateLoopStabilizer(long dt) {
|
||||||
|
if (dt < UPDATE_LOOP_NANOTIME) {
|
||||||
lastReportedTime = System.currentTimeMillis();
|
lastReportedTime = System.currentTimeMillis();
|
||||||
} else {
|
} else {
|
||||||
if (System.currentTimeMillis() - lastReportedTime > 5000) {
|
if (System.currentTimeMillis() - lastReportedTime > 5000) {
|
||||||
System.out.println(
|
System.out.println("WARNING! Game is lagging behind! Frames Behind: " + (dt / UPDATE_LOOP_NANOTIME));
|
||||||
"WARNING! Game is lagging behind! Frames Behind: " + (dt / ((1 / 244d) * 1000000000l)));
|
|
||||||
lastReportedTime = System.currentTimeMillis();
|
lastReportedTime = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,8 +153,6 @@ public class RabiClone {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
//System.out.print(BLANK); //This is hackish. Removing this slows down the game by about 30%. The timer runs slower. ???
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void handleGameControllers() {
|
private static void handleGameControllers() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user