aboutsummaryrefslogtreecommitdiffstats
path: root/src/yabs_core.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/yabs_core.cpp')
-rw-r--r--src/yabs_core.cpp113
1 files changed, 62 insertions, 51 deletions
diff --git a/src/yabs_core.cpp b/src/yabs_core.cpp
index 5ae5b57..31cfa3c 100644
--- a/src/yabs_core.cpp
+++ b/src/yabs_core.cpp
@@ -7,15 +7,17 @@
/*********************************/
#include <raylib.h>
+#include <raymath.h>
#include <stdlib.h>
#include <iostream>
#include <strings.h>
#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 +27,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 +45,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 +65,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 +73,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 +110,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 +=
@@ -128,34 +128,35 @@ int tick(World& world, Scene3D& scene_3d) {
YABS_DOUBLEPI - scene_3d.rotation.x, 0});
Matrix transform = MatrixMultiply(translation, rotation2);
+
// apply camera rotation
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}
+ range = {
+ {scene_3d.camera.target.x - 10.5f,
+ scene_3d.camera.target.y - 1.0f,
+ scene_3d.camera.target.z - 10.5f},
+ {scene_3d.camera.target.x + 10.5f,
+ scene_3d.camera.target.y + 1.5f,
+ scene_3d.camera.target.z + 10.5f}
};
- if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
- {
- for (auto it = world.enemies.begin(); it != world.enemies.end(); ++it)
- {
- if(CheckCollisionBoxes(range, (*it)->enemyBounds))
- {
+
+ if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
+ for (auto i = 0; i < tobjs.vEnemyBounds.size(); ++i) {
+ if(CheckCollisionBoxes(range, *tobjs.vEnemyBounds[i])) { // :^}
std::cerr << "hit enemy\n";
- (*it)->active = false;
- world.enemies.erase(it);
- // erase() invalidates iterators, we have to break
- // empty space
- break;
+ break ;
+ //std::cout << "r: " <<
+ //"{" << range.min.x<< "," << range.min.x << "," << range.min.z << "}," <<
+ //"{" << range.max.x<< "," << range.max.x << "," << range.max.z << "}\n";
+ //std::cout << "b: " <<
+ //"{" << (*it)->min.x<< "," << (*it)->min.x << "," << (*it)->min.z << "}," <<
+ //"{" << (*it)->max.x << ","<< (*it)->max.x << "," << (*it)->max.z << "}\n";
}
}
+ std::cerr << "missed\n";
}
return (1);
}
@@ -187,28 +188,39 @@ 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<Vector3*>(),
model
};
+ TickObjs tobjs = {
+ std::vector<BoundingBox*>(),
+ std::vector<Vector3*>(),
+ &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.x = GetRandomValue(20, 40);
e->enemyStartPos.y = 1.0f;
- e->enemyStartPos.z = rand() % 32;
+ e->enemyStartPos.z = GetRandomValue(20, 40);
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}
+ {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 +233,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 +243,6 @@ void init_game()
0, 20, DARKGRAY);
EndDrawing();
}
- // cleanupAndExit(world);
UnloadTexture(texture);
UnloadModel(model);
}