Entity update + components

This commit is contained in:
2026-03-24 09:00:23 +01:00
parent 0c0e7c41af
commit 8f2a4e9ecb
11 changed files with 197 additions and 7 deletions

View File

@@ -24,8 +24,7 @@ namespace Game::AGame {
}
void Background::update(float deltaTime) {
if (!mIsActive) return;
return;
/*const bool* state = SDL_GetKeyboardState(nullptr);
if (state[SDL_SCANCODE_P]) {
mTransform.scaleX *= 2.f;

View File

@@ -26,7 +26,6 @@ namespace Game::AGame {
}
void Player::update(float deltaTime) {
if (!mIsActive) return;
//mTransform.rotation += 1.f; // Rotate clockwise for testing
//mTransform.scaleX = 1.f + 1.f * std::sin(RUNNING_TIME() / 0.5f); // Pulsate scale for testing
//mTransform.scaleY = 1.f + 0.5f * std::cos(RUNNING_TIME() / 0.5f); // Pulsate scale for testing

11
src/game/agame/trash.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include <game/agame/trash.hpp>
namespace Game::AGame {
void Trash::start() {
mZIndex = 50;
}
void Trash::update(float deltaTime) {
return;
}
}

View File

@@ -29,9 +29,14 @@ namespace Game {
Input::update(); // Update input states at the start of each frame
auto entities = State::GameState::getInstance().getEntitiesSnapshot();
for (auto* entity : entities) {
if (entity) {
entity->update(seconds);
if (!entity || !entity->isActive()) {
continue;
}
// Update components first
entity->updateComponents(seconds);
entity->update(seconds);
}
} catch (const std::exception& e) {
ERROR("Exception in GameManager thread: " << e.what());
@@ -69,4 +74,5 @@ namespace Game {
mSharedBools.erase(key);
}
}
}

View File

@@ -0,0 +1,43 @@
#include <object/components/component.hpp>
namespace Game::Object::Components {
Component::Component(const Component& other) : mName(other.mName), mIsActive(other.mIsActive) {
LOG("Copied Component: " << mName);
}
Component& Component::operator=(const Component& other) {
if (this != &other) {
mName = other.mName;
mIsActive = other.mIsActive;
}
return *this;
}
Component::Component(Component&& other) noexcept : mName(std::move(other.mName)), mIsActive(other.mIsActive) {
LOG("Moved Component: " << mName);
}
Component& Component::operator=(Component&& other) noexcept {
if (this != &other) {
mName = std::move(other.mName);
mIsActive = other.mIsActive;
}
return *this;
}
void Component::setName(const std::string& name) {
mName = name;
}
std::string Component::getName() {
return mName;
}
void Component::setActive(bool active) {
mIsActive = active;
}
bool Component::isActive() {
return mIsActive;
}
}

View File

@@ -39,7 +39,7 @@ namespace Game::Object {
}
void Entity::render(Game::Renderer::Renderer* renderer, Game::Renderer::RendererConfig config) {
if (!mIsActive || !mTex) return; // Don't render if not active or if there's no texture
if (!mIsVisible || !mTex) return; // Don't render if not visible or if there's no texture
if (!mTex->isTiled()) {
float w, h;
@@ -98,4 +98,13 @@ namespace Game::Object {
}
}
}
void Entity::updateComponents(float deltaTime) {
for (const auto& component : mComponents) {
if (!component || !component->isActive()) {
continue;
}
component->update(deltaTime, this);
}
}
}