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

Revision 395, 5.7 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #137 and #138. Patch by Beliar (confirmed by eliedebrauwer). Fixes footlocker animation, added support for opening/closing footlocker animations, added footlocker onto the map, isolates contents of containers. Added copyright notice to layer_fill_utility.py. fixes[s:trac, t:137] fixes[s:trac, t:138]

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