Exemple #1
0
void Bullet::Update(float time) {
	if (name == DIE) {
		DieAnimation(time);
	}
	else if (health <= 0) {
		InitDie();
		return;
	}
	if (speed == DEFAULT_BULLET_SPEED) {
		speed = time;
	}
	float distance = sqrt((temp.x - boost.x)*(temp.x - boost.x) + (temp.y - boost.y)*(temp.y - boost.y));
	rect.left += (temp.x - boost.x) / distance * speed * GET_HALF;
	rect.top +=  (temp.y - boost.y) / distance * speed * GET_HALF;

	if (rect.left <= 0) rect.left = 1;
	if (rect.top <= 0) rect.top = 1;

	for (unsigned int i = 0; i < obj.size(); i++) {
		if (getRect().intersects(obj[i].rect)) {
			health = 0;
		}
	}
	sprite.setPosition(rect.left + rect.width / GET_HALF, rect.top + rect.height / GET_HALF);
}
void NautComponent::Hit(Vec2 position)
{
	// Subtracts from hp and sets direction away from the given position.

	if (hp > 0)
	{
		Vec2 newDirection = parent->transform.GetPosition() - position;
		if (newDirection.length() > 0) newDirection.normalize();
		parent->GetComponent<Rigidbody>("Rigidbody")->SetVelocity(newDirection * 3);
		--hp;

		if (hp <= 0)
		{
			if (parent->HasTag("Astronaut"))
				DieAnimation(0, 12, 7);
			else if (parent->HasTag("Cosmonaut"))
				DieAnimation(10, 15, 6);
		}
	}
}