Example #1
0
def map_exists(mapname, asset_type=AssetType.MAP):
    try:
        map_path = get_asset(asset_type, mapname)
        with open(map_path):
            return True
    except IOError:
        return False
Example #2
0
    def save_settings_to_file(self, settings):
        try:
            settings_path = get_asset(AssetType.ATTRIBUTES, "settings.cfg")

            with open(settings_path, 'w') as settings_file:
                for key, value in settings.items():
                    settings_file.write("{} {}\n".format(key.value, value))
        except IOError as e:
            print("Unable to save settings file. Exception: {}".format(e))
Example #3
0
def load_map_from_file(mapname, asset_type=AssetType.MAP):
    try:
        map_path = get_asset(asset_type, mapname)

        with open(map_path) as mapfile:
            return parse_map_from_string(mapfile.read())
    except IOError as e:
        print("Unable to load file {}. Generating new map. Exception: {}".format(mapname, e))
        return generate_map()
Example #4
0
def read_upgrade_attributes_from_file():
    try:
        attributes_path = get_asset(AssetType.ATTRIBUTES, "upgradeattributes.cfg")

        with open(attributes_path) as attributes_file:
            return json.loads(attributes_file.read().replace('\n', ''), object_hook=as_enum)
    except IOError as e:
        print("Unable to load upgrade attributes. Exception: {}".format(e))
        return None
Example #5
0
def get_logger(name, level, filename):
    handler = logging.FileHandler(get_asset(AssetType.LOG, filename))
    handler.setFormatter(formatter)

    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(handler)

    return logger
Example #6
0
def load_campaign_progress():
    try:
        all_maps = get_loadable_maps(AssetType.CAMPAIGN_MAP)
        progress_path = get_asset(AssetType.ATTRIBUTES, "campaign.cfg")
        progress = []

        with open(progress_path) as progress_file:
            lines = progress_file.read()
            for line in StringIO(lines):
                # Format: <mapname>
                values = line.rstrip().split(' ')
                if len(values) == 1 and values[0] in all_maps:
                    progress.append(values[0])
        return progress
    except (IOError, KeyError) as e:
        print("Unable to load campaign file. Exception: {}".format(e))
Example #7
0
    def load_settings_from_file(self):
        try:
            settings_path = get_asset(AssetType.ATTRIBUTES, "settings.cfg")
            file_settings = {}

            with open(settings_path) as settings_file:
                lines = settings_file.read()
                for line in StringIO(lines):
                    values = line.split(' ')
                    if len(values) == 2:
                        setting = Setting[values[0]]
                        if setting in numeric_settings:
                            file_settings[setting] = int(values[1])
                        else:
                            file_settings[setting] = values[1].rstrip()
            return file_settings
        except (IOError, KeyError) as e:
            print("Unable to load settings file. Using defaults. Exception: {}".format(e))
            return default_settings.copy()
Example #8
0
def save_campaign_progress(map_name):
    all_maps = get_loadable_maps(AssetType.CAMPAIGN_MAP)
    progress = load_campaign_progress()

    # Trim the map name if necessary (.sav -> .map)
    if map_name.endswith(".sav"):
        map_name = map_name[:-4] + ".map"

    # Update the progress structure
    if map_name in all_maps:
        progress.append(map_name)

    # Make the items unique
    progress = list(set(progress))

    # Serialize and save the progress structure back to the file
    try:
        progress_path = get_asset(AssetType.ATTRIBUTES, "campaign.cfg")

        with open(progress_path, 'w') as progress_file:
            for mapname in progress:
                progress_file.write("{}\n".format(mapname))
    except IOError as e:
        print("Unable to save campaign file. Exception: {}".format(e))
Example #9
0
def get_loadable_maps(asset_type=AssetType.MAP):
    maps = []
    for (_, _, filenames) in walk(get_asset(asset_type, "")):
        maps.extend([filename for filename in filenames if filename.endswith((".map", ".sav"))])

    return maps
Example #10
0
def check_log_dir_exists():
    path = Path(get_asset(AssetType.LOG))
    if not path.exists():
        path.mkdir(exist_ok=True)
Example #11
0
def load_sound(resource_name):
    sound = pygame.mixer.Sound(get_asset(AssetType.SOUND, resource_name))
    sound.set_volume(SETTINGS.get(Setting.SFX_VOLUME) / 100)
    all_sounds.append(sound)
    return sound
Example #12
0
# Load a sound and properly set it up
def load_sound(resource_name):
    sound = pygame.mixer.Sound(get_asset(AssetType.SOUND, resource_name))
    sound.set_volume(SETTINGS.get(Setting.SFX_VOLUME) / 100)
    all_sounds.append(sound)
    return sound


pygame.mixer.init()

# General
unit_palette = {
    Team.RED:
    generate_palette_list(
        pygame.image.load(
            get_asset(AssetType.SPRITE, "palettes/Red-Palette.png"))),
    Team.BLUE:
    generate_palette_list(
        pygame.image.load(
            get_asset(AssetType.SPRITE, "palettes/Blue-Palette.png"))),
    Team.GREEN:
    generate_palette_list(
        pygame.image.load(
            get_asset(AssetType.SPRITE, "palettes/Green-Palette.png"))),
    Team.YELLOW:
    generate_palette_list(
        pygame.image.load(
            get_asset(AssetType.SPRITE, "palettes/Yellow-Palette.png"))),
    Team.NONE:
    generate_palette_list(
        pygame.image.load(