pgfw/pgfw/GameChild.py

86 lines
2.6 KiB
Python

from os.path import exists, join, basename, normpath, abspath
from sys import argv
from pygame import mixer, event
from pygame.locals import *
import Game
class GameChild:
def __init__(self, parent=None):
self.parent = parent
def get_game(self):
current = self
while not isinstance(current, Game.Game):
current = current.parent
return current
def get_configuration(self, section=None, option=None):
config = self.get_game().configuration
if option and section:
return config.get(section, option)
if section:
return config.get_section(section)
return config
def get_input(self):
return self.get_game().input
def get_screen(self):
return self.get_game().display.get_screen()
def get_audio(self):
return self.get_game().audio
def get_delegate(self):
return self.get_game().delegate
def get_resource(self, section, option):
config = self.get_configuration()
rel_path = config.get(section, option)
for root in config.get("setup", "resources-search-path"):
if self.is_shared_mode() and not self.is_absolute_path(root):
continue
path = join(root, rel_path)
if exists(path):
return path
self.print_debug("Couldn't find resource: {0}, {1}".\
format(section, option))
def print_debug(self, statement):
if self.is_debug_mode():
print statement
def is_absolute_path(self, path):
return normpath(path) == abspath(path)
def is_shared_mode(self):
return "-s" in argv
def subscribe_to(self, kind, callback):
self.get_game().delegate.add_subscriber(kind, callback)
def unsubscribe_from(self, kind, callback):
self.get_game().delegate.remove_subscriber(kind, callback)
def is_debug_mode(self):
return "-d" in argv
def get_custom_event_id(self):
return globals()[self.get_configuration().get("event",
"custom-event-id")]
def is_command(self, evt, cmd):
name = self.get_configuration().get("event", "command-event-name")
if not isinstance(cmd, list):
cmd = [cmd]
return evt.type == self.get_custom_event_id() and evt.name == name and \
evt.command in cmd
def post_command(self, command):
name = self.get_configuration().get("event", "command-event-name")
event.post(event.Event(self.get_custom_event_id(), name=name,
command=command))