From c205e1b0af539b98d53a0a89a6969964100157ee Mon Sep 17 00:00:00 2001 From: Quantum Date: Thu, 23 Aug 2018 22:26:25 -0400 Subject: [PATCH] Clean up game loading code. --- punyverse/game.py | 43 +++++++++++++++---------------------------- punyverse/world.py | 29 +++++++++-------------------- 2 files changed, 24 insertions(+), 48 deletions(-) diff --git a/punyverse/game.py b/punyverse/game.py index 131d099..cf3bf56 100644 --- a/punyverse/game.py +++ b/punyverse/game.py @@ -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() diff --git a/punyverse/world.py b/punyverse/world.py index 5e99444..0bf6b59 100644 --- a/punyverse/world.py +++ b/punyverse/world.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import division import os from collections import OrderedDict @@ -74,7 +74,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 +87,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 +109,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 +117,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