2016-06-10 15:28:40 +02:00
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* _____ _ __ ___ __ */
|
|
|
|
|
/* Level.cpp | ___(_)/ _| ___ ( _ ) / /_ */
|
|
|
|
|
/* Project: C-Engine | |_ | | |_ / _ \/ _ \| '_ \ */
|
|
|
|
|
/* | _| | | _| __/ (_) | (_) | */
|
|
|
|
|
/* Author: Fife <wasabifife@gmail.com> |_| |_|_| \___|\___/ \___/ */
|
|
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2016-06-11 15:31:10 +02:00
|
|
|
|
#include "..\C-Engine\include\Level.hpp"
|
2016-06-10 15:28:40 +02:00
|
|
|
|
|
|
|
|
|
Level::Level()
|
|
|
|
|
{
|
|
|
|
|
engineLink = NULL;
|
|
|
|
|
listeObject = NULL;
|
|
|
|
|
nbObject = 0;
|
|
|
|
|
|
|
|
|
|
tileset = NULL;
|
|
|
|
|
map = NULL;
|
|
|
|
|
solid = NULL;
|
|
|
|
|
|
|
|
|
|
tileHeight = 0;
|
|
|
|
|
tileWidth = 0;
|
|
|
|
|
tilegap = 0;
|
|
|
|
|
|
|
|
|
|
levelHeight = 0;
|
|
|
|
|
levelWidth = 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Level::AffectEngine( Engine * v )
|
|
|
|
|
{
|
|
|
|
|
engineLink = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Engine * Level::GetEngine()
|
|
|
|
|
{
|
|
|
|
|
return engineLink;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Level::SetMap( const unsigned char * userTileset , unsigned char * userMap , bool * userProperties, int userTileWidth , int userTileHeight , int userWorldWidth , int userWorldHeight)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
tileset = userTileset;
|
|
|
|
|
map = userMap;
|
|
|
|
|
solid = userProperties;
|
|
|
|
|
|
|
|
|
|
tileWidth = userTileWidth;
|
|
|
|
|
tileHeight = userTileHeight;
|
|
|
|
|
|
|
|
|
|
tilegap = userTileHeight / 8;
|
|
|
|
|
if(userTileHeight % 8)tilegap++;
|
|
|
|
|
|
|
|
|
|
levelWidth = userWorldWidth;
|
|
|
|
|
levelHeight = userWorldHeight;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 19:22:20 +02:00
|
|
|
|
int Level::GetIdMap( int x , int y , bool relative)
|
2016-06-10 15:28:40 +02:00
|
|
|
|
{
|
2016-06-15 19:22:20 +02:00
|
|
|
|
int id;
|
|
|
|
|
int dx = x / tileWidth;
|
|
|
|
|
int dy = levelHeight - y / tileHeight - 1;
|
|
|
|
|
|
|
|
|
|
if(relative)
|
|
|
|
|
id = *(map + levelWidth * dy + dx);
|
|
|
|
|
else
|
|
|
|
|
id = *(map + levelWidth * y + x);
|
2016-06-14 15:39:24 +02:00
|
|
|
|
|
|
|
|
|
#ifdef CE_DELTA1
|
|
|
|
|
|
2016-06-15 19:22:20 +02:00
|
|
|
|
id --;
|
2016-06-14 15:39:24 +02:00
|
|
|
|
|
|
|
|
|
#endif // CE_DELTA1
|
|
|
|
|
|
|
|
|
|
return id;
|
2016-06-10 15:28:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Level::ReplaceIdMap( int x , int y , char v )
|
|
|
|
|
{
|
|
|
|
|
*(map + levelWidth * y + x) = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Level::DrawMap()
|
|
|
|
|
{
|
|
|
|
|
int dy = levelHeight * tileHeight - engineLink->GetScreen()->max.y ;
|
|
|
|
|
|
|
|
|
|
int minx = engineLink->GetScreen()->min.x / tileWidth ;
|
|
|
|
|
int miny = dy / tileHeight ;
|
|
|
|
|
|
|
|
|
|
int maxx = 128 / tileWidth + minx + 2;
|
|
|
|
|
int maxy = 64 / tileHeight + miny + 2;
|
|
|
|
|
|
|
|
|
|
int xscroll = engineLink->GetScreen()->min.x ;
|
|
|
|
|
int yscroll = dy % tileHeight;
|
|
|
|
|
|
|
|
|
|
for( int i = minx; i < maxx; i++)
|
|
|
|
|
{
|
|
|
|
|
for( int j = miny ; j < maxy; j++)
|
|
|
|
|
{
|
|
|
|
|
if (!(i < 0 || i >= levelWidth || j < 0 || j >= levelHeight))
|
2016-06-14 15:39:24 +02:00
|
|
|
|
ML_bmp_or_cl( tileset + tileHeight * tilegap * GetIdMap(i,j) , (i * tileWidth ) - xscroll , ((j - miny) * tileHeight) - yscroll, tileWidth , tileHeight);
|
2016-06-10 15:28:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 19:22:20 +02:00
|
|
|
|
void Level::DrawLevel()
|
|
|
|
|
{
|
|
|
|
|
DrawMap();
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < GetNbObject() ; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( GetListeObject()[i]->GetEnable())
|
|
|
|
|
if(GetListeObject()[i]->GetRender())
|
|
|
|
|
GetListeObject()[i]->GetRender()->DrawObject();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-10 15:28:40 +02:00
|
|
|
|
Object ** Level::GetListeObject()
|
|
|
|
|
{
|
|
|
|
|
return listeObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Level::GetNbObject()
|
|
|
|
|
{
|
|
|
|
|
return nbObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Level::AddObject( Object * v)
|
|
|
|
|
{
|
|
|
|
|
Object ** Tab = listeObject; //On cr<63>ai un pointeur sur la liste actuelle d'objet
|
|
|
|
|
|
|
|
|
|
listeObject = (Object**) malloc( sizeof(Object *) * ( nbObject + 1 ) );//On alloue l'espace n<>c<EFBFBD>ssaire pour le nouveau tableau
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < nbObject ; i++ )//On copie les pointeurs
|
|
|
|
|
listeObject[i] = Tab[i];
|
|
|
|
|
|
|
|
|
|
listeObject[ nbObject ] = v; //Et on rajoute l'objet
|
|
|
|
|
v->AffectEngine(GetEngine());
|
|
|
|
|
|
|
|
|
|
nbObject ++;
|
|
|
|
|
|
|
|
|
|
delete Tab;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Level::DelObject( Object * v , bool destroy)
|
|
|
|
|
{
|
|
|
|
|
Object ** Tab = listeObject;
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
|
|
if(!Tab)return false;
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < nbObject ; i++ )
|
|
|
|
|
if(Tab[i] == v )
|
|
|
|
|
index = i ;
|
|
|
|
|
|
|
|
|
|
if(index == -1)return false;
|
|
|
|
|
|
|
|
|
|
listeObject = (Object**) malloc( sizeof(Object *) * ( nbObject - 1) );
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < index ; i++ )
|
|
|
|
|
listeObject[ i ] = Tab[ i ];
|
|
|
|
|
|
|
|
|
|
for(int i = index + 1 ; i < nbObject ; i++ )
|
|
|
|
|
listeObject[ i - 1] = Tab[ i ];
|
|
|
|
|
|
|
|
|
|
nbObject --;
|
|
|
|
|
|
|
|
|
|
if(destroy)delete v;
|
|
|
|
|
|
|
|
|
|
delete Tab;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Level::DelAllObject()
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0 ; i < nbObject ; i++)
|
|
|
|
|
delete listeObject[i];
|
|
|
|
|
|
|
|
|
|
delete listeObject;
|
|
|
|
|
listeObject = NULL;
|
|
|
|
|
}
|