#include "cell.h"
#include "entity.h"

namespace game
{

void cell::set(entity *ent, bool primary)
{
    _primary = primary;
    _entity = ent;
}

entity *cell::get()
{
    return _entity;
}

void cell::draw()
{
    if(_entity && _primary)
        _entity->draw();
}

void cell::destroy()
{
    if(_entity)
        _entity->onCellDestroyed();
}

void cell::free()
{
    if(!_entity)
        return;

    for(int i = 0; i < 4; i++)
        if(_siblings[i])
            if(_siblings[i]->_entity && _siblings[i]->_entity != _entity)
                _siblings[i]->_entity->beforeNeighbourDestroyed(_entity);

    _entity = 0;
    _primary = 0;
}

bool cell::isFree()
{
    return !_entity;
}

cell *cell::sibling(uint32_t d)
{
    if(d > 3)
        return nullptr;
    return _siblings[d];
}

}
