Exemple #1
0
void CSWeapon::OnCollision(float deltaTime, std::vector<CGameObject*>* listObjectCollision)
{
#pragma region XU_LY_VA_CHAM
	float normalX = 0;
	float normalY = 0;
	float moveX = 0.0f;
	float moveY = 0.0f;
	float timeCollision;

	for (std::vector<CBullet*>::iterator it = CPoolingObject::GetInstance()->m_listBulletOfObject.begin(); it != CPoolingObject::GetInstance()->m_listBulletOfObject.end();)
	{
		CBullet* obj = *it;
		if(obj && obj->GetLayer() == LAYER::PLAYER)
		{
			timeCollision = CCollision::GetInstance()->Collision(this, obj, normalX, normalY, moveX, moveY, deltaTime);
			if ((timeCollision > 0.0f && timeCollision < 1.0f) || timeCollision == 2.0f)
			{
				//trên - xuống normalY = 1
				//Trái phải normalX = -1
				// ==0 ko va chạm theo Y, X	
				CExplosionEffect* eff = CPoolingObject::GetInstance()->GetExplosionEffect();
				eff->SetAlive(true);
				eff->SetPos(this->m_pos);

				//
				it = CPoolingObject::GetInstance()->m_listBulletOfObject.erase(it);
				//
				CBulletItem* bulletItem = CPoolingObject::GetInstance()->GetBulletItem();
				bulletItem->SetAlive(true);
				bulletItem->SetPos(this->m_pos);
				bulletItem->m_stateItem = this->m_stateItem;
			
				this->m_isALive = false;
			}
			else
			{
				++it;
			}
		}
		else
		{
			++it;
		}
	}
#pragma endregion 
}
Exemple #2
0
void CSoldier::OnCollision(float deltaTime, std::vector<CGameObject*>* listObjectCollision)
{
	float normalX = 0;
	float normalY = 0;
	float moveX = 0.0f;
	float moveY = 0.0f;
	float timeCollision;

	for (std::vector<CBullet*>::iterator it = CPoolingObject::GetInstance()->m_listBulletOfObject.begin(); it != CPoolingObject::GetInstance()->m_listBulletOfObject.end();)
	{
		CBullet* obj = *it;
		timeCollision = CCollision::GetInstance()->Collision(obj, this, normalX, normalY, moveX, moveY, deltaTime);
		if((timeCollision > 0.0f && timeCollision < 1.0f) || timeCollision == 2.0f)
		{
			if(obj->IsAlive() && obj->GetLayer() == LAYER::PLAYER)
			{
				// Gan trang thai die cho doi tuong
				this->m_stateCurrent = SOLDIER_STATE::S_IS_DIE;
				// Xoa vien dan ra khoi d.s
				it = CPoolingObject::GetInstance()->m_listBulletOfObject.erase(it);
				//Load sound die
				ManageAudio::GetInstance()->playSound(TypeAudio::ENEMY_DEAD_SFX);
				// Tang diem cua contra len
				CContra::GetInstance()->IncreateScore(500);
			}
			else
				++it;
		}
		else
		{
			++it;
		}
	}

	if (this->m_stateCurrent != SOLDIER_STATE::S_IS_DIE)
	{
			//Kiem tra va cham voi ground
		bool checkColWithGround = false;
		// xet va cham vs ground
		for (std::vector<CGameObject*>::iterator it = listObjectCollision->begin(); it != listObjectCollision->end(); it++)
		{
			CGameObject* obj = *it;
			//Lay thoi gian va cham
			//Neu doi tuong la ground va dang va cham
			if(((obj->GetIDType() == 15 && obj->GetID() == 1) || (obj->GetIDType() == 15 && obj->GetID() == 8) || (obj->GetIDType() == 16 && obj->GetID() == 1)) && !checkColWithGround)
			{
				timeCollision = CCollision::GetInstance()->Collision(this, obj, normalX, normalY, moveX, moveY, deltaTime);
				if((timeCollision > 0.0f && timeCollision < 1.0f) || timeCollision == 2.0f)
				{
					if(normalY > 0)
					{

						checkColWithGround = true;
						if (this->m_stateCurrent == SOLDIER_STATE::S_IS_JUMP)
						{
							if (this->m_vy < -200.0f)
							{
								this->m_stateCurrent = SOLDIER_STATE::S_IS_JOGGING;
								if( timeCollision == 2.0f)
								{
									//this->m_isJumping = false;
									this->m_pos.y += moveY;
									this->m_vy = 0;
									this->m_a = 0;
								}
							}
						}
						else
						{
							this->m_stateCurrent = SOLDIER_STATE::S_IS_JOGGING;
							if( timeCollision == 2.0f)
							{
								//this->m_isJumping = false;
								this->m_pos.y += moveY;
								this->m_vy = 0;
								this->m_a = 0;
							}
						}
					}
				}
			}
		}

		if(!checkColWithGround)
		{
			this->m_a = -700.0f;
			
			if (this->m_jump)
			{
				if(this->m_vy == 0.0f)
					this->m_vy = this->m_vyDefault;
				this->m_stateCurrent = SOLDIER_STATE::S_IS_JUMP;
			}
			else
			{
				// Soldier quay dau nguoc lai.
				this->m_left = !this->m_left;
				this->m_countRepeat ++;
				if (m_countRepeat > 2)
				{
					this->m_countRepeat = 0;
					this->m_jump = true;
				}
				// Xet gia tri tiep theo la nhay.
				//this->m_jump = true;
			}
		}
	}
	
}