<file_sep>#include "Box.h"
#include "Sprite.h"
#include "Data.h"
#include "BoxManager.h"
#include "Player.h"
#include "Collision.h"

#include "D3dDevice.h"

CBox::CBox() : m_bLife(true),
			   m_fScale(1.0f),
			   m_fRotation(0.0f),
			   m_fSpinSpeed(0.0f),
			   m_Vector(),
			   m_fMoveAcc(g_Data->m_fMoveAcc), m_fSpinAcc(g_Data->m_fSpinAcc),
			   m_fFixedSpinSpeed(0.0f)
{
	for(int i=0; i<3; i++)
		m_pAfterImage[i] = NULL ;
}
CBox::~CBox()
{
	for(int i=0; i<3; i++)
	{
		if(m_pAfterImage[i]!=NULL)
			delete m_pAfterImage[i] ;
	}
}

void CBox::Init()
{
	m_pSprite = new CSprite ;
	m_pSprite->Init("Resource/Image/Box.png") ;

	for(int i=0; i<3; i++)
	{
		m_pAfterImage[i] = new CSprite ;
		m_pAfterImage[i]->Init("Resource/Image/Box.png") ;
		m_pAfterImage[i]->SetAlpha(255-(63*(i+1))) ;
	}

	m_bLife = true ;
	m_fScale = 1.0f ;
	m_fRotation = 0.0f ;
	m_fSpinSpeed = 0.0f ;
	m_Vector = m_Vector * 0.0f ;
}

void CBox::InitAfterImagePosition()
{
	m_pSprite->SetPosition(m_fX, m_fY) ;

	for(int i=0; i<3; i++)
		m_pAfterImage[i]->SetPosition(m_fX, m_fY) ;
}

void CBox::Update()
{
	Spin() ;
	Move() ;

	SpinColor() ;
}

void CBox::SetScale(float fScale)
{
	m_fScale = fScale ;
	m_pSprite->SetScale(m_fScale, m_fScale) ;
	for(int i=0; i<3; i++)
		m_pAfterImage[i]->SetScale(m_fScale, m_fScale) ;
}

void CBox::SetSpinSpeed(float fSpinSpeed)
{
	m_fSpinSpeed = fSpinSpeed ;
}

void CBox::SetVector(Vector vec)
{
	m_Vector = vec ;
}

void CBox::SetFixedSpinSpeed(float fFixedSpinSpeed)
{
	m_fFixedSpinSpeed = fFixedSpinSpeed ;
}

const float CBox::GetScale() const
{
	return m_fScale ;
}

const float CBox::GetSpinSpeed() const
{
	return m_fSpinSpeed ;
}

const float CBox::GetSpinSpeedAbs() const
{
	if(m_fSpinSpeed<0.0f)
		return -m_fSpinSpeed ;

	return m_fSpinSpeed ;
}

const Vector CBox::GetVector() const
{
	return m_Vector ;
}

const bool CBox::BeLife() const
{
	return m_bLife ;
}

void CBox::SpinAccelerate(float fSpinAcc)
{
	bool bSign1, bSign2 ;

	if(m_fSpinSpeed==0.0f || fSpinAcc==0.0f)
	{
		m_bLife = false ;
	}
	else
	{
		if(m_fSpinSpeed>0.0f)
			bSign1 = true ;
		else if(m_fSpinSpeed<0.0f)
			bSign1 = false ;

		if(fSpinAcc>0.0f)
			bSign2 = true ;
		else if(fSpinAcc<0.0f)
			bSign2 = false ;

		if(bSign1!=bSign2)
			m_bLife = false ;
		else
			m_fSpinSpeed = fSpinAcc ;
	}
}

void CBox::Render()
{
	AfterImage() ;

	for(int i=2; i>=0; i--)
		m_pAfterImage[i]->Render() ;

	m_pSprite->SetPosition(m_fX, m_fY) ;
	m_pSprite->Render() ;
}

void CBox::Spin()
{
	float fSpinSpeed = m_fSpinAcc * g_D3dDevice->GetMoveTime() ;
	float temp = m_fSpinSpeed < 0.0f ? -m_fSpinSpeed : m_fSpinSpeed ;
	float temp2 = m_fFixedSpinSpeed < 0.0f ? -m_fFixedSpinSpeed : m_fFixedSpinSpeed ;

	if(temp - fSpinSpeed>temp2)
	{
		if(m_fSpinSpeed>0.0f)
			m_fSpinSpeed -= fSpinSpeed ;
		else if(m_fSpinSpeed<0.0f)
			m_fSpinSpeed += fSpinSpeed ;
	}
	else
	{
		m_fSpinSpeed = m_fFixedSpinSpeed ;
	}

	if(m_fSpinSpeed>m_fScale * 15.0f)
		m_fSpinSpeed = m_fScale * 15.0f ;
	else if(m_fSpinSpeed<m_fScale * -15.0f)
		m_fSpinSpeed = m_fScale * -15.0f ;
	m_fRotation += m_fSpinSpeed ;

	m_pSprite->SetAngle(m_fRotation) ;
}

void CBox::Move()
{
	float fAcceleration = m_fMoveAcc * g_D3dDevice->GetMoveTime() ;

	CCollision col ;
	float angle ;
	float PlayerX = g_Player->GetPositionX() ;
	float PlayerY = g_Player->GetPositionY() ;

	angle = col.GetAngle(m_fX, m_fY, PlayerX, PlayerY) ;
	angle = angle * D3DX_PI / 180.0f ;

	m_Vector.x += cos(angle) * m_fMoveAcc ;
	m_Vector.y += sin(angle) * m_fMoveAcc ;

	float fBoxForce = g_Data->m_fBoxForceMax ;
	float fForce = sqrt((m_Vector.x * m_Vector.x) + (m_Vector.y * m_Vector.y)) ;
	if(fForce>fBoxForce)
	{
		fForce = fBoxForce / fForce ;
		m_Vector = m_Vector * fForce ;
	}

	m_fX += m_Vector.x ;
	m_fY += m_Vector.y ;
}

void CBox::SpinColor()
{
	// 회전 방향에 따른 색
	int r, g, b ;
	float percentage ;
	r = g = b = 255 ;

	if(m_fSpinSpeed>=0.0f)
	{
		r -= 92 ;
		g -= 200 ;
		b -= 255 ;
		percentage = m_fSpinSpeed / (m_fScale * 15.0f) ;
	}
	else
	{
		r -= 255 ;
		g -= 96 ;
		b -= 96 ;
		percentage = m_fSpinSpeed / (m_fScale * -15.0f) ;
	}
	r = 255 - (int)(r * percentage) ;
	g = 255 - (int)(g * percentage) ;
	b = 255 - (int)(b * percentage) ;
	m_pSprite->SetRGB(r, g, b) ;
	for(int i=0; i<3; i++)
		m_pAfterImage[i]->SetRGB(r, g, b) ;
}

void CBox::AfterImage()
{
	D3DXVECTOR3 position ;
	float angle ;

	for(int i=2; i>=1; i--)
	{
		position = m_pAfterImage[i-1]->GetPosition() ;
		m_pAfterImage[i]->SetPosition(position.x, position.y) ;
		m_pAfterImage[i]->SetAngle(m_fRotation) ;
	}

	position = m_pSprite->GetPosition() ;
	m_pAfterImage[0]->SetPosition(position.x, position.y) ;
	m_pAfterImage[0]->SetAngle(m_fRotation) ;
}<file_sep>#pragma once

