0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 07:53:43 +00:00
SDL_Minigame/src/SpriteComponent.cpp
freezarite cbd1993c20 Extra map for MapTiles generated by TMX file now works like intended.
removed textures map from AssetManager as it is no longer used.
Updated SpriteComponent to now check if it is a normal Texture or a MapTileTexture.
Added if condition in TextureManager::LoadMapTileTexture to check if the texture was able to be loaded by SDL
2024-11-30 15:42:56 +01:00

115 lines
3.1 KiB
C++

#include "SpriteComponent.h"
#include <SDL_timer.h>
#include <cstring>
#include <memory>
#include <magic_enum/magic_enum.hpp>
#include "AnimationHandler.h"
#include "Direction.h"
#include "ProjectileComponent.h"
#include "RenderObject.h"
#include "TextureManager.h"
#include "Entity.h"
#include "TransformComponent.h"
#include "GameInternal.h"
#include "Manager.h"
#include "VEGO.h"
SpriteComponent::SpriteComponent(Textures texture, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(0), textureYOffset(0)
{
this->textureEnum = texture;
this->path = "";
}
SpriteComponent::SpriteComponent(Textures texture, int xOffset, int yOffset, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(xOffset), textureYOffset(yOffset)
{
this->textureEnum = texture;
this->path = "";
}
SpriteComponent::SpriteComponent(const char* path, int xOffset, int yOffset, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(xOffset), textureYOffset(yOffset) {
this->path = path;
}
SpriteComponent::SpriteComponent(
Textures texture,
bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationMap,
std::string defaultAnimation,
int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(0), textureYOffset(0)
{
animated = isAnimated;
animations = animationMap;
playAnimation(defaultAnimation);
this->textureEnum = texture;
this->path = "";
}
SpriteComponent::~SpriteComponent() {}
void SpriteComponent::setTexture(Textures texture)
{
this->texture = VEGO_Game().textureManager->loadTexture(texture);
}
void SpriteComponent::init()
{
if (this->path == "") {
setTexture(this->textureEnum);
}
else {
setMapTileTexture(this->path);
}
this->transform = &entity->getComponent<TransformComponent>();
this->srcRect.w = transform->width;
this->srcRect.h = transform->height;
this->srcRect.x = this->textureXOffset * this->srcRect.w;
this->srcRect.y = this->textureYOffset * this->srcRect.h;;
this->update();
}
void SpriteComponent::update()
{
// This code is not compatible for animated tiles
if (animated) {
srcRect.x = srcRect.w * static_cast<int>((SDL_GetTicks() / speed) % frames);
srcRect.y = animationIndex * transform->height;
}
this->destRect.x = this->transform->position.x;
this->destRect.y = this->transform->position.y;
this->destRect.w = transform->width * transform->scale;
this->destRect.h = transform->height * transform->scale;
}
void SpriteComponent::draw()
{
this->entity->getManager().getGame()->textureManager->draw(VEGO_Game().renderer, this->texture, this->srcRect, this->destRect, this->animated && this->flipped);
}
void SpriteComponent::playAnimation(std::string type)
{
this->animationIndex = animations->at(type)->index;
this->frames = animations->at(type)->frames;
this->speed = animations->at(type)->speed;
}
void SpriteComponent::setDirection(Direction direction)
{
this->flipped = direction == Direction::RIGHT;
}
void SpriteComponent::setMapTileTexture(const char *path) {
this->texture = VEGO_Game().textureManager->loadMapTileTexture(path);
}