From 117ec067578de660444c19a832e18fe8136aa653 Mon Sep 17 00:00:00 2001 From: Edouard Dupin Date: Sun, 3 Jun 2012 17:28:34 +0200 Subject: [PATCH] better scene management --- Sources/libewol/ewol/Game/GameElement.cpp | 21 +++++++ Sources/libewol/ewol/Game/GameElement.h | 11 ++-- Sources/libewol/ewol/Game/GameElementLua.cpp | 62 +++++++++++++++----- Sources/libewol/ewol/Game/GameElementLua.h | 4 +- Sources/libewol/ewol/Game/SceneElement.cpp | 17 ++++-- Sources/libewol/ewol/widget/Scene.cpp | 2 +- 6 files changed, 90 insertions(+), 27 deletions(-) diff --git a/Sources/libewol/ewol/Game/GameElement.cpp b/Sources/libewol/ewol/Game/GameElement.cpp index 483f7b31..df37e6a3 100644 --- a/Sources/libewol/ewol/Game/GameElement.cpp +++ b/Sources/libewol/ewol/Game/GameElement.cpp @@ -26,6 +26,9 @@ #include +#undef __class__ +#define __class__ "GameElement" + /** * @brief Constructor : here are requested all the needed sprite and effect that can be used in the game * @param --- @@ -46,6 +49,7 @@ ewol::GameElement::GameElement(SceneElement & sceneElement, etk::UString& tmpNam m_gravity = 0.0; m_fileNameConfig = tmpName; m_canBeCibled = false; + m_life = 0; } @@ -99,3 +103,20 @@ void ewol::GameElement::GetElementProperty(gameElementGenericProperty_ts &elemen element.size.y = m_size; element.angle = m_angle; } + + +bool ewol::GameElement::HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size) +{ + // check if it was in the same group + if (group == m_group) { + return false; + } + etkFloat_t quadDistance = quadDist(m_position, position); + etkFloat_t radiusElement = m_size * m_size; + if (radiusElement < quadDistance) { + //distance is greten than expected + return false; + } + return true; +} + diff --git a/Sources/libewol/ewol/Game/GameElement.h b/Sources/libewol/ewol/Game/GameElement.h index 0605a581..45fcef26 100644 --- a/Sources/libewol/ewol/Game/GameElement.h +++ b/Sources/libewol/ewol/Game/GameElement.h @@ -31,6 +31,8 @@ #include #include +#define CYCLIC_CALL_PERIODE_US (10000) + namespace ewol { class GameElement @@ -50,6 +52,7 @@ namespace ewol { etkFloat_t m_angle; etkFloat_t m_gravity; bool m_canBeCibled; + etkFloat_t m_life; public: /** * @brief Constructor : here are requested all the needed sprite and effect that can be used in the game @@ -91,8 +94,8 @@ namespace ewol { int32_t GetType(void) { return m_type; }; // DEPRECATED ... int32_t TypeGet(void) { return m_type; }; - int32_t GroupGet(void) { return m_group; }; - void GroupSet(int32_t state) { m_group = state; }; + uint16_t GroupGet(void) { return m_group; }; + void GroupSet(uint16_t state) { m_group = state; }; void GetElementProperty(gameElementGenericProperty_ts &element); /** @@ -119,8 +122,8 @@ namespace ewol { * @return --- */ virtual void RemoveElement(int32_t idOfElement) { }; - virtual bool HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size) {return false;}; - virtual void Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power) { } ; + virtual bool HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size); + virtual bool Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power) { return false; } ; /** * @brief Requuest the draw of the current element, it will be done on the current Sprite list * @param[in,out] listOfElement Reference on the list of sprite that we need to find if it exist or added a new one diff --git a/Sources/libewol/ewol/Game/GameElementLua.cpp b/Sources/libewol/ewol/Game/GameElementLua.cpp index 4ee2ea92..0f8f9b46 100644 --- a/Sources/libewol/ewol/Game/GameElementLua.cpp +++ b/Sources/libewol/ewol/Game/GameElementLua.cpp @@ -222,6 +222,33 @@ LUAMOD_API int lua_SetPower(lua_State *L) return 1; } + +LUAMOD_API int lua_GetGroup(lua_State *L) +{ + if (NULL==tmpObj) { + EWOL_ERROR("NULL obj..."); + lua_pushnumber(L, 0 ); + return 1; + } + int32_t value = tmpObj->GroupGet(); + lua_pushinteger(L, value ); + // return number of parameters + return 1; +} + +LUAMOD_API int lua_SetGroup(lua_State *L) +{ + if (NULL==tmpObj) { + EWOL_ERROR("NULL obj..."); + lua_pushnumber(L, 0 ); + return 1; + } + int32_t value = luaL_checkint(L, 1); + tmpObj->GroupSet(value); + // return number of parameters + return 1; +} + LUAMOD_API int lua_GetAngle(lua_State *L) { if (NULL==tmpObj) { @@ -581,6 +608,8 @@ static const luaL_Reg functionsTable[] = { { "SetSize", lua_SetSize }, { "GetPower", lua_GetPower }, { "SetPower", lua_SetPower }, + { "GetGroup", lua_GetGroup }, + { "SetGroup", lua_SetGroup }, { "GetCanBeCibled", lua_GetCanBeCibled }, { "SetCanBeCibled", lua_SetCanBeCibled }, // other element section @@ -613,12 +642,13 @@ LUAMOD_API int luaopen_myLib(lua_State *L) { ** Lua abstraction (END) ******************************************************************************* */ +#undef __class__ +#define __class__ "GameElementLua" -ewol::GameElementLua::GameElementLua(ewol::SceneElement & sceneElement, etk::UString& tmpName, int32_t group) : +ewol::GameElementLua::GameElementLua(ewol::SceneElement & sceneElement, etk::UString& tmpName) : ewol::GameElement(sceneElement, tmpName), m_luaState(NULL) { - m_group = group; tmpObj = this; tmpScene = &m_sceneElement; etk::File fileElement(tmpName, etk::FILE_TYPE_DATA); @@ -800,15 +830,7 @@ void ewol::GameElementLua::Draw(etk::VectorType & listOfSprite, e bool ewol::GameElementLua::HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size) { // todo set a flag that permit lua to direct control of this ... - - // check if it was in the same group - if (group == m_group) { - return false; - } - etkFloat_t quadDistance = quadDist(m_position, position); - etkFloat_t radiusElement = m_size * m_size; - if (radiusElement < quadDistance) { - //distance is greten than expected + if (false == ewol::GameElement::HaveImpact(group, type, position, size) ) { return false; } //HaveImpact(group, type, posX, posY, size, quadDistance) @@ -826,6 +848,7 @@ bool ewol::GameElementLua::HaveImpact(int32_t group, int32_t type, coord2D_ts po lua_pushnumber(m_luaState, position.x); lua_pushnumber(m_luaState, position.y); lua_pushnumber(m_luaState, size); + etkFloat_t quadDistance = quadDist(m_position, position); lua_pushnumber(m_luaState, quadDistance); // do the call (6 arguments, 1 result) if (lua_pcall(m_luaState, 6, 1, 0) != 0) { @@ -847,9 +870,10 @@ bool ewol::GameElementLua::HaveImpact(int32_t group, int32_t type, coord2D_ts po } -void ewol::GameElementLua::Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power) +bool ewol::GameElementLua::Explosion(int32_t group, int32_t type, coord2D_ts position, etkFloat_t pxAtenuation, etkFloat_t power) { tmpObj = this; + bool retVal = false; if (NULL != m_luaState) { // call the init function lua_getglobal(m_luaState, "Explosion"); @@ -865,13 +889,23 @@ void ewol::GameElementLua::Explosion(int32_t group, int32_t type, coord2D_ts pos lua_pushnumber(m_luaState, power); etkFloat_t quadDistance = quadDist(m_position, position); lua_pushnumber(m_luaState, quadDistance); - // do the call (7 arguments, 0 result) - if (lua_pcall(m_luaState, 7, 0, 0) != 0) { + // do the call (7 arguments, 1 result) + if (lua_pcall(m_luaState, 7, 1, 0) != 0) { EWOL_ERROR("LUA: error running function 'Explosion':" << 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::Message(etk::UString control, etk::UString message) diff --git a/Sources/libewol/ewol/Game/GameElementLua.h b/Sources/libewol/ewol/Game/GameElementLua.h index b76575c8..b069156e 100644 --- a/Sources/libewol/ewol/Game/GameElementLua.h +++ b/Sources/libewol/ewol/Game/GameElementLua.h @@ -40,7 +40,7 @@ namespace ewol { private: lua_State *m_luaState; // internal Lua state public: - GameElementLua(ewol::SceneElement & sceneElement, etk::UString& tmpName, int32_t group = 0); + GameElementLua(ewol::SceneElement & sceneElement, etk::UString& tmpName); ~GameElementLua(void); virtual void Init(void); @@ -48,7 +48,7 @@ namespace ewol { virtual bool Process(int64_t time, int32_t deltaTime); virtual void Draw(etk::VectorType & listOfSprite, etk::VectorType & listOfEffects); 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 bool 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); }; void RegisterLuaElementInFolder(ewol::SceneElement & sceneElement, etk::UString folder); diff --git a/Sources/libewol/ewol/Game/SceneElement.cpp b/Sources/libewol/ewol/Game/SceneElement.cpp index ff2c1c01..982858bf 100644 --- a/Sources/libewol/ewol/Game/SceneElement.cpp +++ b/Sources/libewol/ewol/Game/SceneElement.cpp @@ -156,8 +156,8 @@ uint32_t ewol::SceneElement::AddElement(int32_t group, ewol::GameElement* newEle return 0; } // for statistic - newElement->GroupSet(group); newElement->Init(); + newElement->GroupSet(group); for (int32_t iii=0; iiiname == elementName) { // create a new element : + allocatedElements ++; newElement = (*listCreatorElement[iii]->loadElement)(*this, elementName, listCreatorElement[iii]->userString); // we find a previous element loaded ==> retreve it return AddElement(group, newElement); @@ -256,10 +257,12 @@ uint32_t ewol::SceneElement::GetNearestEnemy(coord2D_ts position, int32_t groupI bool ewol::SceneElement::HaveImpact(int32_t group, int32_t type, coord2D_ts position, etkFloat_t size) { for (int32_t jjj=0; jjjHaveImpact(group, type, position, size )) { - return true; + if (group != jjj) { + for (int32_t iii=0; iiiHaveImpact(group, type, position, size )) { + return true; + } } } } @@ -272,7 +275,9 @@ void ewol::SceneElement::Explosion(int32_t group, int32_t type, coord2D_ts posit for (int32_t jjj=0; jjjExplosion(group, type, position, pxAtenuation, power); + if (true == listAnimatedElements[jjj][iii]->Explosion(group, type, position, pxAtenuation, power) ) { + RmElement(jjj, iii); + } } } } diff --git a/Sources/libewol/ewol/widget/Scene.cpp b/Sources/libewol/ewol/widget/Scene.cpp index 851143ad..2e43ccc4 100644 --- a/Sources/libewol/ewol/widget/Scene.cpp +++ b/Sources/libewol/ewol/widget/Scene.cpp @@ -148,7 +148,7 @@ void ewol::Scene::OnDraw(void) } -#define CYCLIC_CALL_PERIODE_US (10000) + /** * @brief Periodic call of this widget * @param localTime curent system time