fix sprite tests, added for animation
This commit is contained in:
parent
49f20b3248
commit
56dbeec546
|
@ -41,7 +41,7 @@ void Sprite::syncVelocity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sprite::syncAnimation() {
|
void Sprite::syncAnimation() {
|
||||||
if(amountOfFrames == 0) return;
|
if(!animating) return;
|
||||||
int newTileIndex = this->tileIndex + (currentFrame * w);
|
int newTileIndex = this->tileIndex + (currentFrame * w);
|
||||||
|
|
||||||
oam->attr2 &= OAM_TILE_OFFSET_CLEAR;
|
oam->attr2 &= OAM_TILE_OFFSET_CLEAR;
|
||||||
|
@ -55,11 +55,11 @@ void Sprite::syncOam() {
|
||||||
|
|
||||||
void Sprite::updateVelocity() {
|
void Sprite::updateVelocity() {
|
||||||
this->x += this->dx;
|
this->x += this->dx;
|
||||||
this-> y += this->dy;
|
this->y += this->dy;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sprite::updateAnimation() {
|
void Sprite::updateAnimation() {
|
||||||
if(amountOfFrames == 0) return;
|
if(!animating) return;
|
||||||
|
|
||||||
animationCounter++;
|
animationCounter++;
|
||||||
if(animationCounter > animationDelay) {
|
if(animationCounter > animationDelay) {
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
#define GBA_SPRITE_ENGINE_SPRITE_H
|
#define GBA_SPRITE_ENGINE_SPRITE_H
|
||||||
|
|
||||||
#include <engine/gba/tonc_types.h>
|
#include <engine/gba/tonc_types.h>
|
||||||
#include <engine/background/text_stream.h>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#define COLOR_MODE_16 0
|
#define COLOR_MODE_16 0
|
||||||
|
@ -52,6 +51,7 @@ protected:
|
||||||
int w, h, size_bits, shape_bits;
|
int w, h, size_bits, shape_bits;
|
||||||
int imageSize, tileIndex;
|
int imageSize, tileIndex;
|
||||||
int animationDelay, amountOfFrames, currentFrame, animationCounter;
|
int animationDelay, amountOfFrames, currentFrame, animationCounter;
|
||||||
|
bool animating;
|
||||||
|
|
||||||
std::unique_ptr<OBJ_ATTR> oam;
|
std::unique_ptr<OBJ_ATTR> oam;
|
||||||
|
|
||||||
|
@ -66,7 +66,10 @@ public:
|
||||||
void makeAnimated(int amountOfFrames, int animationDelay) {
|
void makeAnimated(int amountOfFrames, int animationDelay) {
|
||||||
this->amountOfFrames = amountOfFrames;
|
this->amountOfFrames = amountOfFrames;
|
||||||
this->animationDelay = animationDelay;
|
this->animationDelay = animationDelay;
|
||||||
|
animate();
|
||||||
}
|
}
|
||||||
|
void animate() { this->animating = true; }
|
||||||
|
void stopAnimating() { this->animating = false; }
|
||||||
void setVelocity(int dx, int dy) {
|
void setVelocity(int dx, int dy) {
|
||||||
this->dx = dx;
|
this->dx = dx;
|
||||||
this->dy = dy;
|
this->dy = dy;
|
||||||
|
@ -83,6 +86,7 @@ public:
|
||||||
int getHeight() { return h; }
|
int getHeight() { return h; }
|
||||||
int getWidth() { return w; }
|
int getWidth() { return w; }
|
||||||
int getY() { return y; }
|
int getY() { return y; }
|
||||||
|
int getCurrentFrame() { return currentFrame; }
|
||||||
|
|
||||||
friend class SpriteManager;
|
friend class SpriteManager;
|
||||||
};
|
};
|
||||||
|
|
|
@ -177,36 +177,67 @@ const u32 kul_data [] = {
|
||||||
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SpriteWithStubOam : public Sprite {
|
||||||
|
public:
|
||||||
class SpriteSuite : public ::testing::Test {
|
SpriteWithStubOam() : Sprite(nullptr, imageSize, x, y, SIZE_8_8) {
|
||||||
protected:
|
oam = std::unique_ptr<OBJ_ATTR>(new OBJ_ATTR());
|
||||||
protected:
|
|
||||||
virtual void TearDown() {
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void SetUp() {
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(SpriteSuite, CollidesWith_B_Right_Of_A_Does_Not_Collide) {
|
class SpriteSuite : public ::testing::Test {
|
||||||
auto a = SpriteBuilder<Sprite>().withLocation(10, 10).withSize(SIZE_16_16).build();
|
protected:
|
||||||
auto b = SpriteBuilder<Sprite>().withLocation(40, 10).withSize(SIZE_16_16).build();
|
SpriteWithStubOam *s;
|
||||||
|
|
||||||
ASSERT_FALSE(a.collidesWith(b));
|
virtual void TearDown() {
|
||||||
|
delete s;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void SetUp() {
|
||||||
|
s = new SpriteWithStubOam();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
TEST_F(SpriteSuite, Animated_Sprite_Increases_Current_Frame_After_Delay) {
|
||||||
|
s->makeAnimated(2, 5);
|
||||||
|
|
||||||
|
ASSERT_EQ(0, s->getCurrentFrame());
|
||||||
|
s->update(); // 1 times
|
||||||
|
ASSERT_EQ(0, s->getCurrentFrame());
|
||||||
|
for(int i = 1; i <= 4; i++) { // 4 times
|
||||||
|
s->update();
|
||||||
|
}
|
||||||
|
ASSERT_EQ(0, s->getCurrentFrame());
|
||||||
|
s->update(); // 5th time
|
||||||
|
ASSERT_EQ(1, s->getCurrentFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SpriteSuite, Animated_Sprite_Resets_Current_Frame_After_Hitting_Max) {
|
||||||
|
s->makeAnimated(2, 0);
|
||||||
|
s->update();
|
||||||
|
ASSERT_EQ(1, s->getCurrentFrame());
|
||||||
|
s->update();
|
||||||
|
ASSERT_EQ(0, s->getCurrentFrame());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SpriteSuite, CollidesWith_B_Right_Of_A_Does_Not_Collide) {
|
||||||
|
auto a = SpriteBuilder<Sprite>().withLocation(10, 10).withSize(SIZE_16_16).buildPtr();
|
||||||
|
auto b = SpriteBuilder<Sprite>().withLocation(40, 10).withSize(SIZE_16_16).buildPtr();
|
||||||
|
|
||||||
|
ASSERT_FALSE(a->collidesWith(*b));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(SpriteSuite, CollidesWith_B_Half_In_A_On_X_Axis_Collides) {
|
TEST_F(SpriteSuite, CollidesWith_B_Half_In_A_On_X_Axis_Collides) {
|
||||||
auto a = SpriteBuilder<Sprite>().withLocation(10, 10).withSize(SIZE_16_16).build();
|
auto a = SpriteBuilder<Sprite>().withLocation(10, 10).withSize(SIZE_16_16).buildPtr();
|
||||||
auto b = SpriteBuilder<Sprite>().withLocation(20, 10).withSize(SIZE_16_16).build();
|
auto b = SpriteBuilder<Sprite>().withLocation(20, 10).withSize(SIZE_16_16).buildPtr();
|
||||||
|
|
||||||
ASSERT_TRUE(a.collidesWith(b));
|
ASSERT_TRUE(a->collidesWith(*b));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(SpriteSuite, BuildingWithSize_SetsWidthAndHeight) {
|
TEST_F(SpriteSuite, BuildingWithSize_SetsWidthAndHeight) {
|
||||||
auto s = SpriteBuilder<Sprite>().withSize(SIZE_64_32).build();
|
auto s = SpriteBuilder<Sprite>().withSize(SIZE_64_32).buildPtr();
|
||||||
ASSERT_EQ(64, s.getWidth());
|
ASSERT_EQ(64, s->getWidth());
|
||||||
ASSERT_EQ(32, s.getHeight());
|
ASSERT_EQ(32, s->getHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(SpriteSuite, SpriteBuilderFillsSpriteWithData) {
|
TEST_F(SpriteSuite, SpriteBuilderFillsSpriteWithData) {
|
||||||
|
@ -214,10 +245,10 @@ TEST_F(SpriteSuite, SpriteBuilderFillsSpriteWithData) {
|
||||||
.withData(kul_data, sizeof(kul_data))
|
.withData(kul_data, sizeof(kul_data))
|
||||||
.withLocation(10, 20)
|
.withLocation(10, 20)
|
||||||
.withSize(SIZE_32_64)
|
.withSize(SIZE_32_64)
|
||||||
.build();
|
.buildPtr();
|
||||||
|
|
||||||
ASSERT_EQ(32, sprite.getWidth());
|
ASSERT_EQ(32, sprite->getWidth());
|
||||||
ASSERT_EQ(64, sprite.getHeight());
|
ASSERT_EQ(64, sprite->getHeight());
|
||||||
ASSERT_EQ(10, sprite.getX());
|
ASSERT_EQ(10, sprite->getX());
|
||||||
ASSERT_EQ(20, sprite.getY());
|
ASSERT_EQ(20, sprite->getY());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue