Skip to content

Commit 8ae050e

Browse files
SpideyWackoGrimmys
authored andcommitted
Replaced Position with subclass of pygame.Vector2
1 parent d3ca808 commit 8ae050e

13 files changed

+203
-199
lines changed

src/gui/position.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
"""
44
from __future__ import annotations
55

6-
from typing import Union
7-
86
import pygame
97

10-
Position = Union[pygame.Vector2, tuple[int, int]]
8+
9+
class Position(pygame.Vector2):
10+
def __init__(self, x, y):
11+
super().__init__(x, y)
12+
13+
def __hash__(self):
14+
return hash((self.x, self.y))
15+
16+
def __str__(self):
17+
return f'({self.x}, {self.y})'

src/gui/tools.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def show_fps(
3131
def blit_alpha(
3232
target: pygame.Surface,
3333
source: pygame.Surface,
34-
location: tuple[int, int],
34+
location: Position,
3535
opacity: int,
3636
):
3737
"""
@@ -44,7 +44,7 @@ def blit_alpha(
4444
opacity -- the opacity of the blitted surface
4545
"""
4646
source.set_alpha(opacity)
47-
target.blit(source, location)
47+
target.blit(source, (location.x, location.y))
4848

4949

5050
def distance(position: Position, other_position: Position) -> int:

src/scenes/level_loading_scene.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from src.gui.animation import Frame
1111
from src.gui.fade_in_out_animation import FadeInOutAnimation
1212
from src.gui.fonts import fonts
13+
from src.gui.position import Position
1314
from src.scenes.level_scene import LevelScene
1415
from src.scenes.scene import Scene
1516
from src.services.language import *
@@ -61,7 +62,7 @@ def _load_level_introduction_animation_frame(self) -> Frame:
6162
),
6263
)
6364

64-
return Frame(level_title_screen, pygame.Vector2(0, 0), DELAY_BETWEEN_FRAMES)
65+
return Frame(level_title_screen, Position(0, 0), DELAY_BETWEEN_FRAMES)
6566

6667
def _generate_level_title_rendering(self) -> pygame.Surface:
6768
"""

src/scenes/level_scene.py

+28-29
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@ def __init__(
246246
self.defeat: bool = False
247247

248248
# Data structures for possible actions
249-
self.possible_moves: dict[tuple[int, int], int] = {}
250-
self.possible_attacks: list[tuple[int, int]] = []
251-
self.possible_interactions: list[tuple[int, int]] = []
249+
self.possible_moves: dict[Position, int] = {}
250+
self.possible_attacks: list[Position] = []
251+
self.possible_interactions: list[Position] = []
252252

253253
# Storage of current selected entity
254254
self.selected_player: Optional[Player] = None
@@ -381,7 +381,7 @@ def load_level_content(self) -> None:
381381

382382
self.sidebar = Sidebar(
383383
(MENU_WIDTH, MENU_HEIGHT),
384-
pygame.Vector2(0, MAX_MAP_HEIGHT),
384+
Position(0, MAX_MAP_HEIGHT),
385385
self.missions,
386386
self.number,
387387
)
@@ -731,7 +731,7 @@ def get_next_cases(self, position: Position) -> list[Optional[Entity]]:
731731
for y_coordinate in (1 - abs(x_coordinate), -1 + abs(x_coordinate)):
732732
tile_x: int = position[0] + (x_coordinate * TILE_SIZE)
733733
tile_y: int = position[1] + (y_coordinate * TILE_SIZE)
734-
tile_position = tile_x, tile_y
734+
tile_position: Position = Position(tile_x, tile_y)
735735
tile_content: Optional[Entity] = self.get_entity_on_tile(tile_position)
736736
tiles_content.append(tile_content)
737737
return tiles_content
@@ -746,7 +746,8 @@ def get_possible_moves(
746746
position -- the starting position
747747
max_moves -- the maximum number of tiles that could be traveled
748748
"""
749-
tiles: dict[Position, int] = {position: 0}
749+
new_position = Position(position.x, position.y)
750+
tiles: dict[Position, int] = {new_position: 0}
750751
previously_computed_tiles: dict[Position, int] = tiles
751752
for i in range(1, max_moves + 1):
752753
tiles_current_level: dict[Position, int] = {}
@@ -755,7 +756,7 @@ def get_possible_moves(
755756
for y_coordinate in (1 - abs(x_coordinate), -1 + abs(x_coordinate)):
756757
tile_x: int = tile[0] + (x_coordinate * TILE_SIZE)
757758
tile_y: int = tile[1] + (y_coordinate * TILE_SIZE)
758-
tile_position = (tile_x, tile_y)
759+
tile_position = Position(tile_x, tile_y)
759760
if (
760761
self.is_tile_available(tile_position)
761762
and tile_position not in tiles
@@ -768,10 +769,10 @@ def get_possible_moves(
768769

769770
def get_possible_attacks(
770771
self,
771-
possible_moves: Sequence[tuple[int, int]],
772+
possible_moves: Sequence[Position],
772773
reach: Sequence[int],
773774
from_ally_side: bool,
774-
) -> set[tuple[float, float]]:
775+
) -> set[Position]:
775776
"""
776777
Return all the tiles that could be targeted for an attack from a specific entity
777778
@@ -780,7 +781,7 @@ def get_possible_attacks(
780781
reach -- the reach of the attacking entity
781782
from_ally_side -- a boolean indicating whether this is a friendly attack or not
782783
"""
783-
tiles: list[tuple[float, float]] = []
784+
tiles: list[Position] = []
784785

785786
entities = list(self.entities.breakables)
786787
if from_ally_side:
@@ -794,11 +795,11 @@ def get_possible_attacks(
794795
for y_coordinate in (i - abs(x_coordinate), -i + abs(x_coordinate)):
795796
tile_x: int = entity.position[0] + (x_coordinate * TILE_SIZE)
796797
tile_y: int = entity.position[1] + (y_coordinate * TILE_SIZE)
797-
tile_position = (tile_x, tile_y)
798+
tile_position = Position(tile_x, tile_y)
798799
if tile_position in possible_moves:
799-
tiles.append(tuple(entity.position))
800+
tiles.append(entity.position)
800801

801-
return set(tiles)
802+
return set([Position(i.x, i.y) for i in tiles])
802803

803804
def is_tile_available(self, tile: Position) -> bool:
804805
"""
@@ -807,8 +808,8 @@ def is_tile_available(self, tile: Position) -> bool:
807808
Keyword arguments:
808809
tile -- the position of the tile
809810
"""
810-
min_case: Position = pygame.Vector2(self.map["x"], self.map["y"])
811-
max_case: Position = pygame.Vector2(
811+
min_case: Position = Position(self.map["x"], self.map["y"])
812+
max_case: Position = Position(
812813
self.map["x"] + self.map["width"],
813814
self.map["y"] + self.map["height"],
814815
)
@@ -842,7 +843,7 @@ def get_entity_on_tile(self, tile: Position) -> Optional[Entity]:
842843
return None
843844

844845
def determine_path_to(
845-
self, destination_tile: Position, distance_for_tile: dict[tuple[int, int], int]
846+
self, destination_tile: Position, distance_for_tile: dict[Position, int]
846847
) -> list[Position]:
847848
"""
848849
Return an ordered list of position that represent the path from one tile to another
@@ -852,18 +853,16 @@ def determine_path_to(
852853
distance -- the distance between the starting tile and the destination
853854
"""
854855
path: list[Position] = [destination_tile]
855-
current_tile: tuple[int, int] = tuple(destination_tile)
856+
current_tile: Position = destination_tile
856857
while distance_for_tile[current_tile] > 1:
857858
# Check for neighbour cases
858-
available_tiles: dict[tuple[int, int], int] = self.get_possible_moves(
859-
tuple(current_tile), 1
860-
)
859+
available_tiles: dict[Position, int] = self.get_possible_moves(current_tile, 1)
861860
for tile in available_tiles:
862861
if tile in distance_for_tile:
863862
distance = distance_for_tile[tile]
864863
if distance < distance_for_tile[current_tile]:
865864
current_tile = tile
866-
path.insert(0, pygame.Vector2(current_tile))
865+
path.insert(0, current_tile)
867866
return path
868867

869868
def distance_between_all(
@@ -876,8 +875,8 @@ def distance_between_all(
876875
entity -- the entity for which the distance from all other entities should be computed
877876
all_other_entities -- all other entities for which the distance should be computed
878877
"""
879-
free_tiles_distance: dict[tuple[int, int], int] = self.get_possible_moves(
880-
tuple(entity.position),
878+
free_tiles_distance: dict[Position, int] = self.get_possible_moves(
879+
entity.position,
881880
(self.map["width"] * self.map["height"]) // (TILE_SIZE * TILE_SIZE),
882881
)
883882
entities_distance: dict[Entity, int] = {
@@ -1099,7 +1098,7 @@ def interact(
10991098
new_based_position: Position = target.linked_to.position
11001099
possible_positions_with_distance: dict[
11011100
Position, int
1102-
] = self.get_possible_moves(tuple(new_based_position), 1)
1101+
] = self.get_possible_moves(new_based_position, 1)
11031102
# Remove portal pos since player cannot be on the portal
11041103
del possible_positions_with_distance[new_based_position]
11051104
if possible_positions_with_distance:
@@ -1326,8 +1325,8 @@ def process_entity_action(self, entity: Movable, is_ally: bool) -> None:
13261325
entity -- the entity for which the action should be computed
13271326
is_ally -- a boolean indicating if the entity is an ally or not
13281327
"""
1329-
possible_moves: dict[tuple[int, int], int] = self.get_possible_moves(
1330-
tuple(entity.position), entity.max_moves
1328+
possible_moves: dict[Position, int] = self.get_possible_moves(
1329+
entity.position, entity.max_moves
13311330
)
13321331
targets: Sequence[Movable] = (
13331332
self.entities.foes if is_ally else self.players + self.entities.allies
@@ -2128,7 +2127,7 @@ def left_click(self, position: Position) -> None:
21282127
player.selected = True
21292128
self.selected_player = player
21302129
self.possible_moves = self.get_possible_moves(
2131-
tuple(player.position),
2130+
player.position,
21322131
player.max_moves + player.get_stat_change("speed"),
21332132
)
21342133
self.possible_attacks = (
@@ -2272,7 +2271,7 @@ def button_down(self, button: int, position: Position) -> None:
22722271
) and entity.get_rect().collidepoint(position_inside_level):
22732272
self.watched_entity = entity
22742273
self.possible_moves = self.get_possible_moves(
2275-
tuple(entity.position), entity.max_moves
2274+
entity.position, entity.max_moves
22762275
)
22772276
reach: Sequence[int] = self.watched_entity.reach
22782277
self.possible_attacks = {}
@@ -2340,7 +2339,7 @@ def _compute_relative_position(self, position: Position) -> Position:
23402339
Keyword arguments:
23412340
position -- the absolute position to be converted
23422341
"""
2343-
return position - pygame.Vector2(
2342+
return position - Position(
23442343
self.screen.get_width() // 2 - self.active_screen_part.get_width() // 2,
23452344
self.screen.get_height() // 2 - self.active_screen_part.get_height() // 2,
23462345
)

src/services/language.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
import pathlib
3+
14
from lxml import etree
25

36
# Get language from options.xml

0 commit comments

Comments
 (0)