Constructor CWeapon::CWeapon()

End Constructor

Destructor CWeapon::~CWeapon()

End Destructor

Sub CWeapon::ResetBullet()
	If agk::GetSpriteExists(m_iBulletSprID) = 0 Then
		m_iBulletSprID = agk::CreateSprite(IMGID_BULLET)
	EndIf
	
	If agk::GetParticlesExists(m_iExplosionPfxID) = 1 Then
		agk::DeleteParticles(m_iExplosionPfxID)
	EndIf
	
	agk::SetSpritePosition(m_iBulletSprID, -100.0f, -100.0f)
	agk::SetSpriteColorAlpha(m_iBulletSprID,0)
	
	m_iActive = 0
	m_fLife = 0
End Sub

Sub CWeapon::DestroyBullet()
	If m_iActive = 1 Then
		Raw As float fX = agk::GetSpriteX(m_iBulletSprID)
		Raw As float fY = agk::GetSpriteY(m_iBulletSprID)
		
		agk::SetSpritePosition(m_iBulletSprID, -100.0f, -100.0f)
		
		Raw As integer explode = agk::CreateParticles(fX,fY)
		
		m_iExplosionPfxID = explode
		
		agk::SetParticlesImage(explode, IMGID_PARTICLES)
		agk::SetParticlesStartZone(explode, -20, 20, -20, 20)
		agk::SetParticlesDirection(explode, 8, 8)
		agk::SetParticlesLife(explode, 2.0f)
		agk::SetParticlesSize(explode, 16)
		agk::SetParticlesAngle(explode, 360)
		agk::SetParticlesFrequency(explode, 120)
		agk::AddParticlesColorKeyFrame(explode, 0.0f, 120, 0, 255, 255)
		agk::AddParticlesColorKeyFrame(explode, 2.0f, 255, 100, 255, 0)
		
		agk::AddParticlesForce(explode, 1.5f, 2.0f, 100, 100)
		agk::AddParticlesForce(explode, 1.5f, 2.0f, 250, 0)
		
		agk::SetParticlesMax(explode, 64)
	EndIf
End Sub

Constructor CPlayer::CPlayer()
	m_iShipSprID = agk::CreateSprite(agk::LoadImage("space/playership.png"))
	
	this->ResetPlayer()
	this->ResetBullets()
End Constructor

Destructor CPlayer::~CPlayer()

End Destructor

Sub CPlayer::SetReferences(pLevel As CLevel PTR, pAliens As CAliens PTR)
	m_refScenery = pLevel
	m_refAliens = pAliens
End Sub

Sub CPlayer::ResetPlayer(iNewGame As integer)
	If iNewGame Then
		m_iScore = 0
		m_iLives = 3
		m_iDelay = 0
		m_iGameOver = 0
		m_iState = 0
	EndIf
	
	agk::SetSpritePosition(m_iShipSprID, 200, 200)
	agk::SetViewOffset(0,0)
	
	agk::SetSpriteVisible(m_iShipSprID, 1)
	
	If agk::GetParticlesExists(m_iEnginePfxID) = 1 Then agk::DeleteParticles(m_iEnginePfxID)
	
	m_iEnginePfxID = agk::CreateParticles(200,208)
	
	agk::SetParticlesImage(m_iEnginePfxID, m_iParticleImgID)
	agk::SetParticlesStartZone(m_iEnginePfxID, 0, 0, 0, 0)
	agk::SetParticlesDirection(m_iEnginePfxID, -50, 0)
	agk::SetParticlesLife(m_iEnginePfxID, 0.3f)
	agk::SetParticlesSize(m_iEnginePfxID, 16)
	agk::SetParticlesAngle(m_iEnginePfxID, 80)
	agk::SetParticlesFrequency(m_iEnginePfxID, 60)
	agk::AddParticlesColorKeyFrame(m_iEnginePfxID, 0.0f, 221, 136, 5, 255)
	agk::AddParticlesColorKeyFrame(m_iEnginePfxID, 0.3f, 221, 136, 5, 0)
	
	If agk::GetParticlesExists(m_iExplosionPfxID) = 1 Then agk::DeleteParticles(m_iExplosionPfxID)
	
	m_iExplosionPfxID = agk::CreateParticles(-1000, -1000)
