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.cpp94
1 files changed, 50 insertions, 44 deletions
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 <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 +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<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.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);
}