1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-07-17 21:35:21 +00:00

Small refactor, add Scrtipable base, add node_v() Lua function

This commit is contained in:
fgenesis 2021-01-12 19:00:05 +01:00
parent 9c180ca5b7
commit 074e92c553
11 changed files with 73 additions and 34 deletions

View file

@ -40,7 +40,6 @@ Path::Path()
pathType = PATH_NONE; pathType = PATH_NONE;
neverSpawned = true; neverSpawned = true;
spawnedEntity = 0; spawnedEntity = 0;
script = 0;
updateFunction = activateFunction = false; updateFunction = activateFunction = false;
cursorActivation = false; cursorActivation = false;
rect.setWidth(64); rect.setWidth(64);
@ -215,11 +214,7 @@ void Path::destroy()
emitter->safeKill(); emitter->safeKill();
emitter = 0; emitter = 0;
} }
if (script) closeScript();
{
dsq->scriptInterface.closeScript(script);
script = 0;
}
} }
Path::~Path() Path::~Path()
@ -724,11 +719,6 @@ void Path::luaDebugMsg(const std::string &func, const std::string &msg)
debugLog("luaScriptError: Path [" + name + "]: " + func + " : " + msg); debugLog("luaScriptError: Path [" + name + "]: " + func + " : " + msg);
} }
int Path::pushLuaVars(lua_State *L)
{
return script ? script->pushLocalVars(L) : 0;
}
MinimapIcon *Path::ensureMinimapIcon() MinimapIcon *Path::ensureMinimapIcon()
{ {
if(!minimapIcon) if(!minimapIcon)

View file

@ -24,12 +24,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../BBGE/Base.h" #include "../BBGE/Base.h"
#include "../BBGE/Particles.h" #include "../BBGE/Particles.h"
#include "../BBGE/ScriptObject.h" #include "../BBGE/ScriptObject.h"
#include "ScriptInterface.h"
#include "Rect.h" #include "Rect.h"
#include "Scriptable.h"
#undef PATH_MAX // May be set by a system header. #undef PATH_MAX // May be set by a system header.
struct MinimapIcon; struct MinimapIcon;
class Entity;
class PathNode class PathNode
{ {
@ -72,7 +73,7 @@ enum PathShape
PATHSHAPE_CIRCLE = 1 PATHSHAPE_CIRCLE = 1
}; };
class Path : public ScriptObject class Path : public ScriptObject, public Scriptable
{ {
public: public:
Path(); Path();
@ -112,7 +113,6 @@ public:
void refreshScript(); void refreshScript();
MinimapIcon *ensureMinimapIcon(); MinimapIcon *ensureMinimapIcon();
Script *script;
bool updateFunction; bool updateFunction;
bool activateFunction; bool activateFunction;
bool cursorActivation; bool cursorActivation;
@ -157,7 +157,6 @@ public:
int messageVariadic(lua_State *L, int nparams); int messageVariadic(lua_State *L, int nparams);
void luaDebugMsg(const std::string &func, const std::string &msg); void luaDebugMsg(const std::string &func, const std::string &msg);
int pushLuaVars(lua_State *L);
}; };
#endif #endif

View file

@ -3057,6 +3057,12 @@ luaFunc(node_v)
return n ? n->pushLuaVars(L) : 0; return n ? n->pushLuaVars(L) : 0;
} }
luaFunc(shot_v)
{
Shot *s = getShot(L);
return s ? s->pushLuaVars(L) : 0;
}
luaFunc(isQuitFlag) luaFunc(isQuitFlag)
{ {
luaReturnBool(dsq->isQuitFlag()); luaReturnBool(dsq->isQuitFlag());
@ -9938,6 +9944,7 @@ static const struct {
luaRegister(entity_v), luaRegister(entity_v),
luaRegister(node_v), luaRegister(node_v),
luaRegister(shot_v),
luaRegister(isQuitFlag), luaRegister(isQuitFlag),
luaRegister(isDeveloperKeys), luaRegister(isDeveloperKeys),

21
Aquaria/Scriptable.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "Scriptable.h"
#include "ScriptInterface.h"
#include "DSQ.h"
Scriptable::Scriptable() : script(0)
{
}
int Scriptable::pushLuaVars(lua_State *L)
{
return script ? script->pushLocalVars(L) : 0;
}
void Scriptable::closeScript()
{
if (script)
{
dsq->scriptInterface.closeScript(script);
script = 0;
}
}

26
Aquaria/Scriptable.h Normal file
View file

@ -0,0 +1,26 @@
#ifndef AQUARIA_SCRIPTABLE_H
#define AQUARIA_SCRIPTABLE_H
class Script;
struct lua_State;
// Object that has a script attached
class Scriptable
{
public:
Scriptable();
Script *script; // NULL if no script is attached
int pushLuaVars(lua_State *L);
void closeScript();
// Note! Before you attempt to move here some common functions like message() or anything that takes a 'this'-pointer:
// ScriptInterface uses raw pointers everywhere. 'this' is always passed as a void*, so if we make any such method that pushes 'this'
// a method of the Scriptable class, then that would pass an offset pointer.
// Eg. Entity *e casted to ((void*)e) is not the same as ((void*)(Scriptable*)e)! And since ScriptInterface does (Entity*)lua_touserdata(L, slot),
// this would break horribly since the necessary type infos to fix the pointer are not preserved.
// A fix would be to dynamic_cast from a common base class, but right now that isn't worth the hassle.
};
#endif

View file

@ -31,7 +31,6 @@ ScriptedEntity::ScriptedEntity(const std::string &scriptName, Vector position, E
{ {
addType(SCO_SCRIPTED_ENTITY); addType(SCO_SCRIPTED_ENTITY);
crushDelay = 0; crushDelay = 0;
script = 0;
songNoteFunction = songNoteDoneFunction = true; songNoteFunction = songNoteDoneFunction = true;
addChild(&pullEmitter, PM_STATIC); addChild(&pullEmitter, PM_STATIC);
@ -93,11 +92,6 @@ int ScriptedEntity::messageVariadic(lua_State *L, int nparams)
return Entity::messageVariadic(L, nparams); return Entity::messageVariadic(L, nparams);
} }
int ScriptedEntity::pushLuaVars(lua_State *L)
{
return script ? script->pushLocalVars(L) : 0;
}
void ScriptedEntity::warpSegments() void ScriptedEntity::warpSegments()
{ {
Segmented::warpSegments(position); Segmented::warpSegments(position);
@ -394,11 +388,7 @@ void ScriptedEntity::destroy()
{ {
CollideEntity::destroy(); CollideEntity::destroy();
if (script) closeScript();
{
dsq->scriptInterface.closeScript(script);
script = 0;
}
} }
void ScriptedEntity::song(SongType songType) void ScriptedEntity::song(SongType songType)

View file

@ -24,11 +24,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "CollideEntity.h" #include "CollideEntity.h"
#include "Segmented.h" #include "Segmented.h"
#include "Particles.h" #include "Particles.h"
#include "Scriptable.h"
struct lua_State; struct lua_State;
class Script; class Script;
class ScriptedEntity : public CollideEntity, public Segmented class ScriptedEntity : public CollideEntity, public Segmented, public Scriptable
{ {
public: public:
ScriptedEntity(const std::string &scriptName, Vector position, EntityType et = ET_ENEMY); ScriptedEntity(const std::string &scriptName, Vector position, EntityType et = ET_ENEMY);
@ -50,7 +51,6 @@ public:
void entityDied(Entity *e); void entityDied(Entity *e);
void message(const std::string &msg, int v); void message(const std::string &msg, int v);
int messageVariadic(lua_State *L, int nparams); int messageVariadic(lua_State *L, int nparams);
int pushLuaVars(lua_State *L);
static bool runningActivation; static bool runningActivation;
@ -105,7 +105,6 @@ protected:
void onHitWall(); void onHitWall();
bool reverseSegments; bool reverseSegments;
Script *script;
void onUpdate(float dt); void onUpdate(float dt);
void onEnterState(int action); void onEnterState(int action);
void onExitState(int action); void onExitState(int action);

View file

@ -298,7 +298,6 @@ Shot::Shot() : Quad(), Segmented(0,0)
enqueuedForDelete = false; enqueuedForDelete = false;
shotIdx = shots.size(); shotIdx = shots.size();
shots.push_back(this); shots.push_back(this);
script = 0;
updateScript = false; updateScript = false;
} }
@ -431,8 +430,7 @@ void Shot::onEndOfLife()
if(script) if(script)
{ {
script->call("dieNormal", this); script->call("dieNormal", this);
dsq->scriptInterface.closeScript(script); closeScript();
script = 0;
} }
destroySegments(0.2f); destroySegments(0.2f);
dead = true; dead = true;

View file

@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "CollideEntity.h" #include "CollideEntity.h"
#include "Segmented.h" #include "Segmented.h"
#include "Scriptable.h"
class ParticleEffect; class ParticleEffect;
class Script; class Script;
@ -73,7 +74,7 @@ struct ShotData
}; };
class Shot : public Quad, public Segmented class Shot : public Quad, public Segmented, public Scriptable
{ {
public: public:
@ -144,7 +145,6 @@ protected:
bool fired; bool fired;
bool enqueuedForDelete; bool enqueuedForDelete;
void onUpdate(float dt); void onUpdate(float dt);
Script *script;
bool updateScript; bool updateScript;
private: private:

View file

@ -453,6 +453,7 @@ SET(AQUARIA_SRCS
${SRCDIR}/RecipeMenuEntry.cpp ${SRCDIR}/RecipeMenuEntry.cpp
${SRCDIR}/SceneEditor.cpp ${SRCDIR}/SceneEditor.cpp
${SRCDIR}/SchoolFish.cpp ${SRCDIR}/SchoolFish.cpp
${SRCDIR}/Scriptable.cpp
${SRCDIR}/ScriptedEntity.cpp ${SRCDIR}/ScriptedEntity.cpp
${SRCDIR}/ScriptInterface.cpp ${SRCDIR}/ScriptInterface.cpp
${SRCDIR}/Segmented.cpp ${SRCDIR}/Segmented.cpp

View file

@ -631,6 +631,14 @@
RelativePath="..\..\Aquaria\SchoolFish.h" RelativePath="..\..\Aquaria\SchoolFish.h"
> >
</File> </File>
<File
RelativePath="..\..\Aquaria\Scriptable.cpp"
>
</File>
<File
RelativePath="..\..\Aquaria\Scriptable.h"
>
</File>
<File <File
RelativePath="..\..\Aquaria\ScriptedEntity.cpp" RelativePath="..\..\Aquaria\ScriptedEntity.cpp"
> >