Source

Fat x Fast / FatxFast / levelsystem.py

# This file is part of FatxFast.
#
#    FatxFast 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 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast 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 FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.4"

import time
import copy
from fileio import LOGGER
from FatxFast.utils import xmlparser
from FatxFast.objects import triggers
import pygame

class LevelSchemeParser(xmlparser.XmlParser):

    def __init__(self, filename):
        super(LevelSchemeParser, self).__init__(filename)
        self._methodhooks = {"item_types": self._parse_items}

    def _parse_level_properties(self, node):
        properties = {}
        for prop in node:
            if prop.tag in self._methodhooks:
                value = self._methodhooks[prop.tag](prop)
            else:
                try:
                    value = int(prop.text)
                except ValueError:
                    value = prop.text
            properties[prop.tag] = value
        return properties

    def _parse_items(self, prop):
        return {item.text for item in prop}

    def _parse_level(self, node):
        level_data = {}
        level_data.update(self._get_attributes(node))
        if not "type" in level_data:
            LOGGER.log_message(
                "Faulty level definition. Map type must be specified")
            return False
        level_data["data"] = self._parse_level_properties(node)
        return level_data

    def load_mode_levels(self, mode):
        return [self._parse_level(level) for level in self.root.find(mode)]

class LevelSystem(object):
    def __init__(self, gamescene, levels=[]):
        super(LevelSystem, self).__init__()
        self.gamescene = gamescene
        self.level = 0
        self.levels = levels
        self.leveltime = 0

    def increase_level(self):
        if self.level < len(self.levels)-1:
            self.level += 1
            return True

    def decrease_level(self):
        if self.level > 0:
            self.level -= 1
            return True

    @property
    def level_complexity(self):
        if self.level < len(self.levels):
            data = self.levels[self.level]
        else:
            data = {}
        return data

    @level_complexity.setter
    def level_complexity(self, value):
        self.levels[self.level] = value

    def load_level(self):
        if self.level_complexity["type"] == "custom":
            self.gamescene.tilemap.load_file(
                self.level_complexity["data"]["filename"], 
                save=self.gamescene.save)
        elif self.level_complexity["type"] == "generated":
            kwargs = copy.deepcopy(self.gamescene.save.mapdata)
            if kwargs.get("minimap_overlay"):
                del kwargs["minimap_overlay"]
            if self.gamescene.save.game["level_complexity"]:
                kwargs.update(copy.deepcopy(
                    self.gamescene.save.game["level_complexity"]))
            kwargs.update(copy.deepcopy(self.level_complexity["data"]))
            self.gamescene.tilemap.load_generated(**kwargs)
        minimap = self.gamescene.get_widget("minimap")
        minimap.load_minimap()
        if self.gamescene.save.mapdata.get("minimap_overlay"):
            minimap.overlay_surf = pygame.image.frombuffer(
                self.gamescene.save.mapdata["minimap_overlay"], 
                minimap.overlay_surf.get_size(), 'RGBA')

        self.gamescene.save.mapdata = {}

    def load_level_scheme_from_file(self, filename="maps.xml"):
        pass
    
    def new_level(self):
        self.load_level()

    def reload_level(self): 
        self.load_level()