⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collision.cpp

📁 射击小游戏
💻 CPP
字号:
#include "stdafx.h"
#include "ShootView.h"
#include "shoot.h"
#include "Collision.h"
#include "EnemySystem.h"
#include<windows.h>
#include<mmsystem.h>
//**************************************************
//碰撞检测系统实现文件
//**************************************************
extern CShootApp theApp;

CollisionSystem::CollisionSystem()
{

}
//更新函数
//Update()负责检查这些类之间的碰撞检测。使用几个循环遍历所有的类对象,调用碰撞检测函数,
//来做碰撞检测。碰撞检测函数负责监测碰撞情况,根据是否发生碰撞来做相应的处理(例如减少生命值,补血等)。
void CollisionSystem::Update()
{
	CShootView * pView = (CShootView*)((CFrameWnd*)(theApp.m_pMainWnd))->GetActiveView();
	//获得主控程序的各个对象系统的指针
	pPlayer				 =  pView->m_pPlayer;
	pEnermySystem		 =  pView->m_pEnermySystem;
	pPlayerBulletSystem	 =  pView->m_pPlayerBulletSystem;
	pEnermyBulletSystem	 =  pView->m_pEnermyBulletSystem;
	pBaoSys  =  pView->m_pBaoSys;
	//声明临时变量
	Enermy	*pEnermy;
	Bullet	*pBullet;
	
	// 遍历敌机
	pEnermy  =  pEnermySystem->GetFirstEnermy();
	while(pEnermy)
	{
		// 检查敌机与玩家战机的碰撞
		CheckForCollision(pPlayer, pEnermy);

		// 检查敌机与玩家子弹的碰撞
		pBullet  =  pPlayerBulletSystem->GetFirstBullet();
		while(pBullet)
		{
			CheckForCollision(pEnermy, pBullet);
			pBullet  =  pPlayerBulletSystem->GetNextBullet();
		}

		pEnermy  =  pEnermySystem->GetNextEnermy();
	}

	// 遍历敌人的子弹
	pBullet  =  pEnermyBulletSystem->GetFirstBullet();
	while(pBullet)
	{
		// 检查玩家与敌人子弹的碰撞
		CheckForCollision(pPlayer, pBullet);
		pBullet  =  pEnermyBulletSystem->GetNextBullet();
	}
	Bao*	pBao;
	pBao  = (Bao*)pBaoSys->GetFirstBao();
	while(pBao)
	{
		CheckForCollision(pBao,pPlayer);
		pBao  =  (Bao*)pBaoSys->GetNextBao();
	}
}

// 碰撞检测:敌机与玩家子弹
//碰撞检测采用矩形检测法
//碰撞成立条件:
//战斗机的最左边坐标小于子弹的最右边坐标&&
//战斗机的最右边坐标大于子弹的最左边坐标&&
//战斗机的最下边坐标大于子弹的最上边坐标&&
//战斗机的最上边坐标小于子弹的最下边坐标
void CollisionSystem::CheckForCollision(Enermy *pFighterPlane, Bullet *pBullet)
{
	CShootView * pView = (CShootView*)((CFrameWnd*)(theApp.m_pMainWnd))->GetActiveView();

	pPlayer				 =  pView->m_pPlayer;
	pEnermySystem		 =  pView->m_pEnermySystem;
	pPlayerBulletSystem	 =  pView->m_pPlayerBulletSystem;
	pEnermyBulletSystem	 =  pView->m_pEnermyBulletSystem;

	if( pFighterPlane->m_iLife <= 0 || pBullet->m_iLife <= 0 ) return;
	if( pFighterPlane->GetX() + pFighterPlane->cx + pFighterPlane->cw > pBullet->GetX() + pBullet->cx &&
		pFighterPlane->GetY() + pFighterPlane->cy + pFighterPlane->ch > pBullet->GetY() + pBullet->cy &&
		pFighterPlane->GetX() + pFighterPlane->cx < pBullet->GetX() + pBullet->cx + pBullet->cw &&
		pFighterPlane->GetY() + pFighterPlane->cy < pBullet->GetY() + pBullet->cy + pBullet->ch )
	{
		//sndPlaySound("BOMB.WAV",SND_SYNC);
//		sndPlaySound(MAKEINTRESOURCE(IDR_WAVE1),SND_SYNC |SND_LOOP | SND_NODEFAULT);
		int temp  =  pFighterPlane->m_iLife;
		pFighterPlane->m_iLife -= pBullet->m_iAttackness;
		pBullet->m_iLife = 0;
		pPlayer->m_iScore += temp;
		if(pFighterPlane->m_iLife <= 0 )
			pPlayer->m_iKillEnermy++;
	}
}

// 碰撞检测:玩家与敌方子弹
void CollisionSystem::CheckForCollision(Player *pFighterPlane, Bullet *pBullet)
{
	CShootView * pView = (CShootView*)((CFrameWnd*)(theApp.m_pMainWnd))->GetActiveView();

	pPlayer				= pView->m_pPlayer;
	pEnermySystem		= pView->m_pEnermySystem;
	pPlayerBulletSystem	= pView->m_pPlayerBulletSystem;
	pEnermyBulletSystem	= pView->m_pEnermyBulletSystem;

	if( pFighterPlane->m_iLife <= 0 || pBullet->m_iLife <= 0 ) return;
	if( pFighterPlane->GetX() + pFighterPlane->cx + pFighterPlane->cw > pBullet->GetX() + pBullet->cx &&
		pFighterPlane->GetY() + pFighterPlane->cy + pFighterPlane->ch > pBullet->GetY() + pBullet->ch &&
		pFighterPlane->GetX() + pFighterPlane->cx < pBullet->GetX() + pBullet->cx + pBullet->cw &&
		pFighterPlane->GetY() + pFighterPlane->cy < pBullet->GetY() + pBullet->cy + pBullet->ch )
	{
		pFighterPlane->m_iLife -= pBullet->m_iAttackness;
		if((pFighterPlane->m_iLife<=0)&&(pFighterPlane->m_iChances>=0))
		{
			pFighterPlane->m_iChances--;
			pFighterPlane->m_iLife = 20;			
		}
//		if(pFighterPlane->Life<=0)
//			sndPlaySound("BOMB.WAV",SND_SYNC);
		pBullet->m_iLife = 0;
	}
}

// 碰撞检测:玩家与敌机
void CollisionSystem::CheckForCollision(Player *pPlayer, Enermy *pEnermy)
{
	CShootView * pView = (CShootView*)((CFrameWnd*)(theApp.m_pMainWnd))->GetActiveView();

	pPlayer				= pView->m_pPlayer;
	pEnermySystem		= pView->m_pEnermySystem;
	pPlayerBulletSystem	= pView->m_pPlayerBulletSystem;
	pEnermyBulletSystem	= pView->m_pEnermyBulletSystem;
	int temp = pEnermy->m_iLife;
	if( pPlayer->m_iLife <= 0 || pEnermy->m_iLife <= 0 ) return;
	if( pPlayer->GetX() + pPlayer->cx + pPlayer->cw > pEnermy->GetX() + pEnermy->cx &&
		pPlayer->GetY() + pPlayer->cy + pPlayer->ch > pEnermy->GetY() + pEnermy->ch &&
		pPlayer->GetX() + pPlayer->cx < pEnermy->GetX() + pEnermy->cx + pEnermy->cw &&
		pPlayer->GetY() + pPlayer->cy < pEnermy->GetY() + pEnermy->cy + pEnermy->ch )
	{
		pPlayer->m_iLife -= pView->GetLevel()*2;
//		if(pPlayer->Life<=0)
//			sndPlaySound("BOMB.WAV",SND_SYNC);
		if((pPlayer->m_iLife<=0)&&(pPlayer->m_iChances>=0))
		{
			pPlayer->m_iChances--;
			pPlayer->m_iLife=20;		
		}
		pPlayer->m_iKillEnermy++;
		pPlayer->m_iScore += temp;
		pEnermy->m_iLife = 0;
	}
}
void CollisionSystem::CheckForCollision(Bao* pBao,Player* pPlayer)
{
	CShootView * pView = (CShootView*)((CFrameWnd*)(theApp.m_pMainWnd))->GetActiveView();

	pPlayer	= pView->m_pPlayer;
	pBaoSys = pView->m_pBaoSys;

	if( pPlayer->m_iLife <= 0 || pBao->m_iLife <= 0 ) return;
	if( pPlayer->GetX() + pPlayer->cx + pPlayer->cw > pBao->GetX() + pBao->cx &&
		pPlayer->GetY() + pPlayer->cy + pPlayer->ch > pBao->GetY() + pBao->ch &&
		pPlayer->GetX() + pPlayer->cx < pBao->GetX() + pBao->cx + pBao->cw &&
		pPlayer->GetY() + pPlayer->cy < pBao->GetY() + pBao->cy + pBao->ch )
	{
		if(pBao->m_iType == Bao::BAO_BLOOD)
		{
			pPlayer->m_iLife+=10;
		}
		if(pBao->m_iType == Bao::BAO_WEAPON)
		{
			switch(rand()%7)//获得散弹的概率是1/7
			{
			case 0:
			case 3:
			case 5:
				pPlayer->BulletType = PLAYER_MISSILE;
				break;
			case 1:
				pPlayer->BulletType = PLAYER_ROUND;
				break;
			case 2:
			case 4:
			case 6:
				pPlayer->BulletType = PLAYER_FIRE;
				break;
			}
		}
		
		pBao->m_iLife = 0;
		pBao->m_bUsed = false;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -