Skip to content

Commit 8874def

Browse files
committed
Changed Font, working on quite a bit more
1 parent e38de31 commit 8874def

File tree

12 files changed

+612
-65
lines changed

12 files changed

+612
-65
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "com.spireprod"
8-
version = "0.1.15-Jyggalag"
8+
version = "0.1.20-Jyggalag"
99

1010
repositories {
1111
// Use Maven Central for resolving dependencies.

src/main/java/com/spireprod/cje/ConsoleJavaEngine.java

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.spireprod.cje;
22

3+
import java.awt.Font;
4+
import java.awt.FontFormatException;
5+
import java.awt.GraphicsEnvironment;
36
import java.awt.event.WindowEvent;
47
import java.awt.event.WindowListener;
8+
import java.io.File;
59
import java.io.IOException;
10+
import java.io.InputStream;
611

712
import javax.swing.JFrame;
813

@@ -13,51 +18,85 @@
1318
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
1419
import com.googlecode.lanterna.terminal.Terminal;
1520
import com.googlecode.lanterna.terminal.TerminalResizeListener;
21+
import com.googlecode.lanterna.terminal.swing.SwingTerminalFontConfiguration;
1622
import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame;
1723
import com.spireprod.cje.core.ConsoleRenderer;
1824
import com.spireprod.cje.core.input.Input;
1925
import com.spireprod.cje.core.scenes.AbstractScene;
2026
import com.spireprod.cje.core.scenes.SceneManager;
21-
import com.spireprod.cje.core.ui.TextUI;
27+
import com.spireprod.cje.core.ui.UIContext;
2228

2329
public abstract class ConsoleJavaEngine {
2430

25-
protected String PIXEL_BLOCK = "\u2588";
26-
protected String PIXEL_SHADE = "\u2591";
27-
protected String PIXEL_SHADE_FULL = "\u2593";
28-
protected String PIXEL_SHADE_HALF = "\u2592";
31+
public static final String PIXEL_BLOCK = "\u2588";
32+
public static final String PIXEL_SHADE = "\u2591";
33+
public static final String PIXEL_SHADE_FULL = "\u2593";
34+
public static final String PIXEL_SHADE_HALF = "\u2592";
35+
public static int termWidth, termHeight;
2936

3037
protected Terminal terminal;
3138
protected SceneManager sceneManager;
32-
protected TextUI ctx;
39+
protected UIContext ctx;
3340
protected ConsoleRenderer renderer;
3441
protected Input input;
3542
protected SwingTerminalFrame frame;
3643
protected Screen screen;
3744
protected boolean isRunning = false;
3845

39-
private int termWidth, termHeight;
40-
4146
private final int targetFPS = 60;
4247
private final long optimalTime = (long) (1E9f / targetFPS);
4348

44-
public static final String CJE_VERSION = "0.1.15-Jyggalag";
49+
public static final String CJE_VERSION = "0.1.20-Jyggalag";
50+
51+
public static String DATA_FOLDER;
52+
public static final String[] DATA_FLDRS = { "Scripts/", "Saves/", "Maps/", "Config/" };
4553

4654
public ConsoleJavaEngine(String title, int width, int height) {
55+
DATA_FOLDER = title.replace(" ", "_").trim() + File.separator;
56+
57+
// Make Sure Data Folder Exists, if not, create it.
58+
File dataFolder = new File(DATA_FOLDER);
59+
if (!dataFolder.exists()) {
60+
dataFolder.mkdir();
61+
62+
for (String folder : DATA_FLDRS)
63+
new File(DATA_FOLDER + folder).mkdirs();
64+
65+
}
66+
4767
DefaultTerminalFactory defaultTermFactory = new DefaultTerminalFactory();
68+
69+
// Setup Default Font 'Ubuntu Mono'
70+
Font font = null;
71+
try {
72+
InputStream is = ConsoleJavaEngine.class.getResourceAsStream("/ascii-sector-16x16-tileset.ttf");
73+
font = Font.createFont(Font.TRUETYPE_FONT, is);
74+
is.close();
75+
} catch (FontFormatException | IOException e) {
76+
e.printStackTrace();
77+
}
78+
79+
if (font == null)
80+
font = new Font(Font.MONOSPACED, Font.PLAIN, 16);
81+
82+
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
83+
4884
defaultTermFactory.setTerminalEmulatorTitle(title + " -" + CJE_VERSION)
85+
.setTerminalEmulatorFontConfiguration(
86+
SwingTerminalFontConfiguration.newInstance(font.deriveFont(Font.PLAIN, 16)))
4987
.setInitialTerminalSize(new TerminalSize(width, height)).setForceAWTOverSwing(false)
5088
.setPreferTerminalEmulator(true);
5189

5290
try {
5391
terminal = defaultTermFactory.createSwingTerminal();
5492
frame = (SwingTerminalFrame) terminal;
93+
frame.setResizable(false);
5594
frame.pack();
5695
frame.setLocationRelativeTo(null);
5796
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
5897
frame.setVisible(true);
5998

60-
ctx = new TextUI();
99+
ctx = new UIContext();
61100
screen = new TerminalScreen(terminal);
62101
termWidth = frame.getTerminalSize().getColumns();
63102
termHeight = frame.getTerminalSize().getRows();
@@ -66,6 +105,9 @@ public ConsoleJavaEngine(String title, int width, int height) {
66105
sceneManager = new SceneManager();
67106
renderer = new ConsoleRenderer(screen.newTextGraphics());
68107

108+
ctx.setConsoleRenderer(renderer);
109+
ctx.setInput(input);
110+
69111
frame.addResizeListener(new TerminalResizeListener() {
70112

71113
@Override
@@ -150,7 +192,7 @@ private void onGameInput(float deltaTime, Input input) {
150192
sceneManager.sceneInput(deltaTime, input);
151193
}
152194

153-
private void onGameUIRender(TextUI ctx, ConsoleRenderer renderer, Input input) {
195+
private void onGameUIRender(UIContext ctx, ConsoleRenderer renderer, Input input) {
154196
sceneManager.sceneUIRender(ctx, renderer, input);
155197
}
156198

@@ -165,6 +207,7 @@ private void loop() throws IOException, InterruptedException {
165207
screen.setCursorPosition(null);
166208

167209
while (isRunning) {
210+
input.beginFrame();
168211
long now = System.nanoTime();
169212
long updateLength = now - lastLoopTime;
170213
lastLoopTime = now;
@@ -173,8 +216,8 @@ private void loop() throws IOException, InterruptedException {
173216

174217
TerminalSize newSize = screen.doResizeIfNecessary();
175218
if (newSize != null) {
176-
this.termWidth = newSize.getColumns();
177-
this.termHeight = newSize.getRows();
219+
termWidth = newSize.getColumns();
220+
termHeight = newSize.getRows();
178221
}
179222

180223
KeyStroke termKey;
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.spireprod.cje.core;
2+
3+
import java.util.Random;
4+
5+
public class Camera {
6+
private float x, y; // top-left world coordinate of the view
7+
private final int viewWidth, viewHeight;
8+
9+
// Panning
10+
private float targetX, targetY;
11+
private float panSpeed = 0;
12+
private boolean isPanning = false;
13+
14+
// Shake
15+
private float shakeTimer = 0;
16+
private float shakeMagnitude = 0;
17+
private final Random rng = new Random();
18+
19+
public Camera(int viewWidth, int viewHeight) {
20+
this.viewWidth = viewWidth;
21+
this.viewHeight = viewHeight;
22+
}
23+
24+
// Center immediately on a world coordinate
25+
public void centerOn(float worldX, float worldY) {
26+
x = worldX - viewWidth / 2f;
27+
y = worldY - viewHeight / 2f;
28+
isPanning = false;
29+
}
30+
31+
// Smoothly pan to a world position (centered)
32+
public void panTo(float worldX, float worldY, float speed) {
33+
targetX = worldX - viewWidth / 2f;
34+
targetY = worldY - viewHeight / 2f;
35+
panSpeed = speed;
36+
isPanning = true;
37+
}
38+
39+
// Shake the camera for a short time
40+
public void shake(float duration, float magnitude) {
41+
shakeTimer = duration;
42+
shakeMagnitude = magnitude;
43+
}
44+
45+
public void update(float deltaSeconds) {
46+
if (Float.isNaN(x) || Float.isNaN(y)) {
47+
System.err.println("Warning: NaN in camera coordinates. Resetting.");
48+
x = 0;
49+
y = 0;
50+
}
51+
52+
// Pan logic
53+
if (isPanning) {
54+
float t = clamp(deltaSeconds * panSpeed, 0f, 1f);
55+
x = lerp(x, targetX, t);
56+
y = lerp(y, targetY, t);
57+
58+
if (Math.abs(x - targetX) < 0.1f && Math.abs(y - targetY) < 0.1f) {
59+
x = targetX;
60+
y = targetY;
61+
isPanning = false;
62+
}
63+
}
64+
65+
// Shake logic
66+
if (shakeTimer > 0) {
67+
shakeTimer -= deltaSeconds;
68+
}
69+
}
70+
71+
public int getViewX() {
72+
return (int) x + getShakeOffset();
73+
}
74+
75+
public int getViewY() {
76+
return (int) y + getShakeOffset();
77+
}
78+
79+
public int worldToScreenX(int worldX) {
80+
return worldX - getViewX();
81+
}
82+
83+
public int worldToScreenY(int worldY) {
84+
return worldY - getViewY();
85+
}
86+
87+
private int getShakeOffset() {
88+
return (shakeTimer > 0) ? rng.nextInt((int) (shakeMagnitude * 2 + 1)) - (int) shakeMagnitude : 0;
89+
}
90+
91+
private float lerp(float a, float b, float t) {
92+
return a + (b - a) * clamp(t, 0f, 1f);
93+
}
94+
95+
private float clamp(float value, float min, float max) {
96+
return Math.max(min, Math.min(max, value));
97+
}
98+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.spireprod.cje.core;
2+
3+
import com.googlecode.lanterna.TextColor.RGB;
4+
5+
public class Colors {
6+
7+
// Reds
8+
public static RGB RED = new RGB(255, 0, 0);
9+
public static RGB RUBY_RED = new RGB(155, 17, 30);
10+
public static RGB SCARLET = new RGB(255, 36, 0);
11+
12+
// Maroons
13+
14+
// Browns
15+
16+
public static RGB PULLMAN_BROWN = new RGB(100, 65, 23);
17+
18+
// Tans
19+
20+
// Oranges
21+
22+
// Peaches
23+
24+
// Golds
25+
26+
// Yellows
27+
28+
// Limes
29+
30+
// Olives
31+
32+
// Greens
33+
34+
public static RGB GREEN = new RGB(0, 255, 0);
35+
public static RGB LAWN_GREEN = new RGB(124, 252, 0);
36+
public static RGB PHTHALO_GREEN = new RGB(18, 53, 36);
37+
38+
// Teals
39+
40+
// Cyans
41+
42+
// Blues
43+
public static RGB BLUE = new RGB(0, 0, 255);
44+
public static RGB DODGER_BLUE = new RGB(30, 144, 255);
45+
46+
// Navys
47+
48+
// Purples
49+
50+
public static RGB PALANTINE_PURPLE = new RGB(104, 40, 96);
51+
public static RGB PURPLE_TAUPE = new RGB(80, 64, 77);
52+
public static RGB TYRIAN_PURPLE = new RGB(102, 2, 60);
53+
54+
// Magentas
55+
56+
// Pinks
57+
58+
// Grays
59+
public static RGB GRAY = new RGB(128, 128, 128);
60+
61+
// Silvers
62+
public static RGB SILVER = new RGB(192, 192, 192);
63+
public static RGB SILVER_CHALICE = new RGB(172, 172, 172);
64+
65+
// Whites
66+
public static RGB WHITE = new RGB(255, 255, 255);
67+
public static RGB GHOST_WHITE = new RGB(248, 248, 255);
68+
public static RGB SNOW = new RGB(255, 250, 250);
69+
70+
// Blacks
71+
public static RGB BLACK = new RGB(0, 0, 0);
72+
public static RGB VAMPIRE_BLACK = new RGB(8, 8, 8);
73+
74+
}

src/main/java/com/spireprod/cje/core/ConsoleRenderer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ public void writeString(String str, int x, int y) {
4444
writeString(str, x, y, TextColor.ANSI.BLACK, TextColor.ANSI.WHITE);
4545
}
4646

47+
public void putChar(char c, int x, int y, TextColor textColor, TextColor backgroundColor) {
48+
if (textColor == null)
49+
textColor = TextColor.ANSI.BLACK;
50+
51+
graphics.setBackgroundColor(backgroundColor);
52+
graphics.setForegroundColor(textColor);
53+
graphics.setCharacter(x, y, c);
54+
}
55+
56+
public void putChar(char c, int x, int y, TextColor textColor) {
57+
graphics.setForegroundColor(textColor);
58+
graphics.setCharacter(x, y, c);
59+
}
60+
4761
public void setTextGraphics(TextGraphics graphics) {
4862
this.graphics = graphics;
4963
}

0 commit comments

Comments
 (0)