2017-08-08 13:53:47 +02:00
|
|
|
#ifndef _CELLHANDLER_H
|
|
|
|
#define _CELLHANDLER_H
|
2018-10-26 11:38:56 +02:00
|
|
|
#include <cassert>
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
#define TilesCount 32
|
|
|
|
#define TileSize 250.0f
|
|
|
|
#define _minY (-TilesCount*TileSize/2)
|
|
|
|
#define _minX (-TilesCount*TileSize/2)
|
|
|
|
|
|
|
|
#define _maxY (TilesCount*TileSize/2)
|
|
|
|
#define _maxX (TilesCount*TileSize/2)
|
|
|
|
|
|
|
|
#define CellsPerTile 4
|
|
|
|
#define _cellSize (TileSize/CellsPerTile)
|
|
|
|
#define _sizeX (TilesCount*CellsPerTile)
|
|
|
|
#define _sizeY (TilesCount*CellsPerTile)
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
#define GetRelatCoord( Coord, CellCoord ) ((_maxX-Coord)-CellCoord*_cellSize)
|
2017-08-08 13:53:47 +02:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
class Zone;
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
2017-08-08 13:53:47 +02:00
|
|
|
class CellHandler
|
|
|
|
{
|
|
|
|
public:
|
2018-08-29 21:40:59 +02:00
|
|
|
CellHandler();
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
~CellHandler();
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
T* getCellPtr( uint32_t x, uint32_t y );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
T* getCellByCoords( float x, float y );
|
|
|
|
|
|
|
|
T* create( uint32_t x, uint32_t y );
|
|
|
|
|
|
|
|
T* createByCoords( float x, float y );
|
|
|
|
|
|
|
|
void remove( uint32_t x, uint32_t y );
|
|
|
|
|
|
|
|
bool allocated( uint32_t x, uint32_t y )
|
|
|
|
{
|
|
|
|
return m_pCells[ x ][ y ] != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t getPosX( float x );
|
|
|
|
|
|
|
|
static uint32_t getPosY( float y );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
protected:
|
2018-08-29 21:40:59 +02:00
|
|
|
void _init();
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
T*** m_pCells;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
CellHandler< T >::CellHandler()
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
_init();
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
void CellHandler< T >::_init()
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
m_pCells = new T** [_sizeX];
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
assert( m_pCells );
|
|
|
|
for( uint32_t i = 0; i < _sizeX; i++ )
|
|
|
|
{
|
|
|
|
m_pCells[ i ] = nullptr;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
CellHandler< T >::~CellHandler()
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_pCells )
|
|
|
|
{
|
|
|
|
for( uint32_t i = 0; i < _sizeX; i++ )
|
|
|
|
{
|
|
|
|
if( !m_pCells[ i ] )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( uint32_t j = 0; j < _sizeY; j++ )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_pCells[ i ][ j ] )
|
|
|
|
{
|
|
|
|
delete m_pCells[ i ][ j ];
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
delete[] m_pCells[ i ];
|
|
|
|
}
|
|
|
|
delete[] m_pCells;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
T* CellHandler< T >::create( uint32_t x, uint32_t y )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( x >= _sizeX || y >= _sizeY )
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( !m_pCells[ x ] )
|
|
|
|
{
|
|
|
|
m_pCells[ x ] = new T* [_sizeY];
|
|
|
|
memset( m_pCells[ x ], 0, sizeof( T* ) * _sizeY );
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
assert( m_pCells[ x ][ y ] == nullptr );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
T* cls = new T;
|
|
|
|
m_pCells[ x ][ y ] = cls;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return cls;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
T* CellHandler< T >::createByCoords( float x, float y )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return create( getPosX( x ), getPosY( y ) );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
void CellHandler< T >::remove( uint32_t x, uint32_t y )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( x >= _sizeX || y >= _sizeY )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( !m_pCells[ x ] )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
assert( m_pCells[ x ][ y ] != nullptr );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
T* cls = m_pCells[ x ][ y ];
|
|
|
|
m_pCells[ x ][ y ] = nullptr;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
delete cls;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
T* CellHandler< T >::getCellPtr( uint32_t x, uint32_t y )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( !m_pCells[ x ] )
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_pCells[ x ][ y ];
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
T* CellHandler< T >::getCellByCoords( float x, float y )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return getCellPtr( getPosX( x ), getPosY( y ) );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
uint32_t CellHandler< T >::getPosX( float x )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
assert( ( x >= _minX ) && ( x <= _maxX ) );
|
|
|
|
return ( uint32_t ) ( ( _maxX - x ) / _cellSize );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
template< class T >
|
|
|
|
uint32_t CellHandler< T >::getPosY( float y )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
assert( ( y >= _minY ) && ( y <= _maxY ) );
|
|
|
|
return ( uint32_t ) ( ( _maxY - y ) / _cellSize );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|