#include "windows.h"
#include "animation.h"

int Creature::get_position() const
{
    return m_position;
}

Creature::Creature( char face, Playland& land )
:
m_position( 0 ),
m_face( face ),
m_back( land.m_landscape[ 0 ] ),
m_landscape( land.m_landscape ),
m_playland( land )
{
    m_landscape[ 0 ] = face;
    land.register_creature( this );
}

void Creature::move( int steps )
{
    if ( steps == 0 ) return;
    int one_step = ( steps > 0 ? 1 : -1 );
    while ( steps ) {
        m_landscape[ m_position ] = m_back;
        m_position = m_position + one_step;
        m_back = m_landscape[ m_position ];
        if ( m_back != LANDSCAPE_TRAIL && m_back != LANDSCAPE_WALL ) {
            // encounter with another creature!
            // because this creature is on the move,
            // it should survive!
            m_playland.stage_fight( this );
        }
        m_landscape[ m_position ] = m_face;
        cout << '\r' << m_landscape;
        make_sound();
        steps = steps - one_step;
    }
}

Monster::Monster( Playland& land )
    : Creature( 'M', land )
{
}


Playland::Playland( string const& landscape )
    : m_landscape( landscape )
{
}

void Playland::register_creature( Creature* cret )
{
    m_vect_creatures.push_back( cret );
}

int Playland::stage_fight( Creature* messenger )
{
    int messenger_position = messenger->get_position();
    size_t idx = 0;
    for ( ; idx < m_vect_creatures.size(); ++idx ) {
        Creature* other_creature = m_vect_creatures[ idx ];
        if (
            other_creature != messenger
            &&
            other_creature->get_position() == messenger_position
            )
        {
            // the other party is found!
            return messenger->fight( other_creature );
        }
    }
    return 0;
}

void make_sound()
{
    int frequency = 500;
    int duration = 50;
    Beep( frequency, duration );
    Beep( frequency*2, duration );
}