aboutsummaryrefslogtreecommitdiffstats
path: root/src/yabs_core.cpp
diff options
context:
space:
mode:
authorsalaaad2 <arthurdurant263@gmail.com>2022-06-26 22:43:58 +0200
committersalaaad2 <arthurdurant263@gmail.com>2022-06-26 22:43:58 +0200
commitfbf68206b15236e41db7f3214a57f80a539dcb15 (patch)
treeac439a71c76df5715ffd62121c4356d01e916eba /src/yabs_core.cpp
parentrefactor (diff)
downloadyabs-fbf68206b15236e41db7f3214a57f80a539dcb15.tar.gz
yabs-fbf68206b15236e41db7f3214a57f80a539dcb15.tar.bz2
yabs-fbf68206b15236e41db7f3214a57f80a539dcb15.tar.xz
yabs-fbf68206b15236e41db7f3214a57f80a539dcb15.tar.zst
yabs-fbf68206b15236e41db7f3214a57f80a539dcb15.zip
small refactor along with cube slaying
Diffstat (limited to 'src/yabs_core.cpp')
-rw-r--r--src/yabs_core.cpp135
1 files changed, 118 insertions, 17 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);
}