class CNumberUI ;
class CSprite ;

class CTimeLimitUI
{
private :
	float m_fX, m_fY ;
	float m_fTime ;
	int m_nNum ;
	CNumberUI *m_pNumber[5] ;
	CSprite *m_pDot ;

public :
	CTimeLimitUI() ;
	~CTimeLimitUI() ;

	void Init() ;

	void SetPosition(float fX, float fY) ;
	const bool Timeout() const ;

	void Update() ;

	void Render() ;
private :
	void SetTime() ;
} ;<file_sep>#pragma once

#include "Singleton.h"

class CData : public Singleton<CData>
{
public :
	float m_fModulus ;
	float m_fMass ;
	float m_fMoveAcc, m_fSpinAcc ;

	bool m_bScore, m_bTime ;
	int m_nMaxScore ;
	float m_fTimeLimit ;

	float m_fBoxForceMax ;
	float m_bBoxCollision ;

public :
	CData() ;
	~CData() ;

	void Init() ;
private :
	bool LoadData() ;
	bool CreateDataFile() ;
} ;

#define g_Data CData::GetInstance()<file_sep>#pragma once

#include "Scene.h"

class CSprite ;
class CScoreUI ;
class CTimeLimitUI ;

class SampleScene : public Scene
{
private :
	bool m_bScore, m_bTime ;
	bool m_bGameover, m_bGameclear ;
	float m_fSpriteTime ;
	CSprite *m_pBackground ;
	CSprite *m_pGameover, *m_pGameclear ;
	CScoreUI *m_pScore ;
	CTimeLimitUI *m_pTimeLimit ;

public :
	static Scene* scene() ;

	SampleScene() ;
	virtual ~SampleScene() ;

	void Init() ;
	void Destroy() ;

	void Update(float dt) ;
	void Render() ;
private :
	void GameOver() ;
} ;<file_sep>#pragma once

#include "Objects.h"
#include "Vector.h"

class CBox : public CObjects
{
protected :
	bool m_bLife ;
	float m_fScale ;
	float m_fRotation, m_fSpinSpeed ;
	Vector m_Vector ;

	float m_fMoveAcc, m_fSpinAcc ;

	//
	CSprite *m_pAfterImage[3] ;
private :
	float m_fFixedSpinSpeed ;

public :
	CBox() ;
	virtual ~CBox() ;

	virtual void Init() ;
	void InitAfterImagePosition() ;

	virtual void Update() ;

	void SetScale(float fScale) ;
	void SetSpinSpeed(float fSpinSpeed) ;
	void SetVector(Vector vec) ;
	//
	void SetFixedSpinSpeed(float fFixedSpinSpeed) ;

	const float GetScale() const ;
	const float GetSpinSpeed() const ;
	const float GetSpinSpeedAbs() const ;
	const Vector GetVector() const ;

	const bool BeLife() const ;

	void SpinAccelerate(float fSpinAcc) ;

	void Render() ;
protected :
	virtual void Spin() ;
	virtual void Move() ;

	void SpinColor() ;
	void AfterImage() ;
} ;<file_sep>#include "Player.h"
#include "Sprite.h"
#include "Keyboard.h"

#include "D3dDevice.h"

CPlayer::CPlayer() : m_nScore(0),
					 m_bGameover(false)
{
	m_fSpinSpeed = 0.0f ;
}
CPlayer::~CPlayer()
{
}

void CPlayer::Init()
{
	m_pSprite = new CSprite ;
	m_pSprite->Init("Resource/Image/Box.png") ;

	for(int i=0; i<3; i++)
	{
		m_pAfterImage[i] = new CSprite ;
		m_pAfterImage[i]->Init("Resource/Image/Box.png") ;
		m_pAfterImage[i]->SetAlpha(255-(63*(i+1))) ;
	}

	m_bLife = true ;
	m_bGameover = false ;
	m_fScale = 1.0f ;
	m_fRotation = 0.0f ;
	m_fSpinSpeed = 0.0f ;
	m_Vector = m_Vector * 0.0f ;
	m_nScore = 0 ;
}

void CPlayer::EnergyAbsorption()
{
	m_nScore += 50 ;
	m_fScale += 0.001f ;
	m_pSprite->SetScale(m_fScale, m_fScale) ;
}

void CPlayer::Gameover()
{
	m_bGameover = true ;
}

const int CPlayer::GetScore() const
{
	return m_nScore ;
}

const bool CPlayer::BeGameover() const
{
	return m_bGameover ;
}

void CPlayer::Move()
{
	float fAcceleration = m_fMoveAcc * g_D3dDevice->GetMoveTime() ;
	float fDeceleration = (m_fMoveAcc/2.0f) * g_D3dDevice->GetMoveTime() ;

	if(g_Keyboard->IsButtonDown(DIK_UP))
		m_Vector.y += fAcceleration ;
	else if(g_Keyboard->IsButtonDown(DIK_DOWN))
		m_Vector.y -= fAcceleration ;
	else
	{
		float temp = m_Vector.y < 0.0f ? -m_Vector.y : m_Vector.y ;

		if(temp - fDeceleration>0.0f)
		{
			if(m_Vector.y>0.0f)
				m_Vector.y -= fDeceleration ;
			else
				m_Vector.y += fDeceleration ;
		}
		else
		{
			m_Vector.y = 0.0f ;
		}
	}

	if(g_Keyboard->IsButtonDown(DIK_LEFT))
		m_Vector.x -= fAcceleration ;
	else if(g_Keyboard->IsButtonDown(DIK_RIGHT))
		m_Vector.x += fAcceleration ;
	else
	{
		float temp = m_Vector.x < 0.0f ? -m_Vector.x : m_Vector.x ;

		if(temp - fDeceleration>0.0f)
		{
			if(m_Vector.x>0.0f)
				m_Vector.x -= fDeceleration ;
			else
				m_Vector.x += fDeceleration ;
		}
		else
		{
			m_Vector.x = 0.0f ;
		}
	}

	m_fX += m_Vector.x ;
	m_fY += m_Vector.y ;
}

void CPlayer::Spin()
{
	float fSpinSpeed = m_fSpinAcc * g_D3dDevice->GetMoveTime() ;

	if(g_Keyboard->IsButtonDown(DIK_A))
		m_fSpinSpeed += fSpinSpeed ;
	else if(g_Keyboard->IsButtonDown(DIK_S))
		m_fSpinSpeed -= fSpinSpeed ;
	else
	{
		float temp = m_fSpinSpeed < 0.0f ? -m_fSpinSpeed : m_fSpinSpeed ;
		fSpinSpeed = 0.05f * g_D3dDevice->GetMoveTime() ;

		if(temp - fSpinSpeed>0.0f)
		{
			if(m_fSpinSpeed>0.0f)
				m_fSpinSpeed -= fSpinSpeed ;
			else if(m_fSpinSpeed<0.0f)
				m_fSpinSpeed += fSpinSpeed ;
		}
		else
		{
			m_fSpinSpeed = 0.0f ;
		}
	}

	if(m_fSpinSpeed>m_fScale * 15.0f)
		m_fSpinSpeed = m_fScale * 15.0f ;
	else if(m_fSpinSpeed<m_fScale * -15.0f)
		m_fSpinSpeed = m_fScale * -15.0f ;
	m_fRotation += m_fSpinSpeed ;

	m_pSprite->SetAngle(m_fRotation) ;
}<file_sep>#include "Particle.h"
#include "D3dDevice.h"

