
#include <stdio.h>
#include <stdlib.h>

#include "gameengine.h"
#include "playstate.h"
#include "menustate.h"



CPlayState CPlayState::m_PlayState;

void CPlayState::Init()
{
    m_ImageManager.Init();
    BulletManager.Init(&m_Factory);
    EnemyManager.Init(&m_Factory);
    ExplosionManager.Init(&m_Factory);

    CSprite* tempsprite2 = new CSprite(m_ImageManager.GetImage("playerplane.bmp"), 0, 0, 50, 3);
	m_Player.Init(tempsprite2);

	firerate = 0;

    m_Factory.Register("Bullet", new CBulletManufacturer());
    m_Factory.Register("Enemy", new CEnemyManufacturer());
    m_Factory.Register("Explosion", new CExplosionManufacturer());

	printf("CPlayState Init\n");
}

void CPlayState::Cleanup()
{
	printf("CPlayState Cleanup\n");
}

void CPlayState::Pause()
{
	printf("CPlayState Pause\n");
}

void CPlayState::Resume()
{
	printf("CPlayState Resume\n");
}

void CPlayState::HandleEvents(CGameEngine* game)
{
	SDL_Event event;

	if (SDL_PollEvent(&event))
	{
		switch (event.type)
		{
			case SDL_QUIT:
				game->Quit();
				break;

			case SDL_KEYDOWN:
				switch (event.key.keysym.sym)
				{
					case SDLK_ESCAPE:
						game->Quit();
						break;
					case SDLK_m:
						game->PushState( CMenuState::Instance() );
						break;
                    case SDLK_SPACE:
                        KeyState[KEY_SPACE] = true;
                        break;
                    case SDLK_UP:
                        KeyState[KEY_UP] = true;
                        break;
                    case SDLK_DOWN:
                        KeyState[KEY_DOWN] = true;
                        break;
                    case SDLK_LEFT:
                        KeyState[KEY_LEFT] = true;
                        break;
                    case SDLK_RIGHT:
                        KeyState[KEY_RIGHT] = true;
                        break;
				}
				break;
            case SDL_KEYUP:
                switch (event.key.keysym.sym)
                {
                    case SDLK_SPACE:
                        KeyState[KEY_SPACE] = false;
                        break;
                    case SDLK_UP:
                        KeyState[KEY_UP] = false;
                        break;
                    case SDLK_DOWN:
                        KeyState[KEY_DOWN] = false;
                        break;
                    case SDLK_LEFT:
                        KeyState[KEY_LEFT] = false;
                        break;
                    case SDLK_RIGHT:
                        KeyState[KEY_RIGHT] = false;
                        break;
                }
                break;
		}
	}
	
	
	m_Player.StopX();
	m_Player.StopY();
	
	if (KeyState[KEY_UP] && !KeyState[KEY_DOWN])
	{
        m_Player.MoveUp();
    }
    if (!KeyState[KEY_UP] && KeyState[KEY_DOWN])
    {
        m_Player.MoveDown();
    }
    if (KeyState[KEY_LEFT] && !KeyState[KEY_RIGHT])
    {
        m_Player.MoveLeft();
    }
    if (!KeyState[KEY_LEFT] && KeyState[KEY_RIGHT])
    {
        m_Player.MoveRight();
    }
    
    firerate -= (int)game->deltaT;
    if (KeyState[KEY_SPACE])
    {
        
        if (firerate <= 0)
        {
        firerate = 1000;
        
        CBullet* tempbullet = static_cast<CBullet*>(m_Factory.Create("Bullet"));
        tempbullet->Init(new CSprite(m_ImageManager.GetImage("bullets.bmp")));
        tempbullet->m_X = m_Player.m_X + (m_Player.m_Sprite->m_DestRect.w/2) - (tempbullet->m_Sprite->m_DestRect.w/2);
        tempbullet->m_Y = m_Player.m_Y - tempbullet->m_Sprite->m_DestRect.h;
        tempbullet->m_Alive = true;

        BulletManager.Add(tempbullet);
        tempbullet = NULL;
        }
        
    }



}

void CPlayState::Update(CGameEngine* game)
{
    /*
    CEnemy* tempenemy = static_cast<CEnemy*>(m_Factory.Create("Enemy"));
    tempenemy->Init(new CSprite(m_ImageManager.GetImage("enemyplane1.bmp"), 0, 0, 20, 3));
    tempenemy->m_X = rand() % 600 + 0;
    tempenemy->m_Y = 0;
    tempenemy->m_Alive = true;
    EnemyManager.Add(tempenemy);
    tempenemy = NULL;
*/



    //if (game->time%100 == 0)

    m_Player.Update(game->deltaT);
    BulletManager.Update(game->deltaT);
    EnemyManager.Update(game->deltaT);


    for (BulletManager.m_Itor = BulletManager.m_BulletList.begin(); BulletManager.m_Itor != BulletManager.m_BulletList.end(); ++BulletManager.m_Itor)
    {
        for (EnemyManager.m_Itor = EnemyManager.m_EnemyList.begin(); EnemyManager.m_Itor != EnemyManager.m_EnemyList.end(); ++EnemyManager.m_Itor)
        {
            if (  (*(BulletManager.m_Itor))->m_Sprite->CheckCollision((*(EnemyManager.m_Itor))->m_Sprite)  )
            {
                (*(BulletManager.m_Itor))->m_Alive = false;
                (*(EnemyManager.m_Itor))->m_Alive = false;

                CExplosion* tempexplosion = static_cast<CExplosion*>(m_Factory.Create("Explosion"));
                tempexplosion->Init((*(EnemyManager.m_Itor)), new CSprite(m_ImageManager.GetImage("explode_7.bmp"), 0, 0, 15, 16));
                tempexplosion->m_Alive = true;
                ExplosionManager.Add(tempexplosion);
                tempexplosion = NULL;
            }
        }

    }


    ExplosionManager.Update(game->deltaT);

}

void CPlayState::Draw(CGameEngine* game)
{
    SDL_FillRect(game->screen,&game->screen->clip_rect,SDL_MapRGB(game->screen->format,0,0,0));

    m_Player.Draw(game->screen);
    BulletManager.Draw(game->screen);
    EnemyManager.Draw(game->screen);
    ExplosionManager.Draw(game->screen);
	SDL_UpdateRect(game->screen, 0, 0, 0, 0);
}

