-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.py
224 lines (185 loc) · 5.96 KB
/
entity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from __future__ import annotations
import copy
import math
from typing import Optional, Tuple, Type, TypeVar, TYPE_CHECKING, Union
from render_order import RenderOrder
if TYPE_CHECKING:
from components.ai import BaseAI
from components.consumable import Consumable
from components.equippable import Equippable
from components.equipment import Equipment
from components.fighter import Fighter
from components.inventory import Inventory
from components.level import Level
from components.status import Status
from game_map import GameMap
T = TypeVar("T", bound="Entity")
class Entity:
"""
A generic object to represent players, enemies, items, etc.
"""
parent: Union[GameMap, Inventory]
def __init__(
self,
parent: Optional[GameMap] = None,
x: int = 0,
y: int = 0,
char: str = "?",
color: Tuple[int, int, int] = (255, 255, 255),
name: str = "<Unnamed>",
description: str = "",
blocks_movement: bool = False,
render_order: RenderOrder = RenderOrder.CORPSE,
has_light: bool = False,
):
self.x = x
self.y = y
self.char = char
self.color = color
self.name = name
self.description = description
self.blocks_movement = blocks_movement
self.render_order = render_order
self.has_light = has_light
if parent:
# If parent isn't provided now then it will be set later.
self.parent = parent
parent.entities.add(self)
@property
def gamemap(self) -> GameMap:
return self.parent.gamemap
def spawn(self: T, x: int, y: int, gamemap: GameMap) -> T:
"""Spawn a copy of this instance at the given location."""
clone = copy.deepcopy(self)
clone.x = x
clone.y = y
clone.parent = gamemap
gamemap.entities.add(clone)
if isinstance(clone, Actor):
gamemap.engine.turn_manager.add_actor(clone)
return clone
def move(self, dx: int, dy: int) -> None:
"""Move the entity by a given amount."""
self.x += dx
self.y += dy
def place(self, x: int, y: int, gamemap: Optional[GameMap] = None) -> None:
"""Place this entity at a new location. Handles moving across GameMaps."""
self.x = x
self.y = y
if gamemap:
if hasattr(self, "parent"): # Possibly uninitialized.
if self.parent is self.gamemap:
self.gamemap.entities.remove(self)
self.parent = gamemap
gamemap.entities.add(self)
def distance(self, x: int, y: int) -> float:
"""
Return the distance between the current entity and the given (x, y) coordinate.
"""
return math.sqrt((x - self.x) ** 2 + (y - self.y) ** 2)
class Actor(Entity):
"""An entity that can act."""
_id_counter = 0
def __init__(
self,
*,
x: int = 0,
y: int = 0,
char: str = "?",
color: Tuple[int, int, int] = (255, 255, 255),
remains_color: Tuple[int, int, int] = (191, 0, 0),
name="<Unnamed>",
ai_cls: Type[BaseAI] | None,
equipment: Equipment,
fighter: Fighter,
inventory: Inventory,
level: Level,
status: Status,
is_alive=True,
has_light: bool = False,
):
self.id = Actor._id_counter
self.is_alive = is_alive
Actor._id_counter += 1
super().__init__(
x=x,
y=y,
char=char,
color=color,
name=name,
blocks_movement=True,
render_order=RenderOrder.ACTOR,
has_light=has_light,
)
if ai_cls is not None:
self.original_ai = ai_cls
self.ai = ai_cls(self)
else:
self.original_ai = None
self.ai = None
self.equipment: Equipment = equipment
self.equipment.parent = self
self.fighter = fighter
self.fighter.parent = self
self.inventory = inventory
self.inventory.parent = self
self.level = level
self.level.parent = self
self.status = status
self.status.parent = self
self.remains_color = remains_color
def __copy__(self):
# Create a shallow copy of the actor
cls = self.__class__
new_actor = cls.__new__(cls)
new_actor.__dict__.update(self.__dict__)
# Assign a new unique ID
new_actor.id = Actor._id_counter
Actor._id_counter += 1
return new_actor
def __deepcopy__(self, memo):
# Create a deep copy of the actor
cls = self.__class__
new_actor = cls.__new__(cls)
memo[id(self)] = new_actor
for k, v in self.__dict__.items():
setattr(new_actor, k, copy.deepcopy(v, memo))
# Assign a new unique ID
new_actor.id = Actor._id_counter
Actor._id_counter += 1
return new_actor
def restore_ai(self):
"""Restore the original AI."""
if self.original_ai:
self.ai = self.original_ai(self)
else:
self.ai = None
class Item(Entity):
def __init__(
self,
*,
x: int = 0,
y: int = 0,
char: str = "?",
color: Tuple[int, int, int] = (255, 255, 255),
name: str = "<Unnamed>",
description: str = "",
consumable: Optional[Consumable] = None,
equippable: Optional[Equippable] = None,
):
super().__init__(
x=x,
y=y,
char=char,
color=color,
name=name,
description=description,
blocks_movement=False,
render_order=RenderOrder.ITEM,
)
self.consumable = consumable
if self.consumable:
self.consumable.parent = self
self.equippable = equippable
if self.equippable:
self.equippable.parent = self