Added namespaces Scripting, Scripting::Physics. Moved collision script code and function,callback binding to Scripting::Physics

This commit is contained in:
sachith500 2014-05-27 11:57:44 +05:30
parent 8c5606be08
commit 6f58864df0
8 changed files with 161 additions and 35 deletions

View File

@ -153,7 +153,7 @@ void World::init()
// Grab the track file
m_track = track_manager->getTrack(race_manager->getTrackName());
m_script_engine = new ScriptEngine();
m_script_engine = new Scripting::ScriptEngine();
if(!m_track)
{
std::ostringstream msg;

View File

@ -41,7 +41,9 @@ class Controller;
class PhysicalObject;
class Physics;
class Track;
class ScriptEngine;
namespace Scripting{
class ScriptEngine;
}
namespace irr
{
namespace scene { class ISceneNode; }
@ -116,7 +118,7 @@ protected:
Track* m_track;
/**Pointer to scripting engine */
ScriptEngine* m_script_engine;
Scripting::ScriptEngine* m_script_engine;
/** Pointer to the race GUI. The race GUI is handled by world. */
RaceGUIBase *m_race_gui;
@ -308,7 +310,7 @@ public:
// ------------------------------------------------------------------------
/** Returns a pointer to the Scripting Engine. */
ScriptEngine *getScriptEngine() const { return m_script_engine; }
Scripting::ScriptEngine *getScriptEngine() const { return m_script_engine; }
//------------------------------------------------------------------------
bool isFogEnabled() const;

View File

@ -170,10 +170,11 @@ void Physics::update(float dt)
p->getContactPointCS(0),
p->getUserPointer(1)->getPointerKart(),
p->getContactPointCS(1) );
ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
int kartid1 = p->getUserPointer(0)->getPointerKart()->getWorldKartId();
int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId();
script_engine->setCollision(kartid1,kartid2);
//script_engine->setCollision(kartid1,kartid2);
Scripting::Physics::setCollision(kartid1,kartid2);
script_engine->runScript("collisions");
continue;
} // if kart-kart collision

View File

@ -16,9 +16,10 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <iostream> // cout
#include <assert.h> // assert()
#include <angelscript.h>
#include "io/file_manager.hpp"
#include <iostream> // cout
#include "karts/kart.hpp"
#include "modes/world.hpp"
#include "script_engine.hpp"
@ -28,12 +29,11 @@
#include "tracks/track_object_manager.hpp"
#include "tracks/track.hpp"
#include "io/file_manager.hpp"
using namespace irr;
using namespace Scripting;
namespace Scripting{
// Function prototypes for binding.
void displayMessage(asIScriptGeneric *gen);
@ -42,9 +42,7 @@ using namespace irr;
void squashKart(asIScriptGeneric *gen);
void enableTrigger(asIScriptGeneric *gen);
void disableTrigger(asIScriptGeneric *gen);
int m_collidingkartid1;
int m_collidingkartid2;
ScriptEngine::ScriptEngine()
{
@ -54,8 +52,6 @@ ScriptEngine::ScriptEngine()
{
std::cout << "Failed to create script engine." << std::endl;
}
m_collidingkartid1 = 0;
m_collidingkartid2 = 0;
// Configure the script engine with all the functions,
// and variables that the script should be able to use.
configureEngine(m_engine);
@ -105,13 +101,9 @@ void squashKart(asIScriptGeneric *gen)
AbstractKart* kart = World::getWorld()->getKart(id);
kart->setSquash(time,0.5); //0.5 * max speed is new max for squashed duration
}
void getCollidingKart1(asIScriptGeneric *gen)
void setCollision(int kartid1,int kartid2)
{
gen->SetReturnDWord(m_collidingkartid1);
}
void getCollidingKart2(asIScriptGeneric *gen)
{
gen->SetReturnDWord(m_collidingkartid2);
Scripting::Physics::setCollision(kartid1,kartid2);
}
std::string getScript(std::string scriptName)
@ -243,11 +235,6 @@ void ScriptEngine::runScript(std::string scriptName)
}
void ScriptEngine::setCollision(int collider1,int collider2)
{
m_collidingkartid1 = collider1;
m_collidingkartid2 = collider2;
}
void ScriptEngine::configureEngine(asIScriptEngine *engine)
@ -263,9 +250,10 @@ void ScriptEngine::configureEngine(asIScriptEngine *engine)
r = engine->RegisterGlobalFunction("void squashKart(int id, float time)", asFUNCTION(squashKart), asCALL_GENERIC); assert(r>=0);
r = engine->RegisterGlobalFunction("void enableTrigger(string &in)", asFUNCTION(enableTrigger), asCALL_GENERIC); assert(r>=0);
r = engine->RegisterGlobalFunction("void disableTrigger(string &in)", asFUNCTION(disableTrigger), asCALL_GENERIC); assert(r>=0);
r = engine->RegisterGlobalFunction("uint getCollidingKart1()", asFUNCTION(getCollidingKart1), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterGlobalFunction("uint getCollidingKart2()", asFUNCTION(getCollidingKart2), asCALL_GENERIC); assert( r >= 0 );
//r = engine->RegisterGlobalFunction("uint getCollidingKart1()", asFUNCTION(getCollidingKart1), asCALL_GENERIC); assert( r >= 0 );
//r = engine->RegisterGlobalFunction("uint getCollidingKart2()", asFUNCTION(getCollidingKart2), asCALL_GENERIC); assert( r >= 0 );
Scripting::Physics::registerScriptFunctions(m_engine);
// It is possible to register the functions, properties, and types in
// configuration groups as well. When compiling the scripts it can then
// be defined which configuration groups should be available for that
@ -318,3 +306,5 @@ int ScriptEngine::compileScript(asIScriptEngine *engine, std::string scriptName)
return 0;
}
}

