Advertisement
copyrat90

variable_wait_sprite_animate_action

Jul 25th, 2021
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.58 KB | None | 0 0
  1. #ifndef ISO_BN_VARIABLE_WAIT_SPRITE_ANIMATE_ACTION_H
  2. #define ISO_BN_VARIABLE_WAIT_SPRITE_ANIMATE_ACTION_H
  3.  
  4. #include "bn_sprite_animate_actions.h"
  5.  
  6. namespace iso_bn
  7. {
  8.  
  9. template <int MaxSize> class variable_wait_sprite_animate_action
  10. {
  11.     static_assert(MaxSize > 1);
  12.  
  13. public:
  14.     /**
  15.      * @brief Generates a variable_wait_sprite_animate_action which loops over the given sprite tile sets only once.
  16.      * @param sprite sprite_ptr to copy.
  17.      * @param tiles_item It creates the new sprite tiles to use by the given sprite_ptr.
  18.      * @param graphics_indexes Indexes of the tile sets to reference in tiles_item.
  19.      * @param wait_updates Variable numbers of times the action must be updated before changing the tiles of the given
  20.      * sprite_ptr.
  21.      * @return The requested sprite_animate_action.
  22.      */
  23.     [[nodiscard]] static variable_wait_sprite_animate_action once(const bn::sprite_ptr& sprite,
  24.                                                                   const bn::sprite_tiles_item& tiles_item,
  25.                                                                   const bn::span<const uint16_t>& graphics_indexes,
  26.                                                                   const bn::span<const uint16_t>& wait_updates)
  27.     {
  28.         return variable_wait_sprite_animate_action(sprite, tiles_item, false, graphics_indexes, wait_updates);
  29.     }
  30.  
  31.     /**
  32.      * @brief Generates a variable_wait_sprite_animate_action which loops over the given sprite tile sets only once.
  33.      * @param sprite sprite_ptr to move.
  34.      * @param tiles_item It creates the new sprite tiles to use by the given sprite_ptr.
  35.      * @param graphics_indexes Indexes of the tile sets to reference in tiles_item.
  36.      * @param wait_updates Variable numbers of times the action must be updated before changing the tiles of the given
  37.      * sprite_ptr.
  38.      * @return The requested sprite_animate_action.
  39.      */
  40.     [[nodiscard]] static variable_wait_sprite_animate_action once(bn::sprite_ptr&& sprite,
  41.                                                                   const bn::sprite_tiles_item& tiles_item,
  42.                                                                   const bn::span<const uint16_t>& graphics_indexes,
  43.                                                                   const bn::span<const uint16_t>& wait_updates)
  44.     {
  45.         return variable_wait_sprite_animate_action(move(sprite), tiles_item, false, graphics_indexes, wait_updates);
  46.     }
  47.  
  48.     /**
  49.      * @brief Generates a variable_wait_sprite_animate_action which loops over the given sprite tile sets forever.
  50.      * @param sprite sprite_ptr to copy.
  51.      * @param tiles_item It creates the new sprite tiles to use by the given sprite_ptr.
  52.      * @param graphics_indexes Indexes of the tile sets to reference in tiles_item.
  53.      * @param wait_updates Variable numbers of times the action must be updated before changing the tiles of the given
  54.      * sprite_ptr.
  55.      * @return The requested sprite_animate_action.
  56.      */
  57.     [[nodiscard]] static variable_wait_sprite_animate_action forever(const bn::sprite_ptr& sprite,
  58.                                                                      const bn::sprite_tiles_item& tiles_item,
  59.                                                                      const bn::span<const uint16_t>& graphics_indexes,
  60.                                                                      const bn::span<const uint16_t>& wait_updates)
  61.     {
  62.         return variable_wait_sprite_animate_action(sprite, tiles_item, true, graphics_indexes, wait_updates);
  63.     }
  64.  
  65.     /**
  66.      * @brief Generates a variable_wait_sprite_animate_action which loops over the given sprite tile sets forever.
  67.      * @param sprite sprite_ptr to move.
  68.      * @param tiles_item It creates the new sprite tiles to use by the given sprite_ptr.
  69.      * @param graphics_indexes Indexes of the tile sets to reference in tiles_item.
  70.      * @param wait_updates Variable numbers of times the action must be updated before changing the tiles of the given
  71.      * sprite_ptr.
  72.      * @return The requested sprite_animate_action.
  73.      */
  74.     [[nodiscard]] static variable_wait_sprite_animate_action forever(bn::sprite_ptr&& sprite,
  75.                                                                      const bn::sprite_tiles_item& tiles_item,
  76.                                                                      const bn::span<const uint16_t>& graphics_indexes,
  77.                                                                      const bn::span<const uint16_t>& wait_updates)
  78.     {
  79.         return variable_wait_sprite_animate_action(move(sprite), tiles_item, true, graphics_indexes, wait_updates);
  80.     }
  81.  
  82.     /**
  83.      * @brief Changes the tile set of the given sprite_ptr when the given amount of update calls are done.
  84.      */
  85.     void update()
  86.     {
  87.         BN_ASSERT(!done(), "Action is done");
  88.  
  89.         if (_started && _current_wait_updates)
  90.         {
  91.             --_current_wait_updates;
  92.         }
  93.         else
  94.         {
  95.             _action.update();
  96.  
  97.             if (!_started)
  98.             {
  99.                 _started = true;
  100.                 _current_wait_updates_index = 0;
  101.             }
  102.             else if (update_forever() && _current_wait_updates_index == _wait_updates.size() - 1)
  103.             {
  104.                 _current_wait_updates_index = 0;
  105.             }
  106.             else
  107.             {
  108.                 ++_current_wait_updates_index;
  109.             }
  110.  
  111.             _current_wait_updates = _wait_updates[_current_wait_updates_index];
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * @brief Indicates if the action must not be updated anymore.
  117.      * @return `true` if it reaches last graphics index AND updates few more times, specified by the last element of
  118.      * wait_updates.
  119.      */
  120.     [[nodiscard]] bool done() const
  121.     {
  122.         return _action.done() && _current_wait_updates <= 0;
  123.     }
  124.  
  125.     /**
  126.      * @brief Returns the sprite_ptr to modify.
  127.      */
  128.     [[nodiscard]] const bn::sprite_ptr& sprite() const
  129.     {
  130.         return _action.sprite();
  131.     }
  132.  
  133.     /**
  134.      * @brief Returns the number of times the action must be updated before changing the tiles of the given sprite_ptr.
  135.      */
  136.     [[nodiscard]] int wait_updates() const
  137.     {
  138.         return _wait_updates[_current_wait_updates_index];
  139.     }
  140.  
  141.     /**
  142.      * @brief Returns the CURRENT number of times the action must be updated before changing the tiles of the given
  143.      * sprite_ptr.
  144.      */
  145.     [[nodiscard]] int current_wait_updates() const
  146.     {
  147.         return _current_wait_updates;
  148.     }
  149.  
  150.     /**
  151.      * @brief Returns the sprite_tiles_item used to create the new sprite tiles to use by the given sprite_ptr.
  152.      */
  153.     [[nodiscard]] const bn::sprite_tiles_item& tiles_item() const
  154.     {
  155.         return _action.tiles_item();
  156.     }
  157.  
  158.     /**
  159.      * @brief Returns the indexes of the tile sets to reference in the given sprite_tiles_item.
  160.      */
  161.     [[nodiscard]] const bn::ivector<uint16_t>& graphics_indexes() const
  162.     {
  163.         return _action.graphics_indexes();
  164.     }
  165.  
  166.     /**
  167.      * @brief Indicates if the action can be updated forever or not.
  168.      */
  169.     [[nodiscard]] bool update_forever() const
  170.     {
  171.         return _action.update_forever();
  172.     }
  173.  
  174.     /**
  175.      * @brief Returns the current index of the given graphics_indexes
  176.      * (not the current index of the tile set to reference in the given tiles_item).
  177.      */
  178.     [[nodiscard]] int current_index() const
  179.     {
  180.         return _action.current_index();
  181.     }
  182.  
  183. private:
  184.     bn::sprite_animate_action<MaxSize> _action;
  185.     bn::vector<uint16_t, MaxSize> _wait_updates;
  186.     uint16_t _current_wait_updates_index = 0;
  187.     uint16_t _current_wait_updates = 0;
  188.     bool _started = false;
  189.  
  190.     variable_wait_sprite_animate_action(const bn::sprite_ptr& sprite, const bn::sprite_tiles_item& tiles_item,
  191.                                         bool forever, const bn::span<const uint16_t>& graphics_indexes,
  192.                                         const bn::span<const uint16_t>& wait_updates)
  193.         : _action(forever ? bn::sprite_animate_action<MaxSize>::forever(sprite, 0, tiles_item, graphics_indexes)
  194.                           : bn::sprite_animate_action<MaxSize>::once(sprite, 0, tiles_item, graphics_indexes))
  195.     {
  196.         BN_ASSERT(graphics_indexes.size() == wait_updates.size(), "graphics_indexes size(", graphics_indexes.size(),
  197.                   ") & wait_updates size(", wait_updates.size(), ") mismatch");
  198.  
  199.         for (uint16_t wait_update : wait_updates)
  200.         {
  201.             BN_ASSERT(wait_update >= 0, "Invalid wait update: ", wait_update);
  202.             _wait_updates.push_back(wait_update);
  203.         }
  204.     }
  205.  
  206.     variable_wait_sprite_animate_action(bn::sprite_ptr&& sprite, const bn::sprite_tiles_item& tiles_item, bool forever,
  207.                                         const bn::span<const uint16_t>& graphics_indexes,
  208.                                         const bn::span<const uint16_t>& wait_updates)
  209.         : _action(forever ? bn::sprite_animate_action<MaxSize>::forever(move(sprite), 0, tiles_item, graphics_indexes)
  210.                           : bn::sprite_animate_action<MaxSize>::once(move(sprite), 0, tiles_item, graphics_indexes))
  211.     {
  212.         BN_ASSERT(graphics_indexes.size() == wait_updates.size(), "graphics_indexes size(", graphics_indexes.size(),
  213.                   ") & wait_updates size(", wait_updates.size(), ") mismatch");
  214.  
  215.         for (uint16_t wait_update : wait_updates)
  216.         {
  217.             BN_ASSERT(wait_update >= 0, "Invalid wait update: ", wait_update);
  218.             _wait_updates.push_back(wait_update);
  219.         }
  220.     }
  221. };
  222.  
  223. } // namespace iso_bn
  224.  
  225. #endif
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement