Player Collisions
Damaging and Collecting
The Player node now incorporates a child RectangleCollider node, intentionally sized slightly smaller than its sprite. Collision detection with the tilemap now relies on this collider's dimensions and position, resulting in more precise hitboxes compared to the sprite's visual bounds. This collider is also used to detect interactions with other mushrooms and bananas. Upon colliding with a falling mushroom, the mushroom is destroyed, and the player receives a small vertical bounce. When a banana is hit, it's considered collected, and a collection sound is triggered. This sound is managed by the player Node to ensure its complete playback, as the banana Node is immediately destroyed upon collection, which would prematurely terminate the sound if played from the banana itself. The player's longer lifespan guarantees the sound finishes.
// -----
#include "engine/rectangle_collider.h" // Add: Include RectangleCollider.
class Player : public ng::Node {
public:
// -----
// --- Add: Velocity getter and TakeDamage. ---
sf::Vector2f GetVelocity() const;
void TakeDamage();
// --- End ---
// -----
private:
// -----
// --- Add: Store the collider so that it's possible to check for collisions and death state and banana sound. ---
const ng::RectangleCollider* collider_ = nullptr;
bool is_dead_ = false;
sf::Sound banana_sound_;
// --- End ---
};Last updated