Skip to content

Simplify animation #2033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions arcade/sprite/animated.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class TextureKeyframe:
:param duration: Duration in milliseconds to display this keyframe.
:param tile_id: Tile ID for this keyframe (only used for tiled maps)
"""
__slots__ = ("texture", "duration", "tile_id")
def __init__(
self,
texture: Texture,
Expand All @@ -46,10 +47,12 @@ class TextureAnimation:
:param keyframes: List of keyframes for the animation.
:param loop: If the animation should loop.
"""
def __init__(self, keyframes: Optional[List[TextureKeyframe]] = None):
self._keyframes = keyframes or []
__slots__ = ("_keyframes", "_duration_ms", "_timeline")

def __init__(self, keyframes: List[TextureKeyframe]):
self._keyframes = keyframes
self._duration_ms = 0
self._timeline: List[int] = self._create_timeline(self._keyframes) if self._keyframes else []
self._timeline: List[int] = self._create_timeline(self._keyframes)

@property
def keyframes(self) -> Tuple[TextureKeyframe, ...]:
Expand Down Expand Up @@ -94,25 +97,6 @@ def _create_timeline(self, keyframes: List[TextureKeyframe]) -> List[int]:
self._duration_ms = current_time_ms
return timeline

def append_keyframe(self, keyframe: TextureKeyframe) -> None:
"""
Add a keyframe to the animation.

:param keyframe: Keyframe to add.
"""
self._keyframes.append(keyframe)
self._timeline.append(self._duration_ms)
self._timeline = self._create_timeline(self._keyframes)

def remove_keyframe(self, index: int) -> None:
"""
Remove a keyframe from the animation.

:param index: Index of the keyframe to remove.
"""
del self._keyframes[index]
self._timeline = self._create_timeline(self._keyframes)

def get_keyframe(self, time: float, loop: bool = True) -> Tuple[int, TextureKeyframe]:
"""
Get the frame at a given time.
Expand Down
12 changes: 0 additions & 12 deletions tests/unit/sprite/test_sprite_texture_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ def test_animation(keyframes):
"""Test animation class"""
anim = arcade.TextureAnimation(keyframes=keyframes)

# Add keyframe
anim.append_keyframe(arcade.TextureKeyframe(keyframes[0].texture, 1000))
assert anim.num_frames == 9
assert anim.duration_ms == 9000
assert anim.duration_seconds == 9.0

# Remove keyframe
anim.remove_keyframe(8)
assert anim.num_frames == 8
assert anim.duration_ms == 8000
assert anim.duration_seconds == 8.0

# Get keyframes at specific times (0.5s increments)
for i in range(16):
time = i / 2
Expand Down