diff --git a/src/engine/sprites/sprite.cpp b/src/engine/sprites/sprite.cpp index 5d66237..e6dc5f2 100644 --- a/src/engine/sprites/sprite.cpp +++ b/src/engine/sprites/sprite.cpp @@ -41,7 +41,7 @@ void Sprite::syncVelocity() { } void Sprite::syncAnimation() { - if(amountOfFrames == 0) return; + if(!animating) return; int newTileIndex = this->tileIndex + (currentFrame * w); oam->attr2 &= OAM_TILE_OFFSET_CLEAR; @@ -55,11 +55,11 @@ void Sprite::syncOam() { void Sprite::updateVelocity() { this->x += this->dx; - this-> y += this->dy; + this->y += this->dy; } void Sprite::updateAnimation() { - if(amountOfFrames == 0) return; + if(!animating) return; animationCounter++; if(animationCounter > animationDelay) { diff --git a/src/engine/sprites/sprite.h b/src/engine/sprites/sprite.h index 54155e7..e6fc335 100644 --- a/src/engine/sprites/sprite.h +++ b/src/engine/sprites/sprite.h @@ -6,7 +6,6 @@ #define GBA_SPRITE_ENGINE_SPRITE_H #include -#include #include #define COLOR_MODE_16 0 @@ -52,6 +51,7 @@ protected: int w, h, size_bits, shape_bits; int imageSize, tileIndex; int animationDelay, amountOfFrames, currentFrame, animationCounter; + bool animating; std::unique_ptr oam; @@ -66,7 +66,10 @@ public: void makeAnimated(int amountOfFrames, int animationDelay) { this->amountOfFrames = amountOfFrames; this->animationDelay = animationDelay; + animate(); } + void animate() { this->animating = true; } + void stopAnimating() { this->animating = false; } void setVelocity(int dx, int dy) { this->dx = dx; this->dy = dy; @@ -83,6 +86,7 @@ public: int getHeight() { return h; } int getWidth() { return w; } int getY() { return y; } + int getCurrentFrame() { return currentFrame; } friend class SpriteManager; }; diff --git a/test/spritetest.cpp b/test/spritetest.cpp index 7c23691..5185afe 100644 --- a/test/spritetest.cpp +++ b/test/spritetest.cpp @@ -177,36 +177,67 @@ const u32 kul_data [] = { 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, }; - - -class SpriteSuite : public ::testing::Test { -protected: -protected: - virtual void TearDown() { - } - - virtual void SetUp() { +class SpriteWithStubOam : public Sprite { +public: + SpriteWithStubOam() : Sprite(nullptr, imageSize, x, y, SIZE_8_8) { + oam = std::unique_ptr(new OBJ_ATTR()); } }; -TEST_F(SpriteSuite, CollidesWith_B_Right_Of_A_Does_Not_Collide) { - auto a = SpriteBuilder().withLocation(10, 10).withSize(SIZE_16_16).build(); - auto b = SpriteBuilder().withLocation(40, 10).withSize(SIZE_16_16).build(); +class SpriteSuite : public ::testing::Test { +protected: + 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().withLocation(10, 10).withSize(SIZE_16_16).buildPtr(); + auto b = SpriteBuilder().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) { - auto a = SpriteBuilder().withLocation(10, 10).withSize(SIZE_16_16).build(); - auto b = SpriteBuilder().withLocation(20, 10).withSize(SIZE_16_16).build(); + auto a = SpriteBuilder().withLocation(10, 10).withSize(SIZE_16_16).buildPtr(); + auto b = SpriteBuilder().withLocation(20, 10).withSize(SIZE_16_16).buildPtr(); - ASSERT_TRUE(a.collidesWith(b)); + ASSERT_TRUE(a->collidesWith(*b)); } TEST_F(SpriteSuite, BuildingWithSize_SetsWidthAndHeight) { - auto s = SpriteBuilder().withSize(SIZE_64_32).build(); - ASSERT_EQ(64, s.getWidth()); - ASSERT_EQ(32, s.getHeight()); + auto s = SpriteBuilder().withSize(SIZE_64_32).buildPtr(); + ASSERT_EQ(64, s->getWidth()); + ASSERT_EQ(32, s->getHeight()); } TEST_F(SpriteSuite, SpriteBuilderFillsSpriteWithData) { @@ -214,10 +245,10 @@ TEST_F(SpriteSuite, SpriteBuilderFillsSpriteWithData) { .withData(kul_data, sizeof(kul_data)) .withLocation(10, 20) .withSize(SIZE_32_64) - .build(); + .buildPtr(); - ASSERT_EQ(32, sprite.getWidth()); - ASSERT_EQ(64, sprite.getHeight()); - ASSERT_EQ(10, sprite.getX()); - ASSERT_EQ(20, sprite.getY()); + ASSERT_EQ(32, sprite->getWidth()); + ASSERT_EQ(64, sprite->getHeight()); + ASSERT_EQ(10, sprite->getX()); + ASSERT_EQ(20, sprite->getY()); }