CParticle::CParticle() : m_bLife(true),
						 m_bTime(false),
						 m_fTime(0.0f),
						 m_vecSpeed(),
						 m_vecAcc()
{
}
CParticle::~CParticle()
{
}

void CParticle::Update()
{
	float fMoveTime = g_D3dDevice->GetMoveTime() ;
	m_fX += m_vecSpeed.x * fMoveTime ;
	m_fY += m_vecSpeed.y * fMoveTime ;

	m_vecSpeed += m_vecAcc * fMoveTime ;

	if(m_bTime)
	{
		m_fTime -= g_D3dDevice->GetTime() ;
		if(m_fTime<0.0f)
			m_bLife = false ;
	}
}

const bool CParticle::BeLife() const
{
	return m_bLife ;
}<file_sep>#include "TimeLimitUI.h"
#include "NumberUI.h"
#include "Sprite.h"
#include "Data.h"
#include "D3dDevice.h"

CTimeLimitUI::CTimeLimitUI() : m_fX(0.0f), m_fY(0.0f),
							   m_fTime(0),
							   m_nNum(NULL),
							   m_pDot(NULL)
{
}
CTimeLimitUI::~CTimeLimitUI()
{
	for(int i=0; i<5; i++)
	{
		if(m_pNumber[i]!=NULL)
			delete m_pNumber[i] ;
	}

	if(m_pDot!=NULL)
		delete m_pDot ;
}

void CTimeLimitUI::Init()
{
	for(int i=0; i<5; i++)
	{
		m_pNumber[i] = new CNumberUI ;
		m_pNumber[i]->Init() ;
	}

	m_pDot = new CSprite ;
	m_pDot->Init("Resource/Image/Dot.png") ;

	m_fTime = g_Data->m_fTimeLimit ;
	SetTime() ;
}

void CTimeLimitUI::SetPosition(float fX, float fY)
{
	m_fX = fX ;
	m_fY = fY ;
}

const bool CTimeLimitUI::Timeout() const
{
	if(m_fTime<=0.0f)
		return true ;

	return false ;
}

void CTimeLimitUI::Update()
{
	m_fTime -= g_D3dDevice->GetTime() ;
	SetTime() ;
}

void CTimeLimitUI::Render()
{
	m_pDot->SetPosition(m_fX, m_fY) ;
	m_pDot->Render() ;

	for(int i=0; i<m_nNum; i++)
	{
		if(i!=0)
			m_pNumber[i]->SetPosition(m_fX - (40.0f * (i-1)) - 25.0f, m_fY) ;
		else
			m_pNumber[i]->SetPosition(m_fX + (40.0f * i) + 25.0f, m_fY) ;
		m_pNumber[i]->Render() ;
	}
}

void CTimeLimitUI::SetTime()
{
	int nTime = (int)(m_fTime * 10.0f) ;
	int num ;
	int i=0 ;

	if(nTime<0)
	{
		m_pNumber[0]->SetNumber(0) ;
		m_pNumber[1]->SetNumber(0) ;
		m_nNum = 2 ;
	}

	while(nTime!=0 || i<2)
	{
		num = nTime % 10 ;
		nTime = nTime / 10 ;

		m_pNumber[i++]->SetNumber(num) ;
	}

	m_nNum = i ;
}<file_sep>#pragma once

class CBox ;

class CCollision
{
public :
	CCollision() ;
	~CCollision() ;

	bool CircleCollision(float x1, float y1, float r1, float x2, float y2, float r2) ;
	void InelasticCollision(CBox *pBox1, CBox *pBox2) ;
	void InelasticCollision(float m1, float m2, float v1, float v2, float modulus, float &_v1, float &_v2) ;
	float GetAngle(float x1, float y1, float x2, float y2) ;
	float AngleOfReflection(float angle1, float angle2) ;
} ;<file_sep>#include "SampleScene.h"
#include "SceneManager.h"
#include "TitleScene.h"

#include "Keyboard.h"
#include "Mouse.h"
#include "Joystick.h"

#include "Sprite.h"
#include "UISprite.h"
#include "CameraManager.h"
#include "MusicManager.h"

#include "Data.h"
#include "BoxManager.h"
#include "Player.h"
#include "ParticleManager.h"
#include "ScoreUI.h"
#include "TimeLimitUI.h"

SampleScene::SampleScene() : m_bScore(false), m_bTime(false),
							 m_bGameover(false), m_bGameclear(false),
							 m_fSpriteTime(0.0f),
							 m_pBackground(NULL),
							 m_pGameover(NULL), m_pGameclear(),
							 m_pScore(NULL),
							 m_pTimeLimit(NULL)
{
}
SampleScene::~SampleScene()
{
	if(m_pBackground!=NULL)
		delete m_pBackground ;
	if(m_pGameover!=NULL)
		delete m_pGameover ;
	if(m_pGameclear!=NULL)
		delete m_pGameclear ;
	if(m_pScore!=NULL)
		delete m_pScore ;
	if(m_pTimeLimit!=NULL)
		delete m_pTimeLimit ;
}

Scene* SampleScene::scene()
{
	Scene *scene = new SampleScene ;

	return scene ;
}

void SampleScene::Init()
{
	g_CameraManager->AddCamera(new CCamera()) ;

	g_Data->Init() ;

	m_bScore = g_Data->m_bScore ;
	m_bTime = g_Data->m_bTime ;

	m_pBackground = new CSprite ;
	m_pBackground->Init("Resource/Image/Background.png") ;

	m_pGameover = new CSprite ;
	m_pGameover->Init("Resource/Image/Gameover.png") ;

	m_pGameclear = new CSprite ;
	m_pGameclear->Init("Resource/Image/Gameclear.png") ;

	if(m_bScore)
	{
		m_pScore = new CScoreUI ;
		m_pScore->Init() ;
	}

	if(m_bTime)
	{
		m_pTimeLimit = new CTimeLimitUI ;
		m_pTimeLimit->Init() ;
	}

	g_Player->Init() ;
	g_Player->SetPosition(0.0f, 0.0f) ;

	g_BoxManager->Init() ;

	g_CameraManager->SetPosition(g_Player->GetPositionX(), g_Player->GetPositionY()) ;
}

void SampleScene::Destroy()
{
}

