source: trunk/game/scripts/objects/composed.py @ 596

Revision 596, 4.7 KB checked in by beliar, 9 years ago (diff)

Patch by Beliar.

  • Items can now be dropped and picked up again
  • Added generic image for items (Made by Q_x, he did the other items too).
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18"""Composite game object classes are kept here"""
19
20from base import GameObject, Container, Lockable, \
21                Scriptable, Trappable, Destructable, Carryable, \
22                Usable
23
24class ImmovableContainer(GameObject, Container, Lockable, Scriptable, 
25                         Trappable, Destructable):
26    """Composite class that can be used for crates, chests, etc."""
27    def __init__ (self, **kwargs):
28        GameObject   .__init__(self, **kwargs)
29        Container    .__init__(self, **kwargs)
30        Lockable     .__init__(self, **kwargs)
31        Scriptable   .__init__(self, **kwargs)
32        Trappable    .__init__(self, **kwargs)
33        Destructable .__init__(self, **kwargs)
34        self.blocking = True
35
36class CarryableContainer(Container, Carryable):
37    """Composite class that will be used for backpack, pouches, etc."""
38    def __init__ (self, **kwargs):
39        Container.__init__(self, **kwargs)
40        Carryable.__init__(self, **kwargs)
41        self.own_bulk = 0
42        self.own_weight = 0
43
44    def getWeight(self):
45        """Resulting weight of a container"""
46        return sum((item.weight for item in self.items.values()), 
47                   self.own_weight)
48
49    def setWeight(self, weight):
50        """Set container's own weight.
51        For compatibility with inherited methods"""
52        self.own_weight = weight
53
54    weight = property(getWeight, setWeight, "Total weight of container")
55
56    def getBulk(self):
57        """Resulting bulk of container"""
58        return self.getContentsBulk()+self.own_bulk
59
60    def setBulk(self, bulk):
61        """Set container's own bulk. For compatibility with inherited methods"""
62        self.own_bulk = bulk
63
64    bulk = property(getBulk, setBulk, "Total bulk of container")
65   
66    def __repr__(self):
67        return "[%s" % self.name + str(reduce((lambda a, b: a + ', ' + \
68                                    str(self.items[b])), self.items, "")) + " ]"
69   
70class SingleItemContainer (CarryableContainer) :
71    """Container that can only store a single item.
72       This class can represent single-item inventory slots"""
73    def __init__ (self, **kwargs):
74        CarryableContainer.__init__(self, **kwargs)
75
76    def placeItem(self,item, index=None):
77        if len(self.items) > 0 :
78            raise self.SlotBusy ('%s is already busy' % self)
79        CarryableContainer.placeItem(self, item)
80
81class CarryableItem (GameObject, Carryable, Usable):
82    """Composite class that will be used for all carryable items"""
83    def __init__(self, item_id, type = "CarryableItem", **kwargs):
84        GameObject.__init__(self, **kwargs)
85        Carryable.__init__(self, **kwargs)
86        Usable.__init__(self, **kwargs)
87        self.item_id = item_id
88        self.item_type = type
89
90    def prepareStateForSaving(self, state):
91        """Prepares state for saving
92        @type state: dictionary
93        @param state: State of the object 
94        """
95        super(CarryableItem, self).prepareStateForSaving(state)
96        del state["in_container"]
97        del state["on_map"]
98        del state["agent"]
99
100    def getStateForSaving(self):
101        """Returns state for saving
102        @type state: dictionary
103        @param state: State of the object 
104        """
105        ret_dict = self.__dict__.copy()
106        self.prepareStateForSaving(ret_dict)
107        return ret_dict
108       
109class Door(GameObject, Lockable, Scriptable, Trappable):
110    """Composite class that can be used to create doors on a map."""
111    def __init__ (self, target_map_name = 'my-map',
112                  target_x = 0.0, target_y = 0.0, **kwargs):
113        GameObject.__init__(self, **kwargs)
114        Lockable.__init__(self, **kwargs)
115        Scriptable.__init__(self, **kwargs)
116        Trappable.__init__(self, **kwargs)
117        self.is_door = True
118        self.target_map_name = target_map_name
119        self.target_pos = (target_x, target_y)
120        self.blocking = True
121
122    def getStateForSaving(self):
123        """Returns state for saving
124        """
125        ret_dict = super(Door, self).getStateForSaving()
126        return ret_dict
Note: See TracBrowser for help on using the repository browser.