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

Revision 596, 13.4 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#   This file is part of PARPG.
3
4#   PARPG is free software: you can redistribute it and/or modify
5#   it under the terms of the GNU General Public License as published by
6#   the Free Software Foundation, either version 3 of the License, or
7#   (at your option) any later version.
8
9#   PARPG is distributed in the hope that it will be useful,
10#   but WITHOUT ANY WARRANTY; without even the implied warranty of
11#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12#   GNU General Public License for more details.
13
14#   You should have received a copy of the GNU General Public License
15#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
16
17#exceptions
18class NoSuchQuestException(Exception):
19    """NoQuestException is used when there is no active quest with the id"""
20    pass
21
22#classes
23
24class Action(object):
25    """Base Action class, to define the structure"""
26
27
28    def __init__(self, controller, commands = None):
29        """Basic action constructor
30        @param controller: A reference to the GameSceneController.
31        @type controller: scripts.GameSceneController
32        @param commands: Special commands that are executed
33        @type commands: Dictionary
34        """
35        self.commands = commands or ()
36        self.controller = controller
37        self.model = controller.model
38   
39    def execute(self):
40        """To be overwritten"""       
41        #Check if there are special commands and execute them
42        for command_data in self.commands:
43            command = command_data["Command"]
44            if command == "SetQuestVariable":
45                quest_id = command_data["ID"]
46                variable = command_data["Variable"]
47                value = command_data["Value"]
48                quest_engine = self.model.game_state.quest_engine
49                if quest_engine.hasQuest(quest_id):
50                    quest_engine[quest_id].setValue(variable, value)
51                else:
52                    raise NoSuchQuestException
53               
54class ChangeMapAction(Action):
55    """A change map scheduled"""
56    def __init__(self, controller, target_map_name, target_pos, commands = None):
57        """Initiates a change of the position of the character
58        possibly flagging a new map to be loaded.
59        @param controller: A reference to the GameSceneController.
60        @type controller: scripts.GameSceneController
61        @param commands: Special commands that are executed
62        @type commands: Dictionary
63        @type view: class derived from scripts.ViewBase
64        @param view: The view
65        @type target_map_name: String
66        @param target_map_name: Target map id
67        @type target_pos: Tuple
68        @param target_pos: (X, Y) coordinates on the target map.
69        @return: None"""
70        super(ChangeMapAction, self).__init__(controller, commands)
71        self.view = controller.view
72        self.target_pos = target_pos
73        self.target_map_name = target_map_name
74
75    def execute(self):
76        """Executes the map change."""
77        self.model.changeMap(self.target_map_name,
78                              self.target_pos)
79        super(ChangeMapAction, self).execute()
80       
81class OpenBoxAction(Action):
82    """Open a box. Needs to be more generic, but will do for now."""
83    def __init__(self, controller, container, commands = None):
84        """
85        @param controller: A reference to the GameSceneController.
86        @type controller: scripts.GameSceneController
87        @param commands: Special commands that are executed
88        @type commands: Dictionary
89        @type view: class derived from scripts.ViewBase
90        @param view: The view
91        @param container: A reference to the container
92        """
93        super(OpenBoxAction, self).__init__(controller, commands)
94        self.view = controller.view
95        self.container = container
96       
97    def execute(self):
98        """Open the box."""
99        try:
100            self.container.open()
101            self.view.hud.createBoxGUI(self.container.name, \
102                                              self.container)
103            super(OpenBoxAction, self).execute()
104
105        except ValueError:
106            self.view.hud.createExamineBox(self.container.name, \
107                                                  "The container is locked")
108       
109class UnlockBoxAction(Action):
110    """Unlocks a box. Needs to be more generic, but will do for now."""
111    def __init__(self, controller, container, commands = None):
112        """
113        @param controller: A reference to the GameSceneController.
114        @type controller: scripts.GameSceneController
115        @param commands: Special commands that are executed
116        @type commands: Dictionary
117        @param container: A reference to the container
118        """
119        super(UnlockBoxAction, self).__init__(controller, commands)
120        self.container = container
121   
122    def execute(self):
123        """Open the box."""
124        self.container.unlock()
125        super(UnlockBoxAction, self).execute()
126       
127class LockBoxAction(Action):
128    """Locks a box. Needs to be more generic, but will do for now."""
129    def __init__(self, controller, container, commands = None):
130        """
131        @param controller: A reference to the GameSceneController.
132        @type controller: scripts.GameSceneController
133        @param commands: Special commands that are executed
134        @type commands: Dictionary
135        @param container: A reference to the container
136        """
137        super(LockBoxAction, self).__init__(controller, commands)
138        self.container = container
139       
140    def execute(self):
141        """Lock the box."""
142        self.container.lock()
143        super(LockBoxAction, self).execute()
144
145
146class ExamineAction(Action):
147    """Examine an object."""
148    def __init__(self, controller, examine_name, examine_desc, commands = None):
149        """
150        @param controller: A reference to the GameSceneController.
151        @type controller: scripts.GameSceneController
152        @param commands: Special commands that are executed
153        @type commands: Dictionary
154        @type view: class derived from scripts.ViewBase
155        @param view: The view
156        @type examine_name: String
157        @param examine_name: Name of the object to be examined.
158        @type examine_name: String
159        @param examine_name: Description of the object to be examined.
160        """
161        super(ExamineAction, self).__init__(controller, commands)
162        self.view = controller.view
163        self.examine_name = examine_name
164        self.examine_desc = examine_desc
165       
166    def execute(self):
167        """Examine the box."""
168        action_text = unicode(self.examine_desc)
169        self.view.hud.addAction(action_text)
170        print action_text
171
172class ReadAction(Action):
173    """Read a text."""
174    def __init__(self, controller, text_name, text, commands = None):
175        """
176        @param controller: A reference to the GameSceneController.
177        @type controller: scripts.GameSceneController
178        @param commands: Special commands that are executed
179        @type commands: Dictionary
180        @param view: The view
181        @type view: class derived from scripts.ViewBase
182        @param text_name: Name of the object containing the text
183        @type text_name: String
184        @param text: Text to be displayied
185        @type text: String
186        """
187        super(ReadAction, self).__init__(controller, commands)
188        self.view = controller.view
189        self.text_name = text_name
190        self.text = text
191       
192    def execute(self):
193        """Examine the box."""
194        action_text = unicode('\n'.join(["You read " + self.text_name + ".", 
195                                         self.text]))
196        self.view.hud.addAction(action_text)
197        print action_text
198        super(ReadAction, self).execute()
199
200class TalkAction(Action):
201    """An action to represent starting a dialogue"""
202    def __init__(self, controller, npc, commands = None):
203        """
204        @param controller: A reference to the GameSceneController.
205        @type controller: scripts.GameSceneController
206        @param commands: Special commands that are executed
207        @type commands: Dictionary
208        @type view: class derived from scripts.ViewBase
209        @param view: The view
210        @type npc: NonPlayerCharacter
211        @param npc: NPC to interact with.
212        """
213        super(TalkAction, self).__init__(controller, commands)
214        self.view = controller.view
215        self.npc = npc
216       
217    def execute(self):
218        """Talk with the NPC when close enough, otherwise move closer.
219           @return: None"""
220     
221        player_char = self.model.game_state.player_character
222        npc_coordinates = self.npc.getLocation().getLayerCoordinates()
223        pc_coordinates = player_char.behaviour.agent.\
224                            getLocation().getLayerCoordinates()
225       
226        distance_squared = (npc_coordinates.x - pc_coordinates.x) *\
227                           (npc_coordinates.x - pc_coordinates.x) +\
228                           (npc_coordinates.y - pc_coordinates.y) *\
229                           (npc_coordinates.y - pc_coordinates.y)
230       
231        # If we are too far away, we approach the NPC again
232        if distance_squared > 2:
233            player_char.approach([self.npc.getLocation().
234                         getLayerCoordinates().x,
235                         self.npc.getLocation().
236                         getLayerCoordinates().y], 
237                        TalkAction(self.controller,
238                                   self.npc, self.commands))       
239        else:
240            player_char.behaviour.agent.act('stand', self.npc.getLocation())
241   
242            if self.npc.dialogue is not None:
243                self.npc.talk(player_char)
244                self.view.hud.showDialogue(self.npc)
245            else:
246                self.npc.behaviour.agent.say("Leave me alone!", 1000)
247               
248            self.model.game_state.player_character.behaviour.idle()
249            self.model.game_state.player_character.nextAction = None
250            super(TalkAction, self).execute()
251
252class UseAction(Action):
253    """Action for carryable items. It executes special commands that can be only
254    used on carryable utens"""
255
256
257    def __init__(self, controller, item, commands = None):
258        """
259        @param controller: A reference to the GameSceneController.
260        @type controller: scripts.GameSceneController
261        @param item: Item on which the action is called
262        @type item: CarryableItem
263        @param commands: Special commands that are executed
264        @type commands: Dictionary
265        """
266        super(UseAction, self).__init__(controller, commands)
267        self.view = controller.view
268        self.item = item
269   
270    def execute(self):
271        #Check if there are special commands and execute them
272        for command_data in self.commands:
273            command = command_data["Command"]
274            if command == "ReplaceItem":
275                object_id = command_data["ID"]
276                object_type = command_data["ObjectType"]
277                container = self.item.in_container
278                inst_dict = {}
279                inst_dict["ID"] = object_id
280                inst_dict["object_type"] = object_type
281                new_item = self.model.createContainerObject(inst_dict)
282                container.replaceItem(self.item, new_item)
283                self.view.hud.inventory.updateInventoryButtons()
284        super(UseAction, self).execute()
285
286class PickUpAction(Action):
287    """Action for picking up items from a map"""
288
289    def __init__(self, controller, map_item, commands = None):
290        super(PickUpAction, self).__init__(controller, commands)
291        self.map_item = map_item
292
293    def execute(self):
294        real_item = self.model.createContainerObject(self.map_item.item)
295        self.model.deleteObject(self.map_item.ID)
296        self.model.game_state.player_character.\
297                                inventory.placeItem(real_item)
298        super(PickUpAction, self).execute()
299       
300class DropItemAction(Action):
301    """Action for dropping an items on a map"""
302
303    def __init__(self, controller, item, container_gui, commands = None):
304        super(DropItemAction, self).__init__(controller, commands)
305        self.item = item
306        self.container_gui = container_gui
307
308    def execute(self):
309        item_data = self.item.getStateForSaving()
310        item_data["type"] = self.item.item_type
311        map_name = self.model.game_state.current_map_name
312        map_item_values = {}
313        map_item_values["ViewName"] = self.item.name
314        map_item_values["ObjectType"] = "MapItem"
315        map_item_values["ItemType"] = self.item.item_type
316        map_item_values["Map"] = map_name
317        coords = self.model.game_state.player_character.\
318                                        getLocation().getExactLayerCoordinates()
319        map_item_values["Position"] = (coords.x - 1.0, coords.y + 1.0)
320        map_item_values["Rotation"] = 0
321        map_item_values["item"] = item_data
322        agent = {}
323        agent[self.item.ID] = map_item_values
324        self.model.addAgent("All", agent)
325        self.model.placeAgents()
326        self.item.in_container.takeItem(self.item)       
327        self.container_gui.updateImages()
328        super(DropItemAction, self).execute()
329
330ACTIONS = {"ChangeMap":ChangeMapAction, 
331           "OpenBox":OpenBoxAction, 
332           "Unlock":UnlockBoxAction,
333           "Lock":LockBoxAction,
334           "Examine":ExamineAction,
335           "Look":ExamineAction,
336           "Read":ReadAction,
337           "Talk":TalkAction,
338           "Use":UseAction,
339           "PickUp":PickUpAction,
340           "Drop":DropItemAction}
Note: See TracBrowser for help on using the repository browser.