void SampleScene::Update(float dt)
{
	g_Keyboard->Update() ;
	g_Mouse->Update() ;
	g_Joystick->Update() ;
	g_MusicManager->Loop() ;

	// GameOver üũ
	m_bGameover = !g_Player->BeLife() ;
	if(m_bTime)
		m_bGameover = m_bGameover | m_pTimeLimit->Timeout() ;

	// GameClear üũ
	if(m_bScore)
		m_bGameclear = m_pScore->AchieveAScore() ;

	// Update
	if(!m_bGameover && !m_bGameclear)
		g_Player->Update() ;
	else
		g_Player->Gameover() ;

	g_BoxManager->Update() ;

	g_ParticleManager->Update() ;

	if(m_bScore && (!m_bGameover && !m_bGameclear))
		m_pScore->Update() ;
	if(m_bTime && (!m_bGameover && !m_bGameclear))
		m_pTimeLimit->Update() ;

	//

	float PlayerX = g_Player->GetPositionX() ;
	float PlayerY = g_Player->GetPositionY() ;
	float CameraX = g_CameraManager->GetPosition().x ;
	float CameraY = g_CameraManager->GetPosition().y ;
	float width = 320.0f / 2.0f ;
	float height = 240.0f / 2.0f ;

	if(PlayerX>CameraX + width)
		CameraX += PlayerX - (CameraX + width) ;
	else if(PlayerX<CameraX - width)
		CameraX += PlayerX - (CameraX - width) ;
	
	if(PlayerY>CameraY + height)
		CameraY += PlayerY - (CameraY + height) ;
	else if(PlayerY<CameraY - height)
		CameraY += PlayerY - (CameraY - height) ;

	g_CameraManager->SetPosition(CameraX, CameraY) ;
	m_pBackground->SetPosition(CameraX, CameraY) ;

	if(m_bGameover || m_bGameclear)
	{
		m_pGameover->SetPosition(CameraX, CameraY) ;
		m_pGameclear->SetPosition(CameraX, CameraY) ;

		if(m_fSpriteTime<3.0f)
			m_fSpriteTime += dt ;

		if(m_fSpriteTime>=3.0f)
		{
			m_pGameover->SetAlpha(255) ;
			m_pGameclear->SetAlpha(255) ;

			if(g_Keyboard->IsPressDown(DIK_RETURN))
			{
				GameOver() ;
				return ;
			}
		}
		else
		{
			int nAlpha = 255 * (m_fSpriteTime / 3.0f) ;
			m_pGameover->SetAlpha(nAlpha) ;
			m_pGameclear->SetAlpha(nAlpha) ;
		}
	}

	if(m_bScore)
		m_pScore->SetPosition(CameraX + 462.0f, CameraY + 325.0f) ;
	if(m_bTime)
		m_pTimeLimit->SetPosition(CameraX, CameraY + 325.0f) ;
}

void SampleScene::Render()
{
	g_CameraManager->CameraRun() ;

	m_pBackground->Render() ;

	if(!m_bGameover && !m_bGameclear)
		g_Player->Render() ;

	g_BoxManager->Render() ;

	g_ParticleManager->Render() ;

	if(m_bScore)
		m_pScore->Render() ;
	if(m_bTime)
		m_pTimeLimit->Render() ;

	if(m_bGameover)
		m_pGameover->Render() ;
	if(m_bGameclear)
		m_pGameclear->Render() ;
}

void SampleScene::GameOver()
{
	g_BoxManager->Clear() ;
	g_ParticleManager->ParticleClear() ;

	g_SceneManager->ChangeScene(TitleScene::scene()) ;
}<file_sep>HUGP1_1week
===========

HU Game Project 1 - 1 week
<file_sep>#pragma once

#include "Particle.h"

class CParticle_Energy : public CParticle
{
private :
	bool m_bGuide ;
	float m_fForce ;

public :
	CParticle_Energy() ;
	~CParticle_Energy() ;

	void Init() ;

	void Update() ;
} ;<file_sep>#pragma once

#include "Singleton.h"
#include <vector>

class CParticle ;

class CParticleManager : public Singleton<CParticleManager>
{
private :
	std::vector<CParticle*> m_ParticleList ;

public :
	CParticleManager() ;
	~CParticleManager() ;

	void Update() ;

	void Render() ;

	void CreateParticle(float fX, float fY, int nNumber) ;

	void ParticleClear() ;
} ;

#define g_ParticleManager CParticleManager::GetInstance()<file_sep>#pragma once

#include "Objects.h"
#include "Vector.h"

class CParticle : public CObjects
{
protected :
	bool m_bLife ;
	bool m_bTime ;
	float m_fTime ;
	Vector m_vecSpeed ;
	Vector m_vecAcc ;

public :
	CParticle() ;
	virtual ~CParticle() ;

	virtual void Update() ;

	const bool BeLife() const ;
} ;<file_sep>#include "Collision.h"
#include "Data.h"
#include <math.h>
#include "Box.h"

CCollision::CCollision()
{
}
CCollision::~CCollision()
{
}

bool CCollision::CircleCollision(float x1, float y1, float r1, float x2, float y2, float r2)
{
	float distance ;
	float x = (float)(x2 - x1) ;
	float y = (float)(y2 - y1) ;

	distance = sqrt((x*x) + (y*y)) ;

	if(distance<=(r1 + r2))
		return true ;

	return false ;
}

void CCollision::InelasticCollision(CBox *pBox1, CBox *pBox2)
{
	Vector vecBox1 = pBox1->GetVector() ;
	Vector vecBox2 = pBox2->GetVector() ;

	float vecForceBox1 = sqrt((vecBox1.x * vecBox1.x) + (vecBox1.y * vecBox1.y)) ;
	float vecForceBox2 = sqrt((vecBox2.x * vecBox2.x) + (vecBox2.y * vecBox2.y)) ;

	// 비탄성 충돌
	float _vecForceBox1, _vecForceBox2 ;
	float spinSpeedBox1 = pBox1->GetSpinSpeedAbs() ;
	float spinSpeedBox2 = pBox2->GetSpinSpeedAbs() ;
	float modulus = g_Data->m_fModulus ;
	float mass = g_Data->m_fMass ;
	InelasticCollision((spinSpeedBox1 * mass) + 1.0f, (spinSpeedBox2 * mass) + 1.0f, vecForceBox1, vecForceBox2, modulus, _vecForceBox1, _vecForceBox2) ;

	// 회전 감속/가속
	float forceBox1 = spinSpeedBox1 * vecForceBox1 ;
	float forceBox2 = spinSpeedBox2 * vecForceBox2 ;
	float spinAccBox1, spinAccBox2 ;
	float spinModulus ;

	if(forceBox1>=forceBox2)
	{
		if(forceBox1!=0.0f)
			spinModulus = forceBox2 / forceBox1 ;
		else
			spinModulus = 1.0f ;

		spinAccBox1 = pBox1->GetSpinSpeed() + (pBox2->GetSpinSpeed() * spinModulus) ;
		spinAccBox2 = pBox2->GetSpinSpeed() + pBox1->GetSpinSpeed() ;
	}
	else
	{
		if(forceBox2!=0.0f)
			spinModulus = forceBox1 / forceBox2 ;
		else
			spinModulus = 1.0f ;

		spinAccBox1 = pBox1->GetSpinSpeed() + pBox2->GetSpinSpeed() ;
		spinAccBox2 = pBox2->GetSpinSpeed() + (pBox1->GetSpinSpeed() * spinModulus) ;
	}
	
	pBox1->SpinAccelerate(spinAccBox1) ;
	pBox2->SpinAccelerate(spinAccBox2) ;

	// 겹침 방지, 반사각
	float Box1X = pBox1->GetPositionX(), Box1Y = pBox1->GetPositionY() ;
	float Box2X = pBox2->GetPositionX(), Box2Y = pBox2->GetPositionY() ;
	float angleBox1 = GetAngle(Box1X, Box1Y, Box2X, Box2Y) ;
	float angleBox2 = GetAngle(Box2X, Box2Y, Box1X, Box1Y) ;
	float angleRBox1 = AngleOfReflection(angleBox2 + 180.0f, angleBox1) ;
	float angleRBox2 = AngleOfReflection(angleBox1 + 180.0f, angleBox2) ;

	float x = Box2X - Box1X ;
	float y = Box2Y - Box1Y ;
	float distance = sqrt((x*x) + (y*y)) ;
	
	// 겹침현상 밀어내기
	if(distance<=(48.0f))
	{
		distance = (48.0f - distance) / 2.0f ;

		vecBox1.x = cos( -((angleBox1 + 180.0f) * 3.141592f) / 180.0f ) * distance ;
		vecBox1.y = sin( -((angleBox1 + 180.0f) * 3.141592f) / 180.0f ) * distance ;
		pBox1->SetPosition(Box1X + vecBox1.x, Box1Y + vecBox1.y) ;

		vecBox2.x = cos( -((angleBox2 + 180.0f) * 3.141592f) / 180.0f ) * distance ;
		vecBox2.y = sin( -((angleBox2 + 180.0f) * 3.141592f) / 180.0f ) * distance ;
		pBox2->SetPosition(Box2X + vecBox2.x, Box2Y + vecBox2.y) ;
	}
	
	// 반사각으로 튕겨내기
	float radian ;

	radian = angleRBox1 * 3.141592f / 180.0f ;
	vecBox1.x = cos(radian) * _vecForceBox1 ;
	vecBox1.y = sin(radian) * _vecForceBox1 ;
	pBox1->SetVector(vecBox1) ;

	radian = angleRBox2 * 3.141592f / 180.0f ;
	vecBox2.x = cos(radian) * _vecForceBox2 ;
	vecBox2.y = sin(radian) * _vecForceBox2 ;
	pBox2->SetVector(vecBox2) ;
}

