Lua script working correctly ==> might test efficiency

This commit is contained in:
Edouard Dupin 2012-05-31 18:28:32 +02:00
parent 196cfef334
commit f434da041a
6 changed files with 286 additions and 130 deletions

View File

@ -33,6 +33,7 @@
*/ */
ewol::GameElement::GameElement(SceneElement & sceneElement, etk::UString& tmpName) : m_fileNameConfig(tmpName), m_sceneElement(sceneElement) ewol::GameElement::GameElement(SceneElement & sceneElement, etk::UString& tmpName) : m_fileNameConfig(tmpName), m_sceneElement(sceneElement)
{ {
m_uniqueId = sceneElement.GetUniqueId();
m_group = -1; m_group = -1;
m_type = -1; m_type = -1;
m_visible = true; m_visible = true;
@ -85,3 +86,14 @@ etkFloat_t quadDist(coord2D_ts pos1, coord2D_ts pos2)
return tmpVal1*tmpVal1 + tmpVal2*tmpVal2; return tmpVal1*tmpVal1 + tmpVal2*tmpVal2;
} }
void ewol::GameElement::GetElementProperty(gameElementGenericProperty_ts &element)
{
element.type = m_type;
element.position = m_position;
element.speed.x = m_speed.x;
element.speed.y = m_speed.y;
element.size.x = m_size;
element.size.y = m_size;
element.angle = m_angle;
}

View File

@ -39,7 +39,8 @@ namespace ewol {
etk::UString m_fileNameConfig; etk::UString m_fileNameConfig;
protected: protected:
SceneElement & m_sceneElement; //!< all element neede in the scene SceneElement & m_sceneElement; //!< all element neede in the scene
int32_t m_group; uint16_t m_uniqueId;
uint16_t m_group;
int32_t m_type; int32_t m_type;
bool m_visible; bool m_visible;
coord2D_ts m_position; coord2D_ts m_position;
@ -61,6 +62,8 @@ namespace ewol {
*/ */
virtual ~GameElement(void) { }; virtual ~GameElement(void) { };
uint16_t GetUniqueId(void) { return m_uniqueId; };
bool HasName(etk::UString tmpName) { return (tmpName == m_fileNameConfig); }; bool HasName(etk::UString tmpName) { return (tmpName == m_fileNameConfig); };
bool IsVisible(void) { return m_visible; }; bool IsVisible(void) { return m_visible; };
void SetVisible(bool state) { m_visible = state; }; void SetVisible(bool state) { m_visible = state; };
@ -81,8 +84,7 @@ namespace ewol {
int32_t GroupGet(void) { return m_group; }; int32_t GroupGet(void) { return m_group; };
void GroupSet(int32_t state) { m_group = state; }; void GroupSet(int32_t state) { m_group = state; };
virtual bool GetElementProperty(gameElementGenericProperty_ts &element, int32_t id) {return false;}; void GetElementProperty(gameElementGenericProperty_ts &element);
virtual int32_t GetNearestEnemy(coord2D_ts position, etkFloat_t& lastQuadDistance) { return -1;};
/** /**
* @brief Periodicly this fuction will be call tu change property of all the dynamic obbjects * @brief Periodicly this fuction will be call tu change property of all the dynamic obbjects
* @param[in] time Current game time (start at 0) * @param[in] time Current game time (start at 0)
@ -117,6 +119,8 @@ namespace ewol {
* @return the id of the sprite requested or -1 if it does not existed * @return the id of the sprite requested or -1 if it does not existed
*/ */
int32_t LoadSprite(etk::VectorType<ewol::Sprite*> listOfElement[NB_BOUBLE_BUFFER], etk::UString fileName, coord2D_ts maxSize); int32_t LoadSprite(etk::VectorType<ewol::Sprite*> listOfElement[NB_BOUBLE_BUFFER], etk::UString fileName, coord2D_ts maxSize);
virtual void Message(etk::UString control, etk::UString message) { } ;
}; };
}; };

View File

@ -33,7 +33,11 @@
//For every acces : //For every acces :
static ewol::GameElementLua * tmpObj = NULL; static ewol::GameElementLua * tmpObj = NULL;
static etk::VectorType<ewol::Sprite*> * tmpSprite = NULL;
static etk::VectorType<ewol::Sprite*> * tmpEffects = NULL;
static ewol::SceneElement * tmpScene = 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*
@ -51,22 +55,138 @@ int lib_a_f_4(type_t *t)
LUAMOD_API int GetPosX(lua_State *L) LUAMOD_API int lua_GetPos(lua_State *L)
{ {
if (NULL==tmpObj) { if (NULL==tmpObj) {
EWOL_ERROR("NULL obj...");
lua_pushnumber(L, (lua_Number)0 ); lua_pushnumber(L, (lua_Number)0 );
return 1; lua_pushnumber(L, (lua_Number)0 );
return 2;
} }
coord2D_ts tmpPos = tmpObj->PositionGet(); coord2D_ts tmpPos = tmpObj->PositionGet();
lua_pushnumber(L, (lua_Number)tmpPos.x );
lua_pushnumber(L, (lua_Number)((int32_t)tmpPos.x) ); lua_pushnumber(L, (lua_Number)tmpPos.y );
// return number of parameters
return 2;
}
LUAMOD_API int lua_SetPos(lua_State *L)
{
if (NULL==tmpObj) {
EWOL_ERROR("NULL obj...");
return 0;
}
float x = luaL_checkint(L, 1);
float y = luaL_checkint(L, 2);
coord2D_ts tmpPos;
tmpPos.x = x;
tmpPos.y = y;
tmpObj->PositionSet(tmpPos);
// return number of parameters
return 0;
}
LUAMOD_API int lua_GetSpeed(lua_State *L)
{
if (NULL==tmpObj) {
EWOL_ERROR("NULL obj...");
lua_pushnumber(L, (lua_Number)0 );
lua_pushnumber(L, (lua_Number)0 );
return 2;
}
coord2D_ts tmpPos = tmpObj->SpeedGet();
lua_pushnumber(L, (lua_Number)tmpPos.x );
lua_pushnumber(L, (lua_Number)tmpPos.y );
// return number of parameters
return 2;
}
LUAMOD_API int lua_SetSpeed(lua_State *L)
{
if (NULL==tmpObj) {
EWOL_ERROR("NULL obj...");
return 0;
}
float x = luaL_checkint(L, 1);
float y = luaL_checkint(L, 2);
coord2D_ts tmpPos;
tmpPos.x = x;
tmpPos.y = y;
tmpObj->SpeedSet(tmpPos);
// return number of parameters
return 0;
}
LUAMOD_API int lua_SpriteLoad(lua_State *L)
{
//LUA : int SpriteLoad("fileName", maxSize); => -1 in error ...
if (NULL==tmpScene || NULL==tmpObj) {
EWOL_ERROR("NULL obj...");
lua_pushnumber(L, (lua_Number)-1 );
return 1;
}
const char *filename = luaL_checkstring(L, 1);
int32_t maxSize = luaL_checkint(L, 2);
coord2D_ts size;
size.x = maxSize;
size.y = size.x;
int32_t spriteID = tmpObj->LoadSprite( tmpScene->animated, filename, size);
if (spriteID < 0) {
EWOL_ERROR("Error to load the sprite : " << filename);
} else {
EWOL_INFO("Load the sprite : " << filename << " ==> " << spriteID);
}
lua_pushnumber(L, (lua_Number)spriteID );
// return number of parameters
return 1; return 1;
} }
LUAMOD_API int lua_SpriteUnLoad(lua_State *L)
{
/*
//LUA : SpriteUnLoad(int);
if (NULL==tmpScene) {
return 1;
}
float a = luaL_checkint(L, 1);
coord2D_ts tmpPos = tmpObj->PositionGet();
tmpPos.y = a;
tmpObj->PositionSet(tmpPos);
*/
// return number of parameters
return 0;
}
LUAMOD_API int lua_SpriteDraw(lua_State *L)
{
//LUA : SpriteDraw(int id, posX, posY, angle, size)
if (NULL==tmpSprite) {
return 0;
}
float spriteID = luaL_checknumber(L, 1);
coord2D_ts tmpPos;
tmpPos.x = luaL_checknumber(L, 2);
tmpPos.y = luaL_checknumber(L, 3);
float angle = luaL_checknumber(L, 4);
float size = luaL_checknumber(L, 5);
if (spriteID < 0) {
EWOL_ERROR("Can not Draw the sprite : " << spriteID);
return 0;
}
//EWOL_INFO("Draw the sprite : " << spriteID << " pos=" << tmpPos << " angle=" << angle);
(*tmpSprite)[spriteID]->Element(tmpPos, size, angle);
// return number of parameters
return 0;
}
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));
// return number of parameters
return 1; return 1;
} }
@ -78,13 +198,20 @@ LUAMOD_API int lua_new_t(lua_State *L) { // get Lua to allocate an initialize a
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
t->a = a; t->a = a;
t->b = b; t->b = b;
// return number of parameters
return 1; return 1;
} }
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 }, { "GetPos", lua_GetPos },
{ "SetPos", lua_SetPos },
{ "GetSpeed", lua_GetSpeed },
{ "SetSpeed", lua_SetSpeed },
{ "SpriteLoad", lua_SpriteLoad },
{ "SpriteUnLoad", lua_SpriteUnLoad },
{ "SpriteDraw", lua_SpriteDraw },
{ NULL, NULL } { NULL, NULL }
}; };
@ -110,6 +237,7 @@ ewol::GameElementLua::GameElementLua(ewol::SceneElement & sceneElement, etk::USt
m_luaState(NULL) m_luaState(NULL)
{ {
tmpObj = this; tmpObj = this;
tmpScene = &m_sceneElement;
etk::File fileElement(tmpName, etk::FILE_TYPE_DATA); etk::File fileElement(tmpName, etk::FILE_TYPE_DATA);
etk::UString tmpCompleatName = fileElement.GetCompleateName(); etk::UString tmpCompleatName = fileElement.GetCompleateName();
@ -171,33 +299,8 @@ ewol::GameElementLua::GameElementLua(ewol::SceneElement & sceneElement, etk::USt
return; return;
} }
} }
/*
lua_getglobal(m_luaState, "f");
if(!lua_isfunction(m_luaState,-1))
{
EWOL_WARNING("LUA: Not a function ... ");
lua_pop(m_luaState,1);
return;
}
lua_pushnumber(m_luaState, 21); // push 1st argument
lua_pushnumber(m_luaState, 31); // 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 'f':" << lua_tostring(m_luaState, -1));
return;
}
// retrieve result
if (!lua_isnumber(m_luaState, -1)) {
EWOL_ERROR("LUA: function `f' must return a number");
return;
}
double z = lua_tonumber(m_luaState, -1);
EWOL_ERROR("LUA: Result:" << z);
lua_pop(m_luaState, 1);
*/
tmpObj = NULL; tmpObj = NULL;
tmpScene = NULL;
} }
@ -227,6 +330,7 @@ ewol::GameElementLua::~GameElementLua(void)
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)
{ {
tmpObj = this; tmpObj = this;
tmpScene = &m_sceneElement;
bool retVal = false; bool retVal = false;
if (NULL != m_luaState) { if (NULL != m_luaState) {
// call the init function // call the init function
@ -252,6 +356,7 @@ bool ewol::GameElementLua::Process(int64_t time, int32_t deltaTime, ewol::SceneE
} }
} }
tmpObj = NULL; tmpObj = NULL;
tmpScene = NULL;
return retVal; return retVal;
} }
@ -260,8 +365,10 @@ bool ewol::GameElementLua::Process(int64_t time, int32_t deltaTime, ewol::SceneE
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; tmpObj = this;
tmpSprite = &listOfSprite;
tmpEffects = &listOfEffects;
if (NULL != m_luaState) { if (NULL != m_luaState) {
// call the init function // call the Draw function
lua_getglobal(m_luaState, "Draw"); lua_getglobal(m_luaState, "Draw");
if(!lua_isfunction(m_luaState,-1)) { if(!lua_isfunction(m_luaState,-1)) {
EWOL_WARNING("LUA: Not Find 'Draw' function "); EWOL_WARNING("LUA: Not Find 'Draw' function ");
@ -274,6 +381,8 @@ void ewol::GameElementLua::Draw(etk::VectorType<ewol::Sprite*> & listOfSprite, e
} }
} }
tmpObj = NULL; tmpObj = NULL;
tmpSprite = NULL;
tmpEffects = NULL;
} }
@ -289,11 +398,11 @@ bool ewol::GameElementLua::HaveImpact(int32_t group, int32_t type, coord2D_ts po
EWOL_WARNING("LUA: Not Find 'Process' function "); EWOL_WARNING("LUA: Not Find 'Process' function ");
lua_pop(m_luaState,1); lua_pop(m_luaState,1);
} else { } else {
lua_pushnumber(m_luaState, (int32_t)group); lua_pushnumber(m_luaState, group);
lua_pushnumber(m_luaState, (int32_t)type); lua_pushnumber(m_luaState, type);
lua_pushnumber(m_luaState, (int32_t)position.x); lua_pushnumber(m_luaState, position.x);
lua_pushnumber(m_luaState, (int32_t)position.y); lua_pushnumber(m_luaState, position.y);
lua_pushnumber(m_luaState, (int32_t)size); lua_pushnumber(m_luaState, size);
// do the call (5 arguments, 1 result) // do the call (5 arguments, 1 result)
if (lua_pcall(m_luaState, 5, 1, 0) != 0) { if (lua_pcall(m_luaState, 5, 1, 0) != 0) {
EWOL_ERROR("LUA: error running function 'Process':" << lua_tostring(m_luaState, -1)); EWOL_ERROR("LUA: error running function 'Process':" << lua_tostring(m_luaState, -1));
@ -324,12 +433,12 @@ void ewol::GameElementLua::Explosion(int32_t group, int32_t type, coord2D_ts pos
EWOL_WARNING("LUA: Not Find 'Process' function "); EWOL_WARNING("LUA: Not Find 'Process' function ");
lua_pop(m_luaState,1); lua_pop(m_luaState,1);
} else { } else {
lua_pushnumber(m_luaState, (int32_t)group); lua_pushnumber(m_luaState, group);
lua_pushnumber(m_luaState, (int32_t)type); lua_pushnumber(m_luaState, type);
lua_pushnumber(m_luaState, (int32_t)position.x); lua_pushnumber(m_luaState, position.x);
lua_pushnumber(m_luaState, (int32_t)position.y); lua_pushnumber(m_luaState, position.y);
lua_pushnumber(m_luaState, (int32_t)pxAtenuation); lua_pushnumber(m_luaState, pxAtenuation);
lua_pushnumber(m_luaState, (int32_t)power); lua_pushnumber(m_luaState, power);
// do the call (6 arguments, 0 result) // do the call (6 arguments, 0 result)
if (lua_pcall(m_luaState, 6, 0, 0) != 0) { if (lua_pcall(m_luaState, 6, 0, 0) != 0) {
EWOL_ERROR("LUA: error running function 'Process':" << lua_tostring(m_luaState, -1)); EWOL_ERROR("LUA: error running function 'Process':" << lua_tostring(m_luaState, -1));
@ -339,11 +448,24 @@ void ewol::GameElementLua::Explosion(int32_t group, int32_t type, coord2D_ts pos
tmpObj = NULL; tmpObj = NULL;
} }
void ewol::GameElementLua::Message(etk::UString control, etk::UString message)
bool ewol::GameElementLua::GetElementProperty(ewol::gameElementGenericProperty_ts &element, int32_t id)
{ {
return false; tmpObj = this;
if (NULL != m_luaState) {
// call the init function
lua_getglobal(m_luaState, "Message");
if(!lua_isfunction(m_luaState,-1)) {
lua_pop(m_luaState,1);
} else {
lua_pushstring(m_luaState, control.Utf8Data());
lua_pushstring(m_luaState, message.Utf8Data());
// do the call (2 arguments, 0 result)
if (lua_pcall(m_luaState, 2, 0, 0) != 0) {
EWOL_ERROR("LUA: error running function 'Message':" << lua_tostring(m_luaState, -1));
}
}
}
tmpObj = NULL;
} }

View File

@ -46,8 +46,7 @@ namespace ewol {
virtual void Draw(etk::VectorType<ewol::Sprite*> & listOfSprite, etk::VectorType<ewol::Sprite*> & listOfEffects); virtual void Draw(etk::VectorType<ewol::Sprite*> & listOfSprite, etk::VectorType<ewol::Sprite*> & listOfEffects);
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 Message(etk::UString control, etk::UString message);
virtual bool GetElementProperty(ewol::gameElementGenericProperty_ts &element, int32_t id);
}; };
}; };

View File

@ -27,6 +27,12 @@
#include <ewol/Game/GameElementLua.h> #include <ewol/Game/GameElementLua.h>
#include <ewol/Game/SceneElement.h> #include <ewol/Game/SceneElement.h>
static uint32_t createUniqueId(uint16_t uniqueID, uint16_t position)
{
return ((uint32_t)uniqueID<< 16) + (uint32_t)position;
}
ewol::SceneElement::SceneElement(void) ewol::SceneElement::SceneElement(void)
{ {
m_id = 1; m_id = 1;
@ -38,29 +44,33 @@ ewol::SceneElement::SceneElement(void)
} }
}; };
void ewol::SceneElement::AddElement(int32_t group, ewol::GameElement* newElement) uint32_t ewol::SceneElement::AddElement(int32_t group, ewol::GameElement* newElement)
{ {
elementIdentifier_ts tmpElement;
if (NULL == newElement) { if (NULL == newElement) {
EWOL_ERROR("newElement is empty ==> not added at the system ..."); EWOL_ERROR("newElement is empty ==> not added at the system ...");
return; return 0;
} }
if (group < 0 || group >= MAX_GROUP_NUMBER) { if (group < 0 || group >= MAX_GROUP_NUMBER) {
EWOL_ERROR("group is wrong " << group << "!=[0," << MAX_GROUP_NUMBER << "]==> not added at the system ..."); EWOL_ERROR("group is wrong " << group << "!=[0," << MAX_GROUP_NUMBER << "]==> not added at the system ...");
return; return 0;
} }
for (int32_t iii=0; iii<listAnimatedElements[group].Size(); iii++) { for (int32_t iii=0; iii<listAnimatedElements[group].Size(); iii++) {
if (NULL == listAnimatedElements[group][iii]) { if (NULL == listAnimatedElements[group][iii]) {
// find an empty slot ... // find an empty slot ...
listAnimatedElements[group][iii] = newElement; listAnimatedElements[group][iii] = newElement;
return; return createUniqueId(newElement->GetUniqueId(), iii);
} }
} }
//did not find empty slot : //did not find empty slot :
listAnimatedElements[group].PushBack(newElement); listAnimatedElements[group].PushBack(newElement);
if (listAnimatedElements[group].Size()>0) {
return createUniqueId(newElement->GetUniqueId(), listAnimatedElements[group].Size()-1);
} else {
return 0;
}
} }
void ewol::SceneElement::AddElementNamed(int32_t group, etk::UString &elementName) uint32_t ewol::SceneElement::AddElementNamed(int32_t group, etk::UString &elementName)
{ {
// try to find the file : // try to find the file :
etk::UString tmpName = "elementGame/"; etk::UString tmpName = "elementGame/";
@ -69,57 +79,76 @@ void ewol::SceneElement::AddElementNamed(int32_t group, etk::UString &elementNam
etk::File fileElement(tmpName, etk::FILE_TYPE_DATA); etk::File fileElement(tmpName, etk::FILE_TYPE_DATA);
if (false == fileElement.Exist()) { if (false == fileElement.Exist()) {
EWOL_ERROR("Can not find Game element : " << elementName << " ==> " << tmpName); EWOL_ERROR("Can not find Game element : " << elementName << " ==> " << tmpName);
return; return 0;
} }
EWOL_INFO("We find Game element : " << elementName << " ==> " << tmpName); EWOL_INFO("We find Game element : " << elementName << " ==> " << tmpName);
ewol::GameElementLua * tmpElement = new ewol::GameElementLua(*this, tmpName, group); ewol::GameElementLua * tmpElement = new ewol::GameElementLua(*this, tmpName, group);
if (NULL == tmpElement) { if (NULL == tmpElement) {
EWOL_ERROR("Can not Allocat : " << elementName); EWOL_ERROR("Can not Allocat : " << elementName);
return; return 0;
} }
AddElement(group, tmpElement); return AddElement(group, tmpElement);
}
bool ewol::SceneElement::GetElementProperty(gameElementGenericProperty_ts &element, elementIdentifier_ts& id)
{
if (id.group > numberOfGroup || id.group > MAX_GROUP_NUMBER) {
return false;
}
if (id.listId >= listAnimatedElements[id.group].Size()) {
return false;
}
if (NULL == listAnimatedElements[id.group][id.listId]) {
return false;
}
// copy the properties
element.id = id;
// get the sub properties ...
return listAnimatedElements[id.group][id.listId]->GetElementProperty(element, id.id);
} }
ewol::elementIdentifier_ts ewol::SceneElement::GetNearestEnemy(coord2D_ts position, int32_t groupId) ewol::GameElement* ewol::SceneElement::GetElement(uint32_t idElement)
{ {
elementIdentifier_ts findId; if (0 == idElement) {
findId.id = -1; return NULL;
findId.group = -1; }
findId.listId = -1; uint16_t realUniqueId = (uint16_t)((idElement >> 16 ) & 0x0000FFFF);
uint16_t posInList = (uint16_t)(idElement & 0x0000FFFF);
for (int32_t iii=0; iii<numberOfGroup; iii++) {
if( posInList < listAnimatedElements[iii].Size()
&& NULL != listAnimatedElements[iii][posInList]
&& realUniqueId == listAnimatedElements[iii][posInList]->GetUniqueId()) {
return listAnimatedElements[iii][posInList];
}
}
return NULL;
}
bool ewol::SceneElement::GetElementProperty(gameElementGenericProperty_ts &element, uint32_t idElement)
{
if (0 == idElement) {
return false;
}
uint16_t realUniqueId = (uint16_t)((idElement >> 16 ) & 0x0000FFFF);
uint16_t posInList = (uint16_t)(idElement & 0x0000FFFF);
for (int32_t iii=0; iii<numberOfGroup; iii++) {
if( posInList < listAnimatedElements[iii].Size()
&& NULL != listAnimatedElements[iii][posInList]
&& realUniqueId == listAnimatedElements[iii][posInList]->GetUniqueId()) {
element.id = idElement;
element.group = iii;
listAnimatedElements[iii][posInList]->GetElementProperty(element);
return true;
}
}
return false;
}
uint32_t ewol::SceneElement::GetNearestEnemy(coord2D_ts position, int32_t groupId)
{
uint32_t result = 0;
etkFloat_t lastQuadDistance = 9999999999999999.0; etkFloat_t lastQuadDistance = 9999999999999999.0;
int32_t jjj=0; int32_t jjj=0;
while (groupEnemy[groupId][jjj] != -1) { while (groupEnemy[groupId][jjj] != -1) {
for (int32_t iii=0; iii<listAnimatedElements[groupEnemy[groupId][jjj]].Size(); iii++) { for (int32_t iii=0; iii<listAnimatedElements[groupEnemy[groupId][jjj]].Size(); iii++) {
if (NULL != listAnimatedElements[groupEnemy[groupId][jjj]][iii]) { if (NULL != listAnimatedElements[groupEnemy[groupId][jjj]][iii]) {
int32_t newID = listAnimatedElements[groupEnemy[groupId][jjj]][iii]->GetNearestEnemy(position, lastQuadDistance); coord2D_ts tmpPos = listAnimatedElements[groupEnemy[groupId][jjj]][iii]->PositionGet();
if (-1 != newID) { etkFloat_t distance = quadDist(position, tmpPos);
findId.id = newID; if (distance <= lastQuadDistance) {
findId.group = groupEnemy[groupId][jjj]; lastQuadDistance = distance;
findId.listId = iii; result = createUniqueId(listAnimatedElements[groupEnemy[groupId][jjj]][iii]->GetUniqueId(), iii);
} }
} }
} }
jjj++; jjj++;
} }
return findId; return result;
} }
@ -148,45 +177,40 @@ void ewol::SceneElement::Explosion(int32_t group, int32_t type, coord2D_ts posit
} }
} }
ewol::elementIdentifier_ts ewol::SceneElement::GetElementAtPos(coord2D_ts position, int32_t maxDistanceDetection)
uint32_t ewol::SceneElement::GetElementAtPos(coord2D_ts position, int32_t maxDistanceDetection)
{ {
elementIdentifier_ts findId; uint32_t result = 0;
findId.id = -1;
findId.group = -1;
findId.listId = -1;
etkFloat_t lastQuadDistance = 9999999999999999.0; etkFloat_t lastQuadDistance = 9999999999999999.0;
int32_t jjj=0;
for (int32_t jjj=0; jjj<MAX_GROUP_NUMBER; jjj++) { for (int32_t jjj=0; jjj<MAX_GROUP_NUMBER; jjj++) {
for (int32_t iii=0; iii<listAnimatedElements[jjj].Size(); iii++) { for (int32_t iii=0; iii<listAnimatedElements[jjj].Size(); iii++) {
if (NULL != listAnimatedElements[jjj][iii]) { if (NULL != listAnimatedElements[jjj][iii]) {
int32_t newID = listAnimatedElements[jjj][iii]->GetNearestEnemy(position, lastQuadDistance); coord2D_ts tmpPos = listAnimatedElements[jjj][iii]->PositionGet();
if (-1 != newID) { etkFloat_t distance = quadDist(position, tmpPos);
findId.id = newID; if (distance <= lastQuadDistance) {
findId.group = jjj; lastQuadDistance = distance;
findId.listId = iii; result = createUniqueId(listAnimatedElements[jjj][iii]->GetUniqueId(), iii);
} }
} }
} }
} }
if (lastQuadDistance > maxDistanceDetection*maxDistanceDetection) { if (lastQuadDistance > maxDistanceDetection*maxDistanceDetection) {
findId.id = -1; return 0;
findId.group = -1;
findId.listId = -1;
} }
return findId; return result;
} }
void ewol::SceneElement::SetEventInput(elementIdentifier_ts& id, coord2D_ts position) void ewol::SceneElement::SetEventInput(uint32_t id, coord2D_ts position)
{ {
EWOL_TODO("but when ..."); EWOL_TODO("but when ...");
} }
void ewol::SceneElement::SetEventExternButton(elementIdentifier_ts& id, int32_t btId, int32_t state) void ewol::SceneElement::SetEventExternButton(uint32_t id, int32_t btId, int32_t state)
{ {
EWOL_TODO("but when ..."); EWOL_TODO("but when ...");
} }
void ewol::SceneElement::SetEventExternJoystick(elementIdentifier_ts& id, int32_t joyId, etkFloat_t angle, etkFloat_t distance, int32_t state) void ewol::SceneElement::SetEventExternJoystick(uint32_t id, int32_t joyId, etkFloat_t angle, etkFloat_t distance, int32_t state)
{ {
EWOL_TODO("but when ..."); EWOL_TODO("but when ...");
} }

