Use C++ random device

This commit is contained in:
2025-06-01 12:17:49 +02:00
parent ae5733ae62
commit 81f4d6157e
3 changed files with 23 additions and 18 deletions

View File

@@ -2,6 +2,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include <random>
static constexpr int WINDOW_WIDTH = 400; static constexpr int WINDOW_WIDTH = 400;
static constexpr int WINDOW_HEIGHT = 240; static constexpr int WINDOW_HEIGHT = 240;
@@ -18,4 +19,5 @@ struct Game
uint32_t ticks; uint32_t ticks;
int32_t time; int32_t time;
int32_t score; int32_t score;
std::default_random_engine random_engine;
}; };

View File

@@ -54,50 +54,53 @@ struct LevelModule
.set<Size>(Size{.w = 64, .h = 16}) .set<Size>(Size{.w = 64, .h = 16})
.add<CollisionBox>(); .add<CollisionBox>();
world.system<Game const, TextureAssets const>("SpawnFruits") world.system<Game, TextureAssets const>("SpawnFruits")
.term_at(0) .term_at(0)
.singleton() .singleton()
.term_at(1) .term_at(1)
.singleton() .singleton()
.each( .each(
[](flecs::iter& it, [](flecs::iter& it, size_t index, Game& game, TextureAssets const& texture_assets)
size_t index,
Game const& game,
TextureAssets const& texture_assets)
{ {
bool spider = std::rand() % 3 == 0; std::bernoulli_distribution spider_dist(0.25);
int vel = std::rand() % 3 + 1; bool spider = spider_dist(game.random_engine);
std::binomial_distribution<> velocity_dist(3, 0.5);
int vel = velocity_dist(game.random_engine) + 1;
if ((game.ticks % 50) == 0) if ((game.ticks % 50) == 0)
{ {
flecs::entity e; flecs::entity e;
std::uniform_int_distribution<> xpos_dist(32, WINDOW_WIDTH - 32);
int xpos = xpos_dist(game.random_engine);
if (!spider) if (!spider)
{ {
std::uniform_int_distribution<> index_dist(0, 228 - 1);
uint16_t index = index_dist(game.random_engine);
e = it.world() e = it.world()
.entity() .entity()
.add<Fruit>() .add<Fruit>()
.add<WorldPosition>() .add<WorldPosition>()
.set<Position>( .set<Position>(Position{.x = xpos, .y = -16})
Position{.x = std::rand() % WINDOW_WIDTH, .y = -16})
.set<Velocity>(Velocity{.x = 0, .y = vel}) .set<Velocity>(Velocity{.x = 0, .y = vel})
.set<Sprite>( .set<Sprite>(Sprite{.texture = &texture_assets.fruits,
Sprite{.texture = &texture_assets.fruits, .texture_atlas_index = index})
.texture_atlas_index =
static_cast<uint16_t>(std::rand() % 228)})
.set<Size>(Size{.w = 32, .h = 32}); .set<Size>(Size{.w = 32, .h = 32});
} }
else else
{ {
std::uniform_int_distribution<> index_dist(0, 8 - 1);
uint16_t index = index_dist(game.random_engine);
e = it.world() e = it.world()
.entity() .entity()
.add<Spider>() .add<Spider>()
.add<WorldPosition>() .add<WorldPosition>()
.set<Position>( .set<Position>(Position{.x = xpos, .y = -16})
Position{.x = std::rand() % WINDOW_WIDTH, .y = -16})
.set<Velocity>(Velocity{.x = 0, .y = vel}) .set<Velocity>(Velocity{.x = 0, .y = vel})
.set<Sprite>(Sprite{.texture = &texture_assets.spiders, .set<Sprite>(Sprite{.texture = &texture_assets.spiders,
.texture_atlas_index = .texture_atlas_index = index})
static_cast<uint16_t>(std::rand() % 8)})
.set<Size>(Size{.w = 32, .h = 32}); .set<Size>(Size{.w = 32, .h = 32});
} }
it.world() it.world()

View File

@@ -45,7 +45,7 @@ int main()
flecs::world world; flecs::world world;
world.set<Game>(Game{.ticks = 0, .time = 60, .score = 0}); world.set<Game>(Game{.ticks = 0, .time = 60, .score = 0, .random_engine = {}});
world.set<ButtonInput>(ButtonInput{}); world.set<ButtonInput>(ButtonInput{});
world.set<SdlHandles>( world.set<SdlHandles>(
SdlHandles{.window = window, .renderer = renderer, .text_engine = text_engine}); SdlHandles{.window = window, .renderer = renderer, .text_engine = text_engine});