The Battle for Wesnoth  1.19.5+dev
animation_component.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 // This class encapsulates the animation functionality of unit.
17 
18 #pragma once
19 
20 #include "halo.hpp"
21 #include "units/animation.hpp" //Note: only needed for enum
22 
23 class config;
24 class unit;
25 class unit_type;
26 
28 {
29 public:
30  /** States for animation. */
31  enum STATE {
32  STATE_STANDING, /** anim must fit in a hex */
33  STATE_FORGET, /** animation will be automatically replaced by a standing anim when finished */
34  STATE_ANIM}; /** normal anims */
35 
36  /** Default construct a unit animation component corresponding to a unit. */
38  : u_(my_unit)
39  , anim_(nullptr)
40  , animations_()
42  , next_idling_()
44  , draw_bars_(false)
45  , refreshing_(false)
46  , unit_halo_()
47  , abil_halos_()
48  , abil_halos_ref_()
49  {
50  }
51 
52  /** Copy construct a unit animation component, for use when copy constructing a unit. */
54  : u_(my_unit)
55  , anim_(nullptr)
57  , state_(o.state_)
58  , next_idling_()
62  , unit_halo_()
63  , abil_halos_()
64  , abil_halos_ref_()
65  {
66  }
67 
68  /** Chooses an appropriate animation from the list of known animations. */
70  const map_location& loc, const std::string& event,
71  const map_location& second_loc = map_location::null_location(),
72  const int damage=0,
73  const strike_result::type hit_type = strike_result::type::invalid,
74  const_attack_ptr attack=nullptr,const_attack_ptr second_attack = nullptr,
75  int swing_num =0);
76 
77  /** Sets the animation state to standing. */
78  void set_standing(bool with_bars = true);
79 
80  /** Sets the animation state to ghosted. (For use with whiteboard / planning mode.) */
81  void set_ghosted(bool with_bars = true);
82 
83  /** Whiteboard related somehow. TODO: Figure out exactly what this does. */
84  void set_disabled_ghosted(bool with_bars = true);
85 
86  /** Sets the animation state to idling. */
87  void set_idling();
88 
89  /** Sets the animation state to that when the unit is selected */
90  void set_selecting();
91 
92  /** Begin an animation. */
93  void start_animation(const std::chrono::milliseconds& start_time, const unit_animation *animation,
94  bool with_bars, const std::string &text = "",
95  color_t text_color = {}, STATE state = STATE_ANIM);
96 
97  /** Invalidates an animation with respect to a display object, preparing it for redraw. */
98  bool invalidate(const display & disp);
99 
100  /** Intermittently activates the idling animations in place of the standing animations. Used by display object. */
101  void refresh();
102 
103  /** Clear the haloes associated to the unit */
104  void clear_haloes();
105 
106  /** Resets the animations list after the unit is advanced. */
107  void reset_after_advance(const unit_type * newtype = nullptr);
108 
109  /** Adds an animation described by a config. Uses an internal cache to avoid redoing work. */
110  void apply_new_animation_effect(const config & effect);
111 
112  /** Get a pointer to the current animation. */
113  unit_animation* get_animation() const { return anim_.get(); }
114 
115  /** Get the flags of all registered animations. */
116  std::vector<std::string> get_flags();
117 
118  friend class unit;
119  friend class unit_drawer;
120 private:
121  /** A reference to the unit that owns this object. It does so with a scoped pointer, so this reference should not dangle. */
122  const unit & u_;
123 
124  /** The current animation. */
125  std::unique_ptr<unit_animation> anim_;
126  /** List of registered animations for this unit. */
127  std::vector<unit_animation> animations_;
128 
129  /** animation state */
131 
132  /** time for next idle animation */
133  std::chrono::steady_clock::time_point next_idling_;
134  /** time for the frame to begin */
135  std::chrono::milliseconds frame_begin_time_;
136 
137  /** bool indicating whether to draw bars with the unit */
139  /** avoid infinite recursion. flag used for drawing / animation */
141 
142  /** handle to the halo of this unit */
144  /** handle to the abilities halos of this unit */
145  std::vector<halo::handle> abil_halos_;
146  /** vector used to check that halo_abilities vector isn't modified between each read */
147  std::vector<std::string> abil_halos_ref_;
148 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:97
void set_idling()
Sets the animation state to idling.
bool draw_bars_
bool indicating whether to draw bars with the unit
std::vector< halo::handle > abil_halos_
handle to the abilities halos of this unit
const unit_animation * choose_animation(const map_location &loc, const std::string &event, const map_location &second_loc=map_location::null_location(), const int damage=0, const strike_result::type hit_type=strike_result::type::invalid, const_attack_ptr attack=nullptr, const_attack_ptr second_attack=nullptr, int swing_num=0)
Chooses an appropriate animation from the list of known animations.
std::vector< unit_animation > animations_
List of registered animations for this unit.
void apply_new_animation_effect(const config &effect)
Adds an animation described by a config.
std::unique_ptr< unit_animation > anim_
The current animation.
halo::handle unit_halo_
handle to the halo of this unit
bool invalidate(const display &disp)
Invalidates an animation with respect to a display object, preparing it for redraw.
void set_ghosted(bool with_bars=true)
Sets the animation state to ghosted.
unit_animation_component(unit &my_unit, const unit_animation_component &o)
Copy construct a unit animation component, for use when copy constructing a unit.
void refresh()
Intermittently activates the idling animations in place of the standing animations.
void reset_after_advance(const unit_type *newtype=nullptr)
Resets the animations list after the unit is advanced.
std::vector< std::string > get_flags()
Get the flags of all registered animations.
void set_standing(bool with_bars=true)
Sets the animation state to standing.
const unit & u_
A reference to the unit that owns this object.
std::vector< std::string > abil_halos_ref_
vector used to check that halo_abilities vector isn't modified between each read
STATE
States for animation.
@ STATE_ANIM
animation will be automatically replaced by a standing anim when finished
@ STATE_FORGET
anim must fit in a hex
void set_selecting()
Sets the animation state to that when the unit is selected.
bool refreshing_
avoid infinite recursion.
void clear_haloes()
Clear the haloes associated to the unit.
unit_animation * get_animation() const
Get a pointer to the current animation.
unit_animation_component(unit &my_unit)
normal anims
void set_disabled_ghosted(bool with_bars=true)
Whiteboard related somehow.
std::chrono::milliseconds frame_begin_time_
time for the frame to begin
void start_animation(const std::chrono::milliseconds &start_time, const unit_animation *animation, bool with_bars, const std::string &text="", color_t text_color={}, STATE state=STATE_ANIM)
Begin an animation.
std::chrono::steady_clock::time_point next_idling_
time for next idle animation
A single unit type that the player may recruit.
Definition: types.hpp:43
This class represents a single unit of a specific type.
Definition: unit.hpp:133
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
Encapsulates the map of the game.
Definition: location.hpp:45
static const map_location & null_location()
Definition: location.hpp:102