diff --git a/engine/samples/CMakeLists.txt b/engine/samples/CMakeLists.txt index 5cc228c49..13efc6804 100644 --- a/engine/samples/CMakeLists.txt +++ b/engine/samples/CMakeLists.txt @@ -47,5 +47,4 @@ make_sample(DIR "gizmos") make_sample(DIR "ui") make_sample(DIR "physics" ASSETS) make_sample(DIR "complex-physics" ASSETS) - -make_sample(DIR "games/subway-surfers" ASSETS) +make_sample(DIR "games/cubosurfers" ASSETS SOURCES "spawner.cpp" "obstacle.cpp" "player.cpp") diff --git a/engine/samples/games/cubosurfers/assets/input.bind b/engine/samples/games/cubosurfers/assets/input.bind new file mode 100644 index 000000000..97b13a586 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/input.bind @@ -0,0 +1,21 @@ +{ + "actions": { + "left": { + "keys": [ + "A", + "Left" + ] + }, + "right": { + "keys": [ + "D", + "Right" + ] + }, + "restart": { + "keys": [ + "R" + ] + } + } +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/input.bind.meta b/engine/samples/games/cubosurfers/assets/input.bind.meta new file mode 100644 index 000000000..9f0941f21 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/input.bind.meta @@ -0,0 +1,3 @@ +{ + "id": "b20900a4-20ee-4caa-8830-14585050bead" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/main.pal b/engine/samples/games/cubosurfers/assets/main.pal new file mode 100644 index 000000000..762980ab5 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/main.pal differ diff --git a/engine/samples/games/cubosurfers/assets/main.pal.meta b/engine/samples/games/cubosurfers/assets/main.pal.meta new file mode 100644 index 000000000..86f0bdf82 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/main.pal.meta @@ -0,0 +1,3 @@ +{ + "id": "101da567-3d23-46ae-a391-c10ec00e8718" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/scenes/main.cubos b/engine/samples/games/cubosurfers/assets/scenes/main.cubos new file mode 100644 index 000000000..53e98e86b --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/scenes/main.cubos @@ -0,0 +1,108 @@ +{ + "entities": { + "render-target": { + "cubos::engine::RenderTargetDefaults": {} + }, + "player": { + "cubos::engine::RenderVoxelGrid": { + "asset": "57d1b886-8543-4b8b-8f78-d911e9c4f896", + "offset": { + "x": -4, + "y": 0, + "z": -4 + } + }, + "cubos::engine::Position": { + "x": 0, + "y": 0, + "z": 0 + }, + "cubos::engine::BoxCollisionShape": { + "x": 3.0, + "y": 7.0, + "z": 2.0 + }, + "cubos::engine::Collider": {}, + "Player": { + "speed": 5, + "laneWidth": 9 + } + }, + "track1": { + "cubos::engine::RenderVoxelGrid": { + "asset": "746088e3-84ec-4483-b4b2-216ae690c3b7", + "offset": { + "x": -16, + "y": -1, + "z": 0 + } + }, + "cubos::engine::Position": { + "x": 0, + "y": 0, + "z": -48 + }, + "cubos::engine::Scale": 2 + }, + "track2": { + "cubos::engine::RenderVoxelGrid": { + "asset": "746088e3-84ec-4483-b4b2-216ae690c3b7", + "offset": { + "x": -16, + "y": -1, + "z": 0 + } + }, + "cubos::engine::Position": { + "x": 0, + "y": 0, + "z": 208 + }, + "cubos::engine::Scale": 2 + }, + "obstacle-spawner": { + "cubos::engine::Position": { + "x": 0, + "y": 0, + "z": 300 + }, + "Spawner": { + "scene": "b15272c5-08c9-47f5-9b24-1192332580a8", + "sceneRoot": "base", + "period": 1.0, + "laneWidth": 9 + } + }, + "sun": { + "cubos::engine::DirectionalLight": { + "color": { + "x": 1.0, + "y": 1.0, + "z": 1.0 + }, + "intensity": 1.0 + }, + "cubos::engine::Rotation": { + "w": 0.783, + "x": 0.488, + "y": 0.327, + "z": 0.204 + } + }, + "camera": { + "cubos::engine::PerspectiveCamera": {}, + "cubos::engine::DrawsTo@render-target": {}, + "cubos::engine::Position": { + "x": 0, + "y": 12.5, + "z": -25 + }, + "cubos::engine::Rotation": { + "x": 0, + "y": 1, + "z": 0, + "w": 0 + } + } + } +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/scenes/main.cubos.meta b/engine/samples/games/cubosurfers/assets/scenes/main.cubos.meta new file mode 100644 index 000000000..270021716 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/scenes/main.cubos.meta @@ -0,0 +1,3 @@ +{ + "id": "ee5bb451-05b7-430f-a641-a746f7009eef" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/scenes/obstacle.cubos b/engine/samples/games/cubosurfers/assets/scenes/obstacle.cubos new file mode 100644 index 000000000..bc59962f3 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/scenes/obstacle.cubos @@ -0,0 +1,34 @@ +{ + "entities": { + "base": { + "cubos::engine::RenderVoxelGrid": { + "asset": "fe862296-fab7-4bf9-b62e-3cc28448dff1", + "offset": { + "x": -4, + "y": 0, + "z": -4 + } + }, + "cubos::engine::Rotation": { + "x": 0, + "y": 1, + "z": 0, + "w": 0 + }, + "cubos::engine::BoxCollisionShape": { + "x": 3.0, + "y": 7.0, + "z": 2.0 + }, + "cubos::engine::Collider": {}, + "Obstacle": { + "velocity": { + "x": 0, + "y": 0, + "z": -100 + }, + "killZ": -10 + } + } + } +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/scenes/obstacle.cubos.meta b/engine/samples/games/cubosurfers/assets/scenes/obstacle.cubos.meta new file mode 100644 index 000000000..5663933ea --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/scenes/obstacle.cubos.meta @@ -0,0 +1,3 @@ +{ + "id": "b15272c5-08c9-47f5-9b24-1192332580a8" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/voxels/enemy/base.grd b/engine/samples/games/cubosurfers/assets/voxels/enemy/base.grd new file mode 100644 index 000000000..b2ff14368 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/enemy/base.grd differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/enemy/base.grd.meta b/engine/samples/games/cubosurfers/assets/voxels/enemy/base.grd.meta new file mode 100644 index 000000000..47bc30ca2 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/voxels/enemy/base.grd.meta @@ -0,0 +1,3 @@ +{ + "id": "fe862296-fab7-4bf9-b62e-3cc28448dff1" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/voxels/enemy/base.qb b/engine/samples/games/cubosurfers/assets/voxels/enemy/base.qb new file mode 100644 index 000000000..ddca40097 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/enemy/base.qb differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/enemy/base.vox b/engine/samples/games/cubosurfers/assets/voxels/enemy/base.vox new file mode 100644 index 000000000..2d9c54e9d Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/enemy/base.vox differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/base.grd b/engine/samples/games/cubosurfers/assets/voxels/player/base.grd new file mode 100644 index 000000000..79d6a61df Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/base.grd differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/base.grd.meta b/engine/samples/games/cubosurfers/assets/voxels/player/base.grd.meta new file mode 100644 index 000000000..7eb5aa1c5 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/voxels/player/base.grd.meta @@ -0,0 +1,3 @@ +{ + "id": "57d1b886-8543-4b8b-8f78-d911e9c4f896" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/base.qb b/engine/samples/games/cubosurfers/assets/voxels/player/base.qb new file mode 100644 index 000000000..63935b7cb Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/base.qb differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/base.vox b/engine/samples/games/cubosurfers/assets/voxels/player/base.vox new file mode 100644 index 000000000..f318d756b Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/base.vox differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.grd b/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.grd new file mode 100644 index 000000000..e5a86aed7 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.grd differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.grd.meta b/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.grd.meta new file mode 100644 index 000000000..d1822138e --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.grd.meta @@ -0,0 +1,3 @@ +{ + "id": "c7263b46-be18-47c2-b3ef-05592b2e9dec" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.qb b/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.qb new file mode 100644 index 000000000..01dc830b2 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.qb differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.vox b/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.vox new file mode 100644 index 000000000..9ea980d87 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/jetpack.vox differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/shield.grd b/engine/samples/games/cubosurfers/assets/voxels/player/shield.grd new file mode 100644 index 000000000..6038d689a Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/shield.grd differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/shield.grd.meta b/engine/samples/games/cubosurfers/assets/voxels/player/shield.grd.meta new file mode 100644 index 000000000..76b984ed0 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/voxels/player/shield.grd.meta @@ -0,0 +1,3 @@ +{ + "id": "4892c2f3-10b3-4ca7-9de3-822b77a0ba7e" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/shield.qb b/engine/samples/games/cubosurfers/assets/voxels/player/shield.qb new file mode 100644 index 000000000..83645cc06 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/shield.qb differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/player/shield.vox b/engine/samples/games/cubosurfers/assets/voxels/player/shield.vox new file mode 100644 index 000000000..985df5363 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/player/shield.vox differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.grd b/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.grd new file mode 100644 index 000000000..6947907a0 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.grd differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.grd.meta b/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.grd.meta new file mode 100644 index 000000000..178774b8c --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.grd.meta @@ -0,0 +1,3 @@ +{ + "id": "732498a6-7675-4efa-86ef-8f35d3bec485" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.qb b/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.qb new file mode 100644 index 000000000..916edbfca Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.qb differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.vox b/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.vox new file mode 100644 index 000000000..fdc5cb9ad Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/power_up/jetpack.vox differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.grd b/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.grd new file mode 100644 index 000000000..3b3f53b19 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.grd differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.grd.meta b/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.grd.meta new file mode 100644 index 000000000..096452591 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.grd.meta @@ -0,0 +1,3 @@ +{ + "id": "fd89da0f-72d2-4ef9-9297-f73ff2ca5c63" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.qb b/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.qb new file mode 100644 index 000000000..f3e640efe Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.qb differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.vox b/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.vox new file mode 100644 index 000000000..4e2d5af2a Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/power_up/shield.vox differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/track/track.grd b/engine/samples/games/cubosurfers/assets/voxels/track/track.grd new file mode 100644 index 000000000..bedac064e Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/track/track.grd differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/track/track.grd.meta b/engine/samples/games/cubosurfers/assets/voxels/track/track.grd.meta new file mode 100644 index 000000000..bb2a58e50 --- /dev/null +++ b/engine/samples/games/cubosurfers/assets/voxels/track/track.grd.meta @@ -0,0 +1,3 @@ +{ + "id": "746088e3-84ec-4483-b4b2-216ae690c3b7" +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/assets/voxels/track/track.qb b/engine/samples/games/cubosurfers/assets/voxels/track/track.qb new file mode 100644 index 000000000..210fdb29f Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/track/track.qb differ diff --git a/engine/samples/games/cubosurfers/assets/voxels/track/track.vox b/engine/samples/games/cubosurfers/assets/voxels/track/track.vox new file mode 100644 index 000000000..ff781a2f8 Binary files /dev/null and b/engine/samples/games/cubosurfers/assets/voxels/track/track.vox differ diff --git a/engine/samples/games/cubosurfers/main.cpp b/engine/samples/games/cubosurfers/main.cpp new file mode 100644 index 000000000..1e3ba1caf --- /dev/null +++ b/engine/samples/games/cubosurfers/main.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "obstacle.hpp" +#include "player.hpp" +#include "spawner.hpp" + +using namespace cubos::engine; + +static const Asset SceneAsset = AnyAsset("ee5bb451-05b7-430f-a641-a746f7009eef"); +static const Asset PaletteAsset = AnyAsset("101da567-3d23-46ae-a391-c10ec00e8718"); +static const Asset InputBindingsAsset = AnyAsset("b20900a4-20ee-4caa-8830-14585050bead"); + +int main() +{ + Cubos cubos{}; + + cubos.plugin(defaultsPlugin); + cubos.plugin(spawnerPlugin); + cubos.plugin(obstaclePlugin); + cubos.plugin(playerPlugin); + + cubos.startupSystem("configure settings").tagged(settingsTag).call([](Settings& settings) { + settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER); + }); + + cubos.startupSystem("set the palette, environment, input bindings and spawn the scene") + .tagged(assetsTag) + .call([](Commands commands, const Assets& assets, RenderPalette& palette, Input& input, + RenderEnvironment& environment) { + palette.asset = PaletteAsset; + environment.ambient = {0.1F, 0.1F, 0.1F}; + environment.skyGradient[0] = {0.2F, 0.4F, 0.8F}; + environment.skyGradient[1] = {0.6F, 0.6F, 0.8F}; + input.bind(*assets.read(InputBindingsAsset)); + commands.spawn(assets.read(SceneAsset)->blueprint); + }); + + cubos.system("restart the game on input") + .call([](Commands cmds, const Assets& assets, const Input& input, Query all) { + if (input.justPressed("restart")) + { + for (auto [ent] : all) + { + cmds.destroy(ent); + } + + cmds.spawn(assets.read(SceneAsset)->blueprint); + } + }); + + cubos.system("detect player vs obstacle collisions") + .call([](Query collisions) { + for (auto [player, collidingWith, obstacle] : collisions) + { + CUBOS_INFO("Player collided with an obstacle!"); + (void)player; // here to shut up 'unused variable warning', you can remove it + } + }); + + cubos.run(); +} diff --git a/engine/samples/games/cubosurfers/obstacle.cpp b/engine/samples/games/cubosurfers/obstacle.cpp new file mode 100644 index 000000000..cd636073b --- /dev/null +++ b/engine/samples/games/cubosurfers/obstacle.cpp @@ -0,0 +1,41 @@ +#include "obstacle.hpp" + +#include +#include +#include +#include + +#include +#include + +using namespace cubos::engine; + +CUBOS_REFLECT_IMPL(Obstacle) +{ + return cubos::core::ecs::TypeBuilder("Obstacle") + .withField("velocity", &Obstacle::velocity) + .withField("killZ", &Obstacle::killZ) + .build(); +} + +void obstaclePlugin(cubos::engine::Cubos& cubos) +{ + cubos.depends(assetsPlugin); + cubos.depends(transformPlugin); + + cubos.component(); + + cubos.system("move obstacles") + .call([](Commands cmds, const DeltaTime& dt, Query obstacles) { + for (auto [ent, obstacle, position] : obstacles) + { + position.vec += obstacle.velocity * dt.value(); + position.vec.y = glm::abs(glm::sin(position.vec.z * 0.15F)) * 1.5F; + + if (position.vec.z < obstacle.killZ) + { + cmds.destroy(ent); + } + } + }); +} diff --git a/engine/samples/games/cubosurfers/obstacle.hpp b/engine/samples/games/cubosurfers/obstacle.hpp new file mode 100644 index 000000000..f4cfd5fe5 --- /dev/null +++ b/engine/samples/games/cubosurfers/obstacle.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include + +struct Obstacle +{ + CUBOS_REFLECT; + + glm::vec3 velocity{0.0F, 0.0F, -1.0F}; + float killZ{0.0F}; +}; + +void obstaclePlugin(cubos::engine::Cubos& cubos); diff --git a/engine/samples/games/cubosurfers/player.cpp b/engine/samples/games/cubosurfers/player.cpp new file mode 100644 index 000000000..aeb538a70 --- /dev/null +++ b/engine/samples/games/cubosurfers/player.cpp @@ -0,0 +1,59 @@ +#include "player.hpp" + +#include +#include + +#include +#include + +using namespace cubos::engine; + +CUBOS_REFLECT_IMPL(Player) +{ + return cubos::core::ecs::TypeBuilder("Player") + .withField("speed", &Player::speed) + .withField("laneWidth", &Player::laneWidth) + .build(); +} + +void playerPlugin(cubos::engine::Cubos& cubos) +{ + cubos.depends(inputPlugin); + cubos.depends(transformPlugin); + + cubos.component(); + + cubos.system("move player").call([](Input& input, const DeltaTime& dt, Query players) { + for (auto [player, position] : players) + { + if (input.pressed("left") && player.lane == player.targetLane) + { + player.targetLane = glm::clamp(player.lane - 1, -1, 1); + } + + if (input.pressed("right") && player.lane == player.targetLane) + { + player.targetLane = glm::clamp(player.lane + 1, -1, 1); + } + + if (player.lane != player.targetLane) + { + auto sourceX = static_cast(-player.lane) * player.laneWidth; + auto targetX = static_cast(-player.targetLane) * player.laneWidth; + float currentT = (position.vec.x - sourceX) / (targetX - sourceX); + float newT = glm::min(1.0F, currentT + dt.value() * player.speed); + position.vec.x = glm::mix(sourceX, targetX, newT); + position.vec.y = glm::sin(currentT * glm::pi()) * 2.0F; + + if (newT == 1.0F) + { + player.lane = player.targetLane; + } + } + else + { + position.vec.y = 0; + } + } + }); +} diff --git a/engine/samples/games/cubosurfers/player.hpp b/engine/samples/games/cubosurfers/player.hpp new file mode 100644 index 000000000..ce24bedb3 --- /dev/null +++ b/engine/samples/games/cubosurfers/player.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +struct Player +{ + CUBOS_REFLECT; + + float speed{1.0F}; // Speed of the player + float laneWidth{1.0F}; // Width of the lane + int lane{0}; // Current lane + int targetLane{0}; // Target lane +}; + +void playerPlugin(cubos::engine::Cubos& cubos); diff --git a/engine/samples/games/cubosurfers/spawner.cpp b/engine/samples/games/cubosurfers/spawner.cpp new file mode 100644 index 000000000..92c0ed4f8 --- /dev/null +++ b/engine/samples/games/cubosurfers/spawner.cpp @@ -0,0 +1,47 @@ +#include "spawner.hpp" + +#include +#include +#include + +#include +#include + +using namespace cubos::engine; + +CUBOS_REFLECT_IMPL(Spawner) +{ + return cubos::core::ecs::TypeBuilder("Spawner") + .withField("scene", &Spawner::scene) + .withField("sceneRoot", &Spawner::sceneRoot) + .withField("period", &Spawner::period) + .withField("laneWidth", &Spawner::laneWidth) + .withField("accumulator", &Spawner::accumulator) + .build(); +} + +void spawnerPlugin(cubos::engine::Cubos& cubos) +{ + cubos.depends(assetsPlugin); + cubos.depends(transformPlugin); + + cubos.component(); + + cubos.system("spawn stuff") + .call([](Commands commands, const DeltaTime& dt, Assets& assets, Query spawners) { + for (auto [spawner, position] : spawners) + { + spawner.accumulator += dt.value(); + if (spawner.accumulator >= spawner.period) + { + spawner.accumulator -= spawner.period; + + Position spawnPosition = position; + int offset = (rand() % 3) - 1; + spawnPosition.vec.x += static_cast(offset) * spawner.laneWidth; + + commands.spawn(assets.read(spawner.scene)->blueprint).add(spawner.sceneRoot, spawnPosition); + } + } + }); +} \ No newline at end of file diff --git a/engine/samples/games/cubosurfers/spawner.hpp b/engine/samples/games/cubosurfers/spawner.hpp new file mode 100644 index 000000000..43901cff6 --- /dev/null +++ b/engine/samples/games/cubosurfers/spawner.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include + +struct Spawner +{ + CUBOS_REFLECT; + + cubos::engine::Asset scene; // Scene which will be spawned + std::string sceneRoot; // Root entity of the scene + float period{1.0F}; // Time between spawns in seconds + float laneWidth{1.0F}; // Width of the lane + float accumulator{0.0F}; // Time accumulator +}; + +void spawnerPlugin(cubos::engine::Cubos& cubos); diff --git a/engine/samples/games/subway-surfers/main.cpp b/engine/samples/games/subway-surfers/main.cpp deleted file mode 100644 index f9058051c..000000000 --- a/engine/samples/games/subway-surfers/main.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace cubos::engine; - -static const Asset SceneAsset = AnyAsset("05db7643-a8b8-49cd-8c16-0992136cfacf"); -static const Asset PaletteAsset = AnyAsset("1aa5e234-28cb-4386-99b4-39386b0fc215"); - -int main() -{ - Cubos cubos{}; - - cubos.plugin(settingsPlugin); - cubos.plugin(windowPlugin); - cubos.plugin(transformPlugin); - cubos.plugin(assetsPlugin); - cubos.plugin(voxelsPlugin); - cubos.plugin(renderDefaultsPlugin); - cubos.plugin(scenePlugin); - - cubos.startupSystem("configure settings").tagged(settingsTag).call([](Settings& settings) { - settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER); - settings.setBool("window.vSync", false); - }); - - cubos.startupSystem("set the palette and environment and spawn the scene") - .tagged(assetsTag) - .call([](Commands commands, const Assets& assets, RenderPalette& palette, RenderEnvironment& environment) { - palette.asset = PaletteAsset; - environment.ambient = {0.1F, 0.1F, 0.1F}; - environment.skyGradient[0] = {0.1F, 0.2F, 0.4F}; - environment.skyGradient[1] = {0.6F, 0.6F, 0.8F}; - commands.spawn(assets.read(SceneAsset)->blueprint); - }); - - cubos.system("log FPS").call([](const DeltaTime& dt) { CUBOS_INFO("FPS: {}", 1.0F / dt.unscaledValue); }); - - cubos.run(); -} diff --git a/engine/samples/games/subway-surfers/readme.md b/engine/samples/games/subway-surfers/readme.md deleted file mode 100644 index f4dfc21c8..000000000 --- a/engine/samples/games/subway-surfers/readme.md +++ /dev/null @@ -1,9 +0,0 @@ -### Subway Surfers sample - -In this directory you can find a simple game base made to be used in the 2nd Cubos recruitment (08/06/2024). - -Compile and run it with -``` -$ /usr/bin/cmake --build /home/roby/repos/cubos/build --config Debug --target engine-sample.games.subway-surfers -$ /home/roby/repos/cubos/build/engine-sample.games.subway-surfers -``` \ No newline at end of file