Skip to content

Commit c37ece7

Browse files
committed
typehints
1 parent b1ee25c commit c37ece7

File tree

4 files changed

+101
-75
lines changed

4 files changed

+101
-75
lines changed

pyscroll/animation.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
from __future__ import annotations
22

3-
from collections import namedtuple
43
from collections.abc import Sequence
5-
from typing import Union
4+
from typing import NamedTuple, Union
5+
6+
from pygame import Surface
7+
8+
9+
class AnimationFrame(NamedTuple):
10+
image: Surface
11+
duration: float
12+
613

7-
AnimationFrame = namedtuple("AnimationFrame", "image duration")
814
TimeLike = Union[float, int]
915

1016
__all__ = ("AnimationFrame", "AnimationToken")
1117

1218

1319
class AnimationToken:
14-
__slots__ = ["next", "positions", "frames", "index"]
15-
16-
def __init__(self, positions, frames: Sequence, initial_time: int = 0) -> None:
20+
__slots__ = ["_next", "positions", "frames", "index"]
21+
22+
def __init__(
23+
self,
24+
positions: set[tuple[int, int, int]],
25+
frames: Sequence[AnimationFrame],
26+
initial_time: float = 0.0,
27+
) -> None:
1728
"""
1829
Constructor
1930
@@ -26,10 +37,10 @@ def __init__(self, positions, frames: Sequence, initial_time: int = 0) -> None:
2637
frames = tuple(AnimationFrame(*i) for i in frames)
2738
self.positions = positions
2839
self.frames = frames
29-
self.next = frames[0].duration + initial_time
40+
self._next = frames[0].duration + initial_time
3041
self.index = 0
3142

32-
def advance(self, last_time: TimeLike):
43+
def advance(self, last_time: TimeLike) -> AnimationFrame:
3344
"""
3445
Advance the frame, and set timer for next frame
3546
@@ -52,11 +63,8 @@ def advance(self, last_time: TimeLike):
5263

5364
# set the timer for the next advance
5465
next_frame = self.frames[self.index]
55-
self.next = next_frame.duration + last_time
66+
self._next = next_frame.duration + last_time
5667
return next_frame
5768

58-
def __lt__(self, other):
59-
try:
60-
return self.next < other.next
61-
except AttributeError:
62-
return self.next < other
69+
def __lt__(self, other: AnimationToken) -> bool:
70+
return self._next < other._next

pyscroll/data.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
from collections.abc import Iterable
1111
from heapq import heappop, heappush
1212
from itertools import product
13+
from typing import Any
1314

1415
import pygame
15-
from pygame import Surface
16+
from pygame import Rect, Surface
1617

1718
try:
1819
# optional pytmx support
@@ -48,20 +49,25 @@ class PyscrollDataAdapter:
4849
tile_size: tuple[int, int] = (0, 0)
4950
# (int, int): size of map in tiles
5051
map_size: tuple[int, int] = (0, 0)
51-
visible_tile_layers = None # list of visible layer integers
52+
# list of visible layer integers
53+
visible_tile_layers: list[int] = []
5254

5355
def __init__(self) -> None:
54-
self._last_time = None # last time map animations were updated
55-
self._animation_queue = list() # list of animation tokens
56-
self._animated_tile = dict() # mapping of tile substitutions when animated
57-
self._tracked_tiles = set() # track the tiles on screen with animations
56+
# last time map animations were updated
57+
self._last_time = 0.0
58+
# list of animation tokens
59+
self._animation_queue: list[AnimationToken] = []
60+
# mapping of tile substitutions when animated
61+
self._animated_tile: dict[tuple[int, int, int], Surface] = {}
62+
# track the tiles on screen with animations
63+
self._tracked_tiles = set()
5864

5965
def reload_data(self) -> None:
6066
raise NotImplementedError
6167

6268
def process_animation_queue(
6369
self,
64-
tile_view: RectLike,
70+
tile_view: Rect,
6571
) -> list[tuple[int, int, int, Surface]]:
6672
"""
6773
Given the time and the tile view, process tile changes and return them
@@ -75,7 +81,7 @@ def process_animation_queue(
7581
# verify that there are tile substitutions ready
7682
self._update_time()
7783
try:
78-
if self._animation_queue[0].next > self._last_time:
84+
if self._animation_queue[0]._next > self._last_time:
7985
return new_tiles
8086

8187
# raised with the animation queue is empty (no animations at all)
@@ -87,7 +93,7 @@ def process_animation_queue(
8793
get_tile_image = self.get_tile_image
8894

8995
# test if the next scheduled tile change is ready
90-
while self._animation_queue[0].next <= self._last_time:
96+
while self._animation_queue[0]._next <= self._last_time:
9197

9298
# get the next tile/frame which is ready to be changed
9399
token = heappop(self._animation_queue)
@@ -134,7 +140,7 @@ def _update_time(self) -> None:
134140
"""
135141
self._last_time = time.time() * 1000
136142

