Basic collision detection implemented

This commit is contained in:
pradyun 2023-03-12 12:16:55 +13:00
parent 36e6fa79ff
commit 6c7e5ab425
3 changed files with 28 additions and 5 deletions

View File

@ -37,9 +37,15 @@ void Sprite::move(float dx, float dy, bool overrideOOB) {
} }
void Sprite::setCenter(float x, float y) { void Sprite::setCenter(float x, float y) {
this->center[0] = x; this->center[1] = y;
C2D_SpriteSetCenter(&this->spr, x, y); C2D_SpriteSetCenter(&this->spr, x, y);
} }
float* Sprite::getCenter() {
return this->center;
}
void Sprite::draw() { void Sprite::draw() {
C2D_DrawSprite(&this->spr); C2D_DrawSprite(&this->spr);
} }

View File

@ -23,6 +23,8 @@ public:
float getPosY(); float getPosY();
float* getCenter();
C2D_Sprite* getSpr(); C2D_Sprite* getSpr();
void setSpr(C2D_Sprite* newSpr); void setSpr(C2D_Sprite* newSpr);
@ -31,6 +33,7 @@ public:
private: private:
float x, y; float x, y;
float center[2];
}; };
#endif #endif

View File

@ -56,6 +56,12 @@ static void initSprites() {
} }
} }
void introScene();
void gameScene();
void gameOverScene();
void (*() currentPhase = &gameScene;
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -100,6 +106,7 @@ int main(int argc, char* argv[]) {
C2D_SpriteSetScale(&sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res) C2D_SpriteSetScale(&sprites[SPR_BG].spr, 2.7778, 2.7907); // scale image to 400x240 (3ds screen res)
size_t score = 0; size_t score = 0;
bool gameOver = false;
// Main loop // Main loop
while (aptMainLoop()) { while (aptMainLoop()) {
@ -118,7 +125,7 @@ int main(int argc, char* argv[]) {
if (kDown & KEY_A) { if (!gameOver && (kDown & KEY_A)) {
v = -5.5; v = -5.5;
sprites[SPR_BIRD].move(0, v); sprites[SPR_BIRD].move(0, v);
} }
@ -126,8 +133,18 @@ int main(int argc, char* argv[]) {
for (int i = 0; i < NUM_PIPES; i++) { for (int i = 0; i < NUM_PIPES; i++) {
pipes[i].move(pipeSpeed, 0, true); pipes[i].move(pipeSpeed, 0, true);
// collision detection
if (pipes[i].getPosX() == sprites[SPR_BIRD].getPosX() &&
(sprites[SPR_BIRD].getPosY() > pipes[i].getPosY() + 41.5 ||
sprites[SPR_BIRD].getPosY() < pipes[i].getPosY() - 41.5))
{
gameOver = true;
break;
}
// inc score // inc score
if (pipes[i].getPosX() == sprites[SPR_BIRD].getPosX()) if (!gameOver && (pipes[i].getPosX() == sprites[SPR_BIRD].getPosX()))
score++; score++;
// send pipes back to front of screen // send pipes back to front of screen
@ -160,10 +177,7 @@ int main(int argc, char* argv[]) {
C2D_TargetClear(bottom, C2D_Color32f(0.3294f, 0.7529f, 0.7882f, 1.0f)); C2D_TargetClear(bottom, C2D_Color32f(0.3294f, 0.7529f, 0.7882f, 1.0f));
C2D_SceneBegin(bottom); C2D_SceneBegin(bottom);
// drawSprite(SPR_SCORECARD);
sprites[SPR_SCORECARD].draw(); sprites[SPR_SCORECARD].draw();
// C2D_DrawSprite(&scoreSprite->spr);
C2D_DrawText(&scoreText, 0, 240, 88, 0.0f, 0.9f, 0.9f); C2D_DrawText(&scoreText, 0, 240, 88, 0.0f, 0.9f, 0.9f);
C3D_FrameEnd(0); C3D_FrameEnd(0);