void CCollision::InelasticCollision(float m1, float m2, float v1, float v2, float modulus, float &_v1, float &_v2)
{
	float a=m1, b=m2, c=-((m1*v1)+(m2*v2)) ;
	float d=1.0f, e=-1.0f, f=modulus * (v1-v2) ;

	float temp ;
	temp = a/d ;
	d *= temp ;
	e *= temp ;
	f *= temp ;

	b -= e ;
	c -= f ;
	_v2 = -c/b ;
	_v1 = (-(e*_v2) - f)/d ;
}

float CCollision::GetAngle(float x1, float y1, float x2, float y2)
{
	float angle ;

	angle = atan2(x2-x1, y2-y1) ;
	angle = -(angle * 180.0f) / 3.141592f ;
	angle += 90.0f ;

	if(angle<0.0f)
		angle += 360.0f ;

	return angle ;
}

float CCollision::AngleOfReflection(float angle1, float angle2)
{
	float angle ;

	angle2 += 180.0f ;
	if(angle2>360.0f)
		angle2 -= 360.0f ;

	angle = angle1 - angle2 ;
	angle = angle1 + angle ;
	if(angle>360.0f)
		angle -= 360.0f ;

	return angle ;
}<file_sep>#include "ParticleManager.h"
#include "Particle_Debris.h"
#include "Particle_Energy.h"

CParticleManager::CParticleManager()
{
}
CParticleManager::~CParticleManager()
{
	ParticleClear() ;
}

void CParticleManager::Update()
{
	int num=m_ParticleList.size() ;

	for(int i=0; i<num; i++)
	{
		m_ParticleList[i]->Update() ;

		if(!m_ParticleList[i]->BeLife())
		{
			CParticle *pParticle = m_ParticleList[i] ;
			m_ParticleList.erase(m_ParticleList.begin() + i) ;
			delete pParticle ;

			--i ;
			--num ;
		}
	}
}

void CParticleManager::Render()
{
	const int num=m_ParticleList.size() ;

	for(int i=0; i<num; i++)
		m_ParticleList[i]->Render() ;
}

void CParticleManager::CreateParticle(float fX, float fY, int nNumber)
{
	CParticle *pParticle ;
	int num ;

	if(nNumber==0)
		num = 15 ;
	else if(nNumber==1)
		num = 15 ;

	for(int i=0; i<num; i++)
	{
		if(nNumber==0)
			pParticle = new CParticle_Debris ;
		else if(nNumber==1)
			pParticle = new CParticle_Energy ;
		pParticle->Init() ;
		pParticle->SetPosition(fX, fY) ;

		m_ParticleList.push_back(pParticle) ;
	}
}

void CParticleManager::ParticleClear()
{
	const int num=m_ParticleList.size() ;
	CParticle *pParticle ;

	for(int i=0; i<num; i++)
	{
		pParticle = m_ParticleList[i] ;
		delete pParticle ;
	}

	m_ParticleList.clear() ;
}<file_sep>#pragma once

void inelastic_collision(float m1, float m2, float v1, float v2, float modulus) ;
float GetAngle(float x1, float y1, float x2, float y2) ;
float GetAngle2(float angle1, float angle2) ;<file_sep>#include "Particle_Energy.h"
#include "Sprite.h"
#include "Collision.h"
#include "Player.h"

#include "D3dDevice.h"

CParticle_Energy::CParticle_Energy() : m_bGuide(false),
									   m_fForce(1.0f)
{
	m_bTime = true ;
	m_fTime = 1.0f ;
}
CParticle_Energy::~CParticle_Energy()
{
}

void CParticle_Energy::Init()
{
	m_pSprite = new CSprite ;
	m_pSprite->Init("Resource/Image/Energy.png") ;

	float angle = rand()%36 ;
	angle = (angle * 10.0f) * D3DX_PI / 180.0f ;

	m_vecSpeed.x = 1.0f * (rand()%3+1) * cos(angle) ;
	m_vecSpeed.y = 1.0f * (rand()%3+1) * sin(angle) ;
}

void CParticle_Energy::Update()
{
	m_fX += m_vecSpeed.x ;
	m_fY += m_vecSpeed.y ;

	if(!m_bGuide)
	{
		m_vecSpeed += m_vecAcc ;
	}
	else
	{
		if(g_Player->BeGameover())
		{
			m_vecSpeed = m_vecSpeed * 0.0f ;
			return ;
		}

		CCollision col ;
		float angle ;
		float PlayerX = g_Player->GetPositionX() ;
		float PlayerY = g_Player->GetPositionY() ;
		float PlayerS = g_Player->GetScale() * 10.0f ;

		angle = col.GetAngle(m_fX, m_fY, PlayerX, PlayerY) ;
		angle = angle * D3DX_PI / 180.0f ;

		m_vecSpeed.x = m_fForce * cos(angle) ;
		m_vecSpeed.y = m_fForce * sin(angle) ;

		m_fForce += 0.5f * g_D3dDevice->GetMoveTime() ;
		if(m_fForce>25.0f)
			m_fForce = 25.0f ;

		if(col.CircleCollision(m_fX, m_fY, 1.0f, PlayerX, PlayerY, PlayerS))
		{
			m_bLife = false ;
			g_Player->EnergyAbsorption() ;
		}
	}

	if(!m_bGuide)
	{
		m_fTime -= g_D3dDevice->GetTime() ;
		if(m_fTime<0.0f)
			m_bGuide = true ;
	}
}<file_sep>#pragma once

#include "Scene.h"
#include <fmod.hpp>

class CSprite ;

class TitleScene : public Scene
{
private :
	CSprite *m_pBackground ;
	CSprite *m_pTutorial1, *m_pTutorial2 ;
	CSprite *m_pDeveloper ;
	CSprite *m_pBox1, *m_pBox2 ;

	int m_nCursor ;
	int m_nImageBox ;
	float m_fAngle ;