End Sub

Sub CPlayer::ResetBullets()
	For integer i = 0 To 9
		m_Bullets[i].ResetBullet()
	Next
End Sub

Sub CPlayer::HidePlayer()
	agk::SetSpriteVisible(m_iShipSprID, 0)
	agk::SetParticlesVisible(m_iEnginePfxID, 0)
End Sub

Sub CPlayer::ShowPlayer()
	agk::SetSpriteVisible(m_iShipSprID, 1)
	agk::SetParticlesVisible(m_iEnginePfxID, 1)
End Sub

Sub CPlayer::UpdatePlayer()
	Select Case m_iState
		Case PLAYERSTATE_ALIVE
			this->MovePlayer()
			this->CheckPlayerWithScenery()
			this->CheckPlayerWithAliens()
			this->FireBullets()
			this->UpdateBullets()
			this->CheckBulletsWithAliens()
			
		Case PLAYERSTATE_DEAD
			this->DestroyPlayer()
	End Select
End Sub

Sub CPlayer::MovePlayer()
	Raw As float x = 1.0f
	Raw As float y = 0.0f
	
	If agk::GetViewOffsetX() > 15300.0f Then
		this->HidePlayer()
		m_iGameOver = 1
	EndIf
	
	agk::SetViewOffset(agk::GetViewOffsetX() + x, 0)
	
	Raw As float fScreenX = agk::WorldToScreenX(agk::GetSpriteX(m_iShipSprID))
	
	Raw As float x1 = agk::GetDirectionX()
	Raw As float y1 = agk::GetDirectionY()
	
	x = x + x1 * 2.0f
	y = y + y1 * 2.0f
	
	If fScreenX > 500.0f And x1 > 0.0f Then x = 1.0f
	If fScreenX < 20.0f And x1 < 0.0f Then x = 1.0f
	
	agk::SetSpritePosition(m_iShipSprID, agk::GetSpriteX(m_iShipSprID) + x, agk::GetSpriteY(m_iShipSprID) + y)
	agk::SetParticlesPosition(m_iEnginePfxID, agk::GetParticlesX(m_iEnginePfxID) + x, agk::GetParticlesY(m_iEnginePfxID) + y)
End Sub

Sub CPlayer::CheckPlayerWithScenery()
	If m_refScenery->CheckCollisionWithLevel(m_iShipSprID) Then this->KillPlayer()
End Sub

Sub CPlayer::CheckPlayerWithAliens()
	Raw As CAlien PTR col = m_refAliens->CheckCollisionWithAliens(m_iShipSprID)
	
	If col Then
		Raw As float x,y
		col->GetPosition(&x, &y)
		
		m_refAliens->ExplodeAlien(x,y)
		col->SetPosition(-300.0f, -300.0f)
		this->KillPlayer()
	EndIf
End Sub

Sub CPlayer::CheckBulletsWithAliens()
	For integer i = 0 To 9
		Raw As CAlien PTR col = m_refAliens->CheckCollisionWithAliens(m_Bullets[i].m_iBulletSprID)
		If col Then
			Raw As float x,y
			col->GetPosition(&x, &y)
			
			m_refAliens->ExplodeAlien(x,y)
			col->SetPosition(-300.0f, -300.0f)
			
			m_Bullets[i].m_iActive = 0
			agk::SetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID, 0)
			agk::SetSpritePosition(m_Bullets[i].m_iBulletSprID, -100.0f, -100.0f)
			m_Bullets[i].m_fLife = 0.0f
			
			m_iScore += 15
		EndIf
	Next
End Sub

Sub CPlayer::FireBullets()
	m_fShootDelay = m_fShootDelay + 0.1f
	
	If m_fShootDelay >= 2.0f Then
		For integer i = 0 to 9
			If m_Bullets[i].m_iActive = 0 Then
				agk::PlaySound(SFXID_LASER)
				agk::SetSpritePosition(m_Bullets[i].m_iBulletSprID, agk::GetSpriteX(m_iShipSprID) + 28, agk::GetSpriteY(m_iShipSprID))
				m_Bullets[i].m_iActive = 1
				Exit For
			EndIf
		Next
		
		m_fShootDelay = 0.0
	EndIf
