11from __future__ import annotations
22
3- from typing import Optional , Tuple , Type , TYPE_CHECKING
3+ from typing import Optional , Tuple , Type , Union , TYPE_CHECKING
44
55import game .render_order
66
77if TYPE_CHECKING :
88 import game .components .ai
9+ import game .components .consumable
910 import game .components .fighter
10- import game .game_map
11+ import game .components .inventory
12+
13+ import game .game_map
1114
1215
1316class Entity :
1417 """
1518 A generic object to represent players, enemies, items, etc.
1619 """
1720
18- # Part 8 refactoring prep: Will become Union[GameMap, Inventory] in Part 8
19- gamemap : Optional [game .game_map .GameMap ]
21+ parent : Union [game .game_map .GameMap , game .components .inventory .Inventory ]
2022
2123 def __init__ (
2224 self ,
23- gamemap : Optional [game .game_map .GameMap ] = None ,
25+ parent : Optional [Union [ game .game_map .GameMap , game . components . inventory . Inventory ] ] = None ,
2426 x : int = 0 ,
2527 y : int = 0 ,
2628 char : str = "?" ,
@@ -35,19 +37,28 @@ def __init__(
3537 self .name = name
3638 self .blocks_movement = blocks_movement
3739 self .render_order = game .render_order .RenderOrder .CORPSE
38- if gamemap :
39- # If gamemap isn't provided now then it will be set later.
40- self .gamemap = gamemap
41- gamemap .entities .add (self )
40+ if parent :
41+ # If parent isn't provided now then it will be set later.
42+ self .parent = parent
43+ if hasattr (parent , 'entities' ):
44+ parent .entities .add (self )
45+
46+ @property
47+ def gamemap (self ) -> game .game_map .GameMap :
48+ if isinstance (self .parent , game .game_map .GameMap ):
49+ return self .parent
50+ else :
51+ return self .parent .gamemap
4252
4353 def place (self , x : int , y : int , gamemap : Optional [game .game_map .GameMap ] = None ) -> None :
4454 """Place this entity at a new location. Handles moving across GameMaps."""
4555 self .x = x
4656 self .y = y
4757 if gamemap :
48- if hasattr (self , "gamemap" ) and self .gamemap is not None : # Possibly uninitialized.
49- self .gamemap .entities .remove (self )
50- self .gamemap = gamemap
58+ if hasattr (self , "parent" ): # Possibly uninitialized.
59+ if hasattr (self .parent , "entities" ):
60+ self .parent .entities .remove (self )
61+ self .parent = gamemap
5162 gamemap .entities .add (self )
5263
5364 def move (self , dx : int , dy : int ) -> None :
@@ -67,9 +78,10 @@ def __init__(
6778 name : str = "<Unnamed>" ,
6879 ai_cls : Type [game .components .ai .BaseAI ],
6980 fighter : game .components .fighter .Fighter ,
81+ inventory : game .components .inventory .Inventory ,
7082 ):
7183 super ().__init__ (
72- gamemap = None ,
84+ parent = None ,
7385 x = x ,
7486 y = y ,
7587 char = char ,
@@ -83,9 +95,41 @@ def __init__(
8395 self .fighter = fighter
8496 self .fighter .parent = self
8597
98+ self .inventory = inventory
99+ self .inventory .parent = self
100+
86101 self .render_order = game .render_order .RenderOrder .ACTOR
87102
88103 @property
89104 def is_alive (self ) -> bool :
90105 """Returns True as long as this actor can perform actions."""
91106 return bool (self .ai )
107+
108+
109+ class Item (Entity ):
110+ def __init__ (
111+ self ,
112+ * ,
113+ x : int = 0 ,
114+ y : int = 0 ,
115+ char : str = "?" ,
116+ color : Tuple [int , int , int ] = (255 , 255 , 255 ),
117+ name : str = "<Unnamed>" ,
118+ consumable : Optional [game .components .consumable .Consumable ] = None ,
119+ ):
120+ super ().__init__ (
121+ parent = None ,
122+ x = x ,
123+ y = y ,
124+ char = char ,
125+ color = color ,
126+ name = name ,
127+ blocks_movement = False ,
128+ )
129+
130+ self .consumable = consumable
131+
132+ if self .consumable :
133+ self .consumable .parent = self
134+
135+ self .render_order = game .render_order .RenderOrder .ITEM
0 commit comments