	FMOD::Sound *m_pSoundButton ;
	FMOD::Sound *m_pBGM ;

public :
	static Scene* scene() ;

	TitleScene() ;
	virtual ~TitleScene() ;

	void Init() ;
	void Destroy() ;

	void Update(float dt) ;
	void Render() ;
} ;<file_sep>#pragma once

#include "Box.h"
#include "Singleton.h"

class CPlayer : public CBox, public Singleton<CPlayer>
{
private :
	unsigned int m_nScore ;
	bool m_bGameover ;

public :
	CPlayer() ;
	~CPlayer() ;

	void Init() ;

	void EnergyAbsorption() ;
	void Gameover() ;

	const int GetScore() const ;
	const bool BeGameover() const ;
private :
	void Spin() ;
	void Move() ;
} ;

#define g_Player CPlayer::GetInstance()<file_sep>#include "Data.h"
#include "LoadManager.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

CData::CData() : m_fModulus(0.5f),
				 m_fMass(1.0f),
				 m_fMoveAcc(0.1f), m_fSpinAcc(0.1f),
				 m_bScore(false), m_bTime(false),
				 m_nMaxScore(10000),
				 m_fTimeLimit(60.0f),
				 m_fBoxForceMax(10.0f),
				 m_bBoxCollision(false)
{
}
CData::~CData()
{
}

void CData::Init()
{
	if(!LoadData())
	{
		CreateDataFile() ;
		LoadData() ;
	}
}

bool CData::LoadData()
{
	if( !g_LoadManager->OpenDat("Resource/Game.ini") )
		return false ;

	char item[100] ;
	char temp[100] ;

	while(g_LoadManager->GetItem(item))
	{
		int len = strlen(item) ;

		if(len==7 && strcmp(item, "MODULUS")==0)
		{
			g_LoadManager->GetString(temp) ;
			m_fModulus = (float)strtod(temp, NULL) ;
		}
		else if(len==4 && strcmp(item, "MASS")==0)
		{
			g_LoadManager->GetString(temp) ;
			m_fMass = (float)strtod(temp, NULL) ;
		}
		else if(len==17 && strcmp(item, "MOVE_ACCELERATION")==0)
		{
			g_LoadManager->GetString(temp) ;
			m_fMoveAcc = (float)strtod(temp, NULL) ;
		}
		else if(len==17 && strcmp(item, "SPIN_ACCELERATION")==0)
		{
			g_LoadManager->GetString(temp) ;
			m_fSpinAcc = (float)strtod(temp, NULL) ;
		}
		else if(len==5 && strcmp(item, "SCORE")==0)
		{
			int b ;
			g_LoadManager->GetValue(b) ;
			m_bScore = b ;
		}
		else if(len==9 && strcmp(item, "MAX_SCORE")==0)
		{
			g_LoadManager->GetValue(m_nMaxScore) ;
		}
		else if(len==4 && strcmp(item, "TIME")==0)
		{
			int b ;
			g_LoadManager->GetValue(b) ;
			m_bTime = b ;
		}
		else if(len==10 && strcmp(item, "TIME_LIMIT")==0)
		{
			g_LoadManager->GetString(temp) ;
			m_fTimeLimit = (float)strtod(temp, NULL) ;
		}
		else if(len==13 && strcmp(item, "BOX_FORCE_MAX")==0)
		{
			g_LoadManager->GetString(temp) ;
			m_fBoxForceMax = (float)strtod(temp, NULL) ;
		}
		else if(len==13 && strcmp(item, "BOX_COLLISION")==0)
		{
			int b ;
			g_LoadManager->GetValue(b) ;
			m_bBoxCollision = b ;
		}
	}

	g_LoadManager->CloseDat() ;

	return true ;
}

bool CData::CreateDataFile()
{
	FILE *pFile ;

	pFile = fopen("Resource/Game.ini", "w") ;
	if(pFile==NULL)
		return false ;

	fprintf(pFile, "# modulus of elasticity\n") ;
	fprintf(pFile, "<modulus>\n") ;
	fprintf(pFile, "%f\n", m_fModulus) ;
	fprintf(pFile, "\n") ;

	fprintf(pFile, "# gyrating mass\n") ;
	fprintf(pFile, "<mass>\n") ;
	fprintf(pFile, "%f\n", m_fMass) ;
	fprintf(pFile, "\n") ;

	fprintf(pFile, "# player move/spin acceleration\n") ;
	fprintf(pFile, "<move_acceleration>\n") ;
	fprintf(pFile, "%f\n", m_fMoveAcc) ;
	fprintf(pFile, "\n") ;
	fprintf(pFile, "<spin_acceleration>\n") ;
	fprintf(pFile, "%f\n", m_fSpinAcc) ;
	fprintf(pFile, "\n") ;

	fprintf(pFile, "# scouring mode\n") ;
	fprintf(pFile, "<score>\n") ;
	fprintf(pFile, "%d\n", m_bScore) ;
	fprintf(pFile, "\n") ;
	fprintf(pFile, "<max_score>\n") ;
	fprintf(pFile, "%d\n", m_nMaxScore) ;
	fprintf(pFile, "\n") ;

	fprintf(pFile, "# time limit mode\n") ;
	fprintf(pFile, "<time>\n") ;
	fprintf(pFile, "%d\n", m_bTime) ;
	fprintf(pFile, "\n") ;
	fprintf(pFile, "<time_limit>\n") ;
	fprintf(pFile, "%f\n", m_fTimeLimit) ;

	//
	fprintf(pFile, "\n") ;

	fprintf(pFile, "# Box Speed Force Max\n") ;
	fprintf(pFile, "<box_force_max>\n") ;
	fprintf(pFile, "%f\n", m_fBoxForceMax) ;
	fprintf(pFile, "\n") ;

	fprintf(pFile, "# Box Collision Mode\n") ;
	fprintf(pFile, "<box_collision>\n") ;
	fprintf(pFile, "%d\n", m_bBoxCollision) ;

	fclose(pFile) ;

	return true ;
}<file_sep>#pragma once

#include "Singleton.h"
#include <vector>
#include <fmod.hpp>

class CBox ;

class CBoxManager : public Singleton<CBoxManager>
{
private :
	std::vector<CBox*> m_BoxList ;

	FMOD::Sound *m_pCrash ;

public :
	CBoxManager() ;
	~CBoxManager() ;

	void Init() ;

	void Update() ;

	void Render() ;

	void Clear() ;
private :
	void Collision() ;
	void CreateBox() ;
	void DeleteBox() ;

	void CreateRandomBox() ;
} ;

#define g_BoxManager CBoxManager::GetInstance()<file_sep>#pragma once

typedef struct _Vector
{
	float x ;
	float y ;

	_Vector() : x(0.0f),
				y(0.0f)
	{}
	_Vector(float X, float Y) : x(X),
								y(Y)
	{}

	const _Vector operator+(_Vector vec)
	{
		_Vector temp ;
		temp.x = x + vec.x ;
		temp.y = y + vec.y ;

		return temp ;
	}
	const _Vector operator*(float scalar)
	{
		_Vector temp ;
		temp.x = x * scalar ;
		temp.y = y * scalar ;

		return temp ;
	}
	const _Vector& operator=(_Vector vec)
	{
		x = vec.x ;
		y = vec.y ;

		return *this ;
	}
	const _Vector& operator+=(_Vector vec)
	{
		*this = *this + vec ;

		return *this ;
	}
} Vector ;<file_sep>#include "TitleScene.h"
#include "SceneManager.h"
#include "SampleScene.h"

