diff options
-rw-r--r-- | src/yabs_core.cpp | 135 | ||||
-rw-r--r-- | src/yabs_core.h | 51 |
2 files changed, 164 insertions, 22 deletions
diff --git a/src/yabs_core.cpp b/src/yabs_core.cpp index 60a74cc..5ae5b57 100644 --- a/src/yabs_core.cpp +++ b/src/yabs_core.cpp @@ -8,6 +8,8 @@ #include <raylib.h> #include <stdlib.h> +#include <iostream> +#include <strings.h> #include "yabs_core.h" @@ -23,21 +25,37 @@ static yabs::Scene3D sc3d{ // mouse {0.0f, 0.0f}}; -int draw(Model& model) { +int draw(World& world) { // draw checkerboard model for (int x = 0; x < 50; x++) { for (int z = 0; z < 50; z++) { Vector3 position; position.x = x; - position.y = 0; + position.y = -0.5f; position.z = z; - DrawModel(model, position, 1.0f, WHITE); + DrawModel(world.ground, position, 1.0f, WHITE); + } + } + for (auto & enemy : world.enemies) { + if (enemy->active) + { + DrawCube(enemy->enemyBoxPos, 2.0f, 2.0f, 2.0f, YABS_COOLPURPLE); } } return (0); } -int tick(Scene3D& scene_3d) { +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) { // mouse look Vector2 mouseMovement = Vector2Subtract(GetMousePosition(), scene_3d.lastMousePos); @@ -48,11 +66,17 @@ int tick(Scene3D& scene_3d) { (mouseMovement.y * -YABS_CAMERA_MOUSE_MOVE_SENSITIVITY); // direction array - bool direction[4] = {IsKeyDown(KEY_W), IsKeyDown(KEY_S), IsKeyDown(KEY_D), - IsKeyDown(KEY_A)}; + if (scene_3d.camera.position.y <= YABS_GROUND_LEVEL) + { + bzero(&direction, sizeof(direction)); + direction[0] = IsKeyDown(KEY_W); + direction[1] = IsKeyDown(KEY_S); + direction[2] = IsKeyDown(KEY_D); + direction[3] = IsKeyDown(KEY_A); + } + // compute movement - scene_3d.movement = (Vector3){0, 0, 0}; scene_3d.movement.x = (sinf(scene_3d.rotation.x) * direction[1] - sinf(scene_3d.rotation.x) * direction[0] - cosf(scene_3d.rotation.x) * direction[3] + @@ -62,12 +86,32 @@ int tick(Scene3D& scene_3d) { cosf(scene_3d.rotation.x) * direction[0] + sinf(scene_3d.rotation.x) * direction[3] - sinf(scene_3d.rotation.x) * direction[2]) / - YABS_PLAYER_MOVEMENT_SENSITIVITY; + YABS_PLAYER_MOVEMENT_SENSITIVITY; + // elevation - if (IsKeyDown(KEY_SPACE)) - scene_3d.movement.y += 0.12f; - if (IsKeyDown(KEY_LEFT_CONTROL)) - scene_3d.movement.y -= 0.12f; + if (scene_3d.camera.position.y > YABS_GROUND_LEVEL) + { + scene_3d.movement.y = vert_accel; + vert_accel -= 0.005f; + } + else + { + vert_accel = 0.0f; + scene_3d.movement.y = 0.0f; + scene_3d.camera.position.y = YABS_GROUND_LEVEL; + } + + if (IsKeyPressed(KEY_SPACE)) + { + if (scene_3d.camera.position.y == YABS_GROUND_LEVEL) + { + scene_3d.camera.position.y += 0.1f; + vert_accel += 0.13; + } + } + + if (IsKeyPressed(KEY_LEFT_CONTROL)) + vert_accel -= 0.05; if (IsKeyPressed(KEY_ESCAPE)) return (0); @@ -88,13 +132,38 @@ int tick(Scene3D& scene_3d) { scene_3d.camera.target.x = scene_3d.camera.position.x - transform.m12; scene_3d.camera.target.y = scene_3d.camera.position.y - transform.m13; scene_3d.camera.target.z = scene_3d.camera.position.z - transform.m14; + + // 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)) + { + std::cerr << "hit enemy\n"; + (*it)->active = false; + world.enemies.erase(it); + // erase() invalidates iterators, we have to break + // empty space + break; + } + } + } return (1); } void init_game() { // create image - Image checked = GenImageChecked(2, 2, 1, 1, BLACK, GRAY); + Image checked = GenImageChecked(2, 2, 1, 1, (Color){ 140, 140, 140, 255 }, GRAY); Texture2D texture = LoadTextureFromImage(checked); UnloadImage(checked); @@ -106,15 +175,45 @@ void init_game() // setup camera SetTargetFPS(60); SetCameraMode(sc3d.camera, CAMERA_FIRST_PERSON); - sc3d.camera.position = (Vector3){50.0f, 1.0f, 50.0f}; + sc3d.camera.position = (Vector3){50.0f, YABS_GROUND_LEVEL + 2.0f, 50.0f}; sc3d.camera.target = (Vector3){50.0f, 1.0f, 50.0f}; sc3d.camera.up = (Vector3){0.0f, 1.0f, 0.0f}; - sc3d.camera.fovy = 90.0f; + sc3d.camera.fovy = 80.0f; sc3d.camera.projection = CAMERA_PERSPECTIVE; sc3d.rotation = (Vector3){0.0f, 0.0f, 0.0f}; sc3d.lastMousePos = GetMousePosition(); bool alive = true; + sc3d.movement = (Vector3){0.0f, 0.0f, 0.0f}; + Vector3 cubeSize = {2.0f, 2.0f, 2.0f}; + + World world = { + model + }; + + // 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; + e->enemyBoxPos.x = e->enemyStartPos.x; + e->enemyBoxPos.y = e->enemyStartPos.y; + e->enemyBoxPos.z = e->enemyStartPos.z; + e->enemyBoxSize = cubeSize; + e->active = true; + e->enemyBounds = { + {e->enemyBoxPos.x - 2.5f, 0.0f, e->enemyBoxPos.z - 2.5f}, + {e->enemyBoxPos.x + 2.5f, 2.5f, e->enemyBoxPos.z + 2.5f} + }; + } + + Texture2D crosshair; + crosshair = LoadTexture(YABS_CROSSHAIR_TEX); + DrawTexture(crosshair, YABS_SCREENWIDTH, YABS_SCREENHEIGHT, WHITE); while (alive) { @@ -122,15 +221,17 @@ void init_game() ClearBackground(GRAY); BeginMode3D(sc3d.camera); - alive = tick(sc3d); - draw(model); + alive = tick(world, sc3d); + draw(world); EndMode3D(); + DrawTexture(crosshair, YABS_HALFSWIDTH, YABS_HALFSHEIGHT, WHITE); DrawFPS(0, 0); DrawText("Controls: Mouse / WASD / SPACE/CTRL.", YABS_SCREENWIDTH - 400, 0, 20, DARKGRAY); EndDrawing(); } + // cleanupAndExit(world); UnloadTexture(texture); UnloadModel(model); } diff --git a/src/yabs_core.h b/src/yabs_core.h index 69d44d0..d8f111d 100644 --- a/src/yabs_core.h +++ b/src/yabs_core.h @@ -11,15 +11,36 @@ #include "raylib.h" #include "raymath.h" +#include <vector> + +#ifdef __FreeBSD__ +# define YABS_SCREENWIDTH 1280 +# define YABS_SCREENHEIGHT 720 +#else +# define YABS_SCREENWIDTH 1600 +# define YABS_SCREENHEIGHT 900 +#endif -#define YABS_SCREENWIDTH 1600 -#define YABS_SCREENHEIGHT 900 #define YABS_TITLE "YABS" #define YABS_DOUBLEPI 6.28318530717958647692f +// half sizes are reduced by 5 pixels to be centered +#define YABS_HALFSWIDTH (YABS_SCREENWIDTH/2) - 15 +#define YABS_HALFSHEIGHT (YABS_SCREENHEIGHT/2) - 10 + +// movement sensibility: lower is faster #define YABS_PLAYER_MOVEMENT_SENSITIVITY 3 #define YABS_CAMERA_MOUSE_MOVE_SENSITIVITY 0.0005f +#define YABS_GROUND_LEVEL 1.0f + +#define YABS_CROSSHAIR_TEX "meta/media/img/crosshair.png" +#define ENEMIES_N 10 + + + +#define YABS_COOLPURPLE \ + CLITERAL(Color) { 153, 0, 0, 255 } // cool Purple namespace yabs { @@ -39,13 +60,33 @@ typedef enum gameState { ENDING } gameState; -// core functions +// 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<Enemy*> enemies; + Player * player; +} World; + int reset_camera(Scene3D& scene_3d); -int draw(Model& model); -int tick(Scene3D& scene_3d); +int draw(World& world); +int tick(World& world, Scene3D& scene_3d); void init_game(); +void cleanupAndExit(World & world); } // namespace core |