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
b094dd8905
|
@ -44,6 +44,8 @@ def entity_distance(x0, y0, z0):
|
|||
|
||||
|
||||
class Applet(pyglet.window.Window):
|
||||
asteroids = ['asteroids/01.obj', 'asteroids/02.obj', 'asteroids/03.obj']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.world_options = kwargs.pop('world_options', {})
|
||||
|
||||
|
@ -66,7 +68,6 @@ class Applet(pyglet.window.Window):
|
|||
]) + '\n\n' + info
|
||||
|
||||
self.loaded = False
|
||||
self.__load_started = False
|
||||
self._loading_phase = pyglet.text.Label(
|
||||
font_name='Consolas', font_size=20, x=10, y=self.height - 50,
|
||||
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)
|
||||
|
||||
def _load_callback(self, phase, message, progress):
|
||||
print(message)
|
||||
self.draw_loading(phase, message, progress)
|
||||
self.flip()
|
||||
self.dispatch_events()
|
||||
|
||||
def load(self, *args, **kwargs):
|
||||
if self.loaded or self.__load_started:
|
||||
return
|
||||
|
||||
self.__load_started = True
|
||||
|
||||
def callback(phase, message, progress):
|
||||
self.draw_loading(phase, message, progress)
|
||||
self.flip()
|
||||
self.dispatch_events()
|
||||
|
||||
start = clock()
|
||||
self.fps = 0
|
||||
self.world = World('world.json', callback, self.world_options)
|
||||
phase = 'Initializing game...'
|
||||
print(phase)
|
||||
callback(phase, '', 0)
|
||||
self.world = World('world.json', self._load_callback, self.world_options)
|
||||
self._load_callback('Initializing game...', '', 0)
|
||||
self.speed = INITIAL_SPEED
|
||||
self.keys = set()
|
||||
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_SPECULAR, fv4(1, 1, 1, 1))
|
||||
|
||||
phase = 'Loading asteroids...'
|
||||
print(phase)
|
||||
|
||||
def load_asteroids(files):
|
||||
for id, file in enumerate(files):
|
||||
callback(phase, 'Loading %s...' % file, float(id) / len(files))
|
||||
Asteroid.load_asteroid(file)
|
||||
|
||||
load_asteroids(['asteroids/01.obj', 'asteroids/02.obj', 'asteroids/03.obj'])
|
||||
for id, file in enumerate(self.asteroids):
|
||||
self._load_callback('Loading asteroids...', 'Loading %s...' % file, float(id) / len(self.asteroids))
|
||||
Asteroid.load_asteroid(file)
|
||||
|
||||
c = self.cam
|
||||
c.x, c.y, c.z = self.world.start
|
||||
c.pitch, c.yaw, c.roll = self.world.direction
|
||||
|
||||
phase = 'Updating entities...'
|
||||
print(phase)
|
||||
callback(phase, '', 0)
|
||||
self._load_callback('Updating entities...', '', 0)
|
||||
for entity in self.world.tracker:
|
||||
entity.update()
|
||||
|
||||
|
@ -386,7 +373,7 @@ class Applet(pyglet.window.Window):
|
|||
progress_bar(10, self.height - 140, self.width - 20, 50, progress)
|
||||
self._info_label.draw()
|
||||
|
||||
def on_draw(self, glMatrixBuffer=GLfloat * 16):
|
||||
def on_draw(self):
|
||||
if not self.loaded:
|
||||
return self.draw_loading()
|
||||
|
||||
|
|
|
@ -1,16 +1,9 @@
|
|||
from __future__ import print_function
|
||||
from __future__ import division
|
||||
|
||||
import json
|
||||
import os
|
||||
from collections import OrderedDict
|
||||
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
try:
|
||||
import simplejson as json
|
||||
except ImportError:
|
||||
raise SystemExit('No JSON module found')
|
||||
|
||||
import six
|
||||
|
||||
try:
|
||||
|
@ -39,7 +32,6 @@ class World(object):
|
|||
|
||||
self.callback = callback
|
||||
self.options = options or {}
|
||||
self._phase = 'Parsing configuration...'
|
||||
self._parse(file)
|
||||
del self.callback # So it can't be used after loading finishes
|
||||
|
||||
|
@ -55,7 +47,7 @@ class World(object):
|
|||
return self._au
|
||||
|
||||
def _parse(self, file):
|
||||
self.callback(self._phase, 'Loading configuration file...', 0)
|
||||
self.callback('Parsing configuration...', 'Loading configuration file...', 0)
|
||||
with open(os.path.join(os.path.dirname(__file__), file)) as f:
|
||||
root = json.load(f, object_pairs_hook=OrderedDict)
|
||||
self._au = root.get('au', 2000)
|
||||
|
@ -74,7 +66,6 @@ class World(object):
|
|||
self._objects += 1
|
||||
count_objects(body.get('satellites', {}))
|
||||
count_objects(root['bodies'])
|
||||
print(self._objects, 'objects to be loaded...')
|
||||
|
||||
if 'start' in root:
|
||||
info = root['start']
|
||||
|
@ -88,27 +79,20 @@ class World(object):
|
|||
self.direction = (pitch, yaw, roll)
|
||||
|
||||
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),
|
||||
message, float(self._current_object) / self._objects)
|
||||
'Loading %s.' % planet, self._current_object / self._objects)
|
||||
self._body(planet, info)
|
||||
self._current_object += 1
|
||||
|
||||
if 'belts' in root:
|
||||
self._phase = 'Loading belts...'
|
||||
self._current_object = 0
|
||||
for name, info in six.iteritems(root['belts']):
|
||||
message = 'Loading %s.' % name
|
||||
print(message)
|
||||
self.callback(self._phase, message, float(self._current_object) / len(root['belts']))
|
||||
belt_count = len(root['belts'])
|
||||
for i, (name, info) in enumerate(six.iteritems(root['belts']), 1):
|
||||
self.callback('Loading belts (%d of %d)...' % (i, belt_count),
|
||||
'Loading %s.' % name, i / belt_count)
|
||||
self.tracker.append(Belt(name, self, info))
|
||||
|
||||
if 'sky' in root:
|
||||
self._phase = 'Loading sky...'
|
||||
message = 'Loading sky.'
|
||||
print(message)
|
||||
self.callback(self._phase, message, 0)
|
||||
self.callback('Loading sky...', 'Loading sky.', 0)
|
||||
self.tracker.append(Sky(self, root['sky']))
|
||||
|
||||
def _body(self, name, info, parent=None):
|
||||
|
@ -117,8 +101,7 @@ class World(object):
|
|||
elif 'model' in info:
|
||||
body = ModelBody(name, self, info, parent)
|
||||
else:
|
||||
print('Nothing to load for %s.' % name)
|
||||
return
|
||||
raise ValueError('Nothing to load for %s.' % name)
|
||||
|
||||
if parent:
|
||||
parent.satellites.append(body)
|
||||
|
@ -126,9 +109,7 @@ class World(object):
|
|||
self.tracker.append(body)
|
||||
|
||||
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),
|
||||
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._current_object += 1
|
||||
|
|
Loading…
Reference in a new issue