#include "Keyboard.h"
#include "Mouse.h"
#include "Joystick.h"

#include "Sprite.h"
#include "CameraManager.h"
#include "MusicManager.h"

#include "D3dDevice.h"

TitleScene::TitleScene() : m_pBackground(NULL),
						   m_pTutorial1(NULL), m_pTutorial2(NULL),
						   m_pDeveloper(NULL),
						   m_pBox1(NULL), m_pBox2(NULL),
						   m_nCursor(0),
						   m_nImageBox(0),
						   m_fAngle(0.0f),
						   m_pSoundButton(NULL),
						   m_pBGM(NULL)
{
}
TitleScene::~TitleScene()
{
	if(m_pBackground!=NULL)
		delete m_pBackground ;
	if(m_pTutorial1!=NULL)
		delete m_pTutorial1 ;
	if(m_pTutorial2!=NULL)
		delete m_pTutorial2 ;
	if(m_pDeveloper!=NULL)
		delete m_pDeveloper ;
	if(m_pBox1!=NULL)
		delete m_pBox1 ;
	if(m_pBox2!=NULL)
		delete m_pBox2 ;
}

Scene* TitleScene::scene()
{
	Scene *scene = new TitleScene ;

	return scene ;
}

void TitleScene::Init()
{
	g_CameraManager->AddCamera(new CCamera()) ;

	float fWinWidth = g_D3dDevice->GetWinWidth() ;
	float fWinHeight = g_D3dDevice->GetWinHeight() ;

	m_pBackground = new CSprite ;
	m_pBackground->Init("Resource/Image/Title.png") ;
	m_pBackground->SetPosition(fWinWidth/2.0f, fWinHeight/2.0f) ;

	m_pTutorial1 = new CSprite ;
	m_pTutorial1->Init("Resource/Image/Tutorial1_ImageBox.png") ;
	m_pTutorial1->SetPosition(fWinWidth/2.0f, fWinHeight/2.0f) ;

	m_pTutorial2 = new CSprite ;
	m_pTutorial2->Init("Resource/Image/Tutorial2_ImageBox.png") ;
	m_pTutorial2->SetPosition(fWinWidth/2.0f, fWinHeight/2.0f) ;

	m_pDeveloper = new CSprite ;
	m_pDeveloper->Init("Resource/Image/Developer_ImageBox.png") ;
	m_pDeveloper->SetPosition(fWinWidth/2.0f, fWinHeight/2.0f) ;

	m_pBox1 = new CSprite() ;
	m_pBox1->Init("Resource/Image/Box.png") ;
	m_pBox1->SetRGB(157, 223, 255) ;

	m_pBox2 = new CSprite() ;
	m_pBox2->Init("Resource/Image/Box.png") ;
	m_pBox2->SetRGB(255, 161, 161) ;

	m_pSoundButton = g_MusicManager->LoadMusic("Resource/Sound/es040.wav", false, false) ;
	m_pBGM = g_MusicManager->LoadMusic("Resource/Sound/Raina-Milk_in_Veins.mp3", true) ;
	//m_pBGM = g_MusicManager->LoadMusic("Resource/Sound/Flutterwonder_UP_NOVOX.mp3", true) ;
	g_MusicManager->PlayMusic(m_pBGM, 0) ;
}

void TitleScene::Destroy()
{
}

void TitleScene::Update(float dt)
{
	g_Keyboard->Update() ;
	g_Mouse->Update() ;
	g_Joystick->Update() ;
	g_MusicManager->Loop() ;

	if(m_nImageBox==0 && g_Keyboard->IsPressDown(DIK_UP))
	{
		g_MusicManager->StopMusic(1) ;
		g_MusicManager->PlayMusic(m_pSoundButton, 1) ;

		--m_nCursor ;
		if(m_nCursor<0)
			m_nCursor = 2 ;
	}
	if(m_nImageBox==0 && g_Keyboard->IsPressDown(DIK_DOWN))
	{
		g_MusicManager->StopMusic(1) ;
		g_MusicManager->PlayMusic(m_pSoundButton, 1) ;

		++m_nCursor ;
		if(m_nCursor>2)
			m_nCursor = 0 ;
	}
	if(g_Keyboard->IsPressDown(DIK_RETURN))
	{
		g_MusicManager->StopMusic(1) ;
		g_MusicManager->PlayMusic(m_pSoundButton, 1) ;

		if(m_nCursor==0)
		{
			g_SceneManager->ChangeScene(SampleScene::scene()) ;
			return ;
		}
		else if(m_nCursor==1)
			m_nImageBox = ++m_nImageBox % 3 ;
		else
			m_nImageBox = ++m_nImageBox % 2 ;
	}

	float x, y ;
	float width ;

	switch(m_nCursor)
	{
	case 0 :
		x = 502.0f ;
		y = 397.5f ;
		width = 290.0f / 2.0f ;
		break ;

	case 1 :
		x = 513.0f ;
		y = 515.0f ;
		width = 450.0f / 2.0f ;
		break ;

	case 2 :
		x = 513.0f ;
		y = 636.0f ;
		width = 544.0f / 2.0f ;
		break ;
	}

	y = g_D3dDevice->GetWinHeight() - y ;
	width += 48.0f ;
	m_pBox1->SetPosition(x-width, y) ;
	m_pBox2->SetPosition(x+width, y) ;
	
	m_fAngle += 3.0f * g_D3dDevice->GetMoveTime() ;
	if(m_fAngle>360.0f)
		m_fAngle -= 360.0f ;
	m_pBox1->SetAngle(m_fAngle) ;
	m_pBox2->SetAngle(-m_fAngle) ;
}

void TitleScene::Render()
{
	g_CameraManager->CameraRun() ;

	m_pBackground->Render() ;
	
	m_pBox1->Render() ;
	m_pBox2->Render() ;

	if(m_nImageBox!=0)
	{
		if(m_nCursor==1)
		{
			if(m_nImageBox==1)
				m_pTutorial1->Render() ;
			else if(m_nImageBox==2)
				m_pTutorial2->Render() ;
		}
		else if(m_nCursor==2)
		{
			if(m_nImageBox==1)
				m_pDeveloper->Render() ;
		}
	}
}<file_sep>#include "inelastic_collision.h"
#include <math.h>

// inelastic_collision 비탄성 충돌
// modulus of elasticity 탄성 계수
void inelastic_collision(float m1, float m2, float v1, float v2, float modulus)
{
	float _v1, _v2 ;
	float _v1v2 = modulus * (v1-v2) ;	// _v1 - _v2

	// m1v1 + m2v2 = m1v1' + m1v2'
	//
	// ax + by + c = 0
	// dx + ey + f = 0
	float a=m1, b=m2, c=-((m1*v1)+(m2*v2)) ;
	float d=1.0f, e=-1.0f, f=modulus * (v1-v2) ;

	float temp ;
	temp = a/d ;
	d *= temp ;
	e *= temp ;
	f *= temp ;

	b -= e ;
	c -= f ;
	_v2 = -c/b ;
	_v1 = (-(e*_v2) - f)/d ;
}

