Skip to content

Commit ba960b3

Browse files
committed
Refactor game timing and Improve console log formatting with highlighting
1 parent 72e3fa0 commit ba960b3

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/com/redomar/game/Game.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,13 @@ public synchronized void stop() {
349349
*/
350350
public void run() {
351351
long lastTime = System.nanoTime();
352-
double nsPerTick = 1000000000D / 60D; // The number of nanoseconds in one tick (number of ticks limited to 60 per update)
352+
int nsPerS = 1_000_000_000;
353+
double nsPerTick = nsPerS / 60D; // The number of nanoseconds in one tick (number of ticks limited to 60 per update)
353354
// 1 billion nanoseconds in one second
354355
int ticks = 0;
355356
int frames = 0;
356357

357-
long lastTimer = System.currentTimeMillis(); // Used for updating ticks and frames once every second
358+
long lastTimer = System.nanoTime(); // Used for updating ticks and frames once every second
358359
double delta = 0;
359360

360361
init(); // Initialize the game environment
@@ -377,8 +378,8 @@ public void run() {
377378
render();
378379
}
379380

380-
if (System.currentTimeMillis() - lastTimer >= 1000) { // If elapsed time is greater than or equal to 1 second, update
381-
lastTimer += 1000; // Updates in another second
381+
if (System.nanoTime() - lastTimer >= nsPerS) { // If elapsed time is greater than or equal to 1 second, update
382+
lastTimer += nsPerS; // Updates in another second
382383
getFrame().setTitle("JavaGame - Version " + WordUtils.capitalize(game_Version).substring(1, game_Version.length()));
383384
fps = frames;
384385
tps = ticks;

src/com/redomar/game/entities/Player.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.redomar.game.event.InputHandler;
99
import com.redomar.game.level.LevelHandler;
1010
import com.redomar.game.lib.HashGen;
11-
import com.redomar.game.log.PrintTypes;
1211

1312
import java.util.Objects;
1413

@@ -83,7 +82,7 @@ public void tick() {
8382
// Continue with shooting logic
8483
shoot(x, y, dir, Game.getMouse().getButton());
8584

86-
entityPrinter.cast().print("Direction: " + dir + \t" + dx + "x\t" + dy + "y", PrintTypes.ERROR);
85+
entityPrinter.highlight().print("Direction: " + dir + \t" + dx + "x\t" + dy + "y");
8786
}
8887
}
8988
}

src/com/redomar/game/event/InputHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ private void toggleKey(int keyCode, boolean isPressed) {
135135

136136
private void quitGame() {
137137
Game.setClosing(true);
138-
if (!inputPrinter.removeLog()) System.err.println("Could not delete Log file");
139138
try {
140139
Thread.sleep(1000);
141140
} catch (InterruptedException e) {
142-
e.printStackTrace();
141+
inputPrinter.exception(e.getMessage());
143142
}
144143
Game.getLevel().removeEntity(Game.getPlayer().getName());
145144
Game.getGame().stop();
146145
Game.getFrame().dispose();
146+
if (!inputPrinter.removeLog()) System.err.println("Could not delete Log file");
147147
System.exit(0);
148148
}
149149

src/com/redomar/game/log/Printer.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Printer {
1313
private String message;
1414
private boolean evalTypeAsException = false;
1515
private boolean mute = false;
16+
private boolean highlight = false;
1617

1718
public Printer() {
1819
this.type = PrintTypes.GAME;
@@ -34,6 +35,9 @@ public void print(String message) {
3435
}
3536

3637
private void printOut() {
38+
String ANSI_RESET = "\u001B[0m";
39+
String ANSI_CYAN = "\u001B[36m";
40+
String ANSI_BACKGROUND_BLACK = "\u001B[40m";
3741
String msgTime = "[" + time.getTime() + "]";
3842
String msgType = "[" + type.toString() + "]";
3943

@@ -55,7 +59,11 @@ private void printOut() {
5559
logFile.log(String.format("%s%s\t%s", msgTime, msgType, message));
5660

5761
if (mute) return;
58-
String formattedStringForConsole = String.format("%s%s %s%n", msgType, msgTime, message);
62+
String formattedStringForConsole = String.format("%s %s\t %s%n", msgTime, msgType, message);
63+
if (highlight) {
64+
formattedStringForConsole = String.format("%s %s\t%s %s%s %s%n", msgTime, msgType, ANSI_BACKGROUND_BLACK, ANSI_CYAN, message, ANSI_RESET);
65+
highlight = false;
66+
}
5967

6068
if (type.equals(PrintTypes.ERROR) || evalTypeAsException) {
6169
System.err.printf(formattedStringForConsole);
@@ -73,6 +81,16 @@ private PrintToLog printToLogType(PrintTypes type) {
7381
}
7482
}
7583

84+
/**
85+
* Highlight the message in the console
86+
*
87+
* @return Printer
88+
*/
89+
public Printer highlight() {
90+
this.highlight = true;
91+
return this;
92+
}
93+
7694
public boolean removeLog() {
7795
return new File(".log.txt").delete();
7896
}
@@ -111,4 +129,6 @@ public Printer exception(String exceptionMessage) {
111129
print(exceptionMessage);
112130
return this;
113131
}
132+
133+
114134
}

0 commit comments

Comments
 (0)