1010from collections .abc import Iterable
1111from heapq import heappop , heappush
1212from itertools import product
13+ from typing import Any
1314
1415import pygame
15- from pygame import Surface
16+ from pygame import Rect , Surface
1617
1718try :
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
0 commit comments