Mercurial > parpg-core
view src/parpg/components/container.py @ 54:5e27c24acaef
Fixed bug that the game crashes in the main menu after a few seconds.
author | KarstenBock@gmx.net |
---|---|
date | Sun, 04 Sep 2011 16:37:37 +0200 |
parents | 3bacef35b252 |
children | 96af64cf3b81 |
line wrap: on
line source
# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from parpg.grease.component import Component class Container(Component): """ Component that allows an entity to contain one or more child entities. """ def __init__(self): Component.__init__(self, children=list, max_bulk=int) class BulkLimitError(Exception): """Error that gets raised when the item would exceed the bulk limit of the container.""" def __init__(self, bulk, max_bulk): self.bulk = bulk self.max_bulk = max_bulk def __str__(self): return "Item would exceed the bulk limit of the container." class NoFreeSlotError(Exception): """Error that gets raised when the container has no free slots.""" def __str__(self): return "Container can't hold any more items." def get_free_slot(container): """Returns the first slot of the container that is not occupied.""" index = 0 for child in container.children: if not child: return index index += 1 raise NoFreeSlotError def get_total_bulk(container): """Returns the bulk of all items in the container.""" total_bulk = 0 for child in container.children: if child: total_bulk += child.bulk return total_bulk def get_total_weight(container): """Returns the weight of all items in the container.""" total_weight = 0 for child in container.children: if child: total_weight += child.weight return total_weight def get_item(container, slot): """Returns the item that is in the slot.""" if len(container.children) >= (slot + 1): return container.children[slot] return None def remove_item(container, slot): """Removes the item at the given slot.""" item = get_item(container, slot) if item: container.children[slot] = None item.container = None item.slot = -1 def take_item(container, slot): """Moves the item at the given slot out of the container and returns it.""" item = get_item(container, slot) if item: remove_item(container, slot) return item def put_item(container, item, slot=-1): """Puts the item at the given slot in the container. Returns the item previously at the slot.""" if slot == -1: slot = get_free_slot(container) total_bulk = get_total_bulk(container) total_bulk += item.bulk old_item = get_item(container, slot) if old_item: total_bulk -= old_item.bulk if total_bulk > container.max_bulk: raise BulkLimitError(total_bulk, container.max_bulk) remove_item(container, slot) container.children[slot] = item if item.container: remove_item(item.container, item.slot) item.container = container item.slot = slot return old_item