better scene management

This commit is contained in:
Edouard Dupin 2012-06-03 17:28:34 +02:00
parent 62a8fddb53
commit 117ec06757
6 changed files with 90 additions and 27 deletions

View File

@ -26,6 +26,9 @@
#include <ewol/Game/GameElement.h>
#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;
}

View File

@ -31,6 +31,8 @@
#include <ewol/Widget.h>
#include <ewol/Game/SceneElement.h>
#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

View File

@ -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<ewol::Sprite*> & 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)

View File

@ -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<ewol::Sprite*> & listOfSprite, etk::VectorType<ewol::Sprite*> & 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);

View File

@ -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; iii<listAnimatedElements[group].Size(); iii++) {
if (NULL == listAnimatedElements[group][iii]) {
// find an empty slot ...
@ -196,6 +196,7 @@ uint32_t ewol::SceneElement::AddElementNamed(int32_t group, etk::UString &elemen
// check his name :
if (listCreatorElement[iii]->name == 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; jjj<MAX_GROUP_NUMBER; jjj++) {
for (int32_t iii=0; iii<listAnimatedElements[jjj].Size(); iii++) {
if (NULL != listAnimatedElements[jjj][iii]) {
if (true == listAnimatedElements[jjj][iii]->HaveImpact(group, type, position, size )) {
return true;
if (group != jjj) {
for (int32_t iii=0; iii<listAnimatedElements[jjj].Size(); iii++) {
if (NULL != listAnimatedElements[jjj][iii]) {
if (true == listAnimatedElements[jjj][iii]->HaveImpact(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; jjj<MAX_GROUP_NUMBER; jjj++) {
for (int32_t iii=0; iii<listAnimatedElements[jjj].Size(); iii++) {
if (NULL != listAnimatedElements[jjj][iii]) {
listAnimatedElements[jjj][iii]->Explosion(group, type, position, pxAtenuation, power);
if (true == listAnimatedElements[jjj][iii]->Explosion(group, type, position, pxAtenuation, power) ) {
RmElement(jjj, iii);
}
}
}
}

View File

@ -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