hexbattle / scene.py

#!/usr/bin/env python2
# encoding: utf-8

"""An example scene.

Goal: Keep as much logic in the scenes themselves as possible. Keep the core lean.

Every scene needs 2 functions:

* start(dt=0): start the scene.
* resume(dt=0): resume the scenes events after it was paused.

To pause a scene,

* append its resume function to core.paused_scene_resume_functions and
* stop its events and store them in a way which tells the resume function how to start them again.

When the scene gets reactivated, its resume function gets popped from the paused_scene_resume_functions and then scheduled.

To remove activated events from the event stack, use core.win.remove_handler(handler). See http://pyglet.org/doc/programming_guide/stacking_event_handlers.html and http://pyglet.org/doc/api/pyglet.event.EventDispatcher-class.html#remove_handler

Both functions have to take the optional dt parameter, so they can be properly scheduled with pyglet.clock.schedule_once(event, delay)

"""

# shared data
from core import core, __copyright__
# event scheduling functions → interactivity
from pyglet.clock import schedule, schedule_once, schedule_interval, unschedule

visible = []
active = True
paused = False
events_to_start = [] #: [(delay, event), …]

def schedule_event(event, delay):
    global active
    global paused
    if paused:
        return events_to_start.append((delay, event))
    if active:
        return schedule_once(event, delay)

def switch_to_intro():
    import intro
    schedule_once(intro.start, 0.01)


## Necessary functions for the simple scene interaction API

def start(dt=0):
    """Start the scene."""
    switch_to_intro()

def resume(dt=0):
    active = True
    paused = False
    for delay, event in events_to_start:
        schedule_event(event, delay)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.