View File

@ -24,6 +24,13 @@
class TrackObjectPresentation;
namespace Scripting{
namespace Physics{
void registerScriptFunctions(asIScriptEngine *engine);
void setCollision(int collider1,int collider2);
}
class ScriptEngine
{
public:
@ -32,8 +39,6 @@ public:
~ScriptEngine();
void runScript(std::string scriptName);
void setCollision(int kartid1,int kartid2);
private:
asIScriptEngine *m_engine;
@ -43,5 +48,7 @@ private:
int compileScript(asIScriptEngine *engine,std::string scriptName);
}; // class ScriptEngine
}
#endif

View File

@ -0,0 +1,77 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2014 SuperTuxKart Team
//
// This program 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 3
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <assert.h> // assert()
#include <angelscript.h>
#include "io/file_manager.hpp"
#include <iostream> // cout
#include "karts/kart.hpp"
#include "modes/world.hpp"
#include "script_engine.hpp"
#include "scriptstdstring.h"
#include <string.h> // strstr()
#include "states_screens/dialogs/tutorial_message_dialog.hpp"
#include "tracks/track_object_manager.hpp"
#include "tracks/track.hpp"
namespace Scripting{
namespace Physics{
// Function prototypes for binding.
void displayMessage(asIScriptGeneric *gen);
void disableAnimation(asIScriptGeneric *gen);
void enableAnimation(asIScriptGeneric *gen);
void squashKart(asIScriptGeneric *gen);
void enableTrigger(asIScriptGeneric *gen);
void disableTrigger(asIScriptGeneric *gen);
int m_collidingkartid1;
int m_collidingkartid2;
void getCollidingKart1(asIScriptGeneric *gen)
{
gen->SetReturnDWord(m_collidingkartid1);
}
void getCollidingKart2(asIScriptGeneric *gen)
{
gen->SetReturnDWord(m_collidingkartid2);
}
void setCollision(int collider1,int collider2)
{
m_collidingkartid1 = collider1;
m_collidingkartid2 = collider2;
}
asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine)
{
asIScriptFunction *func;
func = engine->GetModule(0)->GetFunctionByDecl("void onCollision()");
return func;
}
void registerScriptFunctions(asIScriptEngine *engine)
{
int r;
r = engine->RegisterGlobalFunction("uint getCollidingKart1()", asFUNCTION(getCollidingKart1), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterGlobalFunction("uint getCollidingKart2()", asFUNCTION(getCollidingKart2), asCALL_GENERIC); assert( r >= 0 );
}
}
}

View File

@ -0,0 +1,49 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2014 SuperTuxKart Team
//
// This program 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 3
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef HEADER_SCRIPT_PHYSICS_HPP
#define HEADER_SCRIPT_PHYSICS_HPP
#include <string>
#include <angelscript.h>
namespace Scripting{
namespace Physics{
private:
int m_collidingkartid1;
int m_collidingkartid2;
public:
void setCollision(int collider1,int collider2);
void registerScriptFunctions(asIScriptEngine *engine);
asIScriptFunction*
registerScriptCallbacks(asIScriptEngine *engine);
void getCollidingKart1(asIScriptGeneric *gen);
void getCollidingKart2(asIScriptGeneric *gen);
}
}
#endif

View File

@ -785,7 +785,7 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached(Item* who)
}
else if (m_action == "tutorial_bananas")
{
ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
Scripting::ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
m_action_active = false;
m_script_engine->runScript(m_action);
}
@ -797,7 +797,7 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached(Item* who)
*/
m_action_active=false;
ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
Scripting::ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
m_script_engine->runScript(m_action);
}
else if (m_action == "haybail-activate")
@ -808,7 +808,7 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached(Item* who)
*/
m_action_active=false;
ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
Scripting::ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
m_script_engine->runScript(m_action);
}
else if (m_action == "tutorial_giftboxes")
@ -895,7 +895,7 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached(Item* who)
else
{
//TODO move all above functions into scripts and remove the ifs
ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
Scripting::ScriptEngine* m_script_engine = World::getWorld()->getScriptEngine();
m_action_active = false;
m_script_engine->runScript(m_action);