trying to implement hitboxes
This commit is contained in:
parent
6c7e5ab425
commit
d5562e7ef0
@ -42,6 +42,36 @@ void Sprite::setCenter(float x, float y) {
|
|||||||
C2D_SpriteSetCenter(&this->spr, x, y);
|
C2D_SpriteSetCenter(&this->spr, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Sprite::setHitbox(float w, float h) {
|
||||||
|
this->width = w; this->height = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Sprite::isCollidingWith(Sprite &other) {
|
||||||
|
// hitbox not set
|
||||||
|
if (this->width == 0 && this->height == 0) return false;
|
||||||
|
|
||||||
|
const float thisHalfWidth = this->width/2;
|
||||||
|
const float otherHalfWidth = other.width/2;
|
||||||
|
const float thisHalfHeight = this->height/2;
|
||||||
|
const float otherHalfHeight = other.height/2;
|
||||||
|
|
||||||
|
// check x collision
|
||||||
|
if (this->x + thisHalfWidth >= other.x - otherHalfWidth ||
|
||||||
|
this->x - thisHalfWidth <= other.x + otherHalfWidth
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check y collision
|
||||||
|
if (this->y + thisHalfHeight >= other.y - otherHalfHeight ||
|
||||||
|
this->y - thisHalfHeight <= other.y + otherHalfHeight
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
float* Sprite::getCenter() {
|
float* Sprite::getCenter() {
|
||||||
return this->center;
|
return this->center;
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,10 @@ public:
|
|||||||
|
|
||||||
void setCenter(float x, float y);
|
void setCenter(float x, float y);
|
||||||
|
|
||||||
|
void setHitbox(float width, float height);
|
||||||
|
|
||||||
|
bool isCollidingWith(Sprite &other);
|
||||||
|
|
||||||
void draw();
|
void draw();
|
||||||
|
|
||||||
float getPosX();
|
float getPosX();
|
||||||
@ -33,6 +37,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
float x, y;
|
float x, y;
|
||||||
|
float width, height; // for hitbox
|
||||||
float center[2];
|
float center[2];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -39,6 +39,7 @@ static void initSprites() {
|
|||||||
|
|
||||||
// set bird consts
|
// set bird consts
|
||||||
sprites[SPR_BIRD].setPosition(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 3);
|
sprites[SPR_BIRD].setPosition(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 3);
|
||||||
|
sprites[SPR_BIRD].setHitbox(17, 12);
|
||||||
|
|
||||||
// set bottom screen scorecard
|
// set bottom screen scorecard
|
||||||
sprites[SPR_SCORECARD].setCenter(0.0, 0.0);
|
sprites[SPR_SCORECARD].setCenter(0.0, 0.0);
|
||||||
@ -53,6 +54,7 @@ static void initSprites() {
|
|||||||
memcpy(&pipes[i], &sprites[SPR_BOTHPIPES], sizeof(sprites[SPR_BOTHPIPES]));
|
memcpy(&pipes[i], &sprites[SPR_BOTHPIPES], sizeof(sprites[SPR_BOTHPIPES]));
|
||||||
pipes[i].setCenter(0.5f, 0.5f);
|
pipes[i].setCenter(0.5f, 0.5f);
|
||||||
pipes[i].setPosition(SCREEN_WIDTH + 40 + i * (100), (rand() % 150) + 50);
|
pipes[i].setPosition(SCREEN_WIDTH + 40 + i * (100), (rand() % 150) + 50);
|
||||||
|
pipes[i].setHitbox(26, 403);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +62,7 @@ void introScene();
|
|||||||
void gameScene();
|
void gameScene();
|
||||||
void gameOverScene();
|
void gameOverScene();
|
||||||
|
|
||||||
void (*() currentPhase = &gameScene;
|
// void (*currentScene)(C3D_RenderTarget*, C3D_RenderTarget*) = &gameScene;
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
@ -117,42 +119,44 @@ int main(int argc, char* argv[]) {
|
|||||||
if (kDown & KEY_START)
|
if (kDown & KEY_START)
|
||||||
break; // break in order to return to hbmenu
|
break; // break in order to return to hbmenu
|
||||||
|
|
||||||
|
if (kDown & KEY_SELECT)
|
||||||
|
main(0, 0); // reset
|
||||||
|
|
||||||
|
|
||||||
// gravity calcs
|
// gravity calcs
|
||||||
v += a;
|
v += a;
|
||||||
sprites[SPR_BIRD].move(0, v);
|
sprites[SPR_BIRD].move(0, v);
|
||||||
C2D_SpriteSetRotationDegrees(&sprites[SPR_BIRD].spr, v*9.8);
|
C2D_SpriteSetRotationDegrees(&sprites[SPR_BIRD].spr, v*9.8);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!gameOver && (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, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < NUM_PIPES; i++) {
|
// bird hitting ground is game over
|
||||||
pipes[i].move(pipeSpeed, 0, true);
|
if (sprites[SPR_BIRD].getPosY() == SCREEN_HEIGHT) {
|
||||||
|
gameOver = true;
|
||||||
|
}
|
||||||
|
|
||||||
// collision detection
|
if (!gameOver) {
|
||||||
if (pipes[i].getPosX() == sprites[SPR_BIRD].getPosX() &&
|
for (int i = 0; i < NUM_PIPES; i++) {
|
||||||
(sprites[SPR_BIRD].getPosY() > pipes[i].getPosY() + 41.5 ||
|
pipes[i].move(pipeSpeed, 0, true);
|
||||||
sprites[SPR_BIRD].getPosY() < pipes[i].getPosY() - 41.5))
|
|
||||||
{
|
|
||||||
gameOver = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
|
// collision detection
|
||||||
|
if (pipes[i].isCollidingWith(sprites[SPR_BIRD])) {
|
||||||
|
gameOver = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// inc score
|
||||||
|
if (pipes[i].getPosX() == sprites[SPR_BIRD].getPosX())
|
||||||
|
score++;
|
||||||
|
|
||||||
|
// send pipes back to front of screen
|
||||||
|
if (pipes[i].getPosX() < -(SCREEN_WIDTH/NUM_PIPES)) {
|
||||||
|
pipes[i].setPosition(SCREEN_WIDTH, (rand() % 150) + 50);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// inc score
|
|
||||||
if (!gameOver && (pipes[i].getPosX() == sprites[SPR_BIRD].getPosX()))
|
|
||||||
score++;
|
|
||||||
|
|
||||||
// send pipes back to front of screen
|
|
||||||
if (pipes[i].getPosX() < -(SCREEN_WIDTH/NUM_PIPES)) {
|
|
||||||
pipes[i].setPosition(SCREEN_WIDTH, (rand() % 150) + 50);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user