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

Revision 477, 5.8 KB checked in by maximinus_parpg, 10 years ago (diff)

More clean up of the code.
Finshed first pass.
So should be at least stands compliant.

  • 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        self.engine = engine
55        self.container = container
56       
57    def execute(self):
58        """Open the box."""
59        try:
60            self.container.open()
61            self.engine.view.hud.createBoxGUI(self.container.name, \
62                                              self.container)
63
64        except ValueError:
65            self.engine.view.hud.createExamineBox(self.container.name, \
66                                                  "The container is locked")
67       
68class UnlockBoxAction(Action):
69    """Unlocks a box. Needs to be more generic, but will do for now."""
70    def __init__(self,  container):
71        """@param container: A reference to the container"""
72        self.container = container
73   
74    def execute(self):
75        """Open the box."""
76        self.container.unlock()
77       
78class LockBoxAction(Action):
79    """Locks a box. Needs to be more generic, but will do for now."""
80    def __init__(self, container):
81        """@param container: A reference to the container"""
82        self.container = container
83       
84    def execute(self):
85        """Lock the box."""
86        self.container.lock()
87
88
89class ExamineBoxAction(Action):
90    """Examine a box. Needs to be more generic, but will do for now."""
91    def __init__(self, engine, examine_name, examine_desc):
92        """@type engine: Engine reference
93           @param engine: A reference to the engine.
94           @type examine_name: String
95           @param examine_name: Name of the object to be examined.
96           @type examine_name: String
97           @param examine_name: Description of the object to be examined."""
98        self.engine = engine
99        self.examine_name = examine_name
100        self.examine_desc = examine_desc
101       
102    def execute(self):
103        """Examine the box."""
104        self.engine.view.hud.createExamineBox(self.examine_name, \
105                                              self.examine_desc)
106
107class TalkAction(Action):
108    """An action to represent starting a dialogue"""
109    def __init__(self, engine, npc):
110        """@type engine: Engine reference
111           @param engine: A reference to the engine.
112           @type npc: NonPlayerCharacter
113           @param npc: NPC to interact with."""
114        self.engine = engine
115        self.npc = npc
116       
117    def execute(self):
118        """Talk with the NPC when close enough, otherwise move closer.
119           @return: None"""
120     
121        pc = self.engine.game_state.PC
122        npc_coordinates = self.npc.getLocation().getLayerCoordinates()
123        pc_coordinates = pc.behaviour.agent.getLocation().getLayerCoordinates()
124       
125        distance_squared = (npc_coordinates.x - pc_coordinates.x) *\
126                           (npc_coordinates.x - pc_coordinates.x) +\
127                           (npc_coordinates.y - pc_coordinates.y) *\
128                           (npc_coordinates.y - pc_coordinates.y)
129       
130        # If we are too far away, we approach the NPC again
131        if distance_squared > 2:
132            pc.approach([self.npc.getLocation().\
133                         getLayerCoordinates().x, \
134                         self.npc.getLocation().\
135                         getLayerCoordinates().y], \
136                        TalkAction(self.engine,\
137                                   self.npc))       
138        else:
139            pc.behaviour.agent.act('stand', self.npc.getLocation())
140   
141            if self.npc.dialogue is not None:
142                self.npc.talk(pc)
143                self.engine.view.hud.showDialogue(self.npc)
144            else:
145                self.npc.behaviour.agent.say("Leave me alone!", 1000)
146               
147            self.engine.game_state.PC.behaviour.idle()
148            self.engine.game_state.PC.nextAction = None
Note: See TracBrowser for help on using the repository browser.