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

Revision 563, 6.3 KB checked in by beliar, 10 years ago (diff)

Ticket #200: Patch by Beliar.

  • Merging map_loading_change branch back into trunk

fixes[s:trac, t:200]

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