from __future__ import annotations from typing import List, Optional, TYPE_CHECKING if TYPE_CHECKING: from entity import Entity class Inventory: """Handles inventory for an Entity""" _contents: List[Entity] def __init__(self, contents: List[Entity] = []): self._contents = contents def insert(self, item: Entity) -> bool: if item in self._contents: return False #check for stacking if item.useable.maximum_stack > 1: if self.try_stack_merge(item): return True self._contents.append(item) return True def access(self, index: int) -> Optional[Entity]: if index < 0 or index >= len(self._contents): return None else: return self._contents[index] def withdraw(self, index: int) -> Optional[Entity]: if index < 0 or index >= len(self._contents): return None else: return self._contents.pop(index) def discard(self, index: int) -> None: if index < 0 or index >= len(self._contents): pass else: self._contents.pop(index) @property def contents(self) -> List[Entity]: return self._contents #utils def try_stack_merge(self, new_item: Entity): for item in self._contents: if item.useable.is_stack_mergable(new_item.useable): #NOTE: I'll add a callback in the entity if other components need to be tweaked down the road item.useable.current_stack += new_item.useable.current_stack new_item.useable.current_stack = 0 #just in case return True return False #TODO: items need a weight? #TODO: inventory needs a max capacity?