123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- # Flexlay - A Generic 2D Game Editor
- # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
- #
- # 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 3 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, see <http://www.gnu.org/licenses/>.
- from typing import Any, IO, Optional
- import os
- import logging
- from flexlay.util.config import Config
- from flexlay.util.sexpr_reader import get_value_from_tree, sexpr_filter
- from flexlay.util.sexpr_writer import SExprWriter
- from supertux.sector import Sector
- from supertux.util import load_lisp
- class Level:
- @staticmethod
- def from_file(filename: str) -> 'Level':
- level = Level()
- if filename.endswith(".stwm"):
- level.is_worldmap = True
- else:
- level.is_worldmap = False
- level.filename = filename
- tree: Any = load_lisp(level.filename, "supertux-level")
- data = tree[1:]
- level.version = get_value_from_tree(["version", "_"], data, 0)
- # print("VERSION:", level.filename, " ", level.version)
- if level.version == 1:
- raise Exception("version 1 levels not supported at the moment")
- else:
- level.parse_v2(data)
- return level
- @staticmethod
- def from_size(width: int, height: int) -> 'Level':
- result = Level()
- result.current_sector = Sector(result)
- result.current_sector.new_from_size("main", width, height)
- result.sectors.append(result.current_sector)
- return result
- def __init__(self) -> None:
- self.version: int = 2
- self.filename: Optional[str] = None
- self.name: str = "No Name"
- self.author: str = "No Author"
- self.contact: str = ""
- self.license = "GPL 2+ / CC-by-sa 3.0"
- self.target_time: int = 0
- self.current_sector: Optional[Sector] = None
- self.sectors: list[Sector] = []
- self.tileset_path: str = os.path.join("images", "tiles.strf")
- self.is_worldmap: bool = False
- self.music: str
- self.background: str
- self.width: int
- self.height: int
- def parse_v2(self, data: Any) -> None:
- self.name = get_value_from_tree(["name", "_"], data, "")
- self.author = get_value_from_tree(["author", "_"], data, "")
- self.contact = get_value_from_tree(["contact", "_"], data, "")
- self.license = get_value_from_tree(["license", "_"], data, "")
- self.target_time = get_value_from_tree(["target-time", "_"], data, 0)
- self.tileset_path = get_value_from_tree(["tileset", "_"], data, os.path.join("images", "tiles.strf"))
- # self.tileset_path = get_value_from_tree(["tileset", "_"], data, "images/tiles.strf")
- # self.tileset_path = os.path.join(Config.current.datadir, self.tileset_path)
- # Check tileset path is somewhat valid
- if len(self.tileset_path) < 1:
- self.tileset_path = os.path.join("images", "tiles.strf")
- # Sort out tileset path beginning with os.sep
- if self.tileset_path[0] == os.sep:
- if len(self.tileset_path) > 1:
- self.tileset_path = self.tileset_path[1:] # Remove os.sep
- else:
- self.tileset_path = os.path.join("images", "tiles.strf") # Reset to default
- self.current_sector = None
- self.sectors = []
- for sec in sexpr_filter("sector", data):
- sector = Sector(self)
- sector.load_v2(sec)
- self.sectors.append(sector)
- if sector.name == "main":
- self.current_sector = sector
- if self.current_sector is None:
- logging.error("Error: No main sector defined: " + str(self.sectors))
- self.current_sector = self.sectors[0]
- def save(self, filename: str) -> None:
- with open(filename, "w") as f:
- self.save_io(f)
- def save_io(self, f: IO[str]) -> None:
- writer = SExprWriter(f)
- # writer.write_comment("Generated by Flexlay Level Editor (Development Version)")
- writer.begin_list("supertux-level")
- writer.write_int("version", 2)
- writer.write_tr_string("name", self.name)
- writer.write_string("author", self.author)
- if self.contact:
- writer.write_string("contact", self.contact)
- if self.license:
- writer.write_string("license", self.license)
- if self.target_time != 0:
- writer.write_int("target-time", self.target_time)
- for sector in self.sectors:
- writer.begin_list("sector")
- sector.write(writer)
- writer.end_list()
- tileset_path = self.tileset_path
- assert Config.current is not None
- if tileset_path[:len(Config.current.datadir)] == Config.current.datadir:
- tileset_path = tileset_path.replace(os.path.join(Config.current.datadir, "/"), "", 1)
- if tileset_path is not None:
- if tileset_path != "images/tiles.strf":
- writer.write_string("tileset", tileset_path)
- writer.end_list()
- def add_sector(self, sector: Sector) -> None:
- self.sectors.append(sector)
- def remove_sector(self, name: str) -> None:
- if len(self.sectors) > 1:
- self.sectors = [sec for sec in self.sectors if sec.name != name]
- else:
- logging.warning("Only one sector left, can't delete it")
- def get_sectors(self) -> list[str]:
- return [sec.name for sec in self.sectors]
- # EOF #
|