Skip to content

Commit bb24b82

Browse files
Merge pull request #82 from ArtifactForms/working2
Missing old stuff
2 parents 5cc3972 + e16f9d1 commit bb24b82

2 files changed

Lines changed: 245 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package engine.demos.voxels;
2+
3+
import engine.components.AbstractComponent;
4+
import engine.components.RenderableComponent;
5+
import engine.demos.voxels.client.event.EventManager;
6+
import engine.demos.voxels.client.event.MessageSentEvent;
7+
import engine.input.Input;
8+
import engine.input.Key;
9+
import engine.input.KeyEvent;
10+
import engine.input.KeyListener;
11+
import engine.resources.Font;
12+
import engine.scene.camera.Camera;
13+
import math.Color;
14+
import workspace.ui.Graphics;
15+
16+
public class ChatComponent extends AbstractComponent implements KeyListener, RenderableComponent {
17+
18+
private static final int MAX_MESSAGE_LENGTH = 0;
19+
private StringBuffer textBuffer;
20+
private boolean textInputEnabled;
21+
private Input input;
22+
private Camera camera;
23+
private String message = "";
24+
private int cursorPosition;
25+
private EventManager eventManager;
26+
27+
public ChatComponent(Input input, Camera camera, EventManager eventManager) {
28+
this.input = input;
29+
this.camera = camera;
30+
this.textBuffer = new StringBuffer();
31+
this.cursorPosition = 0;
32+
this.eventManager = eventManager;
33+
input.addKeyListener(this);
34+
}
35+
36+
@Override
37+
public void onAttach() {}
38+
39+
@Override
40+
public void onDetach() {}
41+
42+
@Override
43+
public void onUpdate(float tpf) {
44+
message = textBuffer.toString();
45+
}
46+
47+
@Override
48+
public void render(Graphics g) {
49+
if (!textInputEnabled) return;
50+
51+
Font font = new Font("monogram-extended", 32, Font.PLAIN);
52+
int y = g.getHeight() - 20;
53+
int cursorWidth = 10;
54+
int offsetX = 20;
55+
g.setColor(Color.YELLOW);
56+
57+
g.setFont(font);
58+
g.text(message, 20, g.getHeight() - offsetX);
59+
g.setColor(Color.WHITE);
60+
61+
float pos = g.textWidth(message);
62+
g.drawLine(pos + offsetX, y, pos + offsetX + cursorWidth, y);
63+
}
64+
65+
@Override
66+
public void onKeyPressed(KeyEvent e) {
67+
if (!shouldProcess(e)) return;
68+
processEvent(e);
69+
}
70+
71+
private boolean shouldProcess(KeyEvent e) {
72+
return e.getKey() == Key.BACKSPACE || e.getKey() == Key.ENTER || e.getKey() == Key.DELETE;
73+
}
74+
75+
@Override
76+
public void onKeyReleased(KeyEvent e) {}
77+
78+
private void processEvent(KeyEvent e) {
79+
if (!textInputEnabled) return;
80+
81+
if (e.getKey() == Key.BACKSPACE) {
82+
if (cursorPosition > 0) {
83+
textBuffer.deleteCharAt(cursorPosition - 1);
84+
cursorPosition--;
85+
}
86+
} else if (e.getKey() == Key.DELETE) {
87+
if (cursorPosition < textBuffer.length()) {
88+
textBuffer.deleteCharAt(cursorPosition);
89+
}
90+
} else if (e.getKey() == Key.ENTER) {
91+
String messageToSend = textBuffer.toString();
92+
sendMessage(messageToSend); // Trigger the event here
93+
textInputEnabled = false;
94+
textBuffer.delete(0, textBuffer.length());
95+
cursorPosition = 0;
96+
} else {
97+
textBuffer.insert(cursorPosition, e.getChar());
98+
cursorPosition++;
99+
}
100+
}
101+
102+
@Override
103+
public void onKeyTyped(KeyEvent e) {
104+
if (e.getKey() != Key.BACKSPACE && e.getKey() != Key.DELETE) {
105+
processEvent(e);
106+
}
107+
if (e.getKey() == Key.T && !textInputEnabled) {
108+
textInputEnabled = !textInputEnabled;
109+
}
110+
}
111+
112+
// Method to send the message and trigger the event
113+
private void sendMessage(String message) {
114+
MessageSentEvent messageSentEvent = new MessageSentEvent(message);
115+
eventManager.triggerEvent(messageSentEvent);
116+
}
117+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package engine.demos.voxels;
2+
3+
import engine.components.AbstractComponent;
4+
import engine.components.RenderableComponent;
5+
import engine.scene.camera.Camera;
6+
import math.Color;
7+
import math.Mathf;
8+
import math.Ray3f;
9+
import math.Vector3f;
10+
import mesh.Mesh3D;
11+
import mesh.creator.primitives.CubeCreator;
12+
import workspace.ui.Graphics;
13+
14+
public class RayBlockDetector extends AbstractComponent implements RenderableComponent {
15+
16+
private Camera camera;
17+
private Ray3f ray;
18+
private Vector3f hitBlock; // Coordinates of the block hit
19+
private boolean blockHit; // If the ray hit a block
20+
21+
private World voxelWorld;
22+
private final Mesh3D highlightCube = new CubeCreator(0.5f).create();
23+
private TextDisplay display;
24+
25+
public RayBlockDetector(Camera camera, World voxelWorld, TextDisplay display) {
26+
this.camera = camera;
27+
this.voxelWorld = voxelWorld;
28+
this.ray = new Ray3f(new Vector3f(), new Vector3f());
29+
this.blockHit = false;
30+
this.display = display;
31+
}
32+
33+
@Override
34+
public void render(Graphics g) {
35+
g.disableDepthTest();
36+
37+
if (blockHit) {
38+
g.pushMatrix();
39+
g.setColor(Color.YELLOW);
40+
g.translate(hitBlock.x, -hitBlock.y, hitBlock.z);
41+
g.drawFaces(highlightCube);
42+
g.popMatrix();
43+
44+
BlockType type = voxelWorld.getBlock((int) hitBlock.x, (int) hitBlock.y, (int) hitBlock.z);
45+
display.setText("Hit: " + hitBlock + ", Type: " + type + ", Origin: " + ray.getOrigin());
46+
g.setColor(Color.CYAN);
47+
g.drawLine(
48+
ray.getOrigin().x,
49+
ray.getOrigin().y,
50+
ray.getOrigin().z,
51+
hitBlock.x + 0.5f,
52+
hitBlock.y + 0.5f,
53+
hitBlock.z + 0.5f);
54+
} else {
55+
display.setText("Hit: NONE");
56+
}
57+
58+
g.enableDepthTest();
59+
}
60+
61+
@Override
62+
public void onUpdate(float tpf) {
63+
Vector3f origin = camera.getTransform().getPosition();
64+
Vector3f direction = camera.getTransform().getForward();
65+
ray = new Ray3f(origin, direction);
66+
67+
detectBlock();
68+
}
69+
70+
private void detectBlock() {
71+
Vector3f rayOrigin = ray.getOrigin();
72+
Vector3f rayDir = ray.getDirection();
73+
74+
int x = (int) Math.floor(rayOrigin.x);
75+
int y = (int) Math.floor(rayOrigin.y);
76+
int z = (int) Math.floor(rayOrigin.z);
77+
78+
float stepX = Math.signum(rayDir.x);
79+
float stepY = Math.signum(rayDir.y);
80+
float stepZ = Math.signum(rayDir.z);
81+
82+
float tMaxX = intBound(rayOrigin.x, rayDir.x);
83+
float tMaxY = intBound(rayOrigin.y, rayDir.y);
84+
float tMaxZ = intBound(rayOrigin.z, rayDir.z);
85+
86+
float tDeltaX = Math.abs(1 / rayDir.x);
87+
float tDeltaY = Math.abs(1 / rayDir.y);
88+
float tDeltaZ = Math.abs(1 / rayDir.z);
89+
90+
for (int i = 0; i < 500; i++) {
91+
if (voxelWorld.isBlockAt(x, -y, z)) {
92+
hitBlock = new Vector3f(x, -y, z);
93+
blockHit = true;
94+
return;
95+
}
96+
97+
if (tMaxX < tMaxY && tMaxX < tMaxZ) {
98+
tMaxX += tDeltaX;
99+
x += stepX;
100+
} else if (tMaxY < tMaxZ) {
101+
tMaxY += tDeltaY;
102+
y += stepY;
103+
} else {
104+
tMaxZ += tDeltaZ;
105+
z += stepZ;
106+
}
107+
}
108+
109+
blockHit = false;
110+
}
111+
112+
private float intBound(float s, float ds) {
113+
if (ds == 0) return Float.POSITIVE_INFINITY;
114+
return (ds > 0 ? Mathf.ceil(s) - s : s - Mathf.floor(s)) / Mathf.abs(ds);
115+
}
116+
117+
@Override
118+
public void onAttach() {
119+
// TODO Auto-generated method stub
120+
121+
}
122+
123+
@Override
124+
public void onDetach() {
125+
// TODO Auto-generated method stub
126+
127+
}
128+
}

0 commit comments

Comments
 (0)