[1383] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Benjamin Knecht |
---|
| 24 | * Co-authors: |
---|
[7404] | 25 | * Damian 'Mozork' Frick |
---|
[1383] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef _Script_H__ |
---|
| 30 | #define _Script_H__ |
---|
| 31 | |
---|
[5730] | 32 | #include "objects/ObjectsPrereqs.h" |
---|
[1383] | 33 | |
---|
[3196] | 34 | #include <string> |
---|
[1383] | 35 | #include "core/BaseObject.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
[7404] | 39 | |
---|
| 40 | namespace ScriptMode |
---|
| 41 | { |
---|
| 42 | //! Modes of the Script class. |
---|
| 43 | enum Value |
---|
| 44 | { |
---|
| 45 | normal, |
---|
| 46 | lua |
---|
| 47 | }; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | @brief |
---|
| 52 | The Script class lets you execute a piece of code, either the normal way or in lua, through XML. It can be specified whether the code is executed upon loading (creation) of the object. Additionally the code is executed each time a trigger event comes in. |
---|
| 53 | There are three parameters: |
---|
| 54 | 'code': The code that should be executed. |
---|
| 55 | 'mode': The mode, specifying whether the set code should be executed the normal way ('normal') or in lua ('lua'). Default is 'normal'. |
---|
| 56 | 'onLoad': Whether the code is executed upon loading (creation) of this object. Default is true. |
---|
| 57 | |
---|
| 58 | Here are two examples illustrating the usage: |
---|
| 59 | @code |
---|
| 60 | <Script code="showGUI QuestGUI" /> |
---|
| 61 | @endcode |
---|
| 62 | This would show the QuestGUI opon creation of the object. The mode is 'normal', not specified here since that is the default, also onLoad is true, also not specified, since it is the default as well. |
---|
| 63 | |
---|
| 64 | @code |
---|
| 65 | <Script code="hideGUI QuestGUI" mode="normal" onLoad="false"> |
---|
| 66 | <events> |
---|
| 67 | <trigger> |
---|
| 68 | <DistanceTrigger distance=10 target="Pawn" /> |
---|
| 69 | </trigger> |
---|
| 70 | </events> |
---|
| 71 | </Script> |
---|
| 72 | @endcode |
---|
| 73 | This would hide the QuestGUI as soon as a Pawn got in range of the DistanceTrigger. The mode is 'normal', it is specified here, but could be ommitted as well, since it is the default. OnLoad is false, that is why it can't be ommitted. |
---|
| 74 | @author |
---|
| 75 | Benjamin Knecht |
---|
| 76 | Damian 'Mozork' Frick |
---|
| 77 | */ |
---|
[5730] | 78 | class _ObjectsExport Script : public BaseObject |
---|
[5695] | 79 | { |
---|
[7404] | 80 | public: |
---|
| 81 | Script(BaseObject* creator); |
---|
| 82 | virtual ~Script(); |
---|
[1383] | 83 | |
---|
[7404] | 84 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a Script object through XML. |
---|
[7407] | 85 | virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); //!< Creates a port that can be used to channel events and react to them. |
---|
[5695] | 86 | |
---|
[7404] | 87 | void trigger(bool triggered); //!< Is called when an event comes in trough the event port. |
---|
| 88 | void execute(); //!< Executes the Scripts code, depending on the mode. |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | @brief Sets the code that is executed by this Script. |
---|
| 92 | @param code The code that is executed by this Script. |
---|
| 93 | */ |
---|
| 94 | inline void setCode(const std::string& code) |
---|
| 95 | { code_ = code; } |
---|
| 96 | /** |
---|
| 97 | @brief Get the code that is executed by this Script. |
---|
| 98 | @return Returns the code that is executed by this Script. |
---|
| 99 | */ |
---|
| 100 | inline const std::string& getCode() const |
---|
| 101 | { return code_; } |
---|
| 102 | |
---|
| 103 | void setMode(const std::string& mode); //!< Sets the mode of the Script. |
---|
| 104 | const std::string& getMode(void); //!< Get the mode of the Script. |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | @brief Set whether this Script is executed onLoad or not. |
---|
| 108 | @param onLoad if true the Script is executed onLoad, if false it's not. |
---|
| 109 | */ |
---|
| 110 | inline void setOnLoad(bool onLoad) |
---|
| 111 | { this->onLoad_ = onLoad; } |
---|
| 112 | /** |
---|
| 113 | @brief Get whether this Script is executed onLoad. |
---|
| 114 | @return Returns true if this Script is executed onLoad, false if not. |
---|
| 115 | */ |
---|
| 116 | inline bool isOnLoad(void) |
---|
| 117 | { return this->onLoad_; } |
---|
| 118 | |
---|
| 119 | void setTimes(int times); //!< Set the number of times this Script is executed at the most. |
---|
| 120 | /** |
---|
| 121 | @brief Get the number of times this Script is executed at the most. |
---|
| 122 | @return Returns the number of times this Script is executed at the most. -1 denotes infinity. |
---|
| 123 | */ |
---|
| 124 | inline int getTimes(void) |
---|
| 125 | { return this->times_; } |
---|
| 126 | |
---|
| 127 | private: |
---|
| 128 | //! Static variables to avoid magic strings. |
---|
| 129 | static const std::string NORMAL; |
---|
| 130 | static const std::string LUA; |
---|
[7406] | 131 | static const int INF; |
---|
[7404] | 132 | |
---|
| 133 | std::string code_; //!< The code that is executed by this Script. |
---|
| 134 | ScriptMode::Value mode_; //!< The mode the Script is in. Determines whether the code is executed the normal way or in lua. |
---|
| 135 | bool onLoad_; //!< Whether the Scripts code is executed upon loading (creation) of this Script. |
---|
| 136 | int times_; //!< The number of times the Scripts code is executed at the most. -1 denotes infinity. |
---|
| 137 | |
---|
| 138 | LuaState* luaState_; //!< The LuaState to execute the code in lua. |
---|
| 139 | int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity. |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | @brief Sets the mode of the Script. |
---|
| 143 | @param mode The mode of the Script. |
---|
| 144 | */ |
---|
| 145 | inline void setMode(ScriptMode::Value mode) |
---|
| 146 | { this->mode_ = mode; } |
---|
[5695] | 147 | }; |
---|
[1383] | 148 | } |
---|
| 149 | |
---|
| 150 | #endif /* _Script_H__ */ |
---|