diff --git a/src/modes/world.cpp b/src/modes/world.cpp index ef607f0e9..0815022b2 100644 --- a/src/modes/world.cpp +++ b/src/modes/world.cpp @@ -167,7 +167,7 @@ void World::init() // Grab the track file Track *track = track_manager->getTrack(race_manager->getTrackName()); - m_script_engine = new Scripting::ScriptEngine(); + Scripting::ScriptEngine::getInstance(); if(!track) { std::ostringstream msg; @@ -177,7 +177,7 @@ void World::init() } std::string script_path = track->getTrackFile("scripting.as"); - m_script_engine->loadScript(script_path, true); + Scripting::ScriptEngine::getInstance()->loadScript(script_path, true); // Create the physics Physics::getInstance(); @@ -493,7 +493,7 @@ World::~World() // but kill handles this correctly. Physics::kill(); - delete m_script_engine; + Scripting::ScriptEngine::kill(); m_world = NULL; @@ -998,7 +998,8 @@ void World::update(float dt) PROFILER_POP_CPU_MARKER(); if(race_manager->isRecordingRace()) ReplayRecorder::get()->update(dt); - if (m_script_engine) m_script_engine->update(dt); + Scripting::ScriptEngine *script_engine = Scripting::ScriptEngine::getInstance(); + if (script_engine) script_engine->update(dt); if (!history->dontDoPhysics()) { diff --git a/src/modes/world.hpp b/src/modes/world.hpp index fbdca0382..ce149d099 100644 --- a/src/modes/world.hpp +++ b/src/modes/world.hpp @@ -122,9 +122,6 @@ protected: RaceManager::KartType type, PerPlayerDifficulty difficulty); - /**Pointer to scripting engine */ - Scripting::ScriptEngine* m_script_engine; - /** Pointer to the race GUI. The race GUI is handled by world. */ RaceGUIBase *m_race_gui; @@ -286,6 +283,7 @@ public: AbstractKart* getPlayerKart(unsigned int player) const; AbstractKart* getLocalPlayerKart(unsigned int n) const; virtual const btTransform &getStartTransform(int index); + void moveKartTo(AbstractKart* kart, const btTransform &t); // ------------------------------------------------------------------------ /** Returns a pointer to the race gui. */ RaceGUIBase *getRaceGUI() const { return m_race_gui;} @@ -309,12 +307,6 @@ public: unsigned int getCurrentNumPlayers() const { return m_num_players - m_eliminated_players;} // ------------------------------------------------------------------------ - /** Returns a pointer to the Scripting Engine. */ - Scripting::ScriptEngine *getScriptEngine() - const { return m_script_engine; } - // ------------------------------------------------------------------------ - void moveKartTo(AbstractKart* kart, const btTransform &t); - // ------------------------------------------------------------------------ /** The code that draws the timer should call this first to know * whether the game mode wants a timer drawn. */ virtual bool shouldDrawTimer() const diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 56597a16a..bd3e92f55 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -169,7 +169,8 @@ void Physics::update(float dt) p->getContactPointCS(0), p->getUserPointer(1)->getPointerKart(), p->getContactPointCS(1) ); - Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); + Scripting::ScriptEngine* script_engine = + Scripting::ScriptEngine::getInstance(); int kartid1 = p->getUserPointer(0)->getPointerKart()->getWorldKartId(); int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId(); script_engine->runFunction(false, "void onKartKartCollision(int, int)", @@ -184,7 +185,7 @@ void Physics::update(float dt) { // Kart hits physical object // ------------------------- - Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); + Scripting::ScriptEngine* script_engine = Scripting::ScriptEngine::getInstance(); AbstractKart *kart = p->getUserPointer(1)->getPointerKart(); int kartId = kart->getWorldKartId(); PhysicalObject* obj = p->getUserPointer(0)->getPointerPhysicalObject(); @@ -267,7 +268,7 @@ void Physics::update(float dt) { // Projectile hits physical object // ------------------------------- - Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); + Scripting::ScriptEngine* script_engine = Scripting::ScriptEngine::getInstance(); Flyable* flyable = p->getUserPointer(0)->getPointerFlyable(); PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject(); std::string obj_id = obj->getID(); diff --git a/src/physics/physics.hpp b/src/physics/physics.hpp index a5d1209b7..9f6702a3b 100644 --- a/src/physics/physics.hpp +++ b/src/physics/physics.hpp @@ -154,7 +154,10 @@ private: Physics(); virtual ~Physics(); + + // Give the singleton access to the constructor friend class AbstractSingleton; + public: void init (const Vec3 &min_world, const Vec3 &max_world); void addKart (const AbstractKart *k); diff --git a/src/scriptengine/script_engine.hpp b/src/scriptengine/script_engine.hpp index 7ea19f96c..f542c8220 100644 --- a/src/scriptengine/script_engine.hpp +++ b/src/scriptengine/script_engine.hpp @@ -19,14 +19,15 @@ #ifndef HEADER_SCRIPT_ENGINE_HPP #define HEADER_SCRIPT_ENGINE_HPP -#include -#include -#include -#include - #include "scriptengine/script_utils.hpp" #include "utils/no_copy.hpp" #include "utils/ptr_vector.hpp" +#include "utils/singleton.hpp" + +#include +#include +#include +#include class TrackObjectPresentation; @@ -55,12 +56,16 @@ namespace Scripting ~PendingTimeout(); }; - class ScriptEngine + class ScriptEngine : public AbstractSingleton { + ScriptEngine(); + ~ScriptEngine(); + + // Give the singleton access to the constructor. + friend class AbstractSingleton; + public: - ScriptEngine(); - ~ScriptEngine(); void runFunction(bool warn_if_not_found, std::string function_name); void runFunction(bool warn_if_not_found, std::string function_name, diff --git a/src/scriptengine/script_utils.cpp b/src/scriptengine/script_utils.cpp index 18b3b3496..03f698569 100644 --- a/src/scriptengine/script_utils.cpp +++ b/src/scriptengine/script_utils.cpp @@ -22,7 +22,6 @@ #include "input/device_manager.hpp" #include "input/input_device.hpp" #include "input/input_manager.hpp" -#include "modes/world.hpp" #include "scriptengine/script_engine.hpp" #include "states_screens/dialogs/tutorial_message_dialog.hpp" #include "tracks/track.hpp" @@ -109,8 +108,7 @@ namespace Scripting /** Runs the script function specified by the given string */ void runScript(const std::string* str) { - ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); - script_engine->runFunction(true, *str); + ScriptEngine::getInstance()->runFunction(true, *str); } /** Generate a random integer value */ @@ -129,13 +127,13 @@ namespace Scripting /** Call a function after the specified delay */ void setTimeout(const std::string* callback_name, float delay) { - World::getWorld()->getScriptEngine()->addPendingTimeout(delay, *callback_name); + ScriptEngine::getInstance()->addPendingTimeout(delay, *callback_name); } /** Call a method from the given object after the specified delay */ void setTimeoutDelegate(asIScriptFunction* obj, float delay) { - World::getWorld()->getScriptEngine()->addPendingTimeout(delay, obj); + ScriptEngine::getInstance()->addPendingTimeout(delay, obj); } /** Log to the console */ diff --git a/src/states_screens/dialogs/scripting_console.cpp b/src/states_screens/dialogs/scripting_console.cpp index f7b7228f6..3e0770e58 100644 --- a/src/states_screens/dialogs/scripting_console.cpp +++ b/src/states_screens/dialogs/scripting_console.cpp @@ -21,7 +21,6 @@ #include "guiengine/widgets/button_widget.hpp" #include "guiengine/widgets/label_widget.hpp" #include "guiengine/widgets/text_box_widget.hpp" -#include "modes/world.hpp" #include "scriptengine/script_engine.hpp" #include "states_screens/state_manager.hpp" #include "utils/translation.hpp" @@ -80,7 +79,8 @@ void ScriptingConsole::runScript() core::stringw script = textCtrl->getText(); textCtrl->setText(L""); - World::getWorld()->getScriptEngine()->evalScript(core::stringc(script.c_str()).c_str()); + Scripting::ScriptEngine::getInstance() + ->evalScript(core::stringc(script.c_str()).c_str()); } // ----------------------------------------------------------------------------- diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index 8b866fecd..6218111b8 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -458,9 +458,7 @@ void Track::cleanup() } #endif - Scripting::ScriptEngine* script_engine = - World::getWorld()->getScriptEngine(); - script_engine->cleanupCache(); + Scripting::ScriptEngine::getInstance()->cleanupCache(); m_current_track = NULL; } // cleanup @@ -1449,8 +1447,7 @@ void Track::update(float dt) { if (!m_startup_run) // first time running update = good point to run startup script { - Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); - script_engine->runFunction(false, "void onStart()"); + Scripting::ScriptEngine::getInstance()->runFunction(false, "void onStart()"); m_startup_run = true; } m_track_object_manager->update(dt); @@ -1769,7 +1766,7 @@ void Track::loadTrackModel(bool reverse_track, unsigned int mode_id) model_def_loader.cleanLibraryNodesAfterLoad(); - World::getWorld()->getScriptEngine()->compileLoadedScripts(); + Scripting::ScriptEngine::getInstance()->compileLoadedScripts(); // Init all track objects m_track_object_manager->init(); diff --git a/src/tracks/track_object.cpp b/src/tracks/track_object.cpp index ebf01ff84..2869dedeb 100644 --- a/src/tracks/track_object.cpp +++ b/src/tracks/track_object.cpp @@ -27,7 +27,6 @@ #include "io/xml_node.hpp" #include "input/device_manager.hpp" #include "items/item_manager.hpp" -#include "modes/world.hpp" #include "physics/physical_object.hpp" #include "race/race_manager.hpp" #include "scriptengine/script_engine.hpp" @@ -364,7 +363,8 @@ void TrackObject::onWorldReady() else if (m_visibility_condition.size() > 0) { unsigned char result = -1; - Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); + Scripting::ScriptEngine* script_engine = + Scripting::ScriptEngine::getInstance(); std::ostringstream fn_signature; std::vector arguments; diff --git a/src/tracks/track_object_presentation.cpp b/src/tracks/track_object_presentation.cpp index 2e1630b05..c20ad6179 100644 --- a/src/tracks/track_object_presentation.cpp +++ b/src/tracks/track_object_presentation.cpp @@ -210,13 +210,13 @@ TrackObjectPresentationLibraryNode::TrackObjectPresentationLibraryNode( lib_path = track->getTrackFile("library/" + name); libroot = file_manager->createXMLTree(local_lib_node_path); if (track != NULL) - World::getWorld()->getScriptEngine()->loadScript(local_script_file_path, false); + Scripting::ScriptEngine::getInstance()->loadScript(local_script_file_path, false); } else if (file_manager->fileExists(lib_node_path)) { libroot = file_manager->createXMLTree(lib_node_path); if (track != NULL) - World::getWorld()->getScriptEngine()->loadScript(lib_script_file_path, false); + Scripting::ScriptEngine::getInstance()->loadScript(lib_script_file_path, false); } else { @@ -1098,13 +1098,11 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached() { if (!m_action_active) return; - Scripting::ScriptEngine* script_engine = - World::getWorld()->getScriptEngine(); m_action_active = false; // TODO: allow auto re-activating? int idKart = 0; Camera* camera = Camera::getActiveCamera(); if (camera != NULL && camera->getKart() != NULL) idKart = camera->getKart()->getWorldKartId(); - script_engine->runFunction(true, "void " + m_action + "(int)", - [=](asIScriptContext* ctx) { ctx->SetArgDWord(0, idKart); }); + Scripting::ScriptEngine::getInstance()->runFunction(true, "void " + m_action + "(int)", + [=](asIScriptContext* ctx) { ctx->SetArgDWord(0, idKart); }); } // onTriggerItemApproached