2018-08-01 16:03:16 +02:00
|
|
|
//
|
|
|
|
// Created by Wouter Groeneveld on 28/07/18.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef GBA_SPRITE_ENGINE_SPRITE_BUILDER_H
|
|
|
|
#define GBA_SPRITE_ENGINE_SPRITE_BUILDER_H
|
|
|
|
|
|
|
|
#include "sprite.h"
|
|
|
|
|
|
|
|
template<typename T> class SpriteBuilder {
|
|
|
|
private:
|
2018-08-09 12:35:05 +02:00
|
|
|
u32 imageSize;
|
2018-12-06 12:15:09 +01:00
|
|
|
bool stayWithinBounds = false;
|
2018-08-05 16:51:59 +02:00
|
|
|
const void *imageData;
|
2018-08-09 12:35:05 +02:00
|
|
|
u32 x, y, dx, dy;
|
|
|
|
u32 numberOfFrames, animationDelay;
|
2018-08-01 16:03:16 +02:00
|
|
|
SpriteSize size;
|
|
|
|
|
2018-08-09 12:35:05 +02:00
|
|
|
void setProperties(T* sprite);
|
2018-08-01 16:03:16 +02:00
|
|
|
void reset() {
|
2018-08-05 16:51:59 +02:00
|
|
|
imageSize = x = y = dx = dy = numberOfFrames = animationDelay = 0;
|
2018-08-01 16:03:16 +02:00
|
|
|
imageData = nullptr;
|
2018-12-06 12:15:09 +01:00
|
|
|
stayWithinBounds = false;
|
2018-08-01 16:03:16 +02:00
|
|
|
size = SIZE_16_16;
|
|
|
|
}
|
|
|
|
public:
|
2018-08-05 16:51:59 +02:00
|
|
|
SpriteBuilder() {
|
|
|
|
reset();
|
|
|
|
}
|
2018-08-08 14:43:34 +02:00
|
|
|
SpriteBuilder& withinBounds() {
|
|
|
|
stayWithinBounds = true;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-08-05 16:51:59 +02:00
|
|
|
SpriteBuilder& withData(const void* imageData, int imageSize) {
|
2018-08-01 16:03:16 +02:00
|
|
|
this->imageData = imageData;
|
|
|
|
this->imageSize = imageSize;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
SpriteBuilder& withVelocity(int dx, int dy) {
|
|
|
|
this->dx = dx;
|
|
|
|
this->dy = dy;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-08-05 16:51:59 +02:00
|
|
|
|
2018-08-09 12:35:05 +02:00
|
|
|
SpriteBuilder& withLocation(u32 x, u32 y) {
|
2018-08-01 16:03:16 +02:00
|
|
|
this->x = x;
|
|
|
|
this->y = y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
SpriteBuilder& withSize(const SpriteSize &size) {
|
|
|
|
this->size = size;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-08-05 16:51:59 +02:00
|
|
|
SpriteBuilder& withAnimated(int numberOfFrames, int delay) {
|
|
|
|
this->numberOfFrames = numberOfFrames;
|
|
|
|
this->animationDelay = delay;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-08-01 16:03:16 +02:00
|
|
|
T build();
|
|
|
|
std::unique_ptr<T> buildPtr();
|
2018-08-09 12:35:05 +02:00
|
|
|
std::unique_ptr<T> buildWithDataOf(const Sprite &other);
|
2018-08-01 16:03:16 +02:00
|
|
|
};
|
|
|
|
|
2018-08-09 12:35:05 +02:00
|
|
|
template<typename T> std::unique_ptr<T> SpriteBuilder<T>::buildWithDataOf(const Sprite &other) {
|
|
|
|
auto s = new T(other);
|
|
|
|
s->moveTo(this->x, this->y);
|
|
|
|
setProperties(s);
|
|
|
|
|
|
|
|
reset();
|
|
|
|
return std::unique_ptr<T>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> void SpriteBuilder<T>::setProperties(T* s) {
|
2018-08-01 16:03:16 +02:00
|
|
|
s->setVelocity(this->dx, this->dy);
|
2018-08-05 16:51:59 +02:00
|
|
|
if(this->numberOfFrames > 0) {
|
|
|
|
s->makeAnimated(this->numberOfFrames, this->animationDelay);
|
|
|
|
}
|
2018-08-08 14:43:34 +02:00
|
|
|
s->setStayWithinBounds(stayWithinBounds);
|
2018-08-01 16:03:16 +02:00
|
|
|
|
2018-08-09 12:35:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> std::unique_ptr<T> SpriteBuilder<T>::buildPtr() {
|
|
|
|
auto s = new T(this->imageData, this->imageSize, this->x, this->y, this->size);
|
|
|
|
setProperties(s);
|
|
|
|
|
2018-08-01 16:03:16 +02:00
|
|
|
reset();
|
|
|
|
return std::unique_ptr<T>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> T SpriteBuilder<T>::build() {
|
|
|
|
T s(this->imageData, this->imageSize, this->x, this->y, this->size);
|
2018-08-09 12:35:05 +02:00
|
|
|
setProperties(&s);
|
2018-08-01 16:03:16 +02:00
|
|
|
|
|
|
|
reset();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif //GBA_SPRITE_ENGINE_SPRITE_BUILDER_H
|