/*********************************/ /* YABS ( // */ /* main ( )/ */ /* by salade )(/ */ /* ________________ ( /) */ /* ()__)____________))))) :^} */ /*********************************/ #include #include #include #include #include "yabs_core.h" #include "yabs_utils.h" namespace yabs { namespace core { 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 {0.0f, 0.0f}, // rotation {0.0f, -0.2f, 0.0f}, // mouse {0.0f, 0.0f}}; 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++) { Vector3 position; position.x = x; position.y = -0.5f; position.z = z; DrawModel(robjs.ground, position, 1.0f, WHITE); } } for (auto & pos : robjs.vEnemyPos) { DrawCube(*pos, 2.0f, 2.0f, 2.0f, YABS_COOLPURPLE); } return (0); } int tick(TickObjs& tobjs, Scene3D& scene_3d) { // mouse look Vector2 mouseMovement = Vector2Subtract(GetMousePosition(), scene_3d.lastMousePos); scene_3d.lastMousePos = GetMousePosition(); scene_3d.rotation.x += (mouseMovement.x * -YABS_CAMERA_MOUSE_MOVE_SENSITIVITY); scene_3d.rotation.y += (mouseMovement.y * -YABS_CAMERA_MOUSE_MOVE_SENSITIVITY); // direction array 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.x = (sinf(scene_3d.rotation.x) * direction[1] - sinf(scene_3d.rotation.x) * direction[0] - cosf(scene_3d.rotation.x) * direction[3] + cosf(scene_3d.rotation.x) * direction[2]) / YABS_PLAYER_MOVEMENT_SENSITIVITY; scene_3d.movement.z = (cosf(scene_3d.rotation.x) * direction[1] - 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; // elevation 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)) { *tobjs.alive = false; return (0); } // apply movement scene_3d.camera.position.x += scene_3d.movement.x / YABS_PLAYER_MOVEMENT_SENSITIVITY; scene_3d.camera.position.y += scene_3d.movement.y; scene_3d.camera.position.z += scene_3d.movement.z / YABS_PLAYER_MOVEMENT_SENSITIVITY; Matrix translation = MatrixTranslate(0, 0, (10)); Matrix rotation2 = MatrixRotateXYZ((Vector3){YABS_DOUBLEPI - scene_3d.rotation.y, 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, 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"; break ; // erase() invalidates iterators, we have to break // empty space } } } return (1); } void init_game() { // create image Image checked = GenImageChecked(2, 2, 1, 1, (Color){ 140, 140, 140, 255 }, GRAY); Texture2D texture = LoadTextureFromImage(checked); UnloadImage(checked); // create model with image as texture Model model = {}; model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // setup camera SetTargetFPS(60); SetCameraMode(sc3d.camera, CAMERA_FIRST_PERSON); 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 = 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}; 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(); 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} }; robjs.vEnemyPos.push_back(&e->enemyBoxPos); tobjs.vEnemyBounds.push_back(&e->enemyBounds); tobjs.vEnemyPos.push_back(&e->enemyBoxPos); } Texture2D crosshair; crosshair = LoadTexture(YABS_CROSSHAIR_TEX); DrawTexture(crosshair, YABS_SCREENWIDTH, YABS_SCREENHEIGHT, WHITE); while (alive) { BeginDrawing(); ClearBackground(GRAY); BeginMode3D(sc3d.camera); tick(tobjs, sc3d); render(robjs); EndMode3D(); DrawTexture(crosshair, YABS_HALFSWIDTH, YABS_HALFSHEIGHT, WHITE); DrawFPS(0, 0); DrawText("Controls: Mouse / WASD / SPACE/CTRL.", YABS_SCREENWIDTH - 400, 0, 20, DARKGRAY); EndDrawing(); } UnloadTexture(texture); UnloadModel(model); } int reset_camera(Scene3D& scene_3d) { SetCameraMode(scene_3d.camera, CAMERA_FIRST_PERSON); scene_3d.camera.position = (Vector3){50.0f, 1.0f, 50.0f}; scene_3d.camera.target = (Vector3){50.0f, 1.0f, 50.0f}; scene_3d.camera.up = (Vector3){0.0f, 1.0f, 0.0f}; scene_3d.camera.fovy = 90.0f; scene_3d.camera.projection = CAMERA_PERSPECTIVE; scene_3d.rotation = (Vector3){0.0f, 0.0f, 0.0f}; scene_3d.lastMousePos = GetMousePosition(); return (0); } } // namespace core } // namespace yabs