box collider

This commit is contained in:
2026-04-13 14:03:59 +02:00
parent 0f776347f4
commit dc55af5323
8 changed files with 67 additions and 5 deletions

View File

@@ -10,6 +10,6 @@ GAME_ENTITY(Player)
private:
Object::Sound mSound;
float mSpeed = 200.f; // Pixels per second
float mHealth = 100.f;
[[maybe_unused]] float mHealth = 100.f;
END_GAME_ENTITY()
}

View File

@@ -1,8 +1,13 @@
#pragma once
#include <object/components/component.hpp>
#include <state/gamestate.hpp>
namespace Game::Object::Components {
struct BoxColliderBounds {
float left, right, top, bottom;
};
class BoxCollider : public Component {
public:
BoxCollider() = default;
@@ -15,6 +20,11 @@ namespace Game::Object::Components {
void start(Object::Entity* thisEntity) override;
void update(float deltaTime, Object::Entity* thisEntity) override;
BoxColliderBounds getBounds() const { return mBounds; }
bool isColliding() const { return mIsColliding; }
private:
BoxColliderBounds mBounds;
bool mIsColliding = false; // Whether this collider is currently colliding with another collider; used to determine whether to call onCollisionEnter vs onCollisionStay, and to call onCollisionExit when collisions end
};
}

View File

@@ -35,6 +35,14 @@ namespace Game::Object {
virtual void destroyed() {}; // Pre-destruction call
void render(Game::Renderer::Renderer* renderer, Game::Renderer::RendererConfig config);
// Collision calls
// Called once when a collision begins
virtual void onCollisionEnter(Entity* other) {}
// Called every update while a collision continues
virtual void onCollisionStay(Entity* other) {}
// Called once when a collision ends
virtual void onCollisionExit(Entity* other) {}
// Setters and getters
void setTexture(std::shared_ptr<Game::Renderer::Texture> tex) { mTex = tex; }
void setName(const std::string& name) { mName = name; }

View File

@@ -26,8 +26,8 @@ namespace Game::State {
// Update entity at index, by REFERENCE.
Object::Entity* getAtIndex(size_t at);
// Add an entity to the gamestate.
void addEntity(std::unique_ptr<Object::Entity> entity);
// Add an entity to the gamestate; Returns a temporary pointer to the entity added. Note that the validity of this pointer cannot be guaranteed after the first call.
Object::Entity* addEntity(std::unique_ptr<Object::Entity> entity);
bool removeEntity(const std::string& name);
private:

View File

@@ -3,6 +3,7 @@
#include <iostream>
#include <SDL3/SDL.h>
#include <shared_mutex>
#include <mutex>
#include <random>
#include <cstdlib>