-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinventory_manager.py
More file actions
42 lines (30 loc) · 1.46 KB
/
inventory_manager.py
File metadata and controls
42 lines (30 loc) · 1.46 KB
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
from __future__ import annotations
from tcod.ecs import Entity, IsA
from game.components import Name, Position, Quantity
from game.managers.event_manager import Event
from game.tags import IsIn, IsItem
def add_item(actor: Entity, item: Entity) -> None:
"""Add an item to an actor's inventory."""
# If the actor's inventory already contains an item which is of the same
# type as the newly added item, add the new item's quantity to the held
# item's quantity, rather than registering a separate item.
for held_item in get_items(actor):
if not can_stack(item, held_item):
continue
held_item.components[Quantity] += item.components.get(Quantity)
Event('inventory_item_added', f"{item.components.get(Name)} x {item.components.get(Quantity)}")
item.clear()
return
# Else, add the new item to the inventory.
item.components.pop(Position, None)
item.relation_tag[IsIn] = actor
Event('inventory_item_added', f"{item.components.get(Name)} x {item.components.get(Quantity)}")
def get_items(actor: Entity):
"""Get all items from an actor's inventory."""
return actor.registry.Q.all_of(tags=[IsItem], relations=[(IsIn, actor)])
def can_stack(entity: Entity, onto: Entity, /) -> bool:
"""Return True if two entities can be stacked."""
return bool(
entity.components.get(Name) == onto.components.get(Name)
and entity.relation_tag.get(IsA) is onto.relation_tag.get(IsA)
)