annotate src/parpg/systems/container.py @ 37:799fc37fec6d

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