Beispiel #1
0
    def initialize(self):
        """Initialize the engine and all managers"""
        logging.info("Creating systems...")
        self.entity_manager = EntityManager(self)
        self.block_system = BlockSystem(self)
        self.graphics_system = GraphicsSystem(self)
        self.camera_system = CameraSystem(self)
        self.input_system = InputSystem(self)
        self.physics_system = PhysicsSystem(self)
        self.test_system = TestSystem(self)

        logging.info("Initializing...")
        self.entity_manager.initialize()
        self.block_system.initialize()
        self.graphics_system.initialize()
        self.camera_system.initialize()
        self.input_system.initialize()
        self.physics_system.initialize()
Beispiel #2
0
class Engine(EngineObject):
    """An engine is the main controller of the game.
    Handles startup, looping, states.
    Has pointers to all managers.
    """


    class State(object):
        """Enumeration of possible engine states"""
        RELEASED = 'RELEASED'
        MINIMAL = 'MINIMAL'
        MAINMENU = 'MAINMENU'
        GAMEPLAY = 'GAMEPLAY'
    
    def __init__(self, local_options):
        """Contructor"""
        logging.info("Initializing engine.")
        r = random.Random()
        r.seed(time.time())
        self.execution_handle = abs(int(r.getrandbits(32)))
        self.local_options = local_options
        self.state = self.State.RELEASED
        EngineObject.__init__(self, self)
        

    def transition(self, state):
        """Handle state transitions"""
        ss = (self.state, state)
        if ss == (self.State.RELEASED, self.State.MINIMAL):
            self.initialize()
            self.crosslink_singletons()
        elif ss == (self.State.MINIMAL, self.State.MAINMENU):
            self.init_engine()
            self.init_menu()
        elif ss == (self.State.MAINMENU, self.State.GAMEPLAY):
            self.load_level()
        elif ss == (self.State.GAMEPLAY, self.State.MAINMENU):
            self.release_level()
        elif ss == (self.State.MAINMENU, self.State.RELEASED):
            self.release_engine()
            sys.exit(0)
        else:
            raise Exception('Invalid engine state transition: %s' % ss)
        self.state = state
        
    def initialize(self):
        """Initialize the engine and all managers"""
        logging.info("Creating systems...")
        self.entity_manager = EntityManager(self)
        self.block_system = BlockSystem(self)
        self.graphics_system = GraphicsSystem(self)
        self.camera_system = CameraSystem(self)
        self.input_system = InputSystem(self)
        self.physics_system = PhysicsSystem(self)
        self.test_system = TestSystem(self)

        logging.info("Initializing...")
        self.entity_manager.initialize()
        self.block_system.initialize()
        self.graphics_system.initialize()
        self.camera_system.initialize()
        self.input_system.initialize()
        self.physics_system.initialize()

    def crosslink(self):
        """Hook this engine up to the managers"""
        pass

    def crosslink_singletons(self):
        """Execute all crosslinks on managers and the engine"""
        logging.info("Crosslinking...")
        self.crosslink()
        self.entity_manager.crosslink()
        self.block_system.crosslink()
        self.graphics_system.crosslink()
        self.camera_system.crosslink()
        self.input_system.crosslink()
        self.physics_system.crosslink()

    def init_engine(self):
        """Initialize all managers with the engine"""
        logging.info("Initializing the engine...")
        self.entity_manager.init_engine()
        self.block_system.init_engine()
        self.graphics_system.init_engine()
        self.camera_system.init_engine()
        self.input_system.init_engine()
        self.physics_system.init_engine()

        self.entity_manager.init_engine_post()
        self.block_system.init_engine()
        self.graphics_system.init_engine_post()
        self.camera_system.init_engine_post()
        self.input_system.init_engine_post()
        self.physics_system.init_engine_post()

    def init_menu(self):
        """Initialize all managers for menus"""
        logging.info("Initiailizing the menu...")
        self.entity_manager.init_menu()
        self.block_system.init_menu()
        self.graphics_system.init_menu()
        self.camera_system.init_menu()
        self.input_system.init_menu()
        self.physics_system.init_menu()

    def load_level(self):
        """Load level in all managers"""
        logging.info("Loading the level...")
        self.physics_system.load_level()
        self.entity_manager.load_level()
        self.block_system.load_level()
        self.graphics_system.load_level()
        self.camera_system.load_level()
        self.input_system.load_level()
        self.test_system.load_level()
        #load_world(self.block_system)

    def release_level(self):
        """Release level in all managers"""
        logging.info("Releasing the level...")
        save_world(self.block_system.chunks)
        self.graphics_system.release_level()
        self.entity_manager.release_level()
        self.block_system.release_level()
        self.camera_system.release_level()
        self.input_system.release_level()
        self.physics_system.release_level()

    def release_engine(self):
        """Prepare to shutdown the engine"""
        logging.info("Shutting down the engine...")
        self.input_system.release_engine()
        self.graphics_system.release_engine()
        self.camera_system.release_engine()
        self.entity_manager.release_engine()
        self.block_system.release_engine()
        self.physics_system.release_engine()

    def main_loop(self):
        """ Execute the main game loop! """
        last_time = time.time()
        dtime = 0.0
        weu = ogre.WindowEventUtilities()
        weu.messagePump()
        logging.info("Entering the main loop. Game on.")
        while True:
            self._main_step(dtime)
            self.render()
            weu.messagePump()
            # sleeps to keep a consistent FPS
            new_time = self._sleep_for_fps_balance(last_time)
            dtime = new_time - last_time
            last_time = new_time

    def _sleep_for_fps_balance(self, last_time):
        now = time.time()
        dtime = now - last_time
        sleep_time = max(1.0 / float(MAX_FPS) - dtime, 0)
        time.sleep(sleep_time)
        return now
            
    def _main_step(self, dtime):
        """run a single loop

        pre:
            is_nat(dtime)
        """
        self.input_system.tick(dtime)
        self.entity_manager.tick(dtime)
        self.block_system.tick(dtime)
        self.graphics_system.tick(dtime)
        self.camera_system.tick(dtime)
        self.physics_system.tick(dtime)

    def render(self):
        """Dispatch render request to all managers"""
        self.input_system.render()
        self.entity_manager.render()
        self.block_system.render()
        self.graphics_system.render()
        self.camera_system.render()
        self.physics_system.render()