Exemplo n.º 1
0
    def __init__(self, name: str, data: Dict[str, Any]):
        self.name: str = name
        if not data["display"]:
            raise er.StructureParseError("No display for {}".format(name))

        self.fake_floor = data.get("fake_floor", False)

        self.display: List['displayer.Displayer'] = [
            displayer.parse(d, name) for d in data["display"]
        ]
        self.collision: List[int] = data[
            "collision"] if "collision" in data else EMPTY_COLLISION
        if len(self.collision) != 4:
            raise er.StructureParseError(
                "Invalid collision size ({}) in {} need 4 arg".format(
                    len(self.collision), name))

        self.collision_side: Dict[str, 'co.CollisionEvent'] = {}

        if "collision_side" in data:
            for key, value in data["collision_side"].items():
                name = value["id"]
                self.collision_side[key] = co.EVENTS[name](value)

        self.have_collision: bool = self.collision != EMPTY_COLLISION
Exemplo n.º 2
0
    def __init__(self, images_path: str, start: List[float],
                 data: Dict[str, Any], path: str):
        self.__images_path: str = images_path
        self.__start: List[float] = start

        # if game.DISPLAYER_CACHE.have(path):
        #     self.image: pygame.Surface = game.DISPLAYER_CACHE.get(path)
        # else:
        if game.IMAGE_CACHE.have(images_path):
            self.image: pygame.Surface = game.IMAGE_CACHE.get(images_path)
        else:
            self.image: pygame.Surface = pygame.image.load(
                "assets/textures/{}.png".format(images_path))
            game.IMAGE_CACHE.put(images_path, self.image)

        rescale = data["rescale"] if "rescale" in data else 2
        if "from" in data:
            d = data["from"]
            if isinstance(d, list) and len(d) == 2:
                plot_start = d
            else:
                raise er.StructureParseError(
                    "In valid from ({}) in {} need be [int, int]".format(
                        d, path))

            if "to" not in data:
                raise er.StructureParseError(
                    "No to in {} but there are from ned to [ini, int]".format(
                        path))
            d = data["to"]
            if isinstance(d, list) and len(d) == 2:
                plot_end = d
            else:
                raise er.StructureParseError(
                    "In valid to ({}) in {} need be [int, int]".format(
                        d, path))
            # self.image_size = (plot_end[0] - plot_start[0]) * rescale, (plot_end[1] - plot_start[1]) * rescale

            s = pygame.Surface(
                (plot_end[0] - plot_start[0], plot_end[1] - plot_start[1]),
                pygame.SRCALPHA)
            s.blit(self.image, (0, 0), (pygame.Rect(
                plot_start[0], plot_start[1], plot_end[0], plot_end[1])))
            self.image = s
        # elif rescale == 1:
        #     self.image_size = self.image.get_size()
        if rescale != 1:
            # if "from" not in data:
            #     self.image_size = int(self.image.get_size()[0] * rescale), int(self.image.get_size()[0] * rescale)
            s = self.image.get_rect().size
            self.image = pygame.transform.scale(
                self.image, (s[0] * rescale, s[1] * rescale))
            # game.IMAGE_CACHE.put(path, self.image)
        self.image_size = self.image.get_size()
Exemplo n.º 3
0
def parse(path: str):

    with open("data/build/{}.json".format(path), "r",
              encoding='utf-8') as file:
        data = json.load(file)

    if not data:
        raise er.StructureParseError("No data in {}".format(path))

    # same for the moment
    return Structure(path, data)
Exemplo n.º 4
0
def parse(data: Dict[str, Any], path: str) -> Displayer:
    image = data["image"]
    if not image:
        raise er.StructureParseError("No image from {}".format(path))
    start = data["start"] if "start" in data else DEFAULT_START
    return Displayer(image, start, data, path)