source: trunk/game/scripts/console.py @ 310

Revision 295, 6.0 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #103: Patch by Elie De Brauwer, Added possibility to make the player character run, this can be toggled with the run console command, comment[s:trac, t:103]

Line 
1#!/usr/bin/python
2
3#   This program is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7
8#   This program is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#   GNU General Public License for more details.
12
13#   You should have received a copy of the GNU General Public License
14#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16import re 
17
18class Console:
19    def __init__(self, appListener):
20        """
21        Constructor
22        @type appListener: ApplicationListener
23        @param appListener: ApplicationListener object providing a link with
24        the engine, the world and the model"""
25
26        self.commands = [
27            {"cmd":"exit"  ,"callback":self.handleQuit  ,"help":"Terminate application "},
28            {"cmd":"grid"  ,"callback":self.handleGrid  ,"help":"Toggle grid display   "},
29            {"cmd":"run"   ,"callback":self.handleRun   ,"help":"Toggle player run/walk"},
30            {"cmd":"help"  ,"callback":self.handleHelp  ,"help":"Show this help string "},
31            {"cmd":"load"  ,"callback":self.handleLoad  ,"help":"load directory file   "},
32            {"cmd":"python","callback":self.handlePython,"help":"Run some python code  "},
33            {"cmd":"quit"  ,"callback":self.handleQuit  ,"help":"Terminate application "},
34            {"cmd":"save"  ,"callback":self.handleSave  ,"help":"save directory file   "},
35        ]
36        self.appListener=appListener
37
38    def handleQuit(self, command):
39        """
40        Implements the quit console command
41        @type command: string
42        @param command: The command to run
43        @return: The resultstring"""
44       
45        self.appListener.quitGame()
46        return "Terminating ..."
47
48    def handleGrid(self, command):
49        """
50        Implements the grid console command
51        @type command: string
52        @param command: The command to run
53        @return: The resultstring"""
54
55        self.appListener.world.activeMap.toggle_renderer('GridRenderer')
56        return "Grid toggled"
57
58    def handleRun(self, command):
59        """
60        Toggles run/walk mode of the PC player
61        @type command: string
62        @param command: The command to run.
63        @return: The response"""
64       
65        if self.appListener.model.pc_run==1:
66            self.appListener.model.pc_run=0
67            return "PC is now walking"
68        else:
69            self.appListener.model.pc_run=1
70            return "PC is now running"
71           
72
73    def handleHelp(self, command):
74        """
75        Implements the help console command
76        @type command: string
77        @param command: The command to run
78        @return: The resultstring"""
79
80        res=""
81        for cmd in self.commands:
82            res+= "%10s: %s\n" % (cmd["cmd"], cmd["help"])
83
84        return res
85
86    def handlePython(self, command):
87        """
88        Implements the python console command
89        @type command: string
90        @param command: The command to run
91        @return: The resultstring"""
92
93        result=None
94        python_regex = re.compile('^python')
95        python_matches = python_regex.match(command.lower())
96        if (python_matches != None):
97            end_python = command[python_matches.end()+1:]
98            try:
99                result = str(eval(end_python))
100            except Exception, e:
101                result = str(e)
102               
103        return result
104
105    def handleLoad(self, command):
106        """
107        Implements the load console command
108        @type command: string
109        @param command: The command to run
110        @return: The resultstring"""
111
112        result=None
113        load_regex = re.compile('^load')
114        l_matches = load_regex.match(command.lower())
115        if (l_matches != None):
116            end_load = l_matches.end()
117            try:
118                l_args = command.lower()[end_load+1:].strip()
119                l_path, l_filename = l_args.split(' ')
120                self.appListener.model.load(l_path, l_filename)
121                result = "Loaded file: " + l_path + l_filename
122
123            except Exception, l_error:
124                self.appListener.engine.getGuiManager().getConsole().println('Error: ' + str(l_error))
125                result="Failed to load file"
126
127        return result
128
129    def handleSave(self, command):
130        """
131        Implements the save console command
132        @type command: string
133        @param command: The command to run
134        @return: The resultstring"""
135
136        save_regex = re.compile('^save')
137        s_matches = save_regex.match(command.lower())
138        if (s_matches != None):
139            end_save = s_matches.end()
140            try:
141                s_args = command.lower()[end_save+1:].strip()
142                s_path, s_filename = s_args.split(' ')
143                self.appListener.model.save(s_path, s_filename)
144                result = "Saved to file: " + s_path + s_filename
145
146            except Exception, s_error:
147                self.appListener.engine.getGuiManager().getConsole().println('Error: ' + str(s_error))
148                result = "Failed to save file"
149
150        return result
151
152
153    def handleConsoleCommand(self, command):
154        """
155        Implements the console logic
156        @type command: string
157        @param command: The command to run
158        @return: The response string """
159
160        result = None       
161        for cmd in self.commands:
162            regex = re.compile('^' + cmd["cmd"])
163            if regex.match(command.lower()):
164                result=cmd["callback"](command)
165
166        if result==None:
167            result = "Invalid command, enter help for more information"
168
169        return result
170
171                 
172
Note: See TracBrowser for help on using the repository browser.