Safe zone implementation in Asteroids

Posted by Moaz on Game Development See other posts from Game Development or by Moaz
Published on 2012-11-02T16:18:26Z Indexed on 2012/11/21 17:17 UTC
Read the original article Hit count: 288

Filed under:
|
|

I would like to implement a safe zone for asteroids so that when the ship gets destroyed, it shouldn't be there unless it is safe from other asteroids.

I tried to check the distance between each asteroid and the ship, and if it is above threshold, it sets a flag to the ship that's a safe zone, but sometimes it work and sometimes it doesn't. What am I doing wrong?

Here's my code:

for (list<Asteroid>::iterator itr_astroid = asteroids.begin(); itr_astroid!=asteroids.end(); )
{
    if(currentShip.m_state == Ship::Ship_Dead)
    {
        float distance = itr_astroid->getCenter().distance(Vec2f(getWindowWidth()/2,getWindowHeight()/2));
        if( distance>200)
        {
            currentShip.m_saveField = true;

            break;
        }
        else
        {
            currentShip.m_saveField = false;
            itr_astroid++;
        }
    }
    else
    {
    itr_astroid++;
    }
}

At ship's death:

if(m_state == Ship_Dead && m_saveField==true)
{
    --m_lifeSpan;

}

if(m_lifeSpan<=0 && m_saveField == true)
{
    m_state = Ship_Alive;
    m_Vel   = Vec2f(0,0);
    m_Pos.x = app::getWindowWidth()/2;
    m_Pos.y = app::getWindowHeight()/2;
    m_lifeSpan = 100;

}

© Game Development or respective owner

Related posts about c++

Related posts about opengl