From 4c706e4f6c7adc87284202f564509c6df18bd140 Mon Sep 17 00:00:00 2001 From: naezith Date: Sun, 27 Oct 2019 14:46:21 +0300 Subject: [PATCH] feat(tutorial): flappy bird destroy polish --- tutorials/flappy-bird/step_7/flappy-bird.cpp | 37 +++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/tutorials/flappy-bird/step_7/flappy-bird.cpp b/tutorials/flappy-bird/step_7/flappy-bird.cpp index 8f8210de..e460000e 100644 --- a/tutorials/flappy-bird/step_7/flappy-bird.cpp +++ b/tutorials/flappy-bird/step_7/flappy-bird.cpp @@ -56,12 +56,23 @@ struct flappy_bird_constants { struct pipe { entt::entity body{entt::null}; entt::entity cap{entt::null}; + + void destroy(entt::registry ®istry) { + registry.destroy(body); + registry.destroy(cap); + } }; struct column { //! Entities representing the Flappy Bird pipes pipe top_pipe{entt::null}; pipe bottom_pipe{entt::null}; + + void destroy(entt::registry ®istry, entt::entity entity) { + top_pipe.destroy(registry); + bottom_pipe.destroy(registry); + registry.destroy(entity); + } }; //! Contains all the function that will be used for logic and factory @@ -216,6 +227,7 @@ namespace { } } + //! Factory for creating the player entt::entity create_player(entt::registry ®istry) { //! Retrieve constants const auto [canvas_width, canvas_height] = registry.ctx().canvas.size; @@ -235,7 +247,7 @@ namespace { class column_logic final : public ecs::logic_update_system { public: - column_logic(entt::registry ®istry) noexcept : system(registry) { } + explicit column_logic(entt::registry ®istry) noexcept : system(registry) { } void update() noexcept final { auto& registry = entity_registry_; @@ -244,9 +256,7 @@ class column_logic final : public ecs::logic_update_system { const auto constants = registry.ctx(); // Loop all columns - auto view = registry.view(); - - for(auto entity : view) { + for(auto entity : registry.view()) { auto& col = registry.get(entity); // Move pipes, and check if they are out of the screen @@ -255,11 +265,7 @@ class column_logic final : public ecs::logic_update_system { // If column is out of the screen if(col_out_of_screen) { // Remove this column - registry.destroy(col.top_pipe.body); - registry.destroy(col.top_pipe.cap); - registry.destroy(col.bottom_pipe.body); - registry.destroy(col.bottom_pipe.cap); - registry.destroy(entity); + col.destroy(registry, entity); // Create a new column at far end create_column(registry, furthest_pipe_position(registry) + constants.column_distance); @@ -343,7 +349,7 @@ class player_logic final : public ecs::logic_update_system { private: entt::entity player; - math::vec2f movement_speed; + math::vec2f movement_speed{0.f, 0.f}; bool jump_key_pressed_last_tick = false; }; @@ -468,8 +474,7 @@ class game_scene final : public scenes::base_scene { this->need_reset = true; } - void post_update() noexcept final - { + void post_update() noexcept final { if(need_reset) { //! Reinitialize all these init_dynamic_objects(entity_registry_); @@ -480,10 +485,10 @@ class game_scene final : public scenes::base_scene { ecs::system_manager& system_manager; // States - bool started_playing; - bool player_died; - bool game_over; - bool jump_key_pressed_last_tick; + bool started_playing{false}; + bool player_died{false}; + bool game_over{false}; + bool jump_key_pressed_last_tick{false}; bool need_reset{false}; };