File main.py changed (mode: 100644) (index d1f9d03..60d22a2) |
... |
... |
import math |
5 |
5 |
import random |
import random |
6 |
6 |
import time |
import time |
7 |
7 |
|
|
|
8 |
|
|
|
9 |
|
# Size of sectors used to each block loading. |
8 |
10 |
SECTOR_SIZE = 16 |
SECTOR_SIZE = 16 |
9 |
11 |
|
|
|
12 |
|
|
10 |
13 |
def cube_vertices(x, y, z, n): |
def cube_vertices(x, y, z, n): |
11 |
14 |
return [ |
return [ |
12 |
|
x-n,y+n,z-n, x-n,y+n,z+n, x+n,y+n,z+n, x+n,y+n,z-n, # top |
|
13 |
|
x-n,y-n,z-n, x+n,y-n,z-n, x+n,y-n,z+n, x-n,y-n,z+n, # bottom |
|
14 |
|
x-n,y-n,z-n, x-n,y-n,z+n, x-n,y+n,z+n, x-n,y+n,z-n, # left |
|
15 |
|
x+n,y-n,z+n, x+n,y-n,z-n, x+n,y+n,z-n, x+n,y+n,z+n, # right |
|
16 |
|
x-n,y-n,z+n, x+n,y-n,z+n, x+n,y+n,z+n, x-n,y+n,z+n, # front |
|
17 |
|
x+n,y-n,z-n, x-n,y-n,z-n, x-n,y+n,z-n, x+n,y+n,z-n, # back |
|
|
15 |
|
x-n,y+n,z-n, x-n,y+n,z+n, x+n,y+n,z+n, x+n,y+n,z-n, # top |
|
16 |
|
x-n,y-n,z-n, x+n,y-n,z-n, x+n,y-n,z+n, x-n,y-n,z+n, # bottom |
|
17 |
|
x-n,y-n,z-n, x-n,y-n,z+n, x-n,y+n,z+n, x-n,y+n,z-n, # left |
|
18 |
|
x+n,y-n,z+n, x+n,y-n,z-n, x+n,y+n,z-n, x+n,y+n,z+n, # right |
|
19 |
|
x-n,y-n,z+n, x+n,y-n,z+n, x+n,y+n,z+n, x-n,y+n,z+n, # front |
|
20 |
|
x+n,y-n,z-n, x-n,y-n,z-n, x-n,y+n,z-n, x+n,y+n,z-n, # back |
18 |
21 |
] |
] |
19 |
22 |
|
|
|
23 |
|
|
20 |
24 |
def tex_coord(x, y, n=4): |
def tex_coord(x, y, n=4): |
21 |
25 |
m = 1.0 / n |
m = 1.0 / n |
22 |
26 |
dx = x * m |
dx = x * m |
23 |
27 |
dy = y * m |
dy = y * m |
24 |
28 |
return dx, dy, dx + m, dy, dx + m, dy + m, dx, dy + m |
return dx, dy, dx + m, dy, dx + m, dy + m, dx, dy + m |
25 |
29 |
|
|
|
30 |
|
|
26 |
31 |
def tex_coords(top, bottom, side): |
def tex_coords(top, bottom, side): |
27 |
32 |
top = tex_coord(*top) |
top = tex_coord(*top) |
28 |
33 |
bottom = tex_coord(*bottom) |
bottom = tex_coord(*bottom) |
|
... |
... |
def tex_coords(top, bottom, side): |
33 |
38 |
result.extend(side * 4) |
result.extend(side * 4) |
34 |
39 |
return result |
return result |
35 |
40 |
|
|
|
41 |
|
|
36 |
42 |
GRASS = tex_coords((1, 0), (0, 1), (0, 0)) |
GRASS = tex_coords((1, 0), (0, 1), (0, 0)) |
37 |
43 |
SAND = tex_coords((1, 1), (1, 1), (1, 1)) |
SAND = tex_coords((1, 1), (1, 1), (1, 1)) |
38 |
44 |
BRICK = tex_coords((2, 0), (2, 0), (2, 0)) |
BRICK = tex_coords((2, 0), (2, 0), (2, 0)) |
|
... |
... |
FACES = [ |
47 |
53 |
( 0, 0,-1), |
( 0, 0,-1), |
48 |
54 |
] |
] |
49 |
55 |
|
|
|
56 |
|
|
50 |
57 |
class TextureGroup(pyglet.graphics.Group): |
class TextureGroup(pyglet.graphics.Group): |
|
58 |
|
|
51 |
59 |
def __init__(self, path): |
def __init__(self, path): |
52 |
60 |
super(TextureGroup, self).__init__() |
super(TextureGroup, self).__init__() |
53 |
61 |
self.texture = pyglet.image.load(path).get_texture() |
self.texture = pyglet.image.load(path).get_texture() |
|
62 |
|
|
54 |
63 |
def set_state(self): |
def set_state(self): |
55 |
64 |
glEnable(self.texture.target) |
glEnable(self.texture.target) |
56 |
65 |
glBindTexture(self.texture.target, self.texture.id) |
glBindTexture(self.texture.target, self.texture.id) |
|
66 |
|
|
57 |
67 |
def unset_state(self): |
def unset_state(self): |
58 |
68 |
glDisable(self.texture.target) |
glDisable(self.texture.target) |
59 |
69 |
|
|
|
70 |
|
|
60 |
71 |
def normalize(position): |
def normalize(position): |
61 |
72 |
x, y, z = position |
x, y, z = position |
62 |
73 |
x, y, z = (int(round(x)), int(round(y)), int(round(z))) |
x, y, z = (int(round(x)), int(round(y)), int(round(z))) |
63 |
74 |
return (x, y, z) |
return (x, y, z) |
64 |
75 |
|
|
|
76 |
|
|
65 |
77 |
def sectorize(position): |
def sectorize(position): |
66 |
78 |
x, y, z = normalize(position) |
x, y, z = normalize(position) |
67 |
79 |
x, y, z = x / SECTOR_SIZE, y / SECTOR_SIZE, z / SECTOR_SIZE |
x, y, z = x / SECTOR_SIZE, y / SECTOR_SIZE, z / SECTOR_SIZE |
68 |
80 |
return (x, 0, z) |
return (x, 0, z) |
69 |
81 |
|
|
|
82 |
|
|
70 |
83 |
class Model(object): |
class Model(object): |
|
84 |
|
|
71 |
85 |
def __init__(self): |
def __init__(self): |
72 |
86 |
self.batch = pyglet.graphics.Batch() |
self.batch = pyglet.graphics.Batch() |
73 |
87 |
self.group = TextureGroup('texture.png') |
self.group = TextureGroup('texture.png') |
|
... |
... |
class Model(object): |
77 |
91 |
self.sectors = {} |
self.sectors = {} |
78 |
92 |
self.queue = [] |
self.queue = [] |
79 |
93 |
self.initialize() |
self.initialize() |
|
94 |
|
|
80 |
95 |
def initialize(self): |
def initialize(self): |
81 |
96 |
n = 80 |
n = 80 |
82 |
97 |
s = 1 |
s = 1 |
|
... |
... |
class Model(object): |
106 |
121 |
continue |
continue |
107 |
122 |
self.init_block((x, y, z), t) |
self.init_block((x, y, z), t) |
108 |
123 |
s -= d |
s -= d |
|
124 |
|
|
109 |
125 |
def hit_test(self, position, vector, max_distance=8): |
def hit_test(self, position, vector, max_distance=8): |
110 |
126 |
m = 8 |
m = 8 |
111 |
127 |
x, y, z = position |
x, y, z = position |
|
... |
... |
class Model(object): |
118 |
134 |
previous = key |
previous = key |
119 |
135 |
x, y, z = x + dx / m, y + dy / m, z + dz / m |
x, y, z = x + dx / m, y + dy / m, z + dz / m |
120 |
136 |
return None, None |
return None, None |
|
137 |
|
|
121 |
138 |
def exposed(self, position): |
def exposed(self, position): |
122 |
139 |
x, y, z = position |
x, y, z = position |
123 |
140 |
for dx, dy, dz in FACES: |
for dx, dy, dz in FACES: |
124 |
141 |
if (x + dx, y + dy, z + dz) not in self.world: |
if (x + dx, y + dy, z + dz) not in self.world: |
125 |
142 |
return True |
return True |
126 |
143 |
return False |
return False |
|
144 |
|
|
127 |
145 |
def init_block(self, position, texture): |
def init_block(self, position, texture): |
128 |
146 |
self.add_block(position, texture, False) |
self.add_block(position, texture, False) |
|
147 |
|
|
129 |
148 |
def add_block(self, position, texture, sync=True): |
def add_block(self, position, texture, sync=True): |
130 |
149 |
if position in self.world: |
if position in self.world: |
131 |
150 |
self.remove_block(position, sync) |
self.remove_block(position, sync) |
|
... |
... |
class Model(object): |
135 |
154 |
if self.exposed(position): |
if self.exposed(position): |
136 |
155 |
self.show_block(position) |
self.show_block(position) |
137 |
156 |
self.check_neighbors(position) |
self.check_neighbors(position) |
|
157 |
|
|
138 |
158 |
def remove_block(self, position, sync=True): |
def remove_block(self, position, sync=True): |
139 |
159 |
del self.world[position] |
del self.world[position] |
140 |
160 |
self.sectors[sectorize(position)].remove(position) |
self.sectors[sectorize(position)].remove(position) |
|
... |
... |
class Model(object): |
142 |
162 |
if position in self.shown: |
if position in self.shown: |
143 |
163 |
self.hide_block(position) |
self.hide_block(position) |
144 |
164 |
self.check_neighbors(position) |
self.check_neighbors(position) |
|
165 |
|
|
145 |
166 |
def check_neighbors(self, position): |
def check_neighbors(self, position): |
146 |
167 |
x, y, z = position |
x, y, z = position |
147 |
168 |
for dx, dy, dz in FACES: |
for dx, dy, dz in FACES: |
|
... |
... |
class Model(object): |
154 |
175 |
else: |
else: |
155 |
176 |
if key in self.shown: |
if key in self.shown: |
156 |
177 |
self.hide_block(key) |
self.hide_block(key) |
|
178 |
|
|
157 |
179 |
def show_blocks(self): |
def show_blocks(self): |
158 |
180 |
for position in self.world: |
for position in self.world: |
159 |
181 |
if position not in self.shown and self.exposed(position): |
if position not in self.shown and self.exposed(position): |
160 |
182 |
self.show_block(position) |
self.show_block(position) |
|
183 |
|
|
161 |
184 |
def show_block(self, position, immediate=True): |
def show_block(self, position, immediate=True): |
162 |
185 |
texture = self.world[position] |
texture = self.world[position] |
163 |
186 |
self.shown[position] = texture |
self.shown[position] = texture |
|
... |
... |
class Model(object): |
165 |
188 |
self._show_block(position, texture) |
self._show_block(position, texture) |
166 |
189 |
else: |
else: |
167 |
190 |
self.enqueue(self._show_block, position, texture) |
self.enqueue(self._show_block, position, texture) |
|
191 |
|
|
168 |
192 |
def _show_block(self, position, texture): |
def _show_block(self, position, texture): |
169 |
193 |
x, y, z = position |
x, y, z = position |
170 |
194 |
# only show exposed faces |
# only show exposed faces |
|
... |
... |
class Model(object): |
172 |
196 |
count = 24 |
count = 24 |
173 |
197 |
vertex_data = cube_vertices(x, y, z, 0.5) |
vertex_data = cube_vertices(x, y, z, 0.5) |
174 |
198 |
texture_data = list(texture) |
texture_data = list(texture) |
175 |
|
for dx, dy, dz in []:#FACES: |
|
|
199 |
|
for dx, dy, dz in []: # FACES: |
176 |
200 |
if (x + dx, y + dy, z + dz) in self.world: |
if (x + dx, y + dy, z + dz) in self.world: |
177 |
201 |
count -= 4 |
count -= 4 |
178 |
202 |
i = index * 12 |
i = index * 12 |
|
... |
... |
class Model(object): |
182 |
206 |
else: |
else: |
183 |
207 |
index += 1 |
index += 1 |
184 |
208 |
# create vertex list |
# create vertex list |
185 |
|
self._shown[position] = self.batch.add(count, GL_QUADS, self.group, |
|
|
209 |
|
self._shown[position] = self.batch.add(count, GL_QUADS, self.group, |
186 |
210 |
('v3f/static', vertex_data), |
('v3f/static', vertex_data), |
187 |
211 |
('t2f/static', texture_data)) |
('t2f/static', texture_data)) |
|
212 |
|
|
188 |
213 |
def hide_block(self, position, immediate=True): |
def hide_block(self, position, immediate=True): |
189 |
214 |
self.shown.pop(position) |
self.shown.pop(position) |
190 |
215 |
if immediate: |
if immediate: |
191 |
216 |
self._hide_block(position) |
self._hide_block(position) |
192 |
217 |
else: |
else: |
193 |
218 |
self.enqueue(self._hide_block, position) |
self.enqueue(self._hide_block, position) |
|
219 |
|
|
194 |
220 |
def _hide_block(self, position): |
def _hide_block(self, position): |
195 |
221 |
self._shown.pop(position).delete() |
self._shown.pop(position).delete() |
|
222 |
|
|
196 |
223 |
def show_sector(self, sector): |
def show_sector(self, sector): |
197 |
224 |
for position in self.sectors.get(sector, []): |
for position in self.sectors.get(sector, []): |
198 |
225 |
if position not in self.shown and self.exposed(position): |
if position not in self.shown and self.exposed(position): |
199 |
226 |
self.show_block(position, False) |
self.show_block(position, False) |
|
227 |
|
|
200 |
228 |
def hide_sector(self, sector): |
def hide_sector(self, sector): |
201 |
229 |
for position in self.sectors.get(sector, []): |
for position in self.sectors.get(sector, []): |
202 |
230 |
if position in self.shown: |
if position in self.shown: |
203 |
231 |
self.hide_block(position, False) |
self.hide_block(position, False) |
|
232 |
|
|
204 |
233 |
def change_sectors(self, before, after): |
def change_sectors(self, before, after): |
205 |
234 |
before_set = set() |
before_set = set() |
206 |
235 |
after_set = set() |
after_set = set() |
207 |
236 |
pad = 4 |
pad = 4 |
208 |
237 |
for dx in xrange(-pad, pad + 1): |
for dx in xrange(-pad, pad + 1): |
209 |
|
for dy in [0]: # xrange(-pad, pad + 1): |
|
|
238 |
|
for dy in [0]: # xrange(-pad, pad + 1): |
210 |
239 |
for dz in xrange(-pad, pad + 1): |
for dz in xrange(-pad, pad + 1): |
211 |
240 |
if dx ** 2 + dy ** 2 + dz ** 2 > (pad + 1) ** 2: |
if dx ** 2 + dy ** 2 + dz ** 2 > (pad + 1) ** 2: |
212 |
241 |
continue |
continue |
|
... |
... |
class Model(object): |
222 |
251 |
self.show_sector(sector) |
self.show_sector(sector) |
223 |
252 |
for sector in hide: |
for sector in hide: |
224 |
253 |
self.hide_sector(sector) |
self.hide_sector(sector) |
|
254 |
|
|
225 |
255 |
def enqueue(self, func, *args): |
def enqueue(self, func, *args): |
226 |
256 |
self.queue.append((func, args)) |
self.queue.append((func, args)) |
|
257 |
|
|
227 |
258 |
def dequeue(self): |
def dequeue(self): |
228 |
259 |
func, args = self.queue.pop(0) |
func, args = self.queue.pop(0) |
229 |
260 |
func(*args) |
func(*args) |
|
261 |
|
|
230 |
262 |
def process_queue(self): |
def process_queue(self): |
231 |
263 |
start = time.clock() |
start = time.clock() |
232 |
264 |
while self.queue and time.clock() - start < 1 / 60.0: |
while self.queue and time.clock() - start < 1 / 60.0: |
233 |
265 |
self.dequeue() |
self.dequeue() |
|
266 |
|
|
234 |
267 |
def process_entire_queue(self): |
def process_entire_queue(self): |
235 |
268 |
while self.queue: |
while self.queue: |
236 |
269 |
self.dequeue() |
self.dequeue() |
237 |
270 |
|
|
|
271 |
|
|
238 |
272 |
class Window(pyglet.window.Window): |
class Window(pyglet.window.Window): |
|
273 |
|
|
239 |
274 |
def __init__(self, *args, **kwargs): |
def __init__(self, *args, **kwargs): |
240 |
275 |
super(Window, self).__init__(*args, **kwargs) |
super(Window, self).__init__(*args, **kwargs) |
241 |
276 |
self.exclusive = False |
self.exclusive = False |
|
... |
... |
class Window(pyglet.window.Window): |
247 |
282 |
self.reticle = None |
self.reticle = None |
248 |
283 |
self.dy = 0 |
self.dy = 0 |
249 |
284 |
self.model = Model() |
self.model = Model() |
250 |
|
self.label = pyglet.text.Label('', font_name='Arial', font_size=18, |
|
251 |
|
x=10, y=self.height - 10, anchor_x='left', anchor_y='top', |
|
|
285 |
|
self.label = pyglet.text.Label('', font_name='Arial', font_size=18, |
|
286 |
|
x=10, y=self.height - 10, anchor_x='left', anchor_y='top', |
252 |
287 |
color=(0, 0, 0, 255)) |
color=(0, 0, 0, 255)) |
253 |
288 |
pyglet.clock.schedule_interval(self.update, 1.0 / 60) |
pyglet.clock.schedule_interval(self.update, 1.0 / 60) |
|
289 |
|
|
254 |
290 |
def set_exclusive_mouse(self, exclusive): |
def set_exclusive_mouse(self, exclusive): |
255 |
291 |
super(Window, self).set_exclusive_mouse(exclusive) |
super(Window, self).set_exclusive_mouse(exclusive) |
256 |
292 |
self.exclusive = exclusive |
self.exclusive = exclusive |
|
293 |
|
|
257 |
294 |
def get_sight_vector(self): |
def get_sight_vector(self): |
258 |
295 |
x, y = self.rotation |
x, y = self.rotation |
259 |
296 |
m = math.cos(math.radians(y)) |
m = math.cos(math.radians(y)) |
|
... |
... |
class Window(pyglet.window.Window): |
261 |
298 |
dx = math.cos(math.radians(x - 90)) * m |
dx = math.cos(math.radians(x - 90)) * m |
262 |
299 |
dz = math.sin(math.radians(x - 90)) * m |
dz = math.sin(math.radians(x - 90)) * m |
263 |
300 |
return (dx, dy, dz) |
return (dx, dy, dz) |
|
301 |
|
|
264 |
302 |
def get_motion_vector(self): |
def get_motion_vector(self): |
265 |
303 |
if any(self.strafe): |
if any(self.strafe): |
266 |
304 |
x, y = self.rotation |
x, y = self.rotation |
|
... |
... |
class Window(pyglet.window.Window): |
284 |
322 |
dx = 0.0 |
dx = 0.0 |
285 |
323 |
dz = 0.0 |
dz = 0.0 |
286 |
324 |
return (dx, dy, dz) |
return (dx, dy, dz) |
|
325 |
|
|
287 |
326 |
def update(self, dt): |
def update(self, dt): |
288 |
327 |
self.model.process_queue() |
self.model.process_queue() |
289 |
328 |
sector = sectorize(self.position) |
sector = sectorize(self.position) |
|
... |
... |
class Window(pyglet.window.Window): |
296 |
335 |
dt = min(dt, 0.2) |
dt = min(dt, 0.2) |
297 |
336 |
for _ in xrange(m): |
for _ in xrange(m): |
298 |
337 |
self._update(dt / m) |
self._update(dt / m) |
|
338 |
|
|
299 |
339 |
def _update(self, dt): |
def _update(self, dt): |
300 |
340 |
# walking |
# walking |
301 |
341 |
speed = 15 if self.flying else 5 |
speed = 15 if self.flying else 5 |
|
... |
... |
class Window(pyglet.window.Window): |
304 |
344 |
dx, dy, dz = dx * d, dy * d, dz * d |
dx, dy, dz = dx * d, dy * d, dz * d |
305 |
345 |
# gravity |
# gravity |
306 |
346 |
if not self.flying: |
if not self.flying: |
307 |
|
self.dy -= dt * 0.044 # g force, should be = jump_speed * 0.5 / max_jump_height |
|
308 |
|
self.dy = max(self.dy, -0.5) # terminal velocity |
|
|
347 |
|
self.dy -= dt * 0.044 # g force, should be = jump_speed * 0.5 / max_jump_height |
|
348 |
|
self.dy = max(self.dy, -0.5) # terminal velocity |
309 |
349 |
dy += self.dy |
dy += self.dy |
310 |
350 |
# collisions |
# collisions |
311 |
351 |
x, y, z = self.position |
x, y, z = self.position |
312 |
352 |
x, y, z = self.collide((x + dx, y + dy, z + dz), 2) |
x, y, z = self.collide((x + dx, y + dy, z + dz), 2) |
313 |
353 |
self.position = (x, y, z) |
self.position = (x, y, z) |
|
354 |
|
|
314 |
355 |
def collide(self, position, height): |
def collide(self, position, height): |
315 |
356 |
pad = 0.25 |
pad = 0.25 |
316 |
357 |
p = list(position) |
p = list(position) |
317 |
358 |
np = normalize(position) |
np = normalize(position) |
318 |
|
for face in FACES: # check all surrounding blocks |
|
319 |
|
for i in xrange(3): # check each dimension independently |
|
|
359 |
|
for face in FACES: # check all surrounding blocks |
|
360 |
|
for i in xrange(3): # check each dimension independently |
320 |
361 |
if not face[i]: |
if not face[i]: |
321 |
362 |
continue |
continue |
322 |
363 |
d = (p[i] - np[i]) * face[i] |
d = (p[i] - np[i]) * face[i] |
323 |
364 |
if d < pad: |
if d < pad: |
324 |
365 |
continue |
continue |
325 |
|
for dy in xrange(height): # check each height |
|
|
366 |
|
for dy in xrange(height): # check each height |
326 |
367 |
op = list(np) |
op = list(np) |
327 |
368 |
op[1] -= dy |
op[1] -= dy |
328 |
369 |
op[i] += face[i] |
op[i] += face[i] |
|
... |
... |
class Window(pyglet.window.Window): |
334 |
375 |
self.dy = 0 |
self.dy = 0 |
335 |
376 |
break |
break |
336 |
377 |
return tuple(p) |
return tuple(p) |
|
378 |
|
|
337 |
379 |
def on_mouse_scroll(self, x, y, scroll_x, scroll_y): |
def on_mouse_scroll(self, x, y, scroll_x, scroll_y): |
338 |
380 |
return |
return |
339 |
381 |
x, y, z = self.position |
x, y, z = self.position |
340 |
382 |
dx, dy, dz = self.get_sight_vector() |
dx, dy, dz = self.get_sight_vector() |
341 |
383 |
d = scroll_y * 10 |
d = scroll_y * 10 |
342 |
384 |
self.position = (x + dx * d, y + dy * d, z + dz * d) |
self.position = (x + dx * d, y + dy * d, z + dz * d) |
|
385 |
|
|
343 |
386 |
def on_mouse_press(self, x, y, button, modifiers): |
def on_mouse_press(self, x, y, button, modifiers): |
344 |
387 |
if self.exclusive: |
if self.exclusive: |
345 |
388 |
vector = self.get_sight_vector() |
vector = self.get_sight_vector() |
|
... |
... |
class Window(pyglet.window.Window): |
354 |
397 |
self.model.add_block(previous, BRICK) |
self.model.add_block(previous, BRICK) |
355 |
398 |
else: |
else: |
356 |
399 |
self.set_exclusive_mouse(True) |
self.set_exclusive_mouse(True) |
|
400 |
|
|
357 |
401 |
def on_mouse_motion(self, x, y, dx, dy): |
def on_mouse_motion(self, x, y, dx, dy): |
358 |
402 |
if self.exclusive: |
if self.exclusive: |
359 |
403 |
m = 0.15 |
m = 0.15 |
|
... |
... |
class Window(pyglet.window.Window): |
361 |
405 |
x, y = x + dx * m, y + dy * m |
x, y = x + dx * m, y + dy * m |
362 |
406 |
y = max(-90, min(90, y)) |
y = max(-90, min(90, y)) |
363 |
407 |
self.rotation = (x, y) |
self.rotation = (x, y) |
|
408 |
|
|
364 |
409 |
def on_key_press(self, symbol, modifiers): |
def on_key_press(self, symbol, modifiers): |
365 |
410 |
if symbol == key.W: |
if symbol == key.W: |
366 |
411 |
self.strafe[0] -= 1 |
self.strafe[0] -= 1 |
|
... |
... |
class Window(pyglet.window.Window): |
372 |
417 |
self.strafe[1] += 1 |
self.strafe[1] += 1 |
373 |
418 |
elif symbol == key.SPACE: |
elif symbol == key.SPACE: |
374 |
419 |
if self.dy == 0: |
if self.dy == 0: |
375 |
|
self.dy = 0.015 # jump speed |
|
|
420 |
|
self.dy = 0.015 # jump speed |
376 |
421 |
elif symbol == key.ESCAPE: |
elif symbol == key.ESCAPE: |
377 |
422 |
self.set_exclusive_mouse(False) |
self.set_exclusive_mouse(False) |
378 |
423 |
elif symbol == key.TAB: |
elif symbol == key.TAB: |
379 |
424 |
self.flying = not self.flying |
self.flying = not self.flying |
|
425 |
|
|
380 |
426 |
def on_key_release(self, symbol, modifiers): |
def on_key_release(self, symbol, modifiers): |
381 |
427 |
if symbol == key.W: |
if symbol == key.W: |
382 |
428 |
self.strafe[0] += 1 |
self.strafe[0] += 1 |
|
... |
... |
class Window(pyglet.window.Window): |
386 |
432 |
self.strafe[1] += 1 |
self.strafe[1] += 1 |
387 |
433 |
elif symbol == key.D: |
elif symbol == key.D: |
388 |
434 |
self.strafe[1] -= 1 |
self.strafe[1] -= 1 |
|
435 |
|
|
389 |
436 |
def on_resize(self, width, height): |
def on_resize(self, width, height): |
390 |
437 |
# label |
# label |
391 |
438 |
self.label.y = height - 10 |
self.label.y = height - 10 |
|
... |
... |
class Window(pyglet.window.Window): |
397 |
444 |
self.reticle = pyglet.graphics.vertex_list(4, |
self.reticle = pyglet.graphics.vertex_list(4, |
398 |
445 |
('v2i', (x - n, y, x + n, y, x, y - n, x, y + n)) |
('v2i', (x - n, y, x + n, y, x, y - n, x, y + n)) |
399 |
446 |
) |
) |
|
447 |
|
|
400 |
448 |
def set_2d(self): |
def set_2d(self): |
401 |
449 |
width, height = self.get_size() |
width, height = self.get_size() |
402 |
450 |
glDisable(GL_DEPTH_TEST) |
glDisable(GL_DEPTH_TEST) |
|
... |
... |
class Window(pyglet.window.Window): |
406 |
454 |
glOrtho(0, width, 0, height, -1, 1) |
glOrtho(0, width, 0, height, -1, 1) |
407 |
455 |
glMatrixMode(GL_MODELVIEW) |
glMatrixMode(GL_MODELVIEW) |
408 |
456 |
glLoadIdentity() |
glLoadIdentity() |
|
457 |
|
|
409 |
458 |
def set_3d(self): |
def set_3d(self): |
410 |
459 |
width, height = self.get_size() |
width, height = self.get_size() |
411 |
460 |
glEnable(GL_DEPTH_TEST) |
glEnable(GL_DEPTH_TEST) |
|
... |
... |
class Window(pyglet.window.Window): |
420 |
469 |
glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) |
glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) |
421 |
470 |
x, y, z = self.position |
x, y, z = self.position |
422 |
471 |
glTranslatef(-x, -y, -z) |
glTranslatef(-x, -y, -z) |
|
472 |
|
|
423 |
473 |
def on_draw(self): |
def on_draw(self): |
424 |
474 |
self.clear() |
self.clear() |
425 |
475 |
self.set_3d() |
self.set_3d() |
|
... |
... |
class Window(pyglet.window.Window): |
429 |
479 |
self.set_2d() |
self.set_2d() |
430 |
480 |
self.draw_label() |
self.draw_label() |
431 |
481 |
self.draw_reticle() |
self.draw_reticle() |
|
482 |
|
|
432 |
483 |
def draw_focused_block(self): |
def draw_focused_block(self): |
433 |
484 |
vector = self.get_sight_vector() |
vector = self.get_sight_vector() |
434 |
485 |
block = self.model.hit_test(self.position, vector)[0] |
block = self.model.hit_test(self.position, vector)[0] |
|
... |
... |
class Window(pyglet.window.Window): |
439 |
490 |
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) |
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) |
440 |
491 |
pyglet.graphics.draw(24, GL_QUADS, ('v3f/static', vertex_data)) |
pyglet.graphics.draw(24, GL_QUADS, ('v3f/static', vertex_data)) |
441 |
492 |
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) |
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) |
|
493 |
|
|
442 |
494 |
def draw_label(self): |
def draw_label(self): |
443 |
495 |
x, y, z = self.position |
x, y, z = self.position |
444 |
496 |
self.label.text = '%02d (%.2f, %.2f, %.2f) %d / %d' % ( |
self.label.text = '%02d (%.2f, %.2f, %.2f) %d / %d' % ( |
445 |
|
pyglet.clock.get_fps(), x, y, z, |
|
|
497 |
|
pyglet.clock.get_fps(), x, y, z, |
446 |
498 |
len(self.model._shown), len(self.model.world)) |
len(self.model._shown), len(self.model.world)) |
447 |
499 |
self.label.draw() |
self.label.draw() |
|
500 |
|
|
448 |
501 |
def draw_reticle(self): |
def draw_reticle(self): |
449 |
502 |
glColor3d(0, 0, 0) |
glColor3d(0, 0, 0) |
450 |
503 |
self.reticle.draw(GL_LINES) |
self.reticle.draw(GL_LINES) |
451 |
504 |
|
|
|
505 |
|
|
452 |
506 |
def setup_fog(): |
def setup_fog(): |
453 |
507 |
glEnable(GL_FOG) |
glEnable(GL_FOG) |
454 |
508 |
glFogfv(GL_FOG_COLOR, (c_float * 4)(0.53, 0.81, 0.98, 1)) |
glFogfv(GL_FOG_COLOR, (c_float * 4)(0.53, 0.81, 0.98, 1)) |
|
... |
... |
def setup_fog(): |
458 |
512 |
glFogf(GL_FOG_START, 20.0) |
glFogf(GL_FOG_START, 20.0) |
459 |
513 |
glFogf(GL_FOG_END, 60.0) |
glFogf(GL_FOG_END, 60.0) |
460 |
514 |
|
|
|
515 |
|
|
461 |
516 |
def setup(): |
def setup(): |
462 |
517 |
glClearColor(0.53, 0.81, 0.98, 1) |
glClearColor(0.53, 0.81, 0.98, 1) |
463 |
518 |
glEnable(GL_CULL_FACE) |
glEnable(GL_CULL_FACE) |
|
... |
... |
def setup(): |
465 |
520 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) |
466 |
521 |
setup_fog() |
setup_fog() |
467 |
522 |
|
|
|
523 |
|
|
468 |
524 |
def main(): |
def main(): |
469 |
525 |
window = Window(width=800, height=600, caption='Pyglet', resizable=True) |
window = Window(width=800, height=600, caption='Pyglet', resizable=True) |
470 |
526 |
window.set_exclusive_mouse(True) |
window.set_exclusive_mouse(True) |
471 |
527 |
setup() |
setup() |
472 |
528 |
pyglet.app.run() |
pyglet.app.run() |
473 |
529 |
|
|
|
530 |
|
|
474 |
531 |
if __name__ == '__main__': |
if __name__ == '__main__': |
475 |
532 |
main() |
main() |