-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.java
More file actions
172 lines (148 loc) · 6.13 KB
/
Snake.java
File metadata and controls
172 lines (148 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package Project;
import org.jogamp.java3d.*;
import org.jogamp.java3d.utils.geometry.Box;
import org.jogamp.java3d.utils.geometry.ColorCube;
import org.jogamp.vecmath.*;
import java.awt.AWTEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class Snake {
private TransformGroup snakeTG;
private ArrayList<TransformGroup> segments;
private Vector3f headPosition;
private Vector3f direction;
private final float FIXED_Y = -0.4f;
public Snake(Vector3f startPosition) {
snakeTG = new TransformGroup();
snakeTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
snakeTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
segments = new ArrayList<>();
headPosition = new Vector3f(startPosition);
headPosition.y = FIXED_Y;
direction = new Vector3f(0f, 0f, 0.01f);
TransformGroup headSegment = createSegment(headPosition);
segments.add(headSegment);
snakeTG.addChild(headSegment);
}
private TransformGroup createSegment(Vector3f pos) {
TransformGroup segTG = new TransformGroup();
segTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D t3d = new Transform3D();
t3d.setTranslation(pos);
segTG.setTransform(t3d);
// Replace the ColorCube with a custom green cube.
segTG.addChild(createGreenCube(0.1f));
return segTG;
}
private Node createGreenCube(float halfSize) {
// Create a Box with the desired half extents and set its appearance to green.
Appearance greenApp = new Appearance();
ColoringAttributes ca = new ColoringAttributes(new Color3f(0f, 1f, 0f), ColoringAttributes.NICEST);
greenApp.setColoringAttributes(ca);
// Box takes half extents (so 0.1f means the full cube is 0.2 units on a side).
Box box = new Box(halfSize, halfSize, halfSize, greenApp);
return box;
}
public void move() {
ArrayList<Vector3f> prevPositions = new ArrayList<>();
for (TransformGroup seg : segments) {
Transform3D t3d = new Transform3D();
seg.getTransform(t3d);
Vector3f pos = new Vector3f();
t3d.get(pos);
prevPositions.add(pos);
}
// Update head position using the current direction
headPosition.add(direction);
headPosition.y = FIXED_Y;
// Border collision check
if (headPosition.x < -5f || headPosition.x > 5f ||
headPosition.z < -5f || headPosition.z > 5f) {
System.out.println("Game Over: Snake hit the border!");
if (MainApp.instance != null) {
MainApp.instance.showGameOverScreen();
}
return;
}
// Update the head's position
Transform3D headT3d = new Transform3D();
headT3d.setTranslation(headPosition);
segments.get(0).setTransform(headT3d);
// Move the rest of the segments
for (int i = 1; i < segments.size(); i++) {
Transform3D segT3d = new Transform3D();
segT3d.setTranslation(prevPositions.get(i - 1));
segments.get(i).setTransform(segT3d);
}
}
public void addSegment() {
Transform3D tailT3d = new Transform3D();
segments.get(segments.size() - 1).getTransform(tailT3d);
Vector3f tailPos = new Vector3f();
tailT3d.get(tailPos);
TransformGroup newSegment = createSegment(new Vector3f(tailPos));
segments.add(newSegment);
BranchGroup bg = new BranchGroup();
bg.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
bg.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
bg.addChild(newSegment);
snakeTG.addChild(bg);
System.out.println("New snake segment added!");
}
public void turnLeft() {
// Rotate the direction vector 90 degrees counterclockwise (left)
Transform3D rot = new Transform3D();
rot.rotY(Math.toRadians(90)); // Rotate 90 degrees counterclockwise
rot.transform(direction);
System.out.println("Turn left");
}
public void turnRight() {
// Rotate the direction vector 90 degrees clockwise (right)
Transform3D rot = new Transform3D();
rot.rotY(Math.toRadians(-90)); // Rotate 90 degrees clockwise
rot.transform(direction);
System.out.println("Turn right");
}
public Vector3f getDirection() {
return direction;
}
public TransformGroup getTransformGroup() {
return snakeTG;
}
public Vector3f getHeadPosition() {
Transform3D headT3d = new Transform3D();
segments.get(0).getTransform(headT3d);
Vector3f pos = new Vector3f();
headT3d.get(pos);
return pos;
}
public class SnakeBehavior extends Behavior {
private WakeupCondition wakeupCondition;
@Override
public void initialize() {
wakeupCondition = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
wakeupOn(wakeupCondition);
}
@Override
public void processStimulus(Iterator<WakeupCriterion> criteria) {
while (criteria.hasNext()) {
WakeupCriterion wc = criteria.next();
if (wc instanceof WakeupOnAWTEvent) {
AWTEvent[] events = ((WakeupOnAWTEvent) wc).getAWTEvent();
for (AWTEvent event : events) {
if (event instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) event;
if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
turnLeft();
} else if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
turnRight();
}
}
}
}
}
wakeupOn(wakeupCondition);
}
}
}