float GetAngle(float x1, float y1, float x2, float y2)
{
	/*float angle ;

	float deltaX = x2 - x1 ;
	float deltaY = y2 - y1 ;

	float distance = sqrt(deltaX * deltaX + deltaY * deltaY) ;

	angle = acosf(deltaX/distance) ;

	if(deltaY>0)
	{
		angle = 3.14 + (3.14 - angle) ;
	}*/

	/////

	float angle ;

	angle = atan2(x2-x1, y2-y1) ;
	angle = -(angle * 180.0f) / 3.141592f ;
	angle += 90.0f ;

	if(angle<0.0f)
		angle += 360.0f ;

	return angle ;
}


// angle1 bsg ;
float GetAngle2(float angle1, float angle2)
{
	float angle ;

	angle2 += 180.0f ;
	if(angle2>360.0f)
		angle2 -= 360.0f ;

	angle = angle1 - angle2 ;
	angle = angle1 + angle ;
	if(angle>360.0f)
		angle -= 360.0f ;

	return angle ;
}<file_sep>#include "BoxManager.h"
#include "Box.h"
#include "Player.h"
#include "Collision.h"
#include "ParticleManager.h"
#include "Data.h"

#include "MusicManager.h"

#include "D3dDevice.h"

CBoxManager::CBoxManager() : m_pCrash(NULL)
{
}
CBoxManager::~CBoxManager()
{
	Clear() ;
}

void CBoxManager::Init()
{
	const int num=10 ;

	for(int i=0; i<num; i++)
		CreateRandomBox() ;
	
	m_pCrash = g_MusicManager->LoadMusic("Resource/Sound/Crash.wav", false, false) ;
}

void CBoxManager::Update()
{
	int num=m_BoxList.size() ;

	for(int i=0; i<num; i++)
	{
		if(!m_BoxList[i]->BeLife())
		{
			CBox *pBox = m_BoxList[i] ;
			m_BoxList.erase(m_BoxList.begin() + i) ;
			g_ParticleManager->CreateParticle(pBox->GetPositionX(), pBox->GetPositionY(), 1) ;
			delete pBox ;

			--i ;
			--num ;
			continue ;
		}
		m_BoxList[i]->Update() ;
	}

	Collision() ;

	CreateBox() ;
	DeleteBox() ;
}

void CBoxManager::Render()
{
	const int num=m_BoxList.size() ;

	for(int i=0; i<num; i++)
		m_BoxList[i]->Render() ;
}

void CBoxManager::Clear()
{
	CBox *pBox ;
	const int num=m_BoxList.size() ;

	for(int i=0; i<num; i++)
	{
		pBox = m_BoxList[i] ;
		delete pBox ;
	}
	m_BoxList.clear() ;
}

void CBoxManager::Collision()
{
	const int num=m_BoxList.size() ;
	bool bCol ;

	CCollision collision ;
	
	float PlayerX = g_Player->GetPositionX() ;
	float PlayerY = g_Player->GetPositionY() ;
	float PlayerR = g_Player->GetScale() ;

	for(int i=0; i<num; i++)
	{
		CBox *pBox = m_BoxList[i] ;
		float BoxX = pBox->GetPositionX() ;
		float BoxY = pBox->GetPositionY() ;
		float BoxR = pBox->GetScale() ;

		if(!g_Player->BeGameover())
		{
			bCol = collision.CircleCollision(PlayerX, PlayerY, PlayerR*24.0f, BoxX, BoxY, BoxR*24.0f) ;
			if(bCol)
			{
				collision.InelasticCollision(g_Player, pBox) ;
				g_ParticleManager->CreateParticle(g_Player->GetPositionX(), g_Player->GetPositionY(), 0) ;
				g_MusicManager->PlayMusic(m_pCrash, 1) ;
			}
		}

		if(g_Data->m_bBoxCollision)
		{
			for(int j=0; j<num; j++)
			{
				if(i==j)
					continue ;

				CBox *pBox2 = m_BoxList[j] ;
				float Box2X = pBox2->GetPositionX() ;
				float Box2Y = pBox2->GetPositionY() ;
				float Box2R = pBox2->GetScale() ;
				bCol = collision.CircleCollision(BoxX, BoxY, BoxR*24.0f, Box2X, Box2Y, Box2R*24.0f) ;
				if(bCol)
				{
					collision.InelasticCollision(pBox, pBox2) ;
					g_ParticleManager->CreateParticle(pBox->GetPositionX(), pBox->GetPositionY(), 0) ;
					g_MusicManager->PlayMusic(m_pCrash, 1) ;
				}
			}
		}
	}
}

void CBoxManager::CreateBox()
{
	static float t=0.0f ;
	t += g_D3dDevice->GetTime() ;

	if(t>=0.5f)
	{
		const int num = (int)(t/1.0f) ;
		t -= num * 1.0f ;

		if(m_BoxList.size()<50)
		{
			for(int i=0; i<num; i++)
				CreateRandomBox() ;
		}
	}
}

void CBoxManager::DeleteBox()
{
	int num=m_BoxList.size() ;

	float PlayerX = g_Player->GetPositionX() ;
	float PlayerY = g_Player->GetPositionY() ;

	for(int i=0; i<num; i++)
	{
		CBox *pBox = m_BoxList[i] ;

		float x = PlayerX - pBox->GetPositionX() ;
		float y = PlayerY - pBox->GetPositionY() ;
		float distance = sqrt((x*x) + (y*y)) ;

		if(distance>3000.0f)
		{
			m_BoxList.erase(m_BoxList.begin() + i) ;
			delete pBox ;

			--i ;
			--num ;
		}
	}
}

void CBoxManager::CreateRandomBox()
{
	int length = rand()%10 ;
	int angle = rand()%36 ;

	float x = g_Player->GetPositionX() + (100.0f * length + 664.0f) * cos((angle*10.0f) * D3DX_PI / 180.0f) ;
	float y = g_Player->GetPositionY() + (100.0f * length + 664.0f) * sin((angle*10.0f) * D3DX_PI / 180.0f) ;

	CBox *pBox = new CBox ;
	pBox->Init() ;
	pBox->SetPosition(x, y) ;
	pBox->InitAfterImagePosition() ;

	// 크기 설정
	float Scale = 1.0f - (0.2f * (rand()%4-1)) ;
	Scale *= g_Player->GetScale() ;
	pBox->SetScale(Scale) ;

	// 회전속도 설정
	float Spin = 1.0f - (0.125f * (rand()%5)) ;
	float p = 1.0f - (2.0f * (rand()%2)) ;
	Spin *= Scale * 15.0f * p ;
	pBox->SetFixedSpinSpeed(Spin) ;

	// 리스트에 등록
	m_BoxList.push_back(pBox) ;
}<file_sep>#pragma once

#include "Particle.h"

class CParticle_Debris : public CParticle
{
public :
	CParticle_Debris() ;
	~CParticle_Debris() ;

	void Init() ;
} ;<file_sep>#include "Particle_Debris.h"
#include "Sprite.h"

CParticle_Debris::CParticle_Debris()
{
	m_bTime = true ;
	m_fTime = 1.0f ;
}
CParticle_Debris::~CParticle_Debris()
{
}

void CParticle_Debris::Init()
{
	m_pSprite = new CSprite ;
	m_pSprite->Init("Resource/Image/Debris.png") ;

	m_vecSpeed.x = -1.0f * (rand()%13-6) ;
	m_vecSpeed.y = 2.0f * (rand()%5) ;

	m_vecAcc.x = m_vecSpeed.x * -0.01f ;
	m_vecAcc.y = -0.15f ;
}