Skip to content

Commit cb2d43e

Browse files
committed
Input is being prototyped
1 parent deb5f07 commit cb2d43e

File tree

7 files changed

+139
-30
lines changed

7 files changed

+139
-30
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.10-Talos"
8+
version = "0.1.15-Jyggalag"
99

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

readme.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Console Java Engine - Version 0.1.10-Talos
1+
# Console Java Engine - Version 0.1.15-Talos
2+
[![](https://jitpack.io/v/CaptainSly/ConsoleJavaEngine.svg)](https://jitpack.io/#CaptainSly/ConsoleJavaEngine)
23

34
The Console Java Engine is based off of JavidX9's PixelGameEngine.
45

@@ -8,7 +9,7 @@ Requires Java 17+
89

910
## How to use
1011

11-
To use CJE, include it in your project, then create a class and extend the `ConsoleJavaEngine`. It provides a Skeleton for your project and also provides protected methods to write to the screen.
12+
To use CJE, include it in your project, then create a class and extend the `ConsoleJavaEngine`. It provides a Skeleton for your project. After that all you need to do is create different `Scene` classes for you game. The `ConsoleRenderer` class holds methods to write to the screen.
1213

1314
```Java
1415

@@ -23,36 +24,39 @@ public class Adventure extends ConsoleJavaEngine {
2324

2425
@Override
2526
protected void onGameCreate() {
26-
27+
setScene(new GameScreen());
2728
}
2829

29-
@Override
30-
protected void onGameUpdate(float deltaTime) {
30+
}
3131

32-
}
32+
import com.googlecode.lanterna.input.KeyStroke;
33+
import com.spireprod.cje.core.ConsoleRenderer;
34+
import com.spireprod.cje.core.scenes.Scene;
35+
36+
public class GameScene implements Scene {
3337

3438
@Override
35-
protected void onGameInput(float deltaTime, KeyStroke keyStroke) {
39+
public void onSceneUpdate(float delta) {
3640

3741
}
3842

3943
@Override
40-
protected void onGameRender(float alpha) {
44+
public void onSceneRender(ConsoleRenderer renderer) {
4145

4246
}
4347

44-
public static void main(String[] args) {
45-
Adventure venture = new Adventure();
46-
venture.run();
48+
@Override
49+
public void onSceneInput(float delta, KeyStroke key) {
50+
4751
}
4852

4953
}
5054

55+
5156
```
5257

5358

5459
### Jitpack
55-
[![](https://jitpack.io/v/CaptainSly/ConsoleJavaEngine.svg)](https://jitpack.io/#CaptainSly/ConsoleJavaEngine)
5660

5761
ConsoleJavaEngine is available through [Jitpack](https://jitpack.io). Here's what you want to use:
5862

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

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

3+
import java.awt.event.MouseEvent;
4+
import java.awt.event.MouseListener;
35
import java.awt.event.WindowEvent;
46
import java.awt.event.WindowListener;
57
import java.io.IOException;
@@ -8,14 +10,14 @@
810

911
import com.googlecode.lanterna.TerminalSize;
1012
import com.googlecode.lanterna.input.KeyStroke;
11-
import com.googlecode.lanterna.input.KeyType;
1213
import com.googlecode.lanterna.screen.Screen;
1314
import com.googlecode.lanterna.screen.TerminalScreen;
1415
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
1516
import com.googlecode.lanterna.terminal.Terminal;
1617
import com.googlecode.lanterna.terminal.TerminalResizeListener;
1718
import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame;
1819
import com.spireprod.cje.core.ConsoleRenderer;
20+
import com.spireprod.cje.core.input.Input;
1921
import com.spireprod.cje.core.scenes.Scene;
2022
import com.spireprod.cje.core.scenes.SceneManager;
2123

@@ -29,7 +31,7 @@ public abstract class ConsoleJavaEngine {
2931
protected Terminal terminal;
3032
protected SceneManager sceneManager;
3133
protected ConsoleRenderer renderer;
32-
protected KeyStroke termKey;
34+
protected Input input;
3335
protected SwingTerminalFrame frame;
3436
protected Screen screen;
3537
protected boolean isRunning = false;
@@ -39,7 +41,7 @@ public abstract class ConsoleJavaEngine {
3941
private final int targetFPS = 60;
4042
private final long optimalTime = (long) (1E9f / targetFPS);
4143

42-
public static final String CJE_VERSION = "0.1.15-Talos";
44+
public static final String CJE_VERSION = "0.1.15-Jyggalag";
4345

4446
public ConsoleJavaEngine(String title, int width, int height) {
4547
DefaultTerminalFactory defaultTermFactory = new DefaultTerminalFactory();
@@ -59,6 +61,7 @@ public ConsoleJavaEngine(String title, int width, int height) {
5961
termWidth = frame.getTerminalSize().getColumns();
6062
termHeight = frame.getTerminalSize().getRows();
6163

64+
input = new Input();
6265
sceneManager = new SceneManager();
6366
renderer = new ConsoleRenderer(screen.newTextGraphics());
6467

@@ -72,6 +75,34 @@ public void onResized(Terminal terminal, TerminalSize newSize) {
7275

7376
});
7477

78+
frame.addMouseListener(new MouseListener() {
79+
80+
@Override
81+
public void mouseClicked(MouseEvent e) {
82+
83+
}
84+
85+
@Override
86+
public void mousePressed(MouseEvent e) {
87+
88+
}
89+
90+
@Override
91+
public void mouseReleased(MouseEvent e) {
92+
93+
}
94+
95+
@Override
96+
public void mouseEntered(MouseEvent e) {
97+
98+
}
99+
100+
@Override
101+
public void mouseExited(MouseEvent e) {
102+
103+
}
104+
});
105+
75106
frame.addWindowListener(new WindowListener() {
76107

77108
@Override
@@ -139,8 +170,8 @@ private void onGameUpdate(float deltaTime) {
139170
sceneManager.sceneUpdate(deltaTime);
140171
}
141172

142-
private void onGameInput(float deltaTime, KeyStroke key) {
143-
sceneManager.sceneInput(deltaTime, key);
173+
private void onGameInput(float deltaTime, Input input) {
174+
sceneManager.sceneInput(deltaTime, input);
144175
}
145176

146177
private void onGameRender(ConsoleRenderer renderer) {
@@ -166,16 +197,16 @@ private void loop() throws IOException, InterruptedException {
166197
this.termHeight = newSize.getRows();
167198
}
168199

169-
termKey = screen.pollInput();
170-
171-
if (termKey != null && termKey.getKeyType().equals(KeyType.Escape))
172-
isRunning = false;
200+
KeyStroke termKey;
201+
while ((termKey = screen.pollInput()) != null)
202+
input.update(termKey);
173203

174-
if (termKey != null)
175-
onGameInput(delta, termKey);
204+
onGameInput(delta, input);
205+
input.endFrame();
176206

177207
onGameUpdate(delta);
178208

209+
renderer.setTextGraphics(screen.newTextGraphics());
179210
renderer.clearScreen();
180211

181212
onGameRender(renderer);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class ConsoleRenderer {
77

88
private TextGraphics graphics;
9-
9+
1010
public ConsoleRenderer(TextGraphics graphics) {
1111
this.graphics = graphics;
1212
}
@@ -44,4 +44,8 @@ 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 setTextGraphics(TextGraphics graphics) {
48+
this.graphics = graphics;
49+
}
50+
4751
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.spireprod.cje.core.input;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import com.googlecode.lanterna.input.KeyStroke;
7+
import com.googlecode.lanterna.input.KeyType;
8+
9+
public class Input {
10+
11+
private final Set<KeyStroke> currentKeys = new HashSet<>();
12+
private final Set<KeyStroke> previousKeys = new HashSet<>();
13+
14+
public void update(KeyStroke stroke) {
15+
if (stroke != null) {
16+
currentKeys.add(normalize(stroke));
17+
}
18+
}
19+
20+
public void endFrame() {
21+
previousKeys.clear();
22+
previousKeys.addAll(currentKeys);
23+
currentKeys.clear();
24+
}
25+
26+
// Check if key is currently held down
27+
public boolean isKeyDown(KeyType type) {
28+
return previousKeys.stream().anyMatch(k -> k.getKeyType() == type);
29+
}
30+
31+
public boolean isKeyDown(char c) {
32+
return previousKeys.stream().anyMatch(
33+
k -> k.getKeyType() == KeyType.Character && k.getCharacter() != null && k.getCharacter() == c);
34+
}
35+
36+
// Check if key was just pressed this frame
37+
public boolean isKeyPressed(KeyType type) {
38+
return currentKeys.stream().anyMatch(k -> k.getKeyType() == type)
39+
&& previousKeys.stream().noneMatch(k -> k.getKeyType() == type);
40+
}
41+
42+
public boolean isKeyPressed(char c) {
43+
return currentKeys.stream()
44+
.anyMatch(k -> k.getKeyType() == KeyType.Character && k.getCharacter() != null && k.getCharacter() == c)
45+
&& previousKeys.stream().noneMatch(
46+
k -> k.getKeyType() == KeyType.Character && k.getCharacter() != null && k.getCharacter() == c);
47+
}
48+
49+
// Check if key was released this frame
50+
public boolean isKeyReleased(KeyType type) {
51+
return previousKeys.stream().anyMatch(k -> k.getKeyType() == type)
52+
&& currentKeys.stream().noneMatch(k -> k.getKeyType() == type);
53+
}
54+
55+
public boolean isKeyReleased(char c) {
56+
return previousKeys.stream()
57+
.anyMatch(k -> k.getKeyType() == KeyType.Character && k.getCharacter() != null && k.getCharacter() == c)
58+
&& currentKeys.stream().noneMatch(
59+
k -> k.getKeyType() == KeyType.Character && k.getCharacter() != null && k.getCharacter() == c);
60+
}
61+
62+
// Normalize to avoid issues with Shift+char, etc.
63+
private KeyStroke normalize(KeyStroke stroke) {
64+
// Only consider key type + char, ignore modifiers for now
65+
if (stroke.getKeyType() == KeyType.Character && stroke.getCharacter() != null) {
66+
return new KeyStroke(Character.toLowerCase(stroke.getCharacter()), false, false);
67+
}
68+
return new KeyStroke(stroke.getKeyType());
69+
}
70+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.spireprod.cje.core.scenes;
22

3-
import com.googlecode.lanterna.input.KeyStroke;
43
import com.spireprod.cje.core.ConsoleRenderer;
4+
import com.spireprod.cje.core.input.Input;
55

66
public interface Scene {
77

88
void onSceneUpdate(float delta);
99

1010
void onSceneRender(ConsoleRenderer renderer);
1111

12-
void onSceneInput(float delta, KeyStroke key);
12+
void onSceneInput(float delta, Input input);
1313

1414
}

src/main/java/com/spireprod/cje/core/scenes/SceneManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.spireprod.cje.core.scenes;
22

3-
import com.googlecode.lanterna.input.KeyStroke;
43
import com.spireprod.cje.core.ConsoleRenderer;
4+
import com.spireprod.cje.core.input.Input;
55

66
public class SceneManager {
77

@@ -15,9 +15,9 @@ public void sceneUpdate(float deltaTime) {
1515
currentScene.onSceneUpdate(deltaTime);
1616
}
1717

18-
public void sceneInput(float deltaTime, KeyStroke key) {
18+
public void sceneInput(float deltaTime, Input input) {
1919
if (currentScene != null)
20-
currentScene.onSceneInput(deltaTime, key);
20+
currentScene.onSceneInput(deltaTime, input);
2121
}
2222

2323
public void sceneRender(ConsoleRenderer renderer) {

0 commit comments

Comments
 (0)