-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameWorld.java
More file actions
262 lines (201 loc) · 7.47 KB
/
GameWorld.java
File metadata and controls
262 lines (201 loc) · 7.47 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* GameWorld.java
* Project Disconnect
*
* Created by Drew Malin on 8/27/2011.
* Copyright 2011 Drew Malin. All rights reserved.
*
*/
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.*;
public class GameWorld {
private int width = 1024;
private int height = 768;
private float diagonal = .70710678f;
private float zNear = 0.1f;
private float zFar = 1000f;
private float frustAngle = 60f;
private Camera camera;
private Hero hero;
private Map map;
float mouse[];
private Timer timer;
private long frameDelta;
private long FRAME_LENGTH_MINIMUM = 10000000;
//--------------------- Memory setup ----------------------------//
private static IntBuffer viewport;
private static FloatBuffer modelview;
private static FloatBuffer projection;
private static FloatBuffer positionNear;
private static FloatBuffer positionFar;
static float pos[];
static float r[];
//---------------------------------------------------------------//
// Method to begin setup
public void start() throws InterruptedException {
viewport = BufferUtils.createIntBuffer(16);
modelview = BufferUtils.createFloatBuffer(16);
projection = BufferUtils.createFloatBuffer(16);
positionNear = BufferUtils.createFloatBuffer(3);
positionFar = BufferUtils.createFloatBuffer(3);
pos = new float[3];
r = new float[3];
camera = new Camera();
camera.setPosition(0f, 7f, 7f);
camera.setTarget(0f, 0f, 0f);
camera.setUp(0f, 1f, 0f);
mouse = new float[3];
hero = new Hero();
hero.setPositionArray(0f, 0f, 0f);
init();
timer = new Timer(); //The Timer constructor establishes an origin time.
/* isCloseRequested comes in the form of the X on a window being pushed,
a CTRL-C keystroke, etc. Continue updating until this happens. update()
swaps the buffers.
*/
while (!Display.isCloseRequested()) {
frameDelta = timer.getNanoDelta(); //This value will get passed to all the updates that are time dependent.
if(frameDelta < FRAME_LENGTH_MINIMUM) { //If very little time has passed since the last update, yield the cpu
Thread.sleep(10);
}else{
pollMouse();
pollKeyboard();
updateMap();
drawEntities();
Display.update();
}
}
Display.destroy();
}
// Method to initialize the display
public void init() {
//TODO: lighting, shading, fog, particles
try {
Display.setDisplayMode( new DisplayMode( width, height ) );
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(1);
}
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(frustAngle, (float)width/(float)height, zNear, zFar);
GLU.gluLookAt(
camera.getPosition(0), camera.getPosition(1), camera.getPosition(2),
camera.getTarget(0), camera.getTarget(1), camera.getTarget(2),
camera.getUp(0), camera.getUp(1), camera.getUp(2));
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
}
public void loadMap() {
map = new Map("GridWorld.txt");
}
public void loadWorld() {
//TODO: Read in the objects that make up the game world
}
public void drawEntities() {
hero.draw();
}
public void updateMap() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glTranslatef(hero.getPosition(0), hero.getPosition(1), hero.getPosition(2));
map.draw();
GL11.glPopMatrix();
}
public void pollMouse() {
//TODO: Implement camera angle changes with mouse wheel scroll
//Updated regardless of mouse click
mouse = getMousePosition(Mouse.getX(), Mouse.getY());
hero.setTargetVect(mouse[0], mouse[1], mouse[2]);
if (Mouse.isButtonDown(0)) { // Left click
mouse = getMousePosition(Mouse.getX(), Mouse.getY());
System.out.println(mouse[0] + ", " + mouse[1] + ", " + mouse[2]);
// hero.setTargetVect(mouse[0], mouse[1], mouse[2]);
}
}
public void pollKeyboard() {
//TODO: Implement jumping with the spacebar
if (Keyboard.isKeyDown(Keyboard.KEY_W) && Keyboard.isKeyDown(Keyboard.KEY_A)) {
hero.changePositionZ(hero.movementRate * diagonal);
hero.changePositionX(hero.movementRate * diagonal);
}
else if (Keyboard.isKeyDown(Keyboard.KEY_W) && Keyboard.isKeyDown(Keyboard.KEY_D)) {
hero.changePositionZ(hero.movementRate * diagonal);
hero.changePositionX(-hero.movementRate * diagonal);
}
else if (Keyboard.isKeyDown(Keyboard.KEY_S) && Keyboard.isKeyDown(Keyboard.KEY_A)) {
hero.changePositionZ(-hero.movementRate * diagonal);
hero.changePositionX(hero.movementRate * diagonal);
}
else if (Keyboard.isKeyDown(Keyboard.KEY_S) && Keyboard.isKeyDown(Keyboard.KEY_D)) {
hero.changePositionZ(-hero.movementRate * diagonal);
hero.changePositionX(-hero.movementRate * diagonal);
}
else {
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
hero.changePositionZ(hero.movementRate);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
hero.changePositionX(hero.movementRate);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
hero.changePositionZ(-hero.movementRate);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
hero.changePositionX(-hero.movementRate);
}
}
}
static public float[] getMousePosition(int mouseX, int mouseY) {
//--------------------- Save the view matrices ------------------//
GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview );
GL11.glGetFloat( GL11.GL_PROJECTION_MATRIX, projection );
GL11.glGetInteger( GL11.GL_VIEWPORT, viewport );
//---------------------------------------------------------------//
/*
* The following logic:
* gluUnProject takes the mouse coordinates, the three view matrices, and an empty
* byte buffer to store the 3D world coordinate of the mouse click. By providing 0
* for the mouse click's z value, the position calculated is on the near z clipping
* plane. Likewise, providing a 1 for this value puts the 3D point on the far z
* clipping plane. These 3D coordinates are stored in positionNear and positionFar.
*/
GLU.gluUnProject((float)mouseX, (float)mouseY, 0, modelview, projection, viewport, positionNear);
GLU.gluUnProject((float)mouseX, (float)mouseY, 1, modelview, projection, viewport, positionFar);
/*
* The following logic:
* We now effectively have the beginning and end points of the ray that is produced
* by a mouse click. The direction vector is determined like so:
* r = <P2x, P2y, P2z> - <P1x, P1y, P1z>
* This vector is used in the parametric form for the equation of a line:
* x = P1x + mrx
* y = P1y + mry
* z = P1z + mrz
* We know that the "ground level" is the plane in which y = 0. Therefore if we fix
* this value, we can solve for m (m = (y - P1y) / ry). The newly acquired m value
* allows us to easily solve for the x and y positions on the y = 0 plane.
*/
float m;
float fixedYPlane = 0;
r[0] = positionFar.get(0) - positionNear.get(0);
r[1] = positionFar.get(1) - positionNear.get(1);
r[2] = positionFar.get(2) - positionNear.get(2);
m = (fixedYPlane - positionNear.get(1)) / r[1];
pos[0] = positionNear.get(0) + (m * r[0]);
pos[1] = fixedYPlane;
pos[2] = positionNear.get(2) + (m * r[2]);
return pos;
}
}