diff options
author | nathansmith117 <thenathansmithsmith@gmail.com> | 2024-05-06 23:50:16 +0000 |
---|---|---|
committer | nathansmith117 <thenathansmithsmith@gmail.com> | 2024-05-06 23:50:16 +0000 |
commit | 308aefcbc437664acc4654c51a6d5dfcd346de32 (patch) | |
tree | d6e7e4138d3149fb849a02718ddb42e7e8883538 /src | |
parent | 2e90f9f959b0fcefbea728a00dda8c1cfed222fb (diff) | |
download | PenguinYippies-308aefcbc437664acc4654c51a6d5dfcd346de32.tar.gz PenguinYippies-308aefcbc437664acc4654c51a6d5dfcd346de32.tar.bz2 PenguinYippies-308aefcbc437664acc4654c51a6d5dfcd346de32.zip |
Penguin shooting working
Diffstat (limited to 'src')
-rw-r--r-- | src/shooterScreen.c | 81 | ||||
-rw-r--r-- | src/shooterScreen.h | 5 |
2 files changed, 82 insertions, 4 deletions
diff --git a/src/shooterScreen.c b/src/shooterScreen.c index b3adb53..f306322 100644 --- a/src/shooterScreen.c +++ b/src/shooterScreen.c @@ -24,6 +24,56 @@ void initShooterScreeen(ShooterScreen* shooterScreen, Game* game) resetShooterScreen(shooterScreen); } +void shootBulletShooterScreen(ShooterScreen* shooterScreen, Game* game) +{ + ShooterPlayer* player = &shooterScreen->player; + + // Bullet ray. + Ray ray = (Ray){ + .position = player->position, + .direction = player->direction + }; + + // Bounding box that will be used for all penguins. + BoundingBox penguinBox = (BoundingBox){ + .min = (Vector3){-0.5, -1.0, -0.5}, + .max = (Vector3){0.5, 1.0, 0.5} + }; + + ShooterPenguin* closestPenguin = NULL; + float closestDistance = 0.0; + + for (int i = 0; i < SHOOTER_PENGUIN_COUNT; ++i) + { + ShooterPenguin* penguin = &shooterScreen->penguins[i]; + + // Get the box at the penguins position. + BoundingBox currentBox = (BoundingBox){ + .min = Vector3Add(penguin->position, penguinBox.min), + .max = Vector3Add(penguin->position, penguinBox.max) + }; + + // Check collision. + if (GetRayCollisionBox(ray, currentBox).hit) + { + float distance = Vector3Distance(penguin->position, shooterScreen->player.position); + + // Is closest or first. + if (distance < closestDistance || closestPenguin == NULL) + { + closestPenguin = penguin; + closestDistance = distance; + } + } + } + + // Removed penguin if shot. + if (closestPenguin != NULL) + { + closestPenguin->isDead = true; + } +} + void updateShooterScreenControls(ShooterScreen* shooterScreen, Game* game) { ShooterPlayer* player = &shooterScreen->player; @@ -68,6 +118,12 @@ void updateShooterScreenControls(ShooterScreen* shooterScreen, Game* game) player->jumpStage = 1; } + // Shoot + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) + { + shootBulletShooterScreen(shooterScreen, game); + } + player->velocity = Vector3Scale(player->velocity, PLAYER_SPEED); } @@ -112,6 +168,12 @@ void updateShooterScreenPenguins(ShooterScreen* shooterScreen, Game* game) for (int i = 0; i < SHOOTER_PENGUIN_COUNT; ++i) { + // It is dead ): + if (penguins[i].isDead) + { + continue; + } + // Change velocity. if ((int)(penguins[i].changeSpeedDelay) == 0) // Goes at you. { @@ -156,11 +218,27 @@ void updateShooterScreenPenguins(ShooterScreen* shooterScreen, Game* game) } } +void drawCrosshairShooterScreen(int size, int thickness) +{ + int halfWidth = GetScreenWidth() / 2; + int halfHeight = GetScreenHeight() / 2; + + // Left to right. + DrawLineEx((Vector2){halfWidth - size, halfHeight}, + (Vector2){halfWidth + size, halfHeight}, thickness, BLACK); + + // Top to bottom. + DrawLineEx((Vector2){halfWidth, halfHeight - size}, + (Vector2){halfWidth, halfHeight + size}, thickness, BLACK); +} + void updateShooterScreen(ShooterScreen* shooterScreen, Game* game) { ShooterPlayer* player = &shooterScreen->player; ClearBackground(PINK); + drawCrosshairShooterScreen(10, 2); + updateShooterScreenControls(shooterScreen, game); updateShooterScreenJump(shooterScreen, game); @@ -178,7 +256,6 @@ void updateShooterScreen(ShooterScreen* shooterScreen, Game* game) BeginMode3D(shooterScreen->player.camera); DrawGrid(SHOOTER_MAP_SIZE, 2.0); - updateShooterScreenPenguins(shooterScreen, game); EndMode3D(); @@ -212,10 +289,10 @@ void resetShooterScreen(ShooterScreen* shooterScreen) shooterScreen->penguins[i].position = randomPosition; shooterScreen->penguins[i].velocity = Vector3Zero(); - shooterScreen->penguins[i].sleepyness = 0.0; shooterScreen->penguins[i].changeSpeedDelay = GetRandomValue(SHOOTER_PENGUIN_CHANGE_DELAY_MIN, SHOOTER_PENGUIN_CHANGE_DELAY_MAX); shooterScreen->penguins[i].lastVelocityChange = 0.0; + shooterScreen->penguins[i].isDead = false; } } diff --git a/src/shooterScreen.h b/src/shooterScreen.h index f077ee6..054af15 100644 --- a/src/shooterScreen.h +++ b/src/shooterScreen.h @@ -11,7 +11,7 @@ #define PLAYER_FALL_SPEED 20.0 #define PLAYER_JUMP_HEIGHT 8.0 -#define MOUSE_SPEED 0.01 +#define MOUSE_SPEED 0.005 #define SHOOTER_PENGUIN_COUNT 10 #define SHOOTER_PENGUIN_HEIGHT 1.0 @@ -36,10 +36,11 @@ typedef struct ShooterPlayer { typedef struct ShooterPenguin { Vector3 position; Vector3 velocity; - float sleepyness; double changeSpeedDelay; double lastVelocityChange; + + bool isDead; } ShooterPenguin; typedef struct ShooterScreen { |