1+ from __future__ import annotations
2+
13"""Container class
24
35A container is an item in the world which holds an inventory.
46"""
57
6- from typing import Any , Self
8+ from typing import TYPE_CHECKING , Any , Iterator , Self
79
8- from fantasy_forge .inventory import Inventory
910from fantasy_forge .item import Item
11+ from fantasy_forge .localization import highlight_interactive
12+ from fantasy_forge .messages import Messages
13+ from fantasy_forge .utils import UniqueDict , inflate_contents
1014
1115
12- class Container (Inventory , Item ):
16+ class Container (Item ):
1317 """Container object."""
1418
19+ contents : UniqueDict [str , Item ]
20+ capacity : int
1521 __important_attributes__ = (* Item .__important_attributes__ , "capacity" )
16- __attributes__ = {** Inventory .__attributes__ , ** Item . __attributes__ }
22+ __attributes__ = {** Item .__attributes__ , "capacity" : int }
1723
18- def __init__ (
19- self : Self , config_dict : dict [str , Any ], l10n : FluentLocalization
20- ) -> None :
24+ def __init__ (self : Self , messages : Messages , config_dict : dict [str , Any ]) -> None :
2125 """
2226 config_dict contents
23- inherited from Inventory
24- 'capacity ' (int ): maximum capacity of the inventory
27+ 'capacity' (int): maximum capacity of the container
28+ 'contents ' (list ): contents of the container
2529
2630 inherited from Item
2731 'moveable' (bool): can the item be moved by the player (default: True)
@@ -32,9 +36,43 @@ def __init__(
3236 'description' (str): description of the entity (default: "")
3337 'obvious'(bool): whether the entity will be spotted immediately (default: False)
3438 """
35- Inventory .__init__ (self , config_dict , l10n )
36- Item .__init__ (self , config_dict , l10n )
39+ super ().__init__ (messages , config_dict )
40+ self .capacity = config_dict .get ("capacity" , 10 )
41+ self .contents = UniqueDict ()
42+ inflate_contents (messages , config_dict .get ("contents" , []), self )
43+
44+ def __len__ (self : Self ) -> int :
45+ """Returns current capacity."""
46+ return len (self .contents )
47+
48+ def __iter__ (self : Self ) -> Iterator [Item ]:
49+ """Iterates over items in container."""
50+ yield from self .contents .values ()
51+
52+ def __contains__ (self : Self , other : str ) -> bool :
53+ """Returns if entity is in container."""
54+ return other in self .contents
55+
56+ def on_look (self : Self , player : Player ):
57+ super ().on_look (player )
58+ if not self .contents :
59+ self .messages .to (
60+ [player ], "container-look-empty-message" , container = self .name
61+ )
62+ else :
63+ self .messages .to (
64+ [player ],
65+ "container-look-message" ,
66+ container = self .name ,
67+ contents = ", " .join (
68+ [
69+ highlight_interactive (str (item )).format (None )
70+ + f" (weight: { item .weight } )"
71+ for item in self
72+ ]
73+ ),
74+ )
3775
3876
3977if TYPE_CHECKING :
40- from fluent . runtime import FluentLocalization
78+ from fantasy_forge . player import Player
0 commit comments