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

Revision 451, 5.8 KB checked in by b0rland_parpg, 10 years ago (diff)

Ticket #241: patch by b0rland
*Removed reference from containers to GUI
*Fixed SingleItemContainer? with the new type of exception

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/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
18class Action(object):
19    """Base Action class, to define the structure"""
20    def execute(self):
21        """To be overwritten"""
22        pass
23
24class ChangeMapAction(Action):
25    """A change map scheduled"""
26    def __init__(self, engine, target_map_name, target_map_file , target_pos):
27        """Initiates a change of the position of the character
28           possibly flagging a new map to be loaded.
29           @type engine: Engine reference
30           @param engine: A reference to the engine.
31           @type target_map_name: String
32           @param target_map_name: Target map id
33           @type target_map_file: String
34           @param target_map_file: Target map filename
35           @type target_pos: Tuple
36           @param target_pos: (X, Y) coordinates on the target map.
37           @return: None"""
38        self.engine = engine
39        self.target_pos = target_pos
40        self.target_map_name = target_map_name
41        self.target_map_file = target_map_file
42
43    def execute(self):
44        """Executes the map change."""
45        self.engine.changeMap(self.target_map_name, self.target_map_file,\
46                              self.target_pos)
47       
48class OpenBoxAction(Action):
49    """Open a box. Needs to be more generic, but will do for now."""
50    def __init__(self, engine, container):
51        """@type engine: Engine reference
52           @param engine: A reference to the engine.
53           @param container: A reference to the container
54           """
55        self.engine = engine
56        self.container = container
57       
58    def execute(self):
59        """Open the box."""
60        try:
61            self.container.open()
62            self.engine.view.hud.createBoxGUI(self.container.name, \
63                                              self.container)
64
65        except ValueError:
66            self.engine.view.hud.createExamineBox(self.container.name, \
67                                              "The container is locked")
68       
69class UnlockBoxAction(Action):
70    """Unlocks a box. Needs to be more generic, but will do for now."""
71    def __init__(self,  container):
72        """@param container: A reference to the container
73           """
74        self.container = container
75   
76    def execute(self):
77        """Open the box."""
78        self.container.unlock()
79       
80class LockBoxAction(Action):
81    """Locks a box. Needs to be more generic, but will do for now."""
82    def __init__(self, container):
83        """@param container: A reference to the container
84           """
85        self.container = container
86    def execute(self):
87        """Lock the box."""
88        self.container.lock()
89
90
91class ExamineBoxAction(Action):
92    """Examine a box. Needs to be more generic, but will do for now."""
93    def __init__(self, engine, examine_name, examine_desc):
94        """@type engine: Engine reference
95           @param engine: A reference to the engine.
96           @type examine_name: String
97           @param examine_name: Name of the object to be examined.
98           @type examine_name: String
99           @param examine_name: Description of the object to be examined.
100           """
101        self.engine = engine
102        self.examine_name = examine_name
103        self.examine_desc = examine_desc
104       
105    def execute(self):
106        """Examine the box."""
107        self.engine.view.hud.createExamineBox(self.examine_name, \
108                                              self.examine_desc)
109
110class TalkAction(Action):
111    """An action to represent starting a dialogue"""
112    def __init__(self, engine, npc):
113        """@type engine: Engine reference
114           @param engine: A reference to the engine.
115           @type npc: NonPlayerCharacter
116           @param npc: NPC to interact with.
117           """
118        self.engine = engine
119        self.npc = npc
120       
121    def execute(self):
122        """Talk with the NPC when we are close enough, otherwise
123           we move close.
124           @return: None"""
125       
126        pc = self.engine.game_state.PC
127        npc_coordinates = self.npc.getLocation().getLayerCoordinates()
128        pc_coordinates = pc.behaviour.agent.getLocation().getLayerCoordinates()
129       
130        distance_squared = (npc_coordinates.x - pc_coordinates.x) *\
131                         (npc_coordinates.x - pc_coordinates.x) +\
132                         (npc_coordinates.y - pc_coordinates.y) *\
133                         (npc_coordinates.y - pc_coordinates.y)
134       
135       
136        # If we are too far away, we approach the NPC again
137        if distance_squared > 2:
138            pc.approach([self.npc.getLocation().\
139                         getLayerCoordinates().x, \
140                         self.npc.getLocation().\
141                         getLayerCoordinates().y], \
142                        TalkAction(self.engine,\
143                                   self.npc))       
144        else:
145            pc.behaviour.agent.act('stand', self.npc.getLocation())
146   
147            if self.npc.dialogue is not None:
148                self.npc.talk(pc)
149                self.engine.view.hud.showDialogue(self.npc)
150            else:
151                self.npc.behaviour.agent.say("Leave me alone!", 1000)
152               
153            self.engine.game_state.PC.behaviour.idle()
154            self.engine.game_state.PC.nextAction = None 
Note: See TracBrowser for help on using the repository browser.