View File

@ -32,15 +32,9 @@
namespace ewol { namespace ewol {
class GameElement; class GameElement;
// this is to simplify the time to acces at a specific element
typedef struct { typedef struct {
int32_t group; //!< the element group uint32_t id; //!< unique id of the element
int32_t listId; //!< the id in the list of element int32_t group; //!< element group
int32_t id; //!< Unique ID
} elementIdentifier_ts;
typedef struct {
elementIdentifier_ts id; //!< the element definition
int32_t type; //!< element type int32_t type; //!< element type
coord2D_ts position; //!< current position coord2D_ts position; //!< current position
coord2D_ts speed; //!< current speed coord2D_ts speed; //!< current speed
@ -52,7 +46,7 @@ namespace ewol {
class SceneElement { class SceneElement {
private: private:
int32_t m_id; //!< Unique element ID ==> to reference the element unicly int16_t m_id; //!< Unique element ID ==> to reference the element unicly
public: public:
SceneElement(void); SceneElement(void);
~SceneElement(void) { }; ~SceneElement(void) { };
@ -63,17 +57,18 @@ namespace ewol {
etk::VectorType<ewol::Sprite*> animated[NB_BOUBLE_BUFFER]; //!< element that must be display the second etk::VectorType<ewol::Sprite*> animated[NB_BOUBLE_BUFFER]; //!< element that must be display the second
etk::VectorType<ewol::Sprite*> effects[NB_BOUBLE_BUFFER]; //!< element that must be display the third etk::VectorType<ewol::Sprite*> effects[NB_BOUBLE_BUFFER]; //!< element that must be display the third
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; }; int16_t GetUniqueId(void) { int16_t iddd = m_id; m_id++; return iddd; };
void AddElement(int32_t group, ewol::GameElement* newElement); uint32_t AddElement(int32_t group, ewol::GameElement* newElement);
void AddElementNamed(int32_t group, etk::UString &elementName); uint32_t AddElementNamed(int32_t group, etk::UString &elementName);
bool GetElementProperty(gameElementGenericProperty_ts& element, elementIdentifier_ts& id); ewol::GameElement* GetElement(uint32_t idElement);
elementIdentifier_ts GetNearestEnemy(coord2D_ts position, int32_t groupId); bool GetElementProperty(gameElementGenericProperty_ts& element, uint32_t idElement);
uint32_t 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);
void Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power); void Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power);
elementIdentifier_ts GetElementAtPos(coord2D_ts position, int32_t maxDistanceDetection); uint32_t GetElementAtPos(coord2D_ts position, int32_t maxDistanceDetection);
void SetEventInput(elementIdentifier_ts& id, coord2D_ts position); void SetEventInput(uint32_t id, coord2D_ts position);
void SetEventExternButton(elementIdentifier_ts& id, int32_t btId, int32_t state); void SetEventExternButton(uint32_t id, int32_t btId, int32_t state);
void SetEventExternJoystick(elementIdentifier_ts& id, int32_t joyId, etkFloat_t angle, etkFloat_t distance, int32_t state); void SetEventExternJoystick(uint32_t id, int32_t joyId, etkFloat_t angle, etkFloat_t distance, int32_t state);
}; };
}; };