Switch arcade.math.lerp to use a Protocol bound to a TypeVar by pushfoo · Pull Request #2310 · pythonarcade/arcade · GitHub
Skip to content
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
65 changes: 43 additions & 22 deletions arcade/math.py
33 changes: 16 additions & 17 deletions arcade/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
from __future__ import annotations

import math
from typing import cast

from arcade import Sprite, SpriteList, check_for_collision_with_list, get_sprites_at_point
from arcade.math import get_distance, lerp_2d
from arcade.types import Point, Point2
from arcade.types import Point2

__all__ = ["AStarBarrierList", "astar_calculate_path", "has_line_of_sight"]

Expand All @@ -33,13 +32,13 @@ def _spot_is_blocked(position: Point2, moving_sprite: Sprite, blocking_sprites:
return len(hit_list) > 0


def _heuristic(start: Point, goal: Point) -> float:
def _heuristic(start: Point2, goal: Point2) -> float:
"""
Returns a heuristic value for the passed points.

Args:
start (Point): The 1st point to compare
goal (Point): The 2nd point to compare
start (Point2): The 1st point to compare
goal (Point2): The 2nd point to compare

Returns:
float: The heuristic of the 2 points
Expand Down Expand Up @@ -102,7 +101,7 @@ def __init__(
else:
self.movement_directions = (1, 0), (-1, 0), (0, 1), (0, -1) # type: ignore

def get_vertex_neighbours(self, pos: Point) -> list[tuple[float, float]]:
def get_vertex_neighbours(self, pos: Point2) -> list[tuple[float, float]]:
"""
Return neighbors for this point according to ``self.movement_directions``

Expand All @@ -123,7 +122,7 @@ def get_vertex_neighbours(self, pos: Point) -> list[tuple[float, float]]:
n.append((x2, y2))
return n

def move_cost(self, a: Point, b: Point) -> float:
def move_cost(self, a: Point2, b: Point2) -> float:
"""
Returns a float of the cost to move

Expand Down Expand Up @@ -224,12 +223,12 @@ def _AStarSearch(start: Point2, end: Point2, graph: _AStarGraph) -> list[Point2]
return None


def _collapse(pos: Point, grid_size: float):
def _collapse(pos: Point2, grid_size: float) -> tuple[int, int]:
"""Makes Point pos smaller by grid_size"""
return int(pos[0] // grid_size), int(pos[1] // grid_size)


def _expand(pos: Point, grid_size: float):
def _expand(pos: Point2, grid_size: float) -> tuple[int, int]:
"""Makes Point pos larger by grid_size"""
return int(pos[0] * grid_size), int(pos[1] * grid_size)

Expand Down Expand Up @@ -329,11 +328,11 @@ def recalculate(self):


def astar_calculate_path(
start_point: Point,
end_point: Point,
start_point: Point2,
end_point: Point2,
astar_barrier_list: AStarBarrierList,
diagonal_movement: bool = True,
) -> list[Point] | None:
) -> list[Point2] | None:
"""
Calculates the path using AStarSearch Algorithm and returns the path

Expand Down Expand Up @@ -371,13 +370,13 @@ def astar_calculate_path(

# Currently 'result' is in grid locations. We need to convert them to pixel
# locations.
revised_result = [_expand(p, grid_size) for p in result]
return cast(list[Point], revised_result)
revised_result: list[Point2] = [_expand(p, grid_size) for p in result]
return revised_result


def has_line_of_sight(
observer: Point,
target: Point,
observer: Point2,
target: Point2,
walls: SpriteList,
max_distance: float = float("inf"),
check_resolution: int = 2,
Expand Down Expand Up @@ -429,7 +428,7 @@ def has_line_of_sight(


# NOTE: Rewrite this
# def dda_step(start: Point, end: Point):
# def dda_step(start: Point2, end: Point2):
# """
# Bresenham's line algorithm

Expand Down
20 changes: 19 additions & 1 deletion arcade/types/__init__.py