Files
letnik3koncni-prap/src/object/entity.cpp
2026-05-13 07:58:59 +02:00

110 lines
4.3 KiB
C++

#include <object/entity.hpp>
#include <renderer/renderer.hpp>
#include <renderer/texture.hpp>
#include <object/camera.hpp>
#include <window/window.hpp>
#include <cmath>
namespace Game::Object {
Entity::~Entity() = default;
Entity::Entity(const Entity& other) : mName(other.mName), mTex(other.mTex), mTransform(other.mTransform), mIsActive(other.mIsActive) {
LOG("Kopirana entiteta: " << mName);
}
Entity& Entity::operator=(const Entity& other) {
if (this != &other) {
mName = other.mName;
mTex = other.mTex;
mTransform = other.mTransform;
mIsActive = other.mIsActive;
}
return *this;
}
Entity::Entity(Entity&& other) noexcept : mName(std::move(other.mName)), mTex(other.mTex), mTransform(other.mTransform), mIsActive(other.mIsActive) {
other.mTex = nullptr;
LOG("Premaknjena entiteta: " << mName);
}
Entity& Entity::operator=(Entity&& other) noexcept {
if (this != &other) {
mName = std::move(other.mName);
mTex = other.mTex;
mTransform = other.mTransform;
mIsActive = other.mIsActive;
other.mTex = nullptr;
}
return *this;
}
void Entity::render(Game::Renderer::Renderer* renderer, Game::Renderer::RendererConfig config) {
if (!mIsVisible || !mTex) return; // Don't render if not visible or if there's no texture
if (!mTex->isTiled()) {
float w, h;
SDL_GetTextureSize(mTex->getSDLTexture(), &w, &h);
SDL_FRect dst;
dst.w = w * mTransform.scaleX * UNIVERSAL_SCALE_COEFFICIENT;
dst.h = h * mTransform.scaleY * UNIVERSAL_SCALE_COEFFICIENT;
// Top-left origin; Account for camera position (center the camera on the screen)
dst.x = mTransform.x - config.camX + config.screenW / 2.f;
dst.y = mTransform.y - config.camY + config.screenH / 2.f;
SDL_FPoint center;
center.x = dst.w / 2.f;
center.y = dst.h / 2.f;
SDL_RenderTextureRotated(
renderer->getSDLRenderer(),
mTex->getSDLTexture(),
nullptr,
&dst,
mTransform.rotation,
&center,
mIsFlipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE
);
} else {
// Tiled rendering - render the texture repeatedly to fill the area defined by the entity's transform
// We assume that we always render scaleX by scaleY tiles, and that the texture should be rendered at its original size (i.e., the scaleX and scaleY of the entity only affect how many times the texture is tiled, not the size of each tile)
float tileW, tileH;
SDL_GetTextureSize(mTex->getSDLTexture(), &tileW, &tileH);
SDL_FRect dst;
dst.w = tileW * mTiledScale;
dst.h = tileH * mTiledScale;
// Top-left origin; Account for camera position (center the camera on the screen)
dst.x = mTransform.x - config.camX + config.screenW / 2.f;
dst.y = mTransform.y - config.camY + config.screenH / 2.f;
for (int i = 0; i < mTransform.scaleX; i++) {
for (int j = 0; j < mTransform.scaleY; j++) {
SDL_RenderTextureRotated(
renderer->getSDLRenderer(),
mTex->getSDLTexture(),
nullptr,
&dst,
mTransform.rotation,
nullptr, // No rotation center since each tile is rendered independently
mIsFlipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE
);
dst.y += dst.h; // Move down for the next tile in the column
}
dst.y = mTransform.y - config.camY + config.screenH / 2.f; // Reset y to the top of the column
dst.x += dst.w; // Move right for the next column of tiles
}
}
}
void Entity::updateComponents(float deltaTime) {
for (const auto& component : mComponents) {
if (!component || !component->isActive()) {
continue;
}
component->update(deltaTime, this);
}
}
}