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