End Sub

Sub CPlayer::UpdateBullets()
	For integer i = 0 to 9
		If m_Bullets[i].m_iActive = 1 Then
			agk::SetSpritePosition(m_Bullets[i].m_iBulletSprID, agk::GetSpriteX(m_Bullets[i].m_iBulletSprID) + 4.5f, agk::GetSpriteY(m_Bullets[i].m_iBulletSprID))
			m_Bullets[i].m_fLife = m_Bullets[i].m_fLife + 0.1f
			
			If m_Bullets[i].m_fLife < 10.0f Then
				If agk::GetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID) < 200 Then
					agk::SetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID, agk::GetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID) + 32)
				EndIf
			EndIf
			
			If m_Bullets[i].m_fLife >= 1.5f Then
				If agk::GetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID) > 32 Then
					agk::SetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID, agk::GetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID) - 32)
				Else
					agk::SetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID, 0)
				EndIf
			EndIf
			
			If m_Bullets[i].m_fLife > 6.0f Then
				agk::SetSpriteColorAlpha(m_Bullets[i].m_iBulletSprID, 0)
				agk::SetSpritePosition(m_Bullets[i].m_iBulletSprID, -100.0f, -100.0f)
				m_Bullets[i].m_fLife = 0.0f
				m_Bullets[i].m_iActive = 0
			EndIf
		EndIf
	Next
End Sub

Sub CPlayer::KillPlayer()
	agk::SetSpriteVisible(m_iShipSprID,0)
	agk::SetParticlesVisible(m_iEnginePfxID,0)
	agk::SetParticlesPosition(m_iExplosionPfxID, agk::GetSpriteX(m_iShipSprID) + 22, agk::GetSpriteY(m_iShipSprID))
	agk::SetParticlesImage(m_iExplosionPfxID, m_iParticleImgID)
	agk::SetParticlesStartZone(m_iExplosionPfxID, 0.0f, 0.0f, 0.0f, 0.0f)
	agk::SetParticlesDirection(m_iExplosionPfxID, -32.0f, -32.0f)
	agk::SetParticlesLife(m_iExplosionPfxID, 2.0f)
	agk::SetParticlesSize(m_iExplosionPfxID, 64)
	agk::SetParticlesAngle(m_iExplosionPfxID, 360)
	agk::SetParticlesFrequency(m_iExplosionPfxID, 120)
	agk::AddParticlesColorKeyFrame(m_iExplosionPfxID, 0.0f, 121, 1, 255, 255)
	agk::AddParticlesColorKeyFrame(m_iExplosionPfxID, 2.0f, 1, 252, 255, 0)
	agk::AddParticlesForce(m_iExplosionPfxID, 1.1f, 2.0f, 0.0f, 250.0f)
	agk::AddParticlesForce(m_iExplosionPfxID, 1.5f, 2.0f, -250.0f, 0.0f)
	agk::SetParticlesMax(m_iExplosionPfxID, 250)
	
	this->DestroyAllBullets()
	
	agk::PlaySound(SFXID_HIT)
	
	Decr m_iLives
	m_iState = 1
End Sub

Sub CPlayer::DestroyAllBullets()
	For integer i = 0 to 9
		m_Bullets[i].DestroyBullet()
	Next
End Sub

Sub CPlayer::DestroyPlayer()
	If agk::GetParticlesMaxReached(m_iExplosionPfxID) And m_iDelay = 0 Then m_iDelay = Int(agk::Timer())
	
	If m_iDelay <> 0 And (agk::Timer() + m_iDelay + 2) <> 0 Then
		this->ResetPlayer()
		this->ResetBullets()
		m_refAliens->ResetAliens()
		
		m_iDelay = 0
		m_iState = 0
	EndIf
	
	If m_iLives = 0 Then
		m_iDelay = 0
		m_iGameOver = 1
	EndIf
End Sub