/* * BAGSGame.bsh * * Copyright 2005 M. Aaron Wadley * * This file is part of BAGS (Beany Adventure Game System). * BAGS (Beany Adventure Game System) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * BAGS (Beany Adventure Game System) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with BAGS (Beany Adventure Game System); if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // Load up the BAGS code source("BAGSLogger.bsh"); source("BAGSCommands.bsh"); source("BAGSParser.bsh"); source("BAGSGui.bsh"); source("BAGSRooms.bsh"); source("BAGSStatusBar.bsh"); source("BAGSConnection.bsh"); source("BAGSObject.bsh"); source("BAGSPlayer.bsh"); if (isNotNull(loglevel)) { switch(loglevel) { case "TRACE": logThreshold = TRACE_MODE; break; case "DEBUG": logThreshold = DEBUG_MODE; break; case "INFO": logThreshold = INFO_MODE; break; case "WARN": logThreshold = WARN_MODE; break; case "": logThreshold = TRACE_MODE; break; case "TRACE": logThreshold = TRACE_MODE; break; default: break; } } log=Logger(logThreshold); // Change the interpreter to the game directory String gd = BAGS_GAMES + gameId; cd(gd); statusBar = null; BAGS_version="0.1"; BAGS_url="http://cyberlizard.org/bags"; newline="\n"; waitingForInput = false; /** * This is the driver for the whole BAGS system; * A game calls this method to start. */ Game() { gotInput = false; inputString = ""; /** KeyListener */ keyPressed(e) { } /** KeyListener */ keyReleased(e) { } /** KeyListener */ keyTyped(e) { log.trace("Key typed " + e.getKeyChar()); gotInput = true; } /** * Wait for the user to press any key. */ waitForKeyPress() { // output wait message output.insertStyledString("[Press any key to continue]", STYLE_ITALIC); output.insertLineBreak(); output.insertLineBreak(); log.trace("Adding key listener"); gui.addKeyListener(game); while (!gotInput) { Thread.sleep(1000); Thread.yield(); } gotInput = false; log.trace("Removing key listener"); gui.removeKeyListener(game); gui.commandInput.setText(new String()); } waitForInput() { waitingForInput = true; while (!gotInput) { Thread.sleep(1000); Thread.yield(); } gotInput = false; waitingForInput = false; return inputString; } waitForKeyPress(msg) { // output wait message output.insertStyledString(msg, STYLE_ITALIC); output.insertLineBreak(); output.insertLineBreak(); log.trace("Adding key listener"); gui.addKeyListener(game); while (!gotInput) { Thread.sleep(1000); } gotInput = false; log.trace("Removing key listener"); gui.removeKeyListener(game); gui.commandInput.setText(new String()); } /** * Determine if an object has a method with * a given name * * @param obj Object to be evaluated * @param methodName Name of the method to check for * @return boolean */ hasMethod(obj, methodName) { hasmethod = false; methods = obj.namespace.getMethods(); for (meth : methods) { if (meth.getName().equals(methodName)) { hasmethod = true; break; } } return hasmethod; } /** * Display help text * TODO Make into a URM. */ displayHelp() { parser.displayHelp(output, gameTitle); } getExitsForRoom(room) { exits = new Vector(); for (an_exit : connections) { if (an_exit.pointA.equals(room.roomId) || an_exit.pointB.equals(room.roomId)) { exits.add(an_exit); log.debug("Adding connection " + an_exit.toString()); } } return exits; } /** * Display all the available exits. * TODO make dynamic based on exit availability and * whether or not the player has been to a room * referenced in the exit. */ showExits() { output.insertStyledString("Available exits :", STYLE_ITALIC ); output.insertLineBreak(); exits = getExitsForRoom(currentLocation); for (an_exit : exits) { if (an_exit.pointA.equals(currentLocation.roomId)) { from = an_exit.pointA; fromDir = an_exit.pointADir; to = an_exit.pointB; } else { from = an_exit.pointB; fromDir = an_exit.pointBDir; to = an_exit.pointA; } displayFromDir = dirNames[fromDir]; output.insertStyledString( displayFromDir , STYLE_BOLD); output.insertStyledString ( " to ", STYLE_REGULAR); output.insertStyledString (rooms.getDynamicTitle(rooms.roomList.get(to)), STYLE_ITALIC); output.insertLineBreak(); } output.insertLineBreak(); output.scrollToEnd(); } /** * Display the description of the current location. */ showLocation() { statusBar.update(this); rooms.outputDescription(currentLocation); } /** * Action handler * @param ActionEvent Event that triggered this. */ actionPerformed(event) { String command; command = gui.commandInput.getText(); output.insertStyledString("> " + command, STYLE_BLUE_BOLD); output.insertLineBreak(); // Clear text area game.gui.commandInput.setText (new String()); game.gui.commandInput.requestFocusInWindow(); if (waitingForInput) { inputString = command; gotInput = true; return; } parser.commandString = command; Thread t = new Thread(parser); t.start(); //parser.parseCommand(command); } /** * Increment the turn. */ incrementTurn() { turn++; statusBar.update(this); } /** * Update the status bar. */ updateStatus() { statusBar.update(this); } /** * Display intro text and copyright information */ displayIntro() { output.clear(); if (displayGameInfo.equals(TRUE)) { output.insertStyledString(gameTitle, STYLE_BOLD); output.insertLineBreak(); output.insertString(gameDescription + newline); output.insertLineBreak(); output.insertStyledString(gameTitle, STYLE_ITALIC); output.insertString(" Version " + gameVersion + " Copyright " + copyrightYear + " by " + copyrightHolder + newline); output.insertString("Version " + gameVersion + " released on " + gameCreationDate); output.insertLineBreak(); } if (displayBAGSInfo.equals(TRUE)) { output.insertLineBreak(); output.insertString("BAGS v" + BAGS_version + " " + BAGS_url); output.insertLineBreak(); output.insertLineBreak(); } waitForKeyPress("[Press any key to begin]"); output.clear(); } updateColors(newFg, newBg) { gui.updateColors(newFg, newBg); statusBar.updateColors(newFg,newBg); } updateBackground(newBg) { gui.updateBackground(newBg); statusBar.updateBackground(newBg); } updateForeground(newFg) { gui.updateForeground(newFg); statusBar.updateForeground(newFg); } /*=======================================================================* * Main game logic *=======================================================================*/ tmpDebugInGame = debugInGame; debugInGame = FALSE; log.debug("Starting " + gameTitle); /** Graphical User Interface */ gui=Gui(gameTitle); gui.addCommandActionListener(this); /** Status bar, can be customized */ statusBar = StatusBar(gui.statusPanel); /** Output */ output=Output(gui.displayOutput); /** Command parser */ parser = Parser(this, output, global.customParserImplementation); /** Rooms */ rooms = Rooms(this, output); /** Connections between rooms */ connections = new Vector(); /** All objects in game */ gameObjects = null; /** Player character */ pc = PlayerCharacter(); // Try to load the game's objects try { source(objectsImplementation); customObjects=CustomObjects(this); gameObjects=GameObjects(customObjects); } catch(e) { print(e); return; } // Try to load the game's rooms try { source(roomsImplementation); customRooms=CustomRooms(connections); rooms.loadRooms(customRooms); } catch(e) { print(e); return; } // Init the game // TODO implement dynamic initialization w/URM currentLocation = rooms.getRoom(customRooms.firstRoom); turn = 1; // Display this for the first time in. //displayIntro(); // Display the starting location //showLocation(); debugInGame = tmpDebugInGame; // Closure return statement return this; }