mirror of
https://github.com/quantum5/punyverse.git
synced 2025-04-24 13:11:57 -04:00
Clean up game loading code.
This commit is contained in:
parent
e33fad8bbb
commit
c205e1b0af
|
@ -44,6 +44,8 @@ def entity_distance(x0, y0, z0):
|
||||||
|
|
||||||
|
|
||||||
class Applet(pyglet.window.Window):
|
class Applet(pyglet.window.Window):
|
||||||
|
asteroids = ['asteroids/01.obj', 'asteroids/02.obj', 'asteroids/03.obj']
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.world_options = kwargs.pop('world_options', {})
|
self.world_options = kwargs.pop('world_options', {})
|
||||||
|
|
||||||
|
@ -66,7 +68,6 @@ class Applet(pyglet.window.Window):
|
||||||
]) + '\n\n' + info
|
]) + '\n\n' + info
|
||||||
|
|
||||||
self.loaded = False
|
self.loaded = False
|
||||||
self.__load_started = False
|
|
||||||
self._loading_phase = pyglet.text.Label(
|
self._loading_phase = pyglet.text.Label(
|
||||||
font_name='Consolas', font_size=20, x=10, y=self.height - 50,
|
font_name='Consolas', font_size=20, x=10, y=self.height - 50,
|
||||||
color=(255, 255, 255, 255), width=self.width - 20, align='center',
|
color=(255, 255, 255, 255), width=self.width - 20, align='center',
|
||||||
|
@ -84,23 +85,17 @@ class Applet(pyglet.window.Window):
|
||||||
)
|
)
|
||||||
pyglet.clock.schedule_once(self.load, 0)
|
pyglet.clock.schedule_once(self.load, 0)
|
||||||
|
|
||||||
def load(self, *args, **kwargs):
|
def _load_callback(self, phase, message, progress):
|
||||||
if self.loaded or self.__load_started:
|
print(message)
|
||||||
return
|
|
||||||
|
|
||||||
self.__load_started = True
|
|
||||||
|
|
||||||
def callback(phase, message, progress):
|
|
||||||
self.draw_loading(phase, message, progress)
|
self.draw_loading(phase, message, progress)
|
||||||
self.flip()
|
self.flip()
|
||||||
self.dispatch_events()
|
self.dispatch_events()
|
||||||
|
|
||||||
|
def load(self, *args, **kwargs):
|
||||||
start = clock()
|
start = clock()
|
||||||
self.fps = 0
|
self.fps = 0
|
||||||
self.world = World('world.json', callback, self.world_options)
|
self.world = World('world.json', self._load_callback, self.world_options)
|
||||||
phase = 'Initializing game...'
|
self._load_callback('Initializing game...', '', 0)
|
||||||
print(phase)
|
|
||||||
callback(phase, '', 0)
|
|
||||||
self.speed = INITIAL_SPEED
|
self.speed = INITIAL_SPEED
|
||||||
self.keys = set()
|
self.keys = set()
|
||||||
self.info = True
|
self.info = True
|
||||||
|
@ -212,23 +207,15 @@ class Applet(pyglet.window.Window):
|
||||||
glLightfv(GL_LIGHT1, GL_DIFFUSE, fv4(.5, .5, .5, 1))
|
glLightfv(GL_LIGHT1, GL_DIFFUSE, fv4(.5, .5, .5, 1))
|
||||||
glLightfv(GL_LIGHT1, GL_SPECULAR, fv4(1, 1, 1, 1))
|
glLightfv(GL_LIGHT1, GL_SPECULAR, fv4(1, 1, 1, 1))
|
||||||
|
|
||||||
phase = 'Loading asteroids...'
|
for id, file in enumerate(self.asteroids):
|
||||||
print(phase)
|
self._load_callback('Loading asteroids...', 'Loading %s...' % file, float(id) / len(self.asteroids))
|
||||||
|
|
||||||
def load_asteroids(files):
|
|
||||||
for id, file in enumerate(files):
|
|
||||||
callback(phase, 'Loading %s...' % file, float(id) / len(files))
|
|
||||||
Asteroid.load_asteroid(file)
|
Asteroid.load_asteroid(file)
|
||||||
|
|
||||||
load_asteroids(['asteroids/01.obj', 'asteroids/02.obj', 'asteroids/03.obj'])
|
|
||||||
|
|
||||||
c = self.cam
|
c = self.cam
|
||||||
c.x, c.y, c.z = self.world.start
|
c.x, c.y, c.z = self.world.start
|
||||||
c.pitch, c.yaw, c.roll = self.world.direction
|
c.pitch, c.yaw, c.roll = self.world.direction
|
||||||
|
|
||||||
phase = 'Updating entities...'
|
self._load_callback('Updating entities...', '', 0)
|
||||||
print(phase)
|
|
||||||
callback(phase, '', 0)
|
|
||||||
for entity in self.world.tracker:
|
for entity in self.world.tracker:
|
||||||
entity.update()
|
entity.update()
|
||||||
|
|
||||||
|
@ -386,7 +373,7 @@ class Applet(pyglet.window.Window):
|
||||||
progress_bar(10, self.height - 140, self.width - 20, 50, progress)
|
progress_bar(10, self.height - 140, self.width - 20, 50, progress)
|
||||||
self._info_label.draw()
|
self._info_label.draw()
|
||||||
|
|
||||||
def on_draw(self, glMatrixBuffer=GLfloat * 16):
|
def on_draw(self):
|
||||||
if not self.loaded:
|
if not self.loaded:
|
||||||
return self.draw_loading()
|
return self.draw_loading()
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from __future__ import print_function
|
from __future__ import division
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
@ -74,7 +74,6 @@ class World(object):
|
||||||
self._objects += 1
|
self._objects += 1
|
||||||
count_objects(body.get('satellites', {}))
|
count_objects(body.get('satellites', {}))
|
||||||
count_objects(root['bodies'])
|
count_objects(root['bodies'])
|
||||||
print(self._objects, 'objects to be loaded...')
|
|
||||||
|
|
||||||
if 'start' in root:
|
if 'start' in root:
|
||||||
info = root['start']
|
info = root['start']
|
||||||
|
@ -88,27 +87,20 @@ class World(object):
|
||||||
self.direction = (pitch, yaw, roll)
|
self.direction = (pitch, yaw, roll)
|
||||||
|
|
||||||
for planet, info in six.iteritems(root['bodies']):
|
for planet, info in six.iteritems(root['bodies']):
|
||||||
message = 'Loading %s.' % planet
|
|
||||||
print(message)
|
|
||||||
self.callback('Loading objects (%d of %d)...' % (self._current_object, self._objects),
|
self.callback('Loading objects (%d of %d)...' % (self._current_object, self._objects),
|
||||||
message, float(self._current_object) / self._objects)
|
'Loading %s.' % planet, self._current_object / self._objects)
|
||||||
self._body(planet, info)
|
self._body(planet, info)
|
||||||
self._current_object += 1
|
self._current_object += 1
|
||||||
|
|
||||||
if 'belts' in root:
|
if 'belts' in root:
|
||||||
self._phase = 'Loading belts...'
|
belt_count = len(root['belts'])
|
||||||
self._current_object = 0
|
for i, (name, info) in enumerate(six.iteritems(root['belts']), 1):
|
||||||
for name, info in six.iteritems(root['belts']):
|
self.callback('Loading belts (%d of %d)...' % (i, belt_count),
|
||||||
message = 'Loading %s.' % name
|
'Loading %s.' % name, i / belt_count)
|
||||||
print(message)
|
|
||||||
self.callback(self._phase, message, float(self._current_object) / len(root['belts']))
|
|
||||||
self.tracker.append(Belt(name, self, info))
|
self.tracker.append(Belt(name, self, info))
|
||||||
|
|
||||||
if 'sky' in root:
|
if 'sky' in root:
|
||||||
self._phase = 'Loading sky...'
|
self.callback('Loading sky...', 'Loading sky.', 0)
|
||||||
message = 'Loading sky.'
|
|
||||||
print(message)
|
|
||||||
self.callback(self._phase, message, 0)
|
|
||||||
self.tracker.append(Sky(self, root['sky']))
|
self.tracker.append(Sky(self, root['sky']))
|
||||||
|
|
||||||
def _body(self, name, info, parent=None):
|
def _body(self, name, info, parent=None):
|
||||||
|
@ -117,8 +109,7 @@ class World(object):
|
||||||
elif 'model' in info:
|
elif 'model' in info:
|
||||||
body = ModelBody(name, self, info, parent)
|
body = ModelBody(name, self, info, parent)
|
||||||
else:
|
else:
|
||||||
print('Nothing to load for %s.' % name)
|
raise ValueError('Nothing to load for %s.' % name)
|
||||||
return
|
|
||||||
|
|
||||||
if parent:
|
if parent:
|
||||||
parent.satellites.append(body)
|
parent.satellites.append(body)
|
||||||
|
@ -126,9 +117,7 @@ class World(object):
|
||||||
self.tracker.append(body)
|
self.tracker.append(body)
|
||||||
|
|
||||||
for satellite, info in six.iteritems(info.get('satellites', {})):
|
for satellite, info in six.iteritems(info.get('satellites', {})):
|
||||||
message = 'Loading %s, satellite of %s.' % (satellite, name)
|
|
||||||
print(message)
|
|
||||||
self.callback('Loading objects (%d of %d)...' % (self._current_object, self._objects),
|
self.callback('Loading objects (%d of %d)...' % (self._current_object, self._objects),
|
||||||
message, float(self._current_object) / self._objects)
|
'Loading %s, satellite of %s.' % (satellite, name), self._current_object / self._objects)
|
||||||
self._body(satellite, info, body)
|
self._body(satellite, info, body)
|
||||||
self._current_object += 1
|
self._current_object += 1
|
||||||
|
|
Loading…
Reference in a new issue