#include "SpriteComponent.h" #include #include #include #include "AnimationHandler.h" #include "Direction.h" #include "TextureManager.h" #include "Entity.h" #include "TransformComponent.h" #include "GameInternal.h" #include "Manager.h" SpriteComponent::SpriteComponent(TexturesEnum textureEnum) { this->textureEnum = textureEnum; } SpriteComponent::SpriteComponent( TexturesEnum textureEnum, bool isAnimated, std::map>* animationMap, std::string defaultAnimation) { animated = isAnimated; animations = animationMap; playAnimation(defaultAnimation); this->textureEnum = textureEnum; } SpriteComponent::~SpriteComponent() {} void SpriteComponent::setTexture(TexturesEnum texture) { this->texture = this->entity->getManager().getGame()->textureManager->loadTexture(texture); } void SpriteComponent::init() { setTexture(this->textureEnum); this->transform = &entity->getComponent(); this->srcRect.x = this->srcRect.y = 0; this->srcRect.w = transform->width; this->srcRect.h = transform->height; this->update(); } void SpriteComponent::update() { if (animated) { srcRect.x = srcRect.w * static_cast((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(this->entity->getManager().getGame()->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; }