1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-25 14:15:46 +00:00

Minor changes & simplifications to Lua interface. 5 new functions.

This changes all typical vector interpolation functions that get
value, time, loopType, pingPong, ease as args from the Lua state;
use unified functions for all value type occurances.
This fixes the slightly annoying problem that it was not possible to
pass a boolean true to pingPong or ease, because lua_tonumber() would
always return 0 in that case.

Also changed:
- obj_getWorldPosition() now takes optional x,y-vector

New:
- obj_getWorldPositionAndRotation() is a more efficient shortcut,
	as often both position and rotation are retrieved together,
	and the underlying matrix chain operation is rather expensive.
- entity_getEmitter()
- entity_getNumEmitters()
- setSceneColor2()
- getSceneColor2()
This commit is contained in:
fgenesis 2013-07-20 17:44:27 +02:00
parent 34fbbae71a
commit 7fb9204e97
7 changed files with 141 additions and 49 deletions

View file

@ -416,12 +416,12 @@ void Entity::doFriction(float dt)
} }
} }
void Entity::doFriction(float dt, int len) void Entity::doFriction(float dt, float len)
{ {
Vector v = vel; Vector v = vel;
if (!v.isZero()) if (!v.isZero())
{ {
v.setLength2D(dt * float(len)); v.setLength2D(dt * len);
vel -= v; vel -= v;
} }
} }

View file

@ -356,7 +356,7 @@ public:
void revive(float a); void revive(float a);
void setName(const std::string &name); void setName(const std::string &name);
void doFriction(float dt); void doFriction(float dt);
void doFriction(float dt, int len); void doFriction(float dt, float len);
bool isNormalLayer() const bool isNormalLayer() const
{ {

View file

@ -601,6 +601,50 @@ static bool looksLikeGlobal(const char *s)
return true; return true;
} }
inline float interpolateVec1(lua_State *L, InterpolatedVector& vec, int argOffs)
{
return vec.interpolateTo(
lua_tonumber (L, argOffs ), // value
lua_tonumber (L, argOffs+1), // time
lua_tointeger(L, argOffs+2), // loopType
getBool (L, argOffs+3), // pingPong
getBool (L, argOffs+4)); // ease
}
inline float interpolateVec1z(lua_State *L, InterpolatedVector& vec, int argOffs)
{
return vec.interpolateTo(
Vector(0.0f, 0.0f, lua_tonumber (L, argOffs )), // value, last component
lua_tonumber (L, argOffs+1), // time
lua_tointeger(L, argOffs+2), // loopType
getBool (L, argOffs+3), // pingPong
getBool (L, argOffs+4)); // ease
}
inline float interpolateVec2(lua_State *L, InterpolatedVector& vec, int argOffs)
{
return vec.interpolateTo(
Vector(lua_tonumber (L, argOffs ), // x value
lua_tonumber (L, argOffs+1)), // y value
lua_tonumber (L, argOffs+2), // time
lua_tointeger(L, argOffs+3), // loopType
getBool(L, argOffs+4), // pingPong
getBool(L, argOffs+5)); // ease
}
inline float interpolateVec3(lua_State *L, InterpolatedVector& vec, int argOffs)
{
return vec.interpolateTo(
Vector(lua_tonumber (L, argOffs ), // x value
lua_tonumber (L, argOffs+1), // y value
lua_tonumber (L, argOffs+2)), // z value
lua_tonumber (L, argOffs+3), // time
lua_tointeger(L, argOffs+4), // loopType
getBool(L, argOffs+5), // pingPong
getBool(L, argOffs+6)); // ease
}
//----------------------------------// //----------------------------------//
#define luaFunc(func) static int l_##func(lua_State *L) #define luaFunc(func) static int l_##func(lua_State *L)
@ -781,7 +825,7 @@ luaFunc(loadfile_caseinsensitive)
// ----- RenderObject common functions ----- // ----- RenderObject common functions -----
#define forwardCall(func) return l_##func(L); #define forwardCall(func) l_##func(L)
#define MakeTypeCheckFunc(fname, ty) luaFunc(fname) \ #define MakeTypeCheckFunc(fname, ty) luaFunc(fname) \
{ ScriptObject *r = (ScriptObject*)lua_touserdata(L, 1); luaReturnBool(r ? r->isType(ty) : false); } { ScriptObject *r = (ScriptObject*)lua_touserdata(L, 1); luaReturnBool(r ? r->isType(ty) : false); }
@ -812,8 +856,7 @@ luaFunc(obj_setPosition)
if (r) if (r)
{ {
r->position.stop(); r->position.stop();
r->position.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), interpolateVec2(L, r->position, 2);
lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7));
} }
luaReturnNil(); luaReturnNil();
} }
@ -824,8 +867,7 @@ luaFunc(obj_scale)
if (r) if (r)
{ {
r->scale.stop(); r->scale.stop();
r->scale.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3), 0), interpolateVec2(L, r->scale, 2);
lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7));
} }
luaReturnNil(); luaReturnNil();
} }
@ -845,7 +887,7 @@ luaFunc(obj_alpha)
if (r) if (r)
{ {
r->alpha.stop(); r->alpha.stop();
r->alpha.interpolateTo(lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6)); interpolateVec1(L, r->alpha, 2);
} }
luaReturnNil(); luaReturnNil();
} }
@ -876,8 +918,7 @@ luaFunc(obj_color)
if (r) if (r)
{ {
r->color.stop(); r->color.stop();
r->color.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4)), interpolateVec3(L, r->color, 2);
lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7), lua_tonumber(L, 8));
} }
luaReturnNil(); luaReturnNil();
} }
@ -898,8 +939,7 @@ luaFunc(obj_rotate)
if (r) if (r)
{ {
r->rotation.stop(); r->rotation.stop();
r->rotation.interpolateTo(Vector(0,0,lua_tonumber(L, 2)), interpolateVec1z(L, r->rotation, 2);
lua_tonumber(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5), lua_tointeger(L, 6));
} }
luaReturnNil(); luaReturnNil();
} }
@ -910,8 +950,7 @@ luaFunc(obj_rotateOffset)
if (r) if (r)
{ {
r->rotationOffset.stop(); r->rotationOffset.stop();
r->rotationOffset.interpolateTo(Vector(0,0,lua_tonumber(L, 2)), interpolateVec1z(L, r->rotationOffset, 2);
lua_tonumber(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5), lua_tointeger(L, 6));
} }
luaReturnNil(); luaReturnNil();
} }
@ -934,8 +973,7 @@ luaFunc(obj_offset)
if (r) if (r)
{ {
r->offset.stop(); r->offset.stop();
r->offset.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), interpolateVec2(L, r->offset, 2);
lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7));
} }
luaReturnNil(); luaReturnNil();
} }
@ -946,8 +984,7 @@ luaFunc(obj_internalOffset)
if (r) if (r)
{ {
r->internalOffset.stop(); r->internalOffset.stop();
r->internalOffset.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), interpolateVec2(L, r->internalOffset, 2);
lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7));
} }
luaReturnNil(); luaReturnNil();
} }
@ -1119,8 +1156,7 @@ luaFunc(obj_setInternalVel)
if (r) if (r)
{ {
r->velocity.stop(); r->velocity.stop();
r->velocity.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), interpolateVec2(L, r->velocity, 2);
lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7));
} }
luaReturnNil(); luaReturnNil();
} }
@ -1252,7 +1288,7 @@ luaFunc(obj_getWorldPosition)
float x = 0, y = 0; float x = 0, y = 0;
if (b) if (b)
{ {
Vector v = b->getWorldPosition(); Vector v = b->getWorldCollidePosition(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)));
x = v.x; x = v.x;
y = v.y; y = v.y;
} }
@ -1265,6 +1301,17 @@ luaFunc(obj_getWorldRotation)
luaReturnNum(r ? r->getWorldRotation() : 0.0f); luaReturnNum(r ? r->getWorldRotation() : 0.0f);
} }
luaFunc(obj_getWorldPositionAndRotation)
{
RenderObject *r = robj(L);
if (r)
{
Vector v = r->getWorldPositionAndRotation();
luaReturnVec3(v.x, v.y, v.z);
}
luaReturnVec3(0.0f, 0.0f, 0.0f);
}
luaFunc(obj_moveToFront) luaFunc(obj_moveToFront)
{ {
RenderObject *r = robj(L); RenderObject *r = robj(L);
@ -1565,7 +1612,7 @@ luaFunc(quad_isRepeatTexture)
luaFunc(prefix##_##suffix) \ luaFunc(prefix##_##suffix) \
{ \ { \
typecheckOnly(getter(L)); \ typecheckOnly(getter(L)); \
forwardCall(base##_##suffix); \ return forwardCall(base##_##suffix); \
} }
#define MK_ALIAS(prefix, suffix, alias) // not yet used here. defined to avoid warnings #define MK_ALIAS(prefix, suffix, alias) // not yet used here. defined to avoid warnings
@ -1621,6 +1668,7 @@ luaFunc(quad_isRepeatTexture)
RO_FUNC(getter, prefix, update ) \ RO_FUNC(getter, prefix, update ) \
RO_FUNC(getter, prefix, getWorldPosition) \ RO_FUNC(getter, prefix, getWorldPosition) \
RO_FUNC(getter, prefix, getWorldRotation) \ RO_FUNC(getter, prefix, getWorldRotation) \
RO_FUNC(getter, prefix, getWorldPositionAndRotation)\
RO_FUNC(getter, prefix, moveToFront ) \ RO_FUNC(getter, prefix, moveToFront ) \
RO_FUNC(getter, prefix, moveToBack ) \ RO_FUNC(getter, prefix, moveToBack ) \
RO_FUNC(getter, prefix, setLayer ) \ RO_FUNC(getter, prefix, setLayer ) \
@ -1780,7 +1828,7 @@ luaFunc(setActivePet)
luaFunc(setWaterLevel) luaFunc(setWaterLevel)
{ {
dsq->game->waterLevel.interpolateTo(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tointeger(L, 3), getBool(L, 4), getBool(L, 5)); interpolateVec1(L, dsq->game->waterLevel, 1);
luaReturnNum(dsq->game->waterLevel.x); luaReturnNum(dsq->game->waterLevel.x);
} }
@ -2733,7 +2781,7 @@ luaFunc(entity_getVectorToEntity)
{ {
typecheckOnly(entity(L)); typecheckOnly(entity(L));
typecheckOnly(entity(L, 2)); typecheckOnly(entity(L, 2));
forwardCall(obj_getVectorToObj); return forwardCall(obj_getVectorToObj);
} }
luaFunc(entity_setDropChance) luaFunc(entity_setDropChance)
@ -3613,8 +3661,8 @@ luaFunc(cam_setPosition)
float y = lua_tonumber(L, 2); float y = lua_tonumber(L, 2);
float time = lua_tonumber(L, 3); float time = lua_tonumber(L, 3);
int loopType = lua_tointeger(L, 4); int loopType = lua_tointeger(L, 4);
int pingPong = lua_tointeger(L, 5); bool pingPong = getBool(L, 5);
int ease = lua_tointeger(L, 6); bool ease = getBool(L, 6);
Vector p = dsq->game->getCameraPositionFor(Vector(x,y)); Vector p = dsq->game->getCameraPositionFor(Vector(x,y));
@ -3674,6 +3722,18 @@ luaFunc(entity_stopEmitter)
luaReturnNil(); luaReturnNil();
} }
luaFunc(entity_getEmitter)
{
ScriptedEntity *se = scriptedEntity(L);
luaReturnPtr(se ? se->getEmitter(lua_tointeger(L, 2)) : NULL);
}
luaFunc(entity_getNumEmitters)
{
ScriptedEntity *se = scriptedEntity(L);
luaReturnInt(se ? se->getNumEmitters() : 0);
}
luaFunc(entity_initStrands) luaFunc(entity_initStrands)
{ {
ScriptedEntity *e = scriptedEntity(L); ScriptedEntity *e = scriptedEntity(L);
@ -3750,7 +3810,7 @@ luaFunc(entity_setAnimLayerTimeMult)
AnimationLayer *l = e->skeletalSprite.getAnimationLayer(layer); AnimationLayer *l = e->skeletalSprite.getAnimationLayer(layer);
if (l) if (l)
{ {
l->timeMultiplier.interpolateTo(t, lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); interpolateVec1(L, l->timeMultiplier, 3);
} }
} }
luaReturnNum(t); luaReturnNum(t);
@ -3925,8 +3985,7 @@ luaFunc(beam_setPosition_override)
Beam *b = beam(L); Beam *b = beam(L);
if (b) if (b)
{ {
b->position.interpolateTo(Vector(lua_tonumber(L, 2), lua_tonumber(L, 3)), forwardCall(obj_setPosition);
lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7));
b->trace(); b->trace();
} }
luaReturnNil(); luaReturnNil();
@ -4021,7 +4080,7 @@ luaFunc(clearControlHint)
luaFunc(setSceneColor) luaFunc(setSceneColor)
{ {
dsq->game->sceneColor3.interpolateTo(Vector(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tonumber(L, 3)), lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6), lua_tonumber(L, 7)); interpolateVec3(L, dsq->game->sceneColor3, 1);
luaReturnNil(); luaReturnNil();
} }
@ -4031,6 +4090,18 @@ luaFunc(getSceneColor)
luaReturnVec3(c.x, c.y, c.z); luaReturnVec3(c.x, c.y, c.z);
} }
luaFunc(setSceneColor2)
{
interpolateVec3(L, dsq->game->sceneColor2, 1);
luaReturnNil();
}
luaFunc(getSceneColor2)
{
const Vector& c = dsq->game->sceneColor2;
luaReturnVec3(c.x, c.y, c.z);
}
luaFunc(setCameraLerpDelay) luaFunc(setCameraLerpDelay)
{ {
dsq->game->cameraLerpDelay = lua_tonumber(L, 1); dsq->game->cameraLerpDelay = lua_tonumber(L, 1);
@ -5366,7 +5437,7 @@ luaFunc(entity_setMaxSpeedLerp)
{ {
Entity *e = entity(L); Entity *e = entity(L);
if (e) if (e)
e->maxSpeedLerp.interpolateTo(lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tonumber(L, 6)); interpolateVec1(L, e->maxSpeedLerp, 2);
luaReturnNil(); luaReturnNil();
} }
@ -5515,7 +5586,7 @@ luaFunc(setGameSpeed)
{ {
dsq->gameSpeed.stop(); dsq->gameSpeed.stop();
dsq->gameSpeed.stopPath(); dsq->gameSpeed.stopPath();
dsq->gameSpeed.interpolateTo(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4), lua_tonumber(L, 5)); interpolateVec1(L, dsq->gameSpeed, 1);
luaReturnNil(); luaReturnNil();
} }
@ -7035,11 +7106,10 @@ luaFunc(entity_partAlpha)
float start = lua_tonumber(L, 3); float start = lua_tonumber(L, 3);
if (start != -1) if (start != -1)
r->alpha = start; r->alpha = start;
r->alpha.interpolateTo(lua_tonumber(L, 4), lua_tonumber(L, 5), lua_tointeger(L, 6), lua_tointeger(L, 7), lua_tointeger(L, 8)); interpolateVec1(L, r->alpha, 4);
} }
} }
luaReturnNil();
luaReturnNum(0);
} }
luaFunc(entity_partBlendType) luaFunc(entity_partBlendType)
@ -7047,7 +7117,7 @@ luaFunc(entity_partBlendType)
ScriptedEntity *e = scriptedEntity(L); ScriptedEntity *e = scriptedEntity(L);
if (e) if (e)
e->partMap[getString(L, 2)]->setBlendType(lua_tointeger(L, 3)); e->partMap[getString(L, 2)]->setBlendType(lua_tointeger(L, 3));
luaReturnInt(0); luaReturnNil();
} }
luaFunc(entity_partRotate) luaFunc(entity_partRotate)
@ -7057,12 +7127,9 @@ luaFunc(entity_partRotate)
{ {
RenderObject *r = e->partMap[getString(L, 2)]; RenderObject *r = e->partMap[getString(L, 2)];
if (r) if (r)
{ interpolateVec1z(L, r->rotation, 3);
r->rotation.interpolateTo(Vector(0,0,lua_tointeger(L, 3)), lua_tonumber(L, 4), lua_tointeger(L, 5), lua_tointeger(L, 6), lua_tointeger(L, 7));
}
} }
luaReturnNil();
luaReturnNum(0);
} }
luaFunc(entity_getStateTime) luaFunc(entity_getStateTime)
@ -7199,11 +7266,11 @@ luaFunc(entity_initPart)
{ {
std::string partName(getString(L, 2)); std::string partName(getString(L, 2));
std::string partTex(getString(L, 3)); std::string partTex(getString(L, 3));
Vector partPosition(lua_tointeger(L, 4), lua_tointeger(L, 5)); Vector partPosition(lua_tonumber(L, 4), lua_tonumber(L, 5));
int renderAfter = lua_tointeger(L, 6); bool renderAfter = getBool(L, 6);
bool partFlipH = lua_tointeger(L, 7); bool partFlipH = getBool(L, 7);
bool partFlipV = lua_tointeger(L,8); bool partFlipV = getBool(L,8);
Vector offsetInterpolateTo(lua_tointeger(L, 9), lua_tointeger(L, 10)); Vector offsetInterpolateTo(lua_tonumber(L, 9), lua_tonumber(L, 10));
float offsetInterpolateTime = lua_tonumber(L, 11); float offsetInterpolateTime = lua_tonumber(L, 11);
@ -7227,7 +7294,7 @@ luaFunc(entity_initPart)
e->registerNewPart(q, partName); e->registerNewPart(q, partName);
} }
luaReturnNum(0); luaReturnNil();
} }
luaFunc(entity_findTarget) luaFunc(entity_findTarget)
@ -7244,7 +7311,7 @@ luaFunc(entity_doFriction)
Entity *e = entity(L); Entity *e = entity(L);
if (e) if (e)
{ {
e->doFriction(lua_tonumber(L, 2), lua_tointeger(L, 3)); e->doFriction(lua_tonumber(L, 2), lua_tonumber(L, 3));
} }
luaReturnNil(); luaReturnNil();
} }
@ -8411,6 +8478,8 @@ static const struct {
luaRegister(entity_initEmitter), luaRegister(entity_initEmitter),
luaRegister(entity_startEmitter), luaRegister(entity_startEmitter),
luaRegister(entity_stopEmitter), luaRegister(entity_stopEmitter),
luaRegister(entity_getEmitter),
luaRegister(entity_getNumEmitters),
luaRegister(entity_initPart), luaRegister(entity_initPart),
luaRegister(entity_initSegments), luaRegister(entity_initSegments),
@ -8859,6 +8928,8 @@ static const struct {
luaRegister(setSceneColor), luaRegister(setSceneColor),
luaRegister(getSceneColor), luaRegister(getSceneColor),
luaRegister(setSceneColor2),
luaRegister(getSceneColor2),
luaRegister(entity_watchEntity), luaRegister(entity_watchEntity),

View file

@ -175,6 +175,16 @@ void ScriptedEntity::stopEmitter(int emit)
} }
} }
ParticleEffect *ScriptedEntity::getEmitter(int emit)
{
return (size_t(emit) < emitters.size()) ? emitters[emit] : NULL;
}
int ScriptedEntity::getNumEmitters() const
{
return emitters.size();
}
void ScriptedEntity::registerNewPart(RenderObject *r, const std::string &name) void ScriptedEntity::registerNewPart(RenderObject *r, const std::string &name)
{ {
partMap[name] = r; partMap[name] = r;

View file

@ -78,6 +78,8 @@ public:
void initEmitter(int emit, const std::string &file); void initEmitter(int emit, const std::string &file);
void startEmitter(int emit); void startEmitter(int emit);
void stopEmitter(int emit); void stopEmitter(int emit);
ParticleEffect *getEmitter(int emit);
int getNumEmitters() const;
void shiftWorlds(WorldType lastWorld, WorldType worldType); void shiftWorlds(WorldType lastWorld, WorldType worldType);
void setAutoSkeletalUpdate(bool v); void setAutoSkeletalUpdate(bool v);

View file

@ -319,6 +319,14 @@ float RenderObject::getWorldRotation()
return rot; return rot;
} }
Vector RenderObject::getWorldPositionAndRotation()
{
Vector up = getWorldCollidePosition(Vector(0,1));
Vector orig = getWorldPosition();
MathFunctions::calculateAngleBetweenVectorsInDegrees(orig, up, orig.z);
return orig;
}
Vector RenderObject::getWorldCollidePosition(const Vector &vec) Vector RenderObject::getWorldCollidePosition(const Vector &vec)
{ {
#ifdef BBGE_BUILD_OPENGL #ifdef BBGE_BUILD_OPENGL

View file

@ -206,6 +206,7 @@ public:
Vector getAbsoluteRotation(); Vector getAbsoluteRotation();
float getWorldRotation(); float getWorldRotation();
Vector getWorldPositionAndRotation(); // more efficient shortcut, returns rotation in vector z component
Vector getNormal(); Vector getNormal();
Vector getForward(); Vector getForward();
void setOverrideCullRadius(float ovr); void setOverrideCullRadius(float ovr);