From 3e16eb8d2f15d40239bdf1f375daeab7bcdf95fa Mon Sep 17 00:00:00 2001 From: salaaad2 Date: Mon, 11 Jul 2022 21:24:00 +0200 Subject: dod --- meta/media/img/crosshair.png | Bin 0 -> 184 bytes src/main.cpp | 16 ++++---- src/yabs_core.cpp | 94 +++++++++++++++++++++++-------------------- src/yabs_core.h | 35 ++++------------ src/yabs_structs.h | 48 ++++++++++++++++++++++ src/yabs_utils.cpp | 1 + src/yabs_utils.h | 9 +++-- 7 files changed, 120 insertions(+), 83 deletions(-) create mode 100644 meta/media/img/crosshair.png create mode 100644 src/yabs_structs.h diff --git a/meta/media/img/crosshair.png b/meta/media/img/crosshair.png new file mode 100644 index 0000000..d17bcb5 Binary files /dev/null and b/meta/media/img/crosshair.png differ diff --git a/src/main.cpp b/src/main.cpp index 4ba1970..69702ad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ #include "yabs_core.h" #include "yabs_utils.h" -static yabs::gameState game_state = yabs::GAMEPLAY; +static yabs::core::gameState game_state = yabs::core::GAMEPLAY; int main(void) { // create window @@ -20,20 +20,20 @@ int main(void) { // game loop while (!WindowShouldClose()) { switch (game_state) { - case (yabs::TITLE): { + case (yabs::core::TITLE): { if (IsKeyPressed(KEY_ENTER)) { - game_state = yabs::GAMEPLAY; + game_state = yabs::core::GAMEPLAY; } break; } - case (yabs::PICK): { + case (yabs::core::PICK): { break; } - case (yabs::DEATH): { + case (yabs::core::DEATH): { break; } - case (yabs::GAMEPLAY): { + case (yabs::core::GAMEPLAY): { if (!started) { started = true; @@ -41,10 +41,10 @@ int main(void) { } break; } - case (yabs::NEXT): { + case (yabs::core::NEXT): { break; } - case (yabs::ENDING): { + case (yabs::core::ENDING): { break; } } diff --git a/src/yabs_core.cpp b/src/yabs_core.cpp index 5ae5b57..c6fdb1a 100644 --- a/src/yabs_core.cpp +++ b/src/yabs_core.cpp @@ -12,10 +12,11 @@ #include #include "yabs_core.h" +#include "yabs_utils.h" namespace yabs { namespace core { -static yabs::Scene3D sc3d{ +static Scene3D sc3d{ // camera {{5.0f, 5.0f, 5.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, 45.0f, 0}, // movement @@ -25,7 +26,17 @@ static yabs::Scene3D sc3d{ // mouse {0.0f, 0.0f}}; -int draw(World& world) { +static float vert_accel = 0.0f; + +// input keys array +static bool direction[4] = { + false, + false, + false, + false}; +static BoundingBox range{}; + +int render(RenderObjs & robjs) { // draw checkerboard model for (int x = 0; x < 50; x++) { for (int z = 0; z < 50; z++) { @@ -33,29 +44,16 @@ int draw(World& world) { position.x = x; position.y = -0.5f; position.z = z; - DrawModel(world.ground, position, 1.0f, WHITE); + DrawModel(robjs.ground, position, 1.0f, WHITE); } } - for (auto & enemy : world.enemies) { - if (enemy->active) - { - DrawCube(enemy->enemyBoxPos, 2.0f, 2.0f, 2.0f, YABS_COOLPURPLE); - } + for (auto & pos : robjs.vEnemyPos) { + DrawCube(*pos, 2.0f, 2.0f, 2.0f, YABS_COOLPURPLE); } return (0); } -static float vert_accel = 0.0f; - -// input keys array -static bool direction[4] = { - false, - false, - false, - false}; -static BoundingBox range{}; - -int tick(World& world, Scene3D& scene_3d) { +int tick(TickObjs& tobjs, Scene3D& scene_3d) { // mouse look Vector2 mouseMovement = Vector2Subtract(GetMousePosition(), scene_3d.lastMousePos); @@ -66,8 +64,7 @@ int tick(World& world, Scene3D& scene_3d) { (mouseMovement.y * -YABS_CAMERA_MOUSE_MOVE_SENSITIVITY); // direction array - if (scene_3d.camera.position.y <= YABS_GROUND_LEVEL) - { + if (scene_3d.camera.position.y <= YABS_GROUND_LEVEL) { bzero(&direction, sizeof(direction)); direction[0] = IsKeyDown(KEY_W); direction[1] = IsKeyDown(KEY_S); @@ -75,7 +72,6 @@ int tick(World& world, Scene3D& scene_3d) { direction[3] = IsKeyDown(KEY_A); } - // compute movement scene_3d.movement.x = (sinf(scene_3d.rotation.x) * direction[1] - sinf(scene_3d.rotation.x) * direction[0] - @@ -113,7 +109,10 @@ int tick(World& world, Scene3D& scene_3d) { if (IsKeyPressed(KEY_LEFT_CONTROL)) vert_accel -= 0.05; if (IsKeyPressed(KEY_ESCAPE)) + { + *tobjs.alive = false; return (0); + } // apply movement scene_3d.camera.position.x += @@ -135,25 +134,22 @@ int tick(World& world, Scene3D& scene_3d) { // shoot range = {{ - scene_3d.camera.target.x - 0.5f, - scene_3d.camera.target.y - 4.0f, - scene_3d.camera.target.z - 0.5f,},{ - scene_3d.camera.target.x + 0.5f, - scene_3d.camera.target.y + 5.5f, - scene_3d.camera.target.z + 0.5f} - }; - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) - { - for (auto it = world.enemies.begin(); it != world.enemies.end(); ++it) - { - if(CheckCollisionBoxes(range, (*it)->enemyBounds)) - { + scene_3d.camera.target.x, + scene_3d.camera.target.y, + scene_3d.camera.target.z + }, { + scene_3d.camera.target.x + 1.5f, + scene_3d.camera.target.y + 5.5f, + scene_3d.camera.target.z + 1.5f + }}; + + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + for (auto it = tobjs.vEnemyBounds.begin(); it != tobjs.vEnemyBounds.end(); ++it) { + if(CheckCollisionBoxes(range, *(*it))) { // :^} std::cerr << "hit enemy\n"; - (*it)->active = false; - world.enemies.erase(it); + break ; // erase() invalidates iterators, we have to break // empty space - break; } } } @@ -187,16 +183,24 @@ void init_game() sc3d.movement = (Vector3){0.0f, 0.0f, 0.0f}; Vector3 cubeSize = {2.0f, 2.0f, 2.0f}; - World world = { + RenderObjs robjs = { + std::vector(), model }; + TickObjs tobjs = { + std::vector(), + std::vector(), + &range, + &sc3d.camera.position, + {direction}, + &alive + }; + // baddies for (int i = 0; i < ENEMIES_N; i++) { Enemy * e = new Enemy(); - world.enemies.push_back(e); - e->enemyStartPos.x = rand() % 32; e->enemyStartPos.y = 1.0f; e->enemyStartPos.z = rand() % 32; @@ -209,6 +213,9 @@ void init_game() {e->enemyBoxPos.x - 2.5f, 0.0f, e->enemyBoxPos.z - 2.5f}, {e->enemyBoxPos.x + 2.5f, 2.5f, e->enemyBoxPos.z + 2.5f} }; + robjs.vEnemyPos.push_back(&e->enemyBoxPos); + tobjs.vEnemyBounds.push_back(&e->enemyBounds); + tobjs.vEnemyPos.push_back(&e->enemyBoxPos); } Texture2D crosshair; @@ -221,8 +228,8 @@ void init_game() ClearBackground(GRAY); BeginMode3D(sc3d.camera); - alive = tick(world, sc3d); - draw(world); + tick(tobjs, sc3d); + render(robjs); EndMode3D(); DrawTexture(crosshair, YABS_HALFSWIDTH, YABS_HALFSHEIGHT, WHITE); @@ -231,7 +238,6 @@ void init_game() 0, 20, DARKGRAY); EndDrawing(); } - // cleanupAndExit(world); UnloadTexture(texture); UnloadModel(model); } diff --git a/src/yabs_core.h b/src/yabs_core.h index d8f111d..8ca4cfe 100644 --- a/src/yabs_core.h +++ b/src/yabs_core.h @@ -1,6 +1,3 @@ -#ifndef YABS_CORE_H -#define YABS_CORE_H - /*********************************/ /* YABS ( // */ /* yabs_CORE ( )/ */ @@ -9,10 +6,15 @@ /* ()__)____________))))) :^} */ /*********************************/ +#ifndef YABS_CORE_H +#define YABS_CORE_H + #include "raylib.h" #include "raymath.h" #include +#include "yabs_structs.h" + #ifdef __FreeBSD__ # define YABS_SCREENWIDTH 1280 # define YABS_SCREENHEIGHT 720 @@ -43,6 +45,7 @@ CLITERAL(Color) { 153, 0, 0, 255 } // cool Purple namespace yabs { +namespace core { typedef struct Scene3D { Camera camera; @@ -61,35 +64,13 @@ typedef enum gameState { } gameState; // core functions and structs -namespace core { - -typedef struct Player { - int hp; - Vector3 pos; -}Player; - -typedef struct Enemy { - Vector3 enemyStartPos; - Vector3 enemyBoxPos; - Vector3 enemyBoxSize; - BoundingBox enemyBounds; - bool active; -} Enemy; - -typedef struct World { - Model & ground; - std::vector enemies; - Player * player; -} World; int reset_camera(Scene3D& scene_3d); -int draw(World& world); -int tick(World& world, Scene3D& scene_3d); +int render(RenderObjs & robjs); +int tick(TickObjs & tobjs, Scene3D & scene_3d); void init_game(); -void cleanupAndExit(World & world); } // namespace core - } // namespace yabs #endif /* YABS_CORE_H */ diff --git a/src/yabs_structs.h b/src/yabs_structs.h new file mode 100644 index 0000000..c6f1d30 --- /dev/null +++ b/src/yabs_structs.h @@ -0,0 +1,48 @@ +/*********************************/ +/* YABS ( // */ +/* structs ( )/ */ +/* by salade )(/ */ +/* ________________ ( /) */ +/* ()__)____________))))) :^} */ +/*********************************/ + +#ifndef YABS_STRUCTS_H +#define YABS_STRUCTS_H + +#include "raylib.h" +#include + +namespace yabs { +namespace core { + +typedef struct Player { + int hp; + Vector3 pos; +}Player; + +typedef struct Enemy { + Vector3 enemyStartPos; + Vector3 enemyBoxPos; + Vector3 enemyBoxSize; + BoundingBox enemyBounds; + bool active; +} Enemy; + +typedef struct RenderObjs { + std::vector vEnemyPos; + Model & ground; +} RenderObjs; + +typedef struct TickObjs { + std::vector vEnemyBounds; + std::vector vEnemyPos; + BoundingBox * range; + Vector3 * position; + bool * direction[4]; + bool * alive; +} TickObjs; + +} +} + +#endif /* YABS_STRUCTS_H */ diff --git a/src/yabs_utils.cpp b/src/yabs_utils.cpp index ca60489..0c49228 100644 --- a/src/yabs_utils.cpp +++ b/src/yabs_utils.cpp @@ -7,6 +7,7 @@ /*********************************/ #include "yabs_utils.h" +#include namespace utils { // unused, for now :^} diff --git a/src/yabs_utils.h b/src/yabs_utils.h index 6006935..a931189 100644 --- a/src/yabs_utils.h +++ b/src/yabs_utils.h @@ -1,6 +1,3 @@ -#ifndef YABS_UTILS_H -#define YABS_UTILS_H - /*********************************/ /* YABS ( // */ /* yabs_gameplay ( )/ */ @@ -9,11 +6,15 @@ /* ()__)____________))))) :^} */ /*********************************/ +#ifndef YABS_UTILS_H +#define YABS_UTILS_H + namespace utils { // unused, for now :^} int min(int v1, int v2); int max(int v1, int v2); int clamp(int v, int v1, int v2); -} // namespace utils +} // namespace utils + #endif /* YABS_UTILS_H */ -- cgit v1.2.3