diff --git a/main.cpp b/main.cpp index efe886e..e1a2ae9 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,15 @@ struct Position { int y; }; +struct Velocity { + int x; + int y; +}; + +struct GameTicks { + uint32_t ticks; +}; + int main() { spdlog::info("Initialize SDL..."); @@ -43,6 +52,7 @@ int main() { } flecs::world world; + world.set(GameTicks{0}); world.set(ButtonInput{}); world.set(SdlHandles{.window = window, .renderer = renderer}); init_assets(world); @@ -56,17 +66,46 @@ int main() { .custom_size = Vec2{.x = WINDOW_WIDTH, .y = WINDOW_HEIGHT}}); struct Fruit {}; + struct Basket {}; - world.entity("Sprite1") - .set(Position{.x = 0, .y = 0}) - .set( - Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 0}) - .add(); + world.system("IncrementTicks") + .term_at(0) + .singleton() + .each([](GameTicks &game_ticks) { game_ticks.ticks += 1; }); - world.entity("Sprite2") - .set(Position{.x = 32, .y = 32}) - .set( - Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 5}); + world.entity("Basket") + .set( + Position{.x = WINDOW_WIDTH / 2 - 32, .y = WINDOW_HEIGHT - 32}) + .set(Sprite{.texture = &texture_assets->fruits, + .texture_atlas_index = 212, + .custom_size = Vec2{.x = 64, .y = 16}}) + .add(); + + world.system("SpawnFruits") + .term_at(0) + .singleton() + .term_at(1) + .singleton() + .run([](flecs::iter &it) { + while (it.next()) { + auto game_ticks = it.field(0); + auto texture_assets = it.field(1); + + if ((game_ticks->ticks % 100) == 0) { + it.world() + .entity() + .set(Position{ + .x = static_cast(game_ticks->ticks % WINDOW_WIDTH), + .y = -16}) + .set(Velocity{.x = 0, .y = 1}) + .set( + Sprite{.texture = &texture_assets->fruits, + .texture_atlas_index = + static_cast(game_ticks->ticks % 228), + .custom_size = Vec2{.x = 32, .y = 32}}); + } + } + }); world.system("RenderSprites") .term_at(0) @@ -92,25 +131,27 @@ int main() { &srcrect, &dstrect); }); - world.system("MoveSprites") + world + .system( + "SetSpriteVelocity") .term_at(0) .singleton() .each([](ButtonInput const &input, Position &pos, Sprite const &sprite, - Fruit) { + Basket) { if (input.pressed.contains(SDLK_LEFT)) { - pos.x -= 1; + pos.x -= 5; } if (input.pressed.contains(SDLK_RIGHT)) { - pos.x += 1; - } - if (input.pressed.contains(SDLK_UP)) { - pos.y -= 1; - } - if (input.pressed.contains(SDLK_DOWN)) { - pos.y += 1; + pos.x += 5; } }); + world.system("MoveSprites") + .each([](Position &pos, Velocity const &vel) { + pos.x += vel.x; + pos.y += vel.y; + }); + bool exit_gameloop = false; while (!exit_gameloop) { auto *input = world.get_mut();