|
5 | 5 | from pysurvive.config import SCREEN_RECT
|
6 | 6 |
|
7 | 7 |
|
8 |
| -class Screen: |
| 8 | +class Singleton(type): |
| 9 | + |
| 10 | + _instances = {} |
| 11 | + |
| 12 | + def __call__(cls, *args, **kwargs): |
| 13 | + if cls not in cls._instances: |
| 14 | + cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) |
| 15 | + |
| 16 | + print(cls._instances[cls]) |
| 17 | + return cls._instances[cls] |
| 18 | + |
| 19 | + |
| 20 | +class Screen(pg.rect.Rect, metaclass=Singleton): |
9 | 21 |
|
10 | 22 | """
|
11 | 23 | Class that represent the screen and is used to detect
|
12 | 24 | wheather objects are visible on the screen.
|
13 | 25 | """
|
14 | 26 |
|
15 |
| - _screen = None |
16 |
| - |
17 |
| - def __new__(cls, rect: pg.rect.Rect = None): |
18 |
| - if cls._screen is None: |
19 |
| - cls._screen = super().__new__(cls) |
20 |
| - if rect: |
21 |
| - cls._screen.image = pg.Surface(rect.size) |
22 |
| - cls._screen.rect = rect |
23 |
| - else: |
24 |
| - cls._screen.image = pg.Surface(SCREEN_RECT.size) |
25 |
| - cls._screen.rect = SCREEN_RECT |
26 |
| - |
27 |
| - return cls._screen |
28 |
| - |
29 | 27 | def __str__(self) -> str:
|
30 | 28 | return f"Screen (size={self.width}x{self.height})"
|
31 | 29 |
|
32 | 30 | @classmethod
|
33 |
| - def delete(cls): |
34 |
| - cls._screen = None |
35 |
| - |
36 |
| - @property |
37 |
| - def size(self) -> tuple[int, int]: |
38 |
| - return self.rect.size |
39 |
| - |
40 |
| - @property |
41 |
| - def width(self) -> int: |
42 |
| - return self.rect.width |
| 31 | + def delete(cls) -> None: |
| 32 | + """Unset singleton.""" |
| 33 | + cls._instances = {} |
43 | 34 |
|
44 | 35 | @property
|
45 |
| - def height(self) -> int: |
46 |
| - return self.rect.height |
| 36 | + def rect(self) -> pg.rect.Rect: |
| 37 | + """The function spritecollide expects a property rect.""" |
| 38 | + return self |
47 | 39 |
|
48 |
| - @property |
49 |
| - def centerx(self) -> int: |
50 |
| - return self.rect.centerx |
51 |
| - |
52 |
| - @property |
53 |
| - def centery(self) -> int: |
54 |
| - return self.rect.centery |
55 | 40 |
|
56 |
| - |
57 |
| -class Camera: |
| 41 | +class Camera(metaclass=Singleton): |
58 | 42 |
|
59 | 43 | """
|
60 |
| - Class that represents the absolute position of the camera (player) |
61 |
| - in the game world. |
| 44 | + Class that represents the absolute position of the |
| 45 | + camera (player) in the game world. |
62 | 46 | """
|
63 | 47 |
|
64 |
| - _camera = None |
65 |
| - |
66 |
| - def __new__(cls, centerx: int = 0, centery: int = 0): |
67 |
| - if cls._camera is None: |
68 |
| - cls._camera = super().__new__(cls) |
69 |
| - cls._camera.screen = Screen() |
70 |
| - cls._camera.x = centerx - cls._camera.screen.centerx |
71 |
| - cls._camera.y = centery - cls._camera.screen.centery |
72 |
| - |
73 |
| - return cls._camera |
| 48 | + def __init__(self, x: int = 0, y: int = 0) -> None: |
| 49 | + self.screen = Screen(SCREEN_RECT) |
| 50 | + self.x = x |
| 51 | + self.y = y |
74 | 52 |
|
75 | 53 | def __str__(self) -> str:
|
76 |
| - return f"Camera (x={self.x}, y={self.y}, centerx={self.centerx}, centery={self.centery})" |
| 54 | + return f"Camera (x={self.x}, y={self.y})" |
77 | 55 |
|
78 | 56 | @classmethod
|
79 |
| - def delete(cls): |
80 |
| - cls._camera = None |
| 57 | + def delete(cls) -> None: |
| 58 | + """Unset singleton.""" |
| 59 | + cls._instances = {} |
81 | 60 |
|
82 | 61 | @property
|
83 | 62 | def position(self) -> tuple[int, int]:
|
| 63 | + """Returns camera position in a tuple.""" |
84 | 64 | return (self.x, self.y)
|
85 | 65 |
|
86 |
| - @position.setter |
87 |
| - def position(self, delta: tuple[int, int]) -> None: |
| 66 | + def move(self, delta: tuple[int, int]) -> None: |
| 67 | + """Move the camera by delta x, y.""" |
88 | 68 | self.x = round(self.x - delta[0])
|
89 | 69 | self.y = round(self.y - delta[1])
|
90 | 70 |
|
91 |
| - @property |
92 |
| - def centerx(self) -> int: |
93 |
| - """Returns the center cornor x position.""" |
94 |
| - return self.x + self.screen.centerx |
95 |
| - |
96 |
| - @property |
97 |
| - def centery(self) -> int: |
98 |
| - """Returns the center cornor y position.""" |
99 |
| - return self.y + self.screen.centery |
100 |
| - |
101 |
| - @property |
102 |
| - def position_center(self) -> tuple[int, int]: |
103 |
| - return (self.centerx, self.centery) |
104 |
| - |
105 | 71 | def get_relative_position(self, x: int, y: int) -> tuple[int, int]:
|
106 | 72 | """
|
107 | 73 | Returns the relative position of a coordinate in relation
|
108 |
| - to the camera position. |
| 74 | + to the global camera position in the center of the screen. |
109 | 75 | """
|
110 | 76 | return (
|
111 |
| - round(x - self.x), |
112 |
| - round(y - self.y), |
| 77 | + round(x - self.x + self.screen.centerx), |
| 78 | + round(y - self.y + self.screen.centery), |
113 | 79 | )
|
0 commit comments