You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce SpriteSequence, a covariant supertype of SpriteList. (#2647)
* Remove redundant call to `clear()` in `remove_from_sprite_lists()`.
As we are just out of a while loop whose stopping condition is that
`sprite_lists` is empty, calling `clear()` on it is redundant.
* Concentrate updates to `BasicSprite.sprite_lists` in a pair of methods.
There was already a method `register_sprite_list` to handle all
additions to `sprite_lists`. We add a corresponding method
`_unregister_sprite_list` to handle removals.
We make the typings of these methods stricter, so that we can
enforce the correct typing invariant on `sprite_lists`. We also
make that invariant clearer in a comment.
`sprite_lists` is unfortunately unsafely visible to everyone.
So a user of the class could still violate the invariants. At least
now the *intended* usage is safe.
* Remove dead code attribute Sprite._sprite_list.
* Fix the type signature of `get_closest_sprite`.
This is similar to the fix done to `check_for_collision_with_list`
done in c387717.
* A few better types in arcade.future and arcade.particles.
Adding type parameters to some `SpriteList`s. One allows to get rid
of a cast.
* Introduce SpriteSequence, a covariant supertype of SpriteList.
This is done by analogy to `collections.abc.Sequence`, which is a
covariant supertype of `list`.
Before this commit, many parts of the codebase used `SpriteList`s
without type arguments (defaulting to `Unknown`). That was the only
way to allow reasonable usages of the given methods and attributes.
However, doing so results in weaker typing.
Using `SpriteSequence`, we can add correct type arguments to almost
all of the references that were using `SpriteList`s before.
The only missing pieces are `Scene` and `TileMap`. Unfortunately,
their APIs are fundamentally unsound wrt. the type arguments of
their `SpriteList`s. We cannot make it sound without breaking their
APIs, so we do not change them.
As a bonus, we can now create lists of `SpriteList`s with varying
type arguments, and generically call `draw` or `update` on them.
Previously, the only common supertype of `SpriteList[A]` and
`SpriteList[B]` was `object`, which meant it was not possible to
call those methods on them.
In a sense, that ability mostly subsumes the convenience provided
by `Scene`. A `list[SpriteSequence[BasicSprite]]` is almost as
convenient, while being type-safe.
0 commit comments