hexbattle / core.py

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

"""The core, shared data by all scenes."""

__copyright__ = """ 
  Fungus - Simple game framework building on fungus.
----------------------------------------------------------------- 
© 2008 - 2010 Copyright by Arne Babenhauserheide

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  MA 02110-1301 USA

""" 

from pyglet.clock import schedule, schedule_once, schedule_interval
from pyglet.resource import path as media_paths
from pyglet.resource import reindex
# adapt the media paths: add the graphics folder.
media_paths.append("@graphics")
media_paths.append("@graphics.wesnoth")
reindex()
from pyglet.resource import image

### The core object: The basic carrier for persistent data between scenes.

class Core(object):
    """The basic data exchange object between scenes.

    This gets instantiated in this module, so core.core is a singleton,
    shared by all scenes."""
    def __init__(self):
        #: Scenes which are inactive. If a scene stops, pop the previous resume function.
        self.paused_scene_resume_functions = []

core = Core()

### Helper functions

from pyglet.sprite import Sprite as pyglet_sprite

class Sprite(pyglet_sprite):
    def __init__(self, image_path, x = 212, y = 208, update_func=None, *args, **kwds):
        """Create a simple sprite. 
	
It is a more conveniently callable version of the pyglet_sprite which loads its image itself.
-> For pyglet sprite usage see: http://www.pyglet.org/doc/api/pyglet.sprite.Sprite-class.html

@param image_path: the path to the image, relative to the graphics folder. 
@param x: horizontal position. 
@param y: vertical position. 
@param update_func: A function to update the Sprite position. If defined, it has to take x, y and return new x, y. 
"""	
	## Sprite image
	img = image(image_path)
	super(Sprite, self).__init__(img, x=x, y=y, *args, **kwds)

    def update(self):
        pass

def run(scenefile): 
    """Run a scene with game.py.

    For use from a scene to delegate running the scene to game.py:
    core.run(__file__)
"""
    from subprocess import call
    from os.path import basename
    from sys import argv
    call(["./game.py", "-s", basename(scenefile)] + argv[1:])
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.