コード例 #1
0
ファイル: System.cpp プロジェクト: EnacheB/Gravity
/**
 * Handles collision logic between a ship and a projectile
 */
void System::Collide(Ship& ship, Projectile& proj) {
    if(proj.getMass() == 0 || ship.getMass() == 0) return;

    double dx = proj.getPosition().x-ship.getPosition().x;
    double dy = proj.getPosition().y-ship.getPosition().y;
    double separationSquared = dx*dx+dy*dy;
    //May be inaccurate with verlet
    double totalDamageRange = proj.getCollisionRange() + ship.getCollisionRange();
    if(separationSquared < (totalDamageRange * totalDamageRange))
    {
        ship.Damage(proj.getDamage());
        proj.Destroy();
    }

}
コード例 #2
0
ファイル: Projectile.cpp プロジェクト: shovelware/MySides
bool Projectile::collide(Entity * other, b2Contact& contact, std::string tag)
{
	bool handled = false;
	
	//Get fixture string
	std::string tig = std::string(static_cast<char*>(contact.GetFixtureA()->GetUserData()));

	//If it's the other, get what we are in contact
	if (tig == tag)
		tig = std::string(static_cast<char*>(contact.GetFixtureB()->GetUserData()));

	//If it's our tracking, don't bother
	if (tig == "projtracking")
	{
		handled = true;
	}

	else if (tag == "player" || tag == "enemy" || tag == "shape")////
	{
		Shape* shape = static_cast<Shape*>(other);
		//Projectiles do get hurt by their friends
		if (faction_ != shape->getFaction() && shape->getFaction() != GOD)
		{
			//Onehit projectiles die on hit, precedence over all
			if (oneHit_)
			{
				alive_ = false;
			}

			//If we can't penetrate
			if (penetration_-- <= 0)
			{
				takeDamage(1);
				if (size_.y > 0)
				{
					//b2Vec2 vel = body_->GetLinearVelocity();
					//vel *= (0.1* size_.x * size_.y);
					//
					//body_->SetLinearVelocity(vel);
				}
			}

			//Do our force on penetration
			else
			{
				lastPen_ = b2Vec3(body_->GetPosition().x, body_->GetPosition().y, 1);
			}

			if (alive_ == false)
				contact.SetEnabled(false);
		}

		else contact.SetEnabled(false);

		handled = true;
	}

	else if (tag == "projectile")
	{
		Projectile* proj = static_cast<Projectile*>(other);
		//If we're not friends
		if (faction_ != proj->getFaction())
		{
			//If we can't penetrate
			if (--penetration_ < 0)
			{
				takeDamage(proj->getDamage());

				if (alive_ == false)
					contact.SetEnabled(false);
			}

			//Do our force on penetration
			else
			{
				lastPen_ = b2Vec3(body_->GetPosition().x, body_->GetPosition().y, 1);
			}
			//Maybe set contact to false so stronger proj continues?
		}

		//Else we must have same owner, do not interact
		else contact.SetEnabled(false);

		handled = true;
	}

	else if (tag == "pickup")
	{
		contact.SetEnabled(false);
		handled = true;
	}

	else if (tag == "shield")
	{
		Pickup::Shield* shield = static_cast<Pickup::Shield*>(other);

		//If we don't own the shield
		if (shield->getOwner() != owner_)
		{
			//Penetrate if we can
			if (penetration_ > 0)
			{
				//Shields block more pen
				penetration_ -= shield->getStrength();
			}
		
			//if (contact.IsEnabled())
			//{
			//	b2Vec2 back = body_->GetLinearVelocity();
			//	back *= -0.5;
			//	body_->SetLinearVelocity(back);
			//}
		}
		
		handled = true;
	}

	else if (tag == "bounds")
	{
		takeDamage(1);
		handled = true;
	}

	return handled;
}