The Battle for Wesnoth  1.19.0-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_drawer;
26 class unit_type;
27 
29 {
30 public:
31  /** States for animation. */
32  enum STATE {
33  STATE_STANDING, /** anim must fit in a hex */
34  STATE_FORGET, /** animation will be automatically replaced by a standing anim when finished */
35  STATE_ANIM}; /** normal anims */
36 
37  /** Default construct a unit animation component corresponding to a unit. */
39  u_(my_unit),
40  anim_(nullptr),
41  animations_(),
43  next_idling_(0),
45  draw_bars_(false),
46  refreshing_(false),
47  unit_halo_(),
48  abil_halos_(),
49  abil_halos_ref_() {}
50 
51  /** Copy construct a unit animation component, for use when copy constructing a unit. */
53  u_(my_unit),
54  anim_(nullptr),
56  state_(o.state_),
57  next_idling_(0),
61  unit_halo_(),
62  abil_halos_(),
63  abil_halos_ref_() {}
64 
65  /** Chooses an appropriate animation from the list of known animations. */
67  const map_location& loc, const std::string& event,
68  const map_location& second_loc = map_location::null_location(),
69  const int damage=0,
70  const strike_result::type hit_type = strike_result::type::invalid,
71  const_attack_ptr attack=nullptr,const_attack_ptr second_attack = nullptr,
72  int swing_num =0);
73 
74  /** Sets the animation state to standing. */
75  void set_standing(bool with_bars = true);
76 
77  /** Sets the animation state to ghosted. (For use with whiteboard / planning mode.) */
78  void set_ghosted(bool with_bars = true);
79 
80  /** Whiteboard related somehow. TODO: Figure out exactly what this does. */
81  void set_disabled_ghosted(bool with_bars = true);
82 
83  /** Sets the animation state to idling. */
84  void set_idling();
85 
86  /** Sets the animation state to that when the unit is selected */
87  void set_selecting();
88 
89  /** Begin an animation. */
90  void start_animation (int start_time, const unit_animation *animation,
91  bool with_bars, const std::string &text = "",
92  color_t text_color = {}, STATE state = STATE_ANIM);
93 
94  /** Invalidates an animation with respect to a display object, preparing it for redraw. */
95  bool invalidate(const display & disp);
96 
97  /** Intermittently activates the idling animations in place of the standing animations. Used by display object. */
98  void refresh();
99 
100  /** Clear the haloes associated to the unit */
101  void clear_haloes();
102 
103  /** Resets the animations list after the unit is advanced. */
104  void reset_after_advance(const unit_type * newtype = nullptr);
105 
106  /** Adds an animation described by a config. Uses an internal cache to avoid redoing work. */
107  void apply_new_animation_effect(const config & effect);
108 
109  /** Get a pointer to the current animation. */
110  unit_animation* get_animation() const { return anim_.get(); }
111 
112  /** Get the flags of all registered animations. */
113  std::vector<std::string> get_flags();
114 
115  friend class unit;
116  friend class unit_drawer;
117 private:
118  /** A reference to the unit that owns this object. It does so with a scoped pointer, so this reference should not dangle. */
119  const unit & u_;
120 
121  /** The current animation. */
122  std::unique_ptr<unit_animation> anim_;
123  /** List of registered animations for this unit. */
124  std::vector<unit_animation> animations_;
125 
126  /** animation state */
128 
129  /** time for next idle animation */
131  /** time for the frame to begin */
133 
134  /** bool indicating whether to draw bars with the unit */
136  /** avoid infinite recursion. flag used for drawing / animation */
138 
139  /** handle to the halo of this unit */
141  /** handle to the abilities halos of this unit */
142  std::vector<halo::handle> abil_halos_;
143  /** vector used to check that halo_abilities vector isn't modified between each read */
144  std::vector<std::string> abil_halos_ref_;
145 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:86
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:46
This class represents a single unit of a specific type.
Definition: unit.hpp:135
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:38
static const map_location & null_location()
Definition: location.hpp:81