added support for animated texture

This commit is contained in:
Faraphel 2023-02-21 15:19:43 +01:00
parent d34d610b98
commit 6eb8d2452d
34 changed files with 81 additions and 43 deletions

View file

Before

Width:  |  Height:  |  Size: 156 B

After

Width:  |  Height:  |  Size: 156 B

View file

Before

Width:  |  Height:  |  Size: 572 B

After

Width:  |  Height:  |  Size: 572 B

View file

Before

Width:  |  Height:  |  Size: 609 B

After

Width:  |  Height:  |  Size: 609 B

View file

Before

Width:  |  Height:  |  Size: 644 B

After

Width:  |  Height:  |  Size: 644 B

View file

Before

Width:  |  Height:  |  Size: 695 B

After

Width:  |  Height:  |  Size: 695 B

View file

Before

Width:  |  Height:  |  Size: 712 B

After

Width:  |  Height:  |  Size: 712 B

View file

Before

Width:  |  Height:  |  Size: 753 B

After

Width:  |  Height:  |  Size: 753 B

View file

Before

Width:  |  Height:  |  Size: 766 B

After

Width:  |  Height:  |  Size: 766 B

View file

Before

Width:  |  Height:  |  Size: 708 B

After

Width:  |  Height:  |  Size: 708 B

View file

Before

Width:  |  Height:  |  Size: 691 B

After

Width:  |  Height:  |  Size: 691 B

View file

Before

Width:  |  Height:  |  Size: 533 B

After

Width:  |  Height:  |  Size: 533 B

View file

Before

Width:  |  Height:  |  Size: 192 B

After

Width:  |  Height:  |  Size: 192 B

View file

Before

Width:  |  Height:  |  Size: 418 B

After

Width:  |  Height:  |  Size: 418 B

View file

Before

Width:  |  Height:  |  Size: 339 B

After

Width:  |  Height:  |  Size: 339 B

View file

Before

Width:  |  Height:  |  Size: 264 B

After

Width:  |  Height:  |  Size: 264 B

View file

Before

Width:  |  Height:  |  Size: 213 B

After

Width:  |  Height:  |  Size: 213 B

View file

Before

Width:  |  Height:  |  Size: 231 B

After

Width:  |  Height:  |  Size: 231 B

View file

Before

Width:  |  Height:  |  Size: 281 B

After

Width:  |  Height:  |  Size: 281 B

View file

Before

Width:  |  Height:  |  Size: 321 B

After

Width:  |  Height:  |  Size: 321 B

View file

Before

Width:  |  Height:  |  Size: 381 B

After

Width:  |  Height:  |  Size: 381 B

View file

Before

Width:  |  Height:  |  Size: 445 B

After

Width:  |  Height:  |  Size: 445 B

View file

Before

Width:  |  Height:  |  Size: 467 B

After

Width:  |  Height:  |  Size: 467 B

View file

Before

Width:  |  Height:  |  Size: 513 B

After

Width:  |  Height:  |  Size: 513 B

View file

@ -1,10 +1,10 @@
from . import _image_path from . import path
from .abc import Style from .abc import Style
_image_path = _image_path + "background/" path = path / "background"
class Background(Style): class Background(Style):
main = _image_path + "main.png" main = path / "main.png"
game = _image_path + "game.png" game = path / "game.png"

View file

@ -1,11 +1,11 @@
from . import _image_path from . import path
from .abc import Style from .abc import Style
_image_path = _image_path + "button/" path = path / "button"
class Button: class Button:
class Style1(Style): class Style1(Style):
normal = _image_path + "normal.png" normal = path / "normal.png"
click = _image_path + "clicking.png" click = path / "clicking.png"
hover = _image_path + "hovering.png" hover = path / "hovering.png"

View file

@ -1,10 +1,10 @@
from . import _image_path from . import path
from .abc import Style from .abc import Style
_image_path = _image_path + "checkbox/" path = path / "checkbox"
class Checkbox: class Checkbox:
class Style1(Style): class Style1(Style):
disabled = _image_path + "disabled.png" disabled = path / "disabled.png"
enabled = _image_path + "enabled.png" enabled = path / "enabled.png"

View file

@ -1,17 +1,28 @@
from . import _image_path from . import path
from .abc import Style from .abc import Style
_image_path = _image_path + "grid/" path = path / "grid"
_image_boat_path = _image_path + "boat/" path_boat = path / "boat"
path_bomb = path / "bomb"
class Grid: class Grid:
class Style1(Style): class Style1(Style):
background = _image_path + "background.png" background = path / "background.png"
class Boat: class Boat:
class Style1(Style): class Style1(Style):
body = _image_boat_path + "body.png" body = path_boat / "body.png"
edge = _image_boat_path + "edge.png" edge = path_boat / "edge.png"
broken = _image_boat_path + "broken.png" broken = path_boat / "broken.png"
solo = _image_boat_path + "solo.png" solo = path_boat / "solo.png"
class Bomb:
class Style1(Style):
_animation = sorted(
(path_bomb / "animation").iterdir(),
key=lambda path: int(path.stem)
)
missed = [*_animation, path_bomb / "missed.png"], 0.03, False
touched = [*_animation, path_bomb / "touched.png"], 0.03, False

View file

@ -1,11 +1,11 @@
from . import _image_path from . import path
from .abc import Style from .abc import Style
_image_path = _image_path + "input/" path = path / "input"
class Input: class Input:
class Style1(Style): class Style1(Style):
normal = _image_path + "normal.png" normal = path / "normal.png"
active = _image_path + "active.png" active = path / "active.png"
error = _image_path + "error.png" error = path / "error.png"

View file

@ -1,10 +1,10 @@
from . import _image_path from . import path
from .abc import Style from .abc import Style
_image_path = _image_path + "scroller/" path = path / "scroller"
class Scroller: class Scroller:
class Style1(Style): class Style1(Style):
background = _image_path + "background.png" background = path / "background.png"
cursor = _image_path + "cursor.png" cursor = path / "cursor.png"

View file

@ -1,9 +1,10 @@
_image_path: str = "./assets/image/" from pathlib import Path
path: Path = Path("./assets/image")
from .Background import Background # NOQA: E402 from .Background import Background
from .Button import Button # NOQA: E402 from .Button import Button
from .Checkbox import Checkbox # NOQA: E402 from .Checkbox import Checkbox
from .Grid import Grid # NOQA: E402 from .Grid import Grid
from .Input import Input # NOQA: E402 from .Input import Input
from .Scroller import Scroller # NOQA: E402 from .Scroller import Scroller

View file

@ -1,4 +1,5 @@
from abc import ABC from abc import ABC
from pathlib import Path
from typing import Optional, Any from typing import Optional, Any
import pyglet import pyglet
@ -8,9 +9,23 @@ class Style(ABC):
def __init_subclass__(cls, **kwargs): def __init_subclass__(cls, **kwargs):
atlas = pyglet.image.atlas.TextureAtlas() atlas = pyglet.image.atlas.TextureAtlas()
for name, path in cls.__dict__.items(): for name, args in cls.__dict__.items():
if name.startswith("_"): continue if name.startswith("_"): continue
setattr(cls, name, atlas.add(pyglet.image.load(path)))
if isinstance(args, Path): # if this is a normal path for a normal image
path = args
texture = atlas.add(pyglet.image.load(path))
elif isinstance(args, tuple): # if this is a tuple for an animation
paths, duration, loop = args
textures = map(lambda path: atlas.add(pyglet.image.load(path)), paths)
texture = pyglet.image.Animation.from_image_sequence(textures, duration, loop)
else:
raise ValueError(f"Invalid type : {type(args)}")
setattr(cls, name, texture)
setattr(cls, "_atlas", atlas) setattr(cls, "_atlas", atlas)

View file

@ -1,5 +1,5 @@
from copy import copy from copy import copy
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Type
import pyglet import pyglet
import numpy as np import numpy as np
@ -8,6 +8,7 @@ from source.core.enums import Orientation
from source.core.error import InvalidBoatPosition from source.core.error import InvalidBoatPosition
from source.gui import texture from source.gui import texture
from source.gui.sprite import Sprite from source.gui.sprite import Sprite
from source.gui.texture.abc import Style
from source.gui.widget.grid.abc import GameGrid from source.gui.widget.grid.abc import GameGrid
from source.core import Board, Boat from source.core import Board, Boat
from source.type import Point2D, ColorRGB from source.type import Point2D, ColorRGB
@ -20,13 +21,18 @@ if TYPE_CHECKING:
class GameGridAlly(GameGrid): class GameGridAlly(GameGrid):
def __init__(self, scene: "Scene", def __init__(self, scene: "Scene",
rows: int,
columns: int,
style: Type[Style],
boats_length: list[int], boats_length: list[int],
preview_color: ColorRGB = (150, 255, 150), preview_color: ColorRGB = (150, 255, 150),
**kwargs): **kwargs):
self.cell_sprites: dict[Point2D, "Sprite"] = {} self.cell_sprites: dict[Point2D, "Sprite"] = {}
super().__init__(scene, **kwargs) super().__init__(scene, rows, columns, style, **kwargs)
self.boats_length = boats_length # the list of the size of the boats to place self.boats_length = boats_length # the list of the size of the boats to place
self.preview_color = preview_color self.preview_color = preview_color

View file

@ -1,5 +1,10 @@
from typing import Type
from source.gui.texture.abc import Style
from source.gui.widget.grid.abc import GameGrid from source.gui.widget.grid.abc import GameGrid
class GameGridEnemy(GameGrid): class GameGridEnemy(GameGrid):
pass def __init__(self, scene: "Scene", rows: int, columns: int, style: Type[Style], **kwargs):
super().__init__(scene, rows, columns, style, **kwargs)

View file

@ -8,4 +8,4 @@ ColorRGBA = tuple[int, int, int, int]
DistanceFunction = Callable[[Any], int] # a function that return a distance DistanceFunction = Callable[[Any], int] # a function that return a distance
# a distance, represented either by a whole number, a percentage or a function # a distance, represented either by a whole number, a percentage or a function
Distance = Union[Percentage, int, DistanceFunction] Distance = Union[Percentage, int, DistanceFunction]