Skip to content

Instantly share code, notes, and snippets.

@cogile1312
Created February 13, 2020 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cogile1312/5f616e09f861103f74bc67c31368cb6b to your computer and use it in GitHub Desktop.
Save cogile1312/5f616e09f861103f74bc67c31368cb6b to your computer and use it in GitHub Desktop.
#include "Engine.h"
#include "GamestatePlay.h"
#include <iterator>
#include <vector>
using namespace std;
std::vector<CSprite*> bullets;
/*****************************************************************************/
int CGamestatePlay::Initialize()
{
// Background init
m_pBackground = new CSprite();
m_pBackground->Initialize( "Assets\\Play\\Background_640x384_f1.bmp" );
m_pBackground->SetPosition(
CEngine::GetWindowWidth() / 2.0f, CEngine::GetWindowHeight() / 2.0f );
m_pBackground2 = new CSprite();
m_pBackground2->Initialize( "Assets\\Play\\Background_640x384_f1.bmp" );
m_pBackground2->SetPosition(
CEngine::GetWindowWidth() / 2.0f + CEngine::GetWindowWidth(),
CEngine::GetWindowHeight() / 2.0f );
// Foreground init
m_pForeground = new CSprite();
m_pForeground->Initialize( "Assets\\Play\\BackForeground_640x384_f1.bmp" );
m_pForeground->SetPosition(
CEngine::GetWindowWidth() / 2.0f, CEngine::GetWindowHeight() / 2.0f );
m_pForeground2 = new CSprite();
m_pForeground2->Initialize( "Assets\\Play\\BackForeground_640x384_f1.bmp" );
m_pForeground2->SetPosition(
CEngine::GetWindowWidth() / 2.0f + CEngine::GetWindowWidth(),
CEngine::GetWindowHeight() / 2.0f );
m_pHero = new CSprite();
m_pHero->Initialize( "Assets\\Play\\hero_47x39_f16.bmp", 16 );
m_pHero->SetPosition( 320, 240 );
m_pEnemy = new CSprite();
m_pEnemy->Initialize( "Assets\\Play\\Explosion_33x31_f5.bmp", 5 );
m_pEnemy->SetPosition( 350, 250 );
bullets.clear();
m_maxBullets = 10;
for (int i = 0; i < m_maxBullets;i++)
{
m_pBullet = new CSprite();
m_pBullet->Initialize("Assets\\Play\\shot_8x8_f2.bmp", 2);
m_pBullet->SetActive(false);
bullets.emplace_back(m_pBullet);
}
//?m_pCollision = new CSprite();
//?m_pCollision->Initialize( "Assets\\CollisionCircle_47x39_f1.bmp" );
//m_pHero->IsCollidingWith( m_pEnemy );
return 0;
}
/*****************************************************************************/
void CGamestatePlay::Finalize()
{
for (auto m_pBullet : bullets)
{
FINALIZE_DELETE(m_pBullet);
}
FINALIZE_DELETE( m_pEnemy );
FINALIZE_DELETE( m_pHero );
FINALIZE_DELETE( m_pForeground2 );
FINALIZE_DELETE( m_pForeground );
FINALIZE_DELETE( m_pBackground2 );
FINALIZE_DELETE( m_pBackground );
}
/*****************************************************************************/
int CGamestatePlay::Update()
{
// Background scrollen
float backspeed = 100 * CEngine::GetDeltaTime();
m_pBackground->IncPositionX( -backspeed );
m_pBackground2->IncPositionX( -backspeed );
if( m_pBackground->GetPositionX() <= -320 ) m_pBackground->SetPositionX( 960 );
if( m_pBackground2->GetPositionX() <= -320 ) m_pBackground2->SetPositionX( 960 );
m_pBackground->Update();
m_pBackground2->Update();
// Foreground scrollen
m_pForeground->IncPositionX( -backspeed );
m_pForeground2->IncPositionX( -backspeed );
if( m_pForeground->GetPositionX() <= -320 ) m_pForeground->SetPositionX( 960 );
if( m_pForeground2->GetPositionX() <= -320 ) m_pForeground2->SetPositionX( 960 );
m_pForeground->Update();
m_pForeground2->Update();
// Hero Movement
float herospeed = 200 * CEngine::GetDeltaTime();
if( CEngine::IsKeyPressed( 'w' ) ) m_pHero->IncPositionY( -herospeed );
if( CEngine::IsKeyPressed( 'a' ) ) m_pHero->IncPositionX( -herospeed );
if( CEngine::IsKeyPressed( 's' ) ) m_pHero->IncPositionY( herospeed );
if( CEngine::IsKeyPressed( 'd' ) ) m_pHero->IncPositionX( herospeed );
if( m_pHero->GetPositionX() > 620 ) m_pHero->SetPositionX( 620);
if( m_pHero->GetPositionX() < 20 ) m_pHero->SetPositionX( 20 );
if (m_pHero->GetPositionY() > 360) m_pHero->SetPositionY(360);
if (m_pHero->GetPositionY() < 20) m_pHero->SetPositionY(20);
float herorotation = 200 * CEngine::GetDeltaTime();
if( CEngine::IsKeyPressed( '+' ) ) m_pHero->IncRotation( herorotation );
m_pHero->Update();
m_pEnemy->Update();
//?m_pCollision->Update();
if( m_pEnemy->IsActive() )
{
if( IsColliding( m_pHero, m_pEnemy ) )
{
//m_pHero->SetActive( false );
m_pEnemy->SetActive( false );
}
/*if (IsColliding(m_pBullet , m_pEnemy))
{
m_pEnemy->SetActive(false);
m_pBullet->SetActive(false);
}*/
/*if( IsColliding( m_pBullet, m_pEnemy ) )
{
m_pBullet->SetActive( false );
m_pEnemy->SetActive( false );
}*/
}
// Am i shooting? yes? no? wtf?
if( CEngine::IsKeyPressed('k'))
{
std::vector<CSprite*>::iterator ItBeg =bullets.begin();
std::vector<CSprite*>::iterator ItEnd =bullets.end();
for (; ItBeg != ItEnd; ++ItBeg)
{
if (!m_pBullet->IsActive())
{
m_pBullet->SetActive(true);
Vector v = m_pHero->GetPosition();
m_pBullet->SetPosition(v);
}
}
}
m_fBulletSpeed = 200.0f;
for (auto m_pBullet : bullets)
{
if( m_pBullet->IsActive() )
{
m_pBullet->IncPositionX(
m_fBulletSpeed * CEngine::GetDeltaTime()
);
Vector v = m_pBullet->GetPosition();
if
(
v.x < 0 || v.y < 0 ||
v.x > CEngine::GetWindowWidth() || v.y > CEngine::GetWindowHeight()
)
{
m_pBullet->SetActive( false );
}
}
}
return 0;
}
/*****************************************************************************/
void CGamestatePlay::Render()
{
m_pBackground->Render();
m_pBackground2->Render();
m_pEnemy->Render();
for (auto m_pBullet : bullets)
{
if (m_pBullet->IsActive())
{
m_pBullet->Render();
}
}
m_pHero->Render();
//?m_pCollision->SetPosition( m_pHero->GetPosition() );
//?m_pCollision->Render();
m_pForeground->Render();
m_pForeground2->Render();
/*?
SDL_SetRenderDrawColor( CEngine::GetRenderer(), 255, 0, 0, 255 );
SDL_RenderDrawLine(
CEngine::GetRenderer(),
m_pHero->GetPositionX(),
m_pHero->GetPositionY(),
m_pEnemy->GetPositionX(),
m_pEnemy->GetPositionY()
);
*/
}
#pragma once
#include "Sprite.h"
#include "Gamestate.h"
#include <list>
class CGamestatePlay : public CGamestate
{
public:
CGamestatePlay() { m_sName = "play"; }
int Initialize();
void Finalize();
int Update();
void Render();
private:
CSprite* m_pHero = nullptr;
CSprite* m_pBackground = nullptr;
CSprite* m_pBackground2 = nullptr;
CSprite* m_pForeground = nullptr;
CSprite* m_pForeground2 = nullptr;
CSprite* m_pEnemy = nullptr;
//?CSprite* m_pCollision = nullptr;
CSprite* m_pBullet = nullptr;
Vector m_fBulletDir = { 1.0f, 1.0f };
float m_fBulletSpeed = -1.0f;
int m_maxBullets = -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment