|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | from collections.abc import Sequence
|
4 |
| -from typing import NamedTuple, Union |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Set, Tuple, Union |
5 | 6 |
|
6 | 7 | from pygame import Surface
|
7 | 8 |
|
| 9 | +TimeLike = Union[float, int] |
8 | 10 |
|
9 |
| -class AnimationFrame(NamedTuple): |
10 |
| - image: Surface |
11 |
| - duration: float |
| 11 | +__all__ = ("AnimationFrame", "AnimationToken") |
12 | 12 |
|
13 | 13 |
|
14 |
| -TimeLike = Union[float, int] |
| 14 | +@dataclass(frozen=True) |
| 15 | +class AnimationFrame: |
| 16 | + """ |
| 17 | + Represents a single frame in an animation. |
15 | 18 |
|
16 |
| -__all__ = ("AnimationFrame", "AnimationToken") |
| 19 | + Attributes: |
| 20 | + image: The image surface to display. |
| 21 | + duration: Duration this frame should be shown, in seconds. |
| 22 | + """ |
| 23 | + |
| 24 | + image: Surface |
| 25 | + duration: float |
17 | 26 |
|
18 | 27 |
|
19 | 28 | class AnimationToken:
|
20 |
| - __slots__ = ["next", "positions", "frames", "index"] |
| 29 | + """ |
| 30 | + Manages tile-based animation logic including frame timing, looping, and updates. |
| 31 | +
|
| 32 | + Attributes: |
| 33 | + positions: Set of (x, y, layer) map coordinates where this animation is active. |
| 34 | + frames: Tuple of AnimationFrame instances. |
| 35 | + index: Current frame index. |
| 36 | + next: Time value when the next frame should appear. |
| 37 | + loop: If True, animation loops; if False, plays once. |
| 38 | + done: Indicates whether a non-looping animation has completed. |
| 39 | + """ |
| 40 | + |
| 41 | + __slots__ = ("positions", "frames", "next", "index", "loop", "done") |
21 | 42 |
|
22 | 43 | def __init__(
|
23 | 44 | self,
|
24 |
| - positions: set[tuple[int, int, int]], |
| 45 | + positions: Set[Tuple[int, int, int]], |
25 | 46 | frames: Sequence[AnimationFrame],
|
26 | 47 | initial_time: float = 0.0,
|
| 48 | + loop: bool = True, |
27 | 49 | ) -> None:
|
28 | 50 | """
|
29 |
| - Constructor |
| 51 | + Initializes an AnimationToken instance. |
30 | 52 |
|
31 | 53 | Args:
|
32 |
| - positions: Set of positions where the tile is on the map |
33 |
| - frames: Sequence of frames that compromise the animation |
34 |
| - initial_time: Used to compensate time between starting and changing animations |
| 54 | + positions: Set of map positions for the animation tile. |
| 55 | + frames: Sequence of AnimationFrame instances. |
| 56 | + initial_time: Optional time offset for smoother transitions. |
| 57 | + loop: If False, the animation stops at the last frame. |
35 | 58 |
|
36 | 59 | Raises:
|
37 |
| - ValueError: If the frames sequence is empty |
| 60 | + ValueError: If the frames sequence is empty. |
38 | 61 | """
|
39 | 62 | if not frames:
|
40 |
| - raise ValueError("Frames sequence cannot be empty") |
| 63 | + raise ValueError("Frames sequence cannot be empty.") |
41 | 64 |
|
42 |
| - frames = tuple(AnimationFrame(*frame_data) for frame_data in frames) |
43 | 65 | self.positions = positions
|
44 |
| - self.frames = frames |
45 |
| - self.next = frames[0].duration + initial_time |
| 66 | + self.frames = tuple(frames) |
46 | 67 | self.index = 0
|
| 68 | + self.next = self.frames[0].duration + initial_time |
| 69 | + self.loop = loop |
| 70 | + self.done = False |
47 | 71 |
|
48 | 72 | def advance(self, last_time: TimeLike) -> AnimationFrame:
|
49 | 73 | """
|
50 |
| - Advance the frame, and set timer for next frame |
51 |
| -
|
52 |
| - Timer value is calculated by adding last_time and the |
53 |
| - duration of the next frame |
54 |
| -
|
55 |
| - The next frame is returned |
56 |
| -
|
57 |
| - * This API may change in the future |
| 74 | + Advances to the next frame in the animation sequence. |
58 | 75 |
|
59 | 76 | Args:
|
60 |
| - last_time: Duration of the last frame |
| 77 | + last_time: Time since the last frame update. |
61 | 78 |
|
62 | 79 | Returns:
|
63 |
| - AnimationFrame: The next frame in the animation |
| 80 | + The next AnimationFrame in the sequence. |
64 | 81 | """
|
65 |
| - # advance the animation frame index, looping by default |
| 82 | + if self.done: |
| 83 | + return self.frames[self.index] |
| 84 | + |
66 | 85 | if self.index == len(self.frames) - 1:
|
67 |
| - self.index = 0 |
| 86 | + if self.loop: |
| 87 | + self.index = 0 |
| 88 | + else: |
| 89 | + self.done = True |
68 | 90 | else:
|
69 | 91 | self.index += 1
|
70 | 92 |
|
71 |
| - # set the timer for the next advance |
72 | 93 | next_frame = self.frames[self.index]
|
73 | 94 | self.next = next_frame.duration + last_time
|
74 | 95 | return next_frame
|
75 | 96 |
|
76 |
| - def __lt__(self, other): |
| 97 | + def update(self, current_time: TimeLike, elapsed_time: TimeLike) -> AnimationFrame: |
| 98 | + """ |
| 99 | + Updates the animation frame based on simulated elapsed time. |
| 100 | +
|
| 101 | + Args: |
| 102 | + current_time: The current time used to evaluate frame progression. |
| 103 | + elapsed_time: Simulated time passed since last update. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + The active AnimationFrame. |
| 107 | + """ |
| 108 | + if self.done: |
| 109 | + return self.frames[self.index] |
| 110 | + |
| 111 | + while current_time >= self.next: |
| 112 | + self.advance(self.next) |
| 113 | + current_time -= elapsed_time |
| 114 | + return self.frames[self.index] |
| 115 | + |
| 116 | + def __lt__(self, other: Union[AnimationToken, float, int]) -> bool: |
77 | 117 | """
|
78 |
| - Compare the animation token with another object based on the next frame time |
| 118 | + Compares this token's next frame time with another. |
79 | 119 |
|
80 | 120 | Args:
|
81 |
| - other: The object to compare with |
| 121 | + other: Another AnimationToken or time value. |
82 | 122 |
|
83 | 123 | Returns:
|
84 |
| - bool: True if the next frame time is less than the other object's time |
| 124 | + True if this token's next frame time is earlier. |
85 | 125 | """
|
86 | 126 | try:
|
87 | 127 | return self.next < other.next
|
|
0 commit comments