annotate src/parpg/components/equip.py @ 91:1bd609af9329

Added equip function.
author KarstenBock@gmx.net
date Wed, 21 Sep 2011 15:43:12 +0200
parents 0319dcc47603
children 0f659c7675f6
rev   line source
72
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
1 # This program is free software: you can redistribute it and/or modify
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
2 # it under the terms of the GNU General Public License as published by
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
3 # the Free Software Foundation, either version 3 of the License, or
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
4 # (at your option) any later version.
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
5 #
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
6 # This program is distributed in the hope that it will be useful,
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
9 # GNU General Public License for more details.
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
10 #
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
11 # You should have received a copy of the GNU General Public License
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
13
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
14 from bGrease.component import Component
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
15
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
16 class Equip(Component):
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
17 """
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
18 Component that stores the equipment (what is being worn/wieled).
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
19 """
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
20
9aebbf4e02f7 Added equip component, wich stores the equipment data of an entity.
KarstenBock@gmx.net
parents:
diff changeset
21 def __init__(self):
91
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
22 Component.__init__(self, head=object, body=object, leg=object, feet=object, l_arm=object, r_arm=object)
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
23
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
24 class SlotInvalidError(Exception):
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
25 """Error that gets raised when the slot is invalid."""
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
26
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
27 def __init__(self, slot):
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
28 self.slot = slot
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
29
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
30 def __str__(self):
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
31 return "\"%s\" is not a valid slot." % self.slot
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
32
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
33 def equip(wearer, equipable, slot):
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
34 """Equip the wearer with the given equipable.
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
35 @returns The equipable that was at the given slot, or None"""
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
36 if slot in equipable.possible_slots:
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
37 try:
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
38 old_item = getattr(wearer, slot)
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
39 setattr(wearer, slot, equipable)
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
40 return old_item
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
41 except AttributeError:
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
42 raise SlotInvalidError(slot)
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
43 return None
1bd609af9329 Added equip function.
KarstenBock@gmx.net
parents: 90
diff changeset
44