Made ScriptingEngine a singleton and removed it from World, which reduces

more dependencies on world.
This commit is contained in:
hiker 2016-12-13 09:18:59 +11:00
parent b12453e9ca
commit 4d406490e1
10 changed files with 40 additions and 45 deletions

View File

@ -167,7 +167,7 @@ void World::init()
// Grab the track file // Grab the track file
Track *track = track_manager->getTrack(race_manager->getTrackName()); Track *track = track_manager->getTrack(race_manager->getTrackName());
m_script_engine = new Scripting::ScriptEngine(); Scripting::ScriptEngine::getInstance<Scripting::ScriptEngine>();
if(!track) if(!track)
{ {
std::ostringstream msg; std::ostringstream msg;
@ -177,7 +177,7 @@ void World::init()
} }
std::string script_path = track->getTrackFile("scripting.as"); 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 // Create the physics
Physics::getInstance<Physics>(); Physics::getInstance<Physics>();
@ -493,7 +493,7 @@ World::~World()
// but kill handles this correctly. // but kill handles this correctly.
Physics::kill(); Physics::kill();
delete m_script_engine; Scripting::ScriptEngine::kill();
m_world = NULL; m_world = NULL;
@ -998,7 +998,8 @@ void World::update(float dt)
PROFILER_POP_CPU_MARKER(); PROFILER_POP_CPU_MARKER();
if(race_manager->isRecordingRace()) ReplayRecorder::get()->update(dt); 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()) if (!history->dontDoPhysics())
{ {

View File

@ -122,9 +122,6 @@ protected:
RaceManager::KartType type, RaceManager::KartType type,
PerPlayerDifficulty difficulty); PerPlayerDifficulty difficulty);
/**Pointer to scripting engine */
Scripting::ScriptEngine* m_script_engine;
/** Pointer to the race GUI. The race GUI is handled by world. */ /** Pointer to the race GUI. The race GUI is handled by world. */
RaceGUIBase *m_race_gui; RaceGUIBase *m_race_gui;
@ -286,6 +283,7 @@ public:
AbstractKart* getPlayerKart(unsigned int player) const; AbstractKart* getPlayerKart(unsigned int player) const;
AbstractKart* getLocalPlayerKart(unsigned int n) const; AbstractKart* getLocalPlayerKart(unsigned int n) const;
virtual const btTransform &getStartTransform(int index); virtual const btTransform &getStartTransform(int index);
void moveKartTo(AbstractKart* kart, const btTransform &t);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns a pointer to the race gui. */ /** Returns a pointer to the race gui. */
RaceGUIBase *getRaceGUI() const { return m_race_gui;} RaceGUIBase *getRaceGUI() const { return m_race_gui;}
@ -309,12 +307,6 @@ public:
unsigned int getCurrentNumPlayers() const { return m_num_players - unsigned int getCurrentNumPlayers() const { return m_num_players -
m_eliminated_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 /** The code that draws the timer should call this first to know
* whether the game mode wants a timer drawn. */ * whether the game mode wants a timer drawn. */
virtual bool shouldDrawTimer() const virtual bool shouldDrawTimer() const

View File

@ -169,7 +169,8 @@ void Physics::update(float dt)
p->getContactPointCS(0), p->getContactPointCS(0),
p->getUserPointer(1)->getPointerKart(), p->getUserPointer(1)->getPointerKart(),
p->getContactPointCS(1) ); 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 kartid1 = p->getUserPointer(0)->getPointerKart()->getWorldKartId();
int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId(); int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId();
script_engine->runFunction(false, "void onKartKartCollision(int, int)", script_engine->runFunction(false, "void onKartKartCollision(int, int)",
@ -184,7 +185,7 @@ void Physics::update(float dt)
{ {
// Kart hits physical object // Kart hits physical object
// ------------------------- // -------------------------
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); Scripting::ScriptEngine* script_engine = Scripting::ScriptEngine::getInstance();
AbstractKart *kart = p->getUserPointer(1)->getPointerKart(); AbstractKart *kart = p->getUserPointer(1)->getPointerKart();
int kartId = kart->getWorldKartId(); int kartId = kart->getWorldKartId();
PhysicalObject* obj = p->getUserPointer(0)->getPointerPhysicalObject(); PhysicalObject* obj = p->getUserPointer(0)->getPointerPhysicalObject();
@ -267,7 +268,7 @@ void Physics::update(float dt)
{ {
// Projectile hits physical object // Projectile hits physical object
// ------------------------------- // -------------------------------
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); Scripting::ScriptEngine* script_engine = Scripting::ScriptEngine::getInstance();
Flyable* flyable = p->getUserPointer(0)->getPointerFlyable(); Flyable* flyable = p->getUserPointer(0)->getPointerFlyable();
PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject(); PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject();
std::string obj_id = obj->getID(); std::string obj_id = obj->getID();

View File

@ -154,7 +154,10 @@ private:
Physics(); Physics();
virtual ~Physics(); virtual ~Physics();
// Give the singleton access to the constructor
friend class AbstractSingleton<Physics>; friend class AbstractSingleton<Physics>;
public: public:
void init (const Vec3 &min_world, const Vec3 &max_world); void init (const Vec3 &min_world, const Vec3 &max_world);
void addKart (const AbstractKart *k); void addKart (const AbstractKart *k);

View File

@ -19,14 +19,15 @@
#ifndef HEADER_SCRIPT_ENGINE_HPP #ifndef HEADER_SCRIPT_ENGINE_HPP
#define HEADER_SCRIPT_ENGINE_HPP #define HEADER_SCRIPT_ENGINE_HPP
#include <string>
#include <angelscript.h>
#include <functional>
#include <map>
#include "scriptengine/script_utils.hpp" #include "scriptengine/script_utils.hpp"
#include "utils/no_copy.hpp" #include "utils/no_copy.hpp"
#include "utils/ptr_vector.hpp" #include "utils/ptr_vector.hpp"
#include "utils/singleton.hpp"
#include <angelscript.h>
#include <functional>
#include <map>
#include <string>
class TrackObjectPresentation; class TrackObjectPresentation;
@ -55,13 +56,17 @@ namespace Scripting
~PendingTimeout(); ~PendingTimeout();
}; };
class ScriptEngine class ScriptEngine : public AbstractSingleton<ScriptEngine>
{ {
public:
ScriptEngine(); ScriptEngine();
~ScriptEngine(); ~ScriptEngine();
// Give the singleton access to the constructor.
friend class AbstractSingleton<ScriptEngine>;
public:
void runFunction(bool warn_if_not_found, std::string function_name); void runFunction(bool warn_if_not_found, std::string function_name);
void runFunction(bool warn_if_not_found, std::string function_name, void runFunction(bool warn_if_not_found, std::string function_name,
std::function<void(asIScriptContext*)> callback); std::function<void(asIScriptContext*)> callback);

View File

@ -22,7 +22,6 @@
#include "input/device_manager.hpp" #include "input/device_manager.hpp"
#include "input/input_device.hpp" #include "input/input_device.hpp"
#include "input/input_manager.hpp" #include "input/input_manager.hpp"
#include "modes/world.hpp"
#include "scriptengine/script_engine.hpp" #include "scriptengine/script_engine.hpp"
#include "states_screens/dialogs/tutorial_message_dialog.hpp" #include "states_screens/dialogs/tutorial_message_dialog.hpp"
#include "tracks/track.hpp" #include "tracks/track.hpp"
@ -109,8 +108,7 @@ namespace Scripting
/** Runs the script function specified by the given string */ /** Runs the script function specified by the given string */
void runScript(const std::string* str) void runScript(const std::string* str)
{ {
ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); ScriptEngine::getInstance()->runFunction(true, *str);
script_engine->runFunction(true, *str);
} }
/** Generate a random integer value */ /** Generate a random integer value */
@ -129,13 +127,13 @@ namespace Scripting
/** Call a function after the specified delay */ /** Call a function after the specified delay */
void setTimeout(const std::string* callback_name, float 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 */ /** Call a method from the given object after the specified delay */
void setTimeoutDelegate(asIScriptFunction* obj, float delay) void setTimeoutDelegate(asIScriptFunction* obj, float delay)
{ {
World::getWorld()->getScriptEngine()->addPendingTimeout(delay, obj); ScriptEngine::getInstance()->addPendingTimeout(delay, obj);
} }
/** Log to the console */ /** Log to the console */

View File

@ -21,7 +21,6 @@
#include "guiengine/widgets/button_widget.hpp" #include "guiengine/widgets/button_widget.hpp"
#include "guiengine/widgets/label_widget.hpp" #include "guiengine/widgets/label_widget.hpp"
#include "guiengine/widgets/text_box_widget.hpp" #include "guiengine/widgets/text_box_widget.hpp"
#include "modes/world.hpp"
#include "scriptengine/script_engine.hpp" #include "scriptengine/script_engine.hpp"
#include "states_screens/state_manager.hpp" #include "states_screens/state_manager.hpp"
#include "utils/translation.hpp" #include "utils/translation.hpp"
@ -80,7 +79,8 @@ void ScriptingConsole::runScript()
core::stringw script = textCtrl->getText(); core::stringw script = textCtrl->getText();
textCtrl->setText(L""); 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());
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -458,9 +458,7 @@ void Track::cleanup()
} }
#endif #endif
Scripting::ScriptEngine* script_engine = Scripting::ScriptEngine::getInstance()->cleanupCache();
World::getWorld()->getScriptEngine();
script_engine->cleanupCache();
m_current_track = NULL; m_current_track = NULL;
} // cleanup } // cleanup
@ -1449,8 +1447,7 @@ void Track::update(float dt)
{ {
if (!m_startup_run) // first time running update = good point to run startup script if (!m_startup_run) // first time running update = good point to run startup script
{ {
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); Scripting::ScriptEngine::getInstance()->runFunction(false, "void onStart()");
script_engine->runFunction(false, "void onStart()");
m_startup_run = true; m_startup_run = true;
} }
m_track_object_manager->update(dt); m_track_object_manager->update(dt);
@ -1769,7 +1766,7 @@ void Track::loadTrackModel(bool reverse_track, unsigned int mode_id)
model_def_loader.cleanLibraryNodesAfterLoad(); model_def_loader.cleanLibraryNodesAfterLoad();
World::getWorld()->getScriptEngine()->compileLoadedScripts(); Scripting::ScriptEngine::getInstance()->compileLoadedScripts();
// Init all track objects // Init all track objects
m_track_object_manager->init(); m_track_object_manager->init();

View File

@ -27,7 +27,6 @@
#include "io/xml_node.hpp" #include "io/xml_node.hpp"
#include "input/device_manager.hpp" #include "input/device_manager.hpp"
#include "items/item_manager.hpp" #include "items/item_manager.hpp"
#include "modes/world.hpp"
#include "physics/physical_object.hpp" #include "physics/physical_object.hpp"
#include "race/race_manager.hpp" #include "race/race_manager.hpp"
#include "scriptengine/script_engine.hpp" #include "scriptengine/script_engine.hpp"
@ -364,7 +363,8 @@ void TrackObject::onWorldReady()
else if (m_visibility_condition.size() > 0) else if (m_visibility_condition.size() > 0)
{ {
unsigned char result = -1; unsigned char result = -1;
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); Scripting::ScriptEngine* script_engine =
Scripting::ScriptEngine::getInstance();
std::ostringstream fn_signature; std::ostringstream fn_signature;
std::vector<std::string> arguments; std::vector<std::string> arguments;

View File

@ -210,13 +210,13 @@ TrackObjectPresentationLibraryNode::TrackObjectPresentationLibraryNode(
lib_path = track->getTrackFile("library/" + name); lib_path = track->getTrackFile("library/" + name);
libroot = file_manager->createXMLTree(local_lib_node_path); libroot = file_manager->createXMLTree(local_lib_node_path);
if (track != NULL) 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)) else if (file_manager->fileExists(lib_node_path))
{ {
libroot = file_manager->createXMLTree(lib_node_path); libroot = file_manager->createXMLTree(lib_node_path);
if (track != NULL) if (track != NULL)
World::getWorld()->getScriptEngine()->loadScript(lib_script_file_path, false); Scripting::ScriptEngine::getInstance()->loadScript(lib_script_file_path, false);
} }
else else
{ {
@ -1098,13 +1098,11 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached()
{ {
if (!m_action_active) return; if (!m_action_active) return;
Scripting::ScriptEngine* script_engine =
World::getWorld()->getScriptEngine();
m_action_active = false; // TODO: allow auto re-activating? m_action_active = false; // TODO: allow auto re-activating?
int idKart = 0; int idKart = 0;
Camera* camera = Camera::getActiveCamera(); Camera* camera = Camera::getActiveCamera();
if (camera != NULL && camera->getKart() != NULL) if (camera != NULL && camera->getKart() != NULL)
idKart = camera->getKart()->getWorldKartId(); idKart = camera->getKart()->getWorldKartId();
script_engine->runFunction(true, "void " + m_action + "(int)", Scripting::ScriptEngine::getInstance()->runFunction(true, "void " + m_action + "(int)",
[=](asIScriptContext* ctx) { ctx->SetArgDWord(0, idKart); }); [=](asIScriptContext* ctx) { ctx->SetArgDWord(0, idKart); });
} // onTriggerItemApproached } // onTriggerItemApproached