comparison systems/container.py @ 14:a9019d0ac5f8

Added system for containers
author KarstenBock@gmx.net
date Sun, 31 Jul 2011 23:47:28 +0200
parents
children 5c587a0d9ad7
comparison
equal deleted inserted replaced
13:bf165b30254f 14:a9019d0ac5f8
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13
14 from parpg.grease import System
15 from parpg.components import container
16
17 class Container(System):
18 """System that controls changes to containers"""
19
20 def __init__(self, drag_container,
21 container_component="container",
22 containable_component="containable"
23 ):
24 """Constructor"""
25 self.drag_container = drag_container
26 self.container_component = container_component
27 self.containable_component = containable_component
28 self.on_bulk_to_big = []
29
30 def step(self, dt):
31 """Perform changes to containers and items"""
32 assert self.world is not None, "Cannot run with no world set"
33 for containable in self.world.components.join(
34 self.containable_component
35 ):
36 if containable.new_container:
37 new_container = containable.new_container
38 new_slot = containable.new_slot
39 bulk_add = containable.bulk
40 if new_slot >= 0 and new_slot < len(new_container.children):
41 bulk_add -= new_container.children[new_slot].bulk
42 elif new_slot >= len(new_container.children):
43 new_slot = -1
44 bulk = get_bulk(new_container) + bulk_add
45 if bulk < new_container.max_bulk:
46 if new_slot >= 0:
47 new_container.children.insert(new_slot + 1, containable)
48 containable.current_container = new_container
49 old_containable = new_container.children.pop(new_slot)
50 old_containable.current_container = drag_container
51 drag_container.children = [old_containable]
52 else:
53 for listener in self.on_bulk_to_big:
54 listener(bulk, new_container.max_bulk)
55
56