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_(0),
44  draw_bars_(false),
45  refreshing_(false),
46  unit_halo_(),
47  abil_halos_(),
48  abil_halos_ref_() {}
49 
50  /** Copy construct a unit animation component, for use when copy constructing a unit. */
52  u_(my_unit),
53  anim_(nullptr),
55  state_(o.state_),
56  next_idling_(0),
60  unit_halo_(),
61  abil_halos_(),
62  abil_halos_ref_() {}
63 
64  /** Chooses an appropriate animation from the list of known animations. */
66  const map_location& loc, const std::string& event,
67  const map_location& second_loc = map_location::null_location(),
68  const int damage=0,
69  const strike_result::type hit_type = strike_result::type::invalid,
70  const_attack_ptr attack=nullptr,const_attack_ptr second_attack = nullptr,
71  int swing_num =0);
72 
73  /** Sets the animation state to standing. */
74  void set_standing(bool with_bars = true);
75 
76  /** Sets the animation state to ghosted. (For use with whiteboard / planning mode.) */
77  void set_ghosted(bool with_bars = true);
78 
79  /** Whiteboard related somehow. TODO: Figure out exactly what this does. */
80  void set_disabled_ghosted(bool with_bars = true);
81 
82  /** Sets the animation state to idling. */
83  void set_idling();
84 
85  /** Sets the animation state to that when the unit is selected */
86  void set_selecting();
87 
88  /** Begin an animation. */
89  void start_animation (int start_time, const unit_animation *animation,
90  bool with_bars, const std::string &text = "",
91  color_t text_color = {}, STATE state = STATE_ANIM);
92 
93  /** Invalidates an animation with respect to a display object, preparing it for redraw. */
94  bool invalidate(const display & disp);
95 
96  /** Intermittently activates the idling animations in place of the standing animations. Used by display object. */
97  void refresh();
98 
99  /** Clear the haloes associated to the unit */
100  void clear_haloes();
101 
102  /** Resets the animations list after the unit is advanced. */
103  void reset_after_advance(const unit_type * newtype = nullptr);
104 
105  /** Adds an animation described by a config. Uses an internal cache to avoid redoing work. */
106  void apply_new_animation_effect(const config & effect);
107 
108  /** Get a pointer to the current animation. */
109  unit_animation* get_animation() const { return anim_.get(); }
110 
111  /** Get the flags of all registered animations. */
112  std::vector<std::string> get_flags();
113 
114  friend class unit;
115  friend class unit_drawer;
116 private:
117  /** A reference to the unit that owns this object. It does so with a scoped pointer, so this reference should not dangle. */
118  const unit & u_;
119 
120  /** The current animation. */
121  std::unique_ptr<unit_animation> anim_;
122  /** List of registered animations for this unit. */
123  std::vector<unit_animation> animations_;
124 
125  /** animation state */
127 
128  /** time for next idle animation */
130  /** time for the frame to begin */
132 
133  /** bool indicating whether to draw bars with the unit */
135  /** avoid infinite recursion. flag used for drawing / animation */
137 
138  /** handle to the halo of this unit */
140  /** handle to the abilities halos of this unit */
141  std::vector<halo::handle> abil_halos_;
142  /** vector used to check that halo_abilities vector isn't modified between each read */
143  std::vector<std::string> abil_halos_ref_;
144 };
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.
void start_animation(int start_time, const unit_animation *animation, bool with_bars, const std::string &text="", color_t text_color={}, STATE state=STATE_ANIM)
Begin an animation.
int frame_begin_time_
time for the frame to begin
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.
int next_idling_
time for next idle animation
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.
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