Example #1
0
void CEntity::OnMove(float fTime, CMap* pMap)
{
	float MovementAtom = 1.0f / BLOCK_SIZE;

	if ((0.1f > Vel.X) && (Vel.X > -0.1f)) {
		Vel.X = Mov.X = 0;
	}
	if ((0.1f > Vel.Y) && (Vel.Y > -0.1f)) {
		Vel.Y = Mov.Y = 0;
	}

	if (CanMove(CVector(0, MovementAtom), pMap))
		Vel += CVector(0, 9.81f * fTime * 8);

	if (Vel != CVector(0, 0)) {

		Vel *= pow(0.125f, fTime * 4);

		Mov += Vel * fTime;
		int X = abs(Mov.X * BLOCK_SIZE), Y = abs(Mov.Y * BLOCK_SIZE);

		int Xdir = 0, Ydir = 0;
		if (X > 0)
			Xdir = (int) (Mov.X * BLOCK_SIZE) / abs(Mov.X * BLOCK_SIZE);
		if (Y > 0)
			Ydir = (int) (Mov.Y * BLOCK_SIZE) / abs(Mov.Y * BLOCK_SIZE);

		while(X > 0 || Y > 0) {

			if (X > 0) {
				if (CanMove(CVector(Xdir * MovementAtom, 0), pMap)) {
					Pos.X += MovementAtom * Xdir;
					Mov.X -= Xdir * MovementAtom;
					--X;
				} else {
					X = 0;
					Vel.X = Mov.X = 0;
				}
			}
			if (Y > 0) {
				if (CanMove(CVector(0, Ydir * MovementAtom), pMap)) {
					Pos.Y += MovementAtom * Ydir;
					Mov.Y -= Ydir * MovementAtom;
					--Y;
				} else {
					Y = 0;
					Vel.Y = Mov.Y = 0;
				}
			}
		}
	}

	if (AttackTimer > 0) {
		AttackTimer -= fTime;
		if (AttackTimer < 0)
			AttackTimer = 0.0f;
	}

	if(!IsCollectible()) { //Collect Things
		PtrList<CEntity*> CollectibleList = pMap->GetEntitiesInRadius(Pos, 1);
		for(Uint16 i=0;i<CollectibleList.size();i++) {
			if(!CollectibleList[i]->IsCollectible())
				continue;
			else CollectibleList[i]->OnCollect(this, pMap);
		}
	}
}
Example #2
0
void Item::UpdatePlayerMagnet(float dt)
{
	if(IsItemPickedUp())
	{
		if(m_disappear)
		{
			m_renderScale = m_disappearScale;

			if(m_disappearTimer <= 0.0f)
			{
				if(m_disappearAnimationStarted == false)
				{
					Interpolator::GetInstance()->AddFloatInterpolation(&m_disappearScale, m_disappearScale, 0.0f, 0.5f, -100.0f, NULL, _PickupAnimationFinished, this);

					m_disappearAnimationStarted = true;
				}
			}
		}
		else
		{
			vec3 diff = m_pickupPos - m_position;
			float lengthSize = length(diff);

			if(lengthSize < 0.01f)
			{
				m_disappear = true;
				m_disappearTimer = m_disappearDelay;
				m_disappearScale = m_renderScale;
			}
			else
			{
				diff = normalize(diff);

				m_position += diff * lengthSize*0.25f;
			}
		}
	}
	else
	{
		if(IsCollectible())
		{
			if(m_droppedInventoryItem == NULL || m_pInventoryManager->CanAddInventoryItem(GetItemTitle(), GetItemType(), 1))
			{
				float yAdditionalMaagnetOffset = 0.5f;

				// Magnet towards the player
				if(m_pPlayer->IsDead() == false && length(m_pPlayer->GetCenter() - GetCenter()) < m_pPlayer->GetRadius() + 4.0f)
				{
					vec3 toPlayer = (m_pPlayer->GetCenter()+vec3(0.0f, yAdditionalMaagnetOffset, 0.0f)) - GetCenter();
					SetGravityDirection(toPlayer);
					SetVelocity(normalize(toPlayer)*(20.0f/length(toPlayer)));
					SetWorldCollide(false);
				}
				else
				{
					SetGravityDirection(vec3(0.0f, -1.0f, 0.0f));
					SetWorldCollide(true);
				}

				// Check if we have been picked up by the player
				if(m_pPlayer->IsDead() == false && length((m_pPlayer->GetCenter()+vec3(0.0f, yAdditionalMaagnetOffset, 0.0f)) - GetCenter()) < m_pPlayer->GetRadius())
				{
					SetPickupGotoPosition(m_pPlayer->GetCenter() + vec3(0.0f, 2.5f, 0.0f));
					SetVelocity(vec3(0.0f, 0.0f, 0.0f));
					SetGravityDirection(vec3(0.0f, 0.0f, 0.0f));

					if(m_itemType == eItem_Heart)
					{
						m_pPlayer->GiveHealth(10.0f);
					}

					if(m_itemType == eItem_Coin)
					{
						m_pPlayer->GiveCoins(1);
					}

					if(m_droppedInventoryItem != NULL)
					{
						m_pInventoryManager->AddInventoryItem(m_droppedInventoryItem, -1, -1);
					}
				}
			}
		}
	}
}