137-
def prepare_tiles(self, tiles: RectLike):
143+
def prepare_tiles(self, tiles: RectLike) -> None:
138144
"""
139145
Somewhat experimental: The renderer will advise data layer of its view
140146
@@ -158,14 +164,13 @@ def reload_animations(self) -> None:
158164
159165
"""
160166
self._update_time()
161-
self._animation_queue = list()
162-
self._tracked_gids = set()
163-
self._animation_map = dict()
167+
self._tracked_gids: set[int] = set()
168+
self._animation_map: dict[int, AnimationToken] = {}
164169

165170
for gid, frame_data in self.get_animations():
166171
self._tracked_gids.add(gid)
167172

168-
frames = list()
173+
frames: list[AnimationFrame] = []
169174
for frame_gid, frame_duration in frame_data:
170175
image = self._get_tile_image_by_id(frame_gid)
171176
frames.append(AnimationFrame(image, frame_duration))
@@ -177,7 +182,7 @@ def reload_animations(self) -> None:
177182
# locations of an animation, but searching for their locations
178183
# is slow. so it will be updated as the map is drawn.
179184

180-
positions = set()
185+
positions: set[tuple[int, int, int]] = set()
181186
ani = AnimationToken(positions, frames, self._last_time)
182187
self._animation_map[gid] = ani
183188
heappush(self._animation_queue, ani)
@@ -224,7 +229,7 @@ def _get_tile_image(self, x: int, y: int, l: int) -> Surface:
224229
"""
225230
raise NotImplementedError
226231

227-
def _get_tile_image_by_id(self, id):
232+
def _get_tile_image_by_id(self, id: int) -> Any:
228233
"""
229234
Return Image by a custom ID.
230235
@@ -248,6 +253,9 @@ def get_animations(self) -> None:
248253
Where Frames is:
249254
[ (ID, Duration), ... ]
250255
256+
[tuple[int, tuple[int, float]]]
257+
[tuple[gid, tuple[frame_gid, frame_duration]]]
258+
251259
And ID is a reference to a tile image.
252260
This will be something accessible using _get_tile_image_by_id
253261
@@ -348,14 +356,14 @@ def _get_tile_image(self, x: int, y: int, l: int):
348356
except ValueError:
349357
return None
350358

351-
def _get_tile_image_by_id(self, id) -> Surface:
359+
def _get_tile_image_by_id(self, id: int) -> Surface:
352360
return self.tmx.images[id]
353361

354362
def get_tile_images_by_rect(self, rect: RectLike):
355-
def rev(seq:list[int], start:int, stop: int) -> enumerate[int]:
363+
def rev(seq: list[int], start: int, stop: int) -> enumerate[int]:
356364
if start < 0:
357365
start = 0
358-
return enumerate(seq[start:stop + 1], start)
366+
return enumerate(seq[start : stop + 1], start)
359367

360368
x1, y1, x2, y2 = rect_to_bb(rect)
361369
images = self.tmx.images
@@ -411,7 +419,7 @@ def _get_tile_image(self, x: int, y: int, l: int) -> Surface:
411419
"""
412420
pass
413421

414-
def _get_tile_image_by_id(self, id) -> None:
422+
def _get_tile_image_by_id(self, id: int) -> None:
415423
"""
416424
Required for sprite collation - not implemented
417425

pyscroll/isometric.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import logging
22

3+
from pyscroll.common import Vector2D, Vector2DInt
34
from pyscroll.orthographic import BufferedRenderer
45

56
log = logging.getLogger(__file__)
67

78

8-
def vector3_to_iso(vector3):
9-
offset = 0, 0
9+
def vector3_to_iso(vector3: tuple[int, int, int]) -> tuple[int, int]:
10+
offset = (0, 0)
1011
return (
1112
(vector3[0] - vector3[1]) + offset[0],
1213
((vector3[0] + vector3[1]) >> 1) - vector3[2] + offset[1],
1314
)
1415

1516

16-
def vector2_to_iso(vector2):
17-
offset = 0, 0
17+
def vector2_to_iso(vector2: tuple[int, int]) -> tuple[int, int]:
18+
offset = (0, 0)
1819
return (
1920
(vector2[0] - vector2[1]) + offset[0],
2021
((vector2[0] + vector2[1]) >> 1) + offset[1],
@@ -34,7 +35,7 @@ def _draw_surfaces(self, surface, rect, surfaces) -> None:
3435
if surfaces is not None:
3536
[(surface.blit(i[0], i[1]), i[2]) for i in surfaces]
3637

37-
def _initialize_buffers(self, view_size) -> None:
38+
def _initialize_buffers(self, view_size: Vector2DInt) -> None:
3839
"""Create the buffers to cache tile drawing
3940
4041
:param view_size: (int, int): size of the draw area
@@ -85,7 +86,7 @@ def _flush_tile_queue(self) -> None:
8586
iso_y = (x + y) * thh
8687
surface_blit(tile, (iso_x, iso_y))
8788

88-
def center(self, coords) -> None:
89+
def center(self, coords: Vector2D) -> None:
8990
"""center the map on a "map pixel" """
9091
x, y = [round(i, 0) for i in coords]
9192
self.view_rect.center = x, y
@@ -102,6 +103,7 @@ def center(self, coords) -> None:
102103
self._y_offset = iso[1]
103104

104105
print(self._tile_view.size)
106+
assert self._buffer
105107
print(self._buffer.get_size())
106108

107109
# center the buffer on the screen

0 commit comments

Comments
 (0)