basic nearly working lua game element
This commit is contained in:
parent
81ad7792e0
commit
196cfef334
@ -31,6 +31,10 @@
|
|||||||
|
|
||||||
#include <lua/lua.hpp>
|
#include <lua/lua.hpp>
|
||||||
|
|
||||||
|
//For every acces :
|
||||||
|
|
||||||
|
static ewol::GameElementLua * tmpObj = NULL;
|
||||||
|
|
||||||
const char *metaname = "mine.type_t"; // associated with userdata of type type_t*
|
const char *metaname = "mine.type_t"; // associated with userdata of type type_t*
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -44,6 +48,22 @@ int lib_a_f_4(type_t *t)
|
|||||||
return t->a * t->b ;
|
return t->a * t->b ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
LUAMOD_API int GetPosX(lua_State *L)
|
||||||
|
{
|
||||||
|
if (NULL==tmpObj) {
|
||||||
|
lua_pushnumber(L, (lua_Number)0 );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
coord2D_ts tmpPos = tmpObj->PositionGet();
|
||||||
|
|
||||||
|
lua_pushnumber(L, (lua_Number)((int32_t)tmpPos.x) );
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
LUAMOD_API int lua_lib_a_f_4(lua_State *L) {
|
LUAMOD_API int lua_lib_a_f_4(lua_State *L) {
|
||||||
type_t *t = (type_t *)luaL_checkudata(L, 1, metaname); // check argument type
|
type_t *t = (type_t *)luaL_checkudata(L, 1, metaname); // check argument type
|
||||||
lua_pushnumber(L, (lua_Number)lib_a_f_4(t));
|
lua_pushnumber(L, (lua_Number)lib_a_f_4(t));
|
||||||
@ -64,6 +84,7 @@ LUAMOD_API int lua_new_t(lua_State *L) { // get Lua to allocate an initialize a
|
|||||||
static const luaL_Reg functionsTable[] = {
|
static const luaL_Reg functionsTable[] = {
|
||||||
{ "lib_a_f_4", lua_lib_a_f_4 },
|
{ "lib_a_f_4", lua_lib_a_f_4 },
|
||||||
{ "new_t", lua_new_t },
|
{ "new_t", lua_new_t },
|
||||||
|
{ "GetPosX", GetPosX },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -71,7 +92,7 @@ static const luaL_Reg functionsTable[] = {
|
|||||||
//http://www.swig.org/Doc1.3/Lua.html#Lua_nn13
|
//http://www.swig.org/Doc1.3/Lua.html#Lua_nn13
|
||||||
//http://stackoverflow.com/questions/2521244/how-to-wrap-a-c-function-whose-parameters-are-pointer-to-structs-so-that-it-can
|
//http://stackoverflow.com/questions/2521244/how-to-wrap-a-c-function-whose-parameters-are-pointer-to-structs-so-that-it-can
|
||||||
LUAMOD_API int luaopen_myLib(lua_State *L) {
|
LUAMOD_API int luaopen_myLib(lua_State *L) {
|
||||||
luaL_register(L, "mylib", functionsTable);
|
luaL_register(L, "GameElement", functionsTable);
|
||||||
luaL_newmetatable(L, metaname);
|
luaL_newmetatable(L, metaname);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
//luaL_newlib(L, functionsTable);
|
//luaL_newlib(L, functionsTable);
|
||||||
@ -88,6 +109,10 @@ ewol::GameElementLua::GameElementLua(ewol::SceneElement & sceneElement, etk::USt
|
|||||||
ewol::GameElement(sceneElement, tmpName),
|
ewol::GameElement(sceneElement, tmpName),
|
||||||
m_luaState(NULL)
|
m_luaState(NULL)
|
||||||
{
|
{
|
||||||
|
tmpObj = this;
|
||||||
|
etk::File fileElement(tmpName, etk::FILE_TYPE_DATA);
|
||||||
|
etk::UString tmpCompleatName = fileElement.GetCompleateName();
|
||||||
|
|
||||||
// create state
|
// create state
|
||||||
m_luaState = luaL_newstate();
|
m_luaState = luaL_newstate();
|
||||||
if (m_luaState == NULL) {
|
if (m_luaState == NULL) {
|
||||||
@ -98,15 +123,59 @@ ewol::GameElementLua::GameElementLua(ewol::SceneElement & sceneElement, etk::USt
|
|||||||
// open internal specific elements ...
|
// open internal specific elements ...
|
||||||
luaopen_myLib(m_luaState);
|
luaopen_myLib(m_luaState);
|
||||||
|
|
||||||
if (luaL_loadfile(m_luaState, "/home/edupin/dev/perso/TethysDefender/last.lua") || lua_pcall(m_luaState, 0, 0, 0)) {
|
int32_t fileSize = fileElement.Size();
|
||||||
|
if (0==fileSize) {
|
||||||
|
EWOL_ERROR("This file is empty : " << fileElement);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (false == fileElement.fOpenRead()) {
|
||||||
|
EWOL_ERROR("Can not open the file : " << fileElement);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// allocate data
|
||||||
|
char * fileBuffer = new char[fileSize+5];
|
||||||
|
if (NULL == fileBuffer) {
|
||||||
|
EWOL_ERROR("Error Memory allocation size=" << fileSize);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memset(fileBuffer, 0, (fileSize+5)*sizeof(char));
|
||||||
|
// load data from the file :
|
||||||
|
fileElement.fRead(fileBuffer, 1, fileSize);
|
||||||
|
// close the file:
|
||||||
|
fileElement.fClose();
|
||||||
|
|
||||||
|
if (luaL_loadbuffer(m_luaState, fileBuffer, fileSize, tmpName.Utf8Data())) {
|
||||||
|
//if (luaL_loadfile(m_luaState, "/home/heero/dev/perso/TethysDefender/assets/elementGame/Cube.lua" ) ) {
|
||||||
EWOL_ERROR("LUA: " << lua_tostring(m_luaState, -1));
|
EWOL_ERROR("LUA: " << lua_tostring(m_luaState, -1));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (NULL != fileBuffer) {
|
||||||
|
delete[] fileBuffer;
|
||||||
|
}
|
||||||
|
// Call the basic for initialise all the element defined by the user...
|
||||||
|
if (lua_pcall(m_luaState, 0, 0, 0)) {
|
||||||
|
EWOL_ERROR("LUA: " << lua_tostring(m_luaState, -1));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// call the init function
|
||||||
|
lua_getglobal(m_luaState, "Init");
|
||||||
|
if(!lua_isfunction(m_luaState,-1)) {
|
||||||
|
EWOL_WARNING("LUA: Not Find Init function ");
|
||||||
|
lua_pop(m_luaState,1);
|
||||||
|
} else {
|
||||||
|
// do the call (0 arguments, 0 result)
|
||||||
|
if (lua_pcall(m_luaState, 0, 0, 0) != 0) {
|
||||||
|
EWOL_ERROR("LUA: error running function 'Init':" << lua_tostring(m_luaState, -1));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
lua_getglobal(m_luaState, "f");
|
lua_getglobal(m_luaState, "f");
|
||||||
if(!lua_isfunction(m_luaState,-1))
|
if(!lua_isfunction(m_luaState,-1))
|
||||||
{
|
{
|
||||||
EWOL_ERROR("LUA: Not a function ... ");
|
EWOL_WARNING("LUA: Not a function ... ");
|
||||||
lua_pop(m_luaState,1);
|
lua_pop(m_luaState,1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -128,48 +197,149 @@ ewol::GameElementLua::GameElementLua(ewol::SceneElement & sceneElement, etk::USt
|
|||||||
EWOL_ERROR("LUA: Result:" << z);
|
EWOL_ERROR("LUA: Result:" << z);
|
||||||
lua_pop(m_luaState, 1);
|
lua_pop(m_luaState, 1);
|
||||||
*/
|
*/
|
||||||
|
tmpObj = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ewol::GameElementLua::~GameElementLua(void)
|
ewol::GameElementLua::~GameElementLua(void)
|
||||||
{
|
{
|
||||||
|
tmpObj = this;
|
||||||
if (NULL != m_luaState) {
|
if (NULL != m_luaState) {
|
||||||
|
// call the init function
|
||||||
|
lua_getglobal(m_luaState, "UnInit");
|
||||||
|
if(!lua_isfunction(m_luaState,-1)) {
|
||||||
|
EWOL_WARNING("LUA: Not Find 'UnInit' function ");
|
||||||
|
lua_pop(m_luaState,1);
|
||||||
|
} else {
|
||||||
|
// do the call (0 arguments, 0 result)
|
||||||
|
if (lua_pcall(m_luaState, 0, 0, 0) != 0) {
|
||||||
|
EWOL_ERROR("LUA: error running function 'UnInit':" << lua_tostring(m_luaState, -1));
|
||||||
|
}
|
||||||
|
}
|
||||||
EWOL_DEBUG("RemoveLua Element");
|
EWOL_DEBUG("RemoveLua Element");
|
||||||
lua_close(m_luaState);
|
lua_close(m_luaState);
|
||||||
m_luaState = NULL;
|
m_luaState = NULL;
|
||||||
}
|
}
|
||||||
|
tmpObj = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool ewol::GameElementLua::Process(int64_t time, int32_t deltaTime, ewol::SceneElement & sceneElement)
|
bool ewol::GameElementLua::Process(int64_t time, int32_t deltaTime, ewol::SceneElement & sceneElement)
|
||||||
{
|
{
|
||||||
return false;
|
tmpObj = this;
|
||||||
|
bool retVal = false;
|
||||||
|
if (NULL != m_luaState) {
|
||||||
|
// call the init function
|
||||||
|
lua_getglobal(m_luaState, "Process");
|
||||||
|
if(!lua_isfunction(m_luaState,-1)) {
|
||||||
|
EWOL_WARNING("LUA: Not Find 'Process' function ");
|
||||||
|
lua_pop(m_luaState,1);
|
||||||
|
} else {
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)time); // push 1st argument
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)deltaTime); // push 2nd argument
|
||||||
|
// do the call (2 arguments, 1 result)
|
||||||
|
if (lua_pcall(m_luaState, 2, 1, 0) != 0) {
|
||||||
|
EWOL_ERROR("LUA: error running function 'Process':" << lua_tostring(m_luaState, -1));
|
||||||
|
} else {
|
||||||
|
// retrieve result
|
||||||
|
if (!lua_isboolean(m_luaState, -1)) {
|
||||||
|
EWOL_ERROR("LUA: function 'Process' must return a boolean");
|
||||||
|
} else {
|
||||||
|
retVal = lua_toboolean(m_luaState, -1);
|
||||||
|
lua_pop(m_luaState, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmpObj = NULL;
|
||||||
|
|
||||||
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ewol::GameElementLua::Draw(etk::VectorType<ewol::Sprite*> & listOfSprite, etk::VectorType<ewol::Sprite*> & listOfEffects)
|
void ewol::GameElementLua::Draw(etk::VectorType<ewol::Sprite*> & listOfSprite, etk::VectorType<ewol::Sprite*> & listOfEffects)
|
||||||
{
|
{
|
||||||
|
tmpObj = this;
|
||||||
|
if (NULL != m_luaState) {
|
||||||
|
// call the init function
|
||||||
|
lua_getglobal(m_luaState, "Draw");
|
||||||
|
if(!lua_isfunction(m_luaState,-1)) {
|
||||||
|
EWOL_WARNING("LUA: Not Find 'Draw' function ");
|
||||||
|
lua_pop(m_luaState,1);
|
||||||
|
} else {
|
||||||
|
// do the call (0 arguments, 0 result)
|
||||||
|
if (lua_pcall(m_luaState, 0, 0, 0) != 0) {
|
||||||
|
EWOL_ERROR("LUA: error running function 'Draw':" << lua_tostring(m_luaState, -1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmpObj = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool ewol::GameElementLua::HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size)
|
bool ewol::GameElementLua::HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size)
|
||||||
{
|
{
|
||||||
return false;
|
//HaveImpact(group, type, posX, posY, size)
|
||||||
|
tmpObj = this;
|
||||||
|
bool retVal = false;
|
||||||
|
if (NULL != m_luaState) {
|
||||||
|
// call the init function
|
||||||
|
lua_getglobal(m_luaState, "Process");
|
||||||
|
if(!lua_isfunction(m_luaState,-1)) {
|
||||||
|
EWOL_WARNING("LUA: Not Find 'Process' function ");
|
||||||
|
lua_pop(m_luaState,1);
|
||||||
|
} else {
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)group);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)type);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)position.x);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)position.y);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)size);
|
||||||
|
// do the call (5 arguments, 1 result)
|
||||||
|
if (lua_pcall(m_luaState, 5, 1, 0) != 0) {
|
||||||
|
EWOL_ERROR("LUA: error running function 'Process':" << lua_tostring(m_luaState, -1));
|
||||||
|
} else {
|
||||||
|
// retrieve result
|
||||||
|
if (!lua_isboolean(m_luaState, -1)) {
|
||||||
|
EWOL_ERROR("LUA: function 'Process' must return a boolean");
|
||||||
|
} else {
|
||||||
|
retVal = lua_toboolean(m_luaState, -1);
|
||||||
|
lua_pop(m_luaState, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmpObj = NULL;
|
||||||
|
|
||||||
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ewol::GameElementLua::Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power)
|
void ewol::GameElementLua::Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power)
|
||||||
{
|
{
|
||||||
|
tmpObj = this;
|
||||||
|
if (NULL != m_luaState) {
|
||||||
|
// call the init function
|
||||||
|
lua_getglobal(m_luaState, "Process");
|
||||||
|
if(!lua_isfunction(m_luaState,-1)) {
|
||||||
|
EWOL_WARNING("LUA: Not Find 'Process' function ");
|
||||||
|
lua_pop(m_luaState,1);
|
||||||
|
} else {
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)group);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)type);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)position.x);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)position.y);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)pxAtenuation);
|
||||||
|
lua_pushnumber(m_luaState, (int32_t)power);
|
||||||
|
// do the call (6 arguments, 0 result)
|
||||||
|
if (lua_pcall(m_luaState, 6, 0, 0) != 0) {
|
||||||
|
EWOL_ERROR("LUA: error running function 'Process':" << lua_tostring(m_luaState, -1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmpObj = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ewol::GameElementLua::Add(int32_t group, coord2D_ts position, coord2D_ts speed, int32_t groupEnemy, int32_t dificulty)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool ewol::GameElementLua::GetElementProperty(ewol::gameElementGenericProperty_ts &element, int32_t id)
|
bool ewol::GameElementLua::GetElementProperty(ewol::gameElementGenericProperty_ts &element, int32_t id)
|
||||||
{
|
{
|
||||||
|
@ -47,7 +47,6 @@ namespace ewol {
|
|||||||
virtual bool HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size);
|
virtual bool HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size);
|
||||||
virtual void Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power);
|
virtual void Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power);
|
||||||
|
|
||||||
virtual void Add(int32_t group, coord2D_ts position, coord2D_ts speed, int32_t groupEnemy, int32_t dificulty);
|
|
||||||
virtual bool GetElementProperty(ewol::gameElementGenericProperty_ts &element, int32_t id);
|
virtual bool GetElementProperty(ewol::gameElementGenericProperty_ts &element, int32_t id);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
#include <ewol/Debug.h>
|
#include <ewol/Debug.h>
|
||||||
#include <ewol/Game/GameElement.h>
|
#include <ewol/Game/GameElement.h>
|
||||||
|
#include <ewol/Game/GameElementLua.h>
|
||||||
#include <ewol/Game/SceneElement.h>
|
#include <ewol/Game/SceneElement.h>
|
||||||
|
|
||||||
ewol::SceneElement::SceneElement(void)
|
ewol::SceneElement::SceneElement(void)
|
||||||
@ -59,6 +60,25 @@ void ewol::SceneElement::AddElement(int32_t group, ewol::GameElement* newElement
|
|||||||
listAnimatedElements[group].PushBack(newElement);
|
listAnimatedElements[group].PushBack(newElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ewol::SceneElement::AddElementNamed(int32_t group, etk::UString &elementName)
|
||||||
|
{
|
||||||
|
// try to find the file :
|
||||||
|
etk::UString tmpName = "elementGame/";
|
||||||
|
tmpName += elementName;
|
||||||
|
tmpName += ".lua";
|
||||||
|
etk::File fileElement(tmpName, etk::FILE_TYPE_DATA);
|
||||||
|
if (false == fileElement.Exist()) {
|
||||||
|
EWOL_ERROR("Can not find Game element : " << elementName << " ==> " << tmpName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
EWOL_INFO("We find Game element : " << elementName << " ==> " << tmpName);
|
||||||
|
ewol::GameElementLua * tmpElement = new ewol::GameElementLua(*this, tmpName, group);
|
||||||
|
if (NULL == tmpElement) {
|
||||||
|
EWOL_ERROR("Can not Allocat : " << elementName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
AddElement(group, tmpElement);
|
||||||
|
}
|
||||||
|
|
||||||
bool ewol::SceneElement::GetElementProperty(gameElementGenericProperty_ts &element, elementIdentifier_ts& id)
|
bool ewol::SceneElement::GetElementProperty(gameElementGenericProperty_ts &element, elementIdentifier_ts& id)
|
||||||
{
|
{
|
||||||
|
@ -65,6 +65,7 @@ namespace ewol {
|
|||||||
etk::VectorType<ewol::GameElement*> listAnimatedElements[MAX_GROUP_NUMBER]; //!< generic element to display order in the diffferent group
|
etk::VectorType<ewol::GameElement*> listAnimatedElements[MAX_GROUP_NUMBER]; //!< generic element to display order in the diffferent group
|
||||||
int32_t GetUniqueId(void) { int32_t iddd = m_id; m_id++; return iddd; };
|
int32_t GetUniqueId(void) { int32_t iddd = m_id; m_id++; return iddd; };
|
||||||
void AddElement(int32_t group, ewol::GameElement* newElement);
|
void AddElement(int32_t group, ewol::GameElement* newElement);
|
||||||
|
void AddElementNamed(int32_t group, etk::UString &elementName);
|
||||||
bool GetElementProperty(gameElementGenericProperty_ts& element, elementIdentifier_ts& id);
|
bool GetElementProperty(gameElementGenericProperty_ts& element, elementIdentifier_ts& id);
|
||||||
elementIdentifier_ts GetNearestEnemy(coord2D_ts position, int32_t groupId);
|
elementIdentifier_ts GetNearestEnemy(coord2D_ts position, int32_t groupId);
|
||||||
bool HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size);
|
bool HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user