The Battle for Wesnoth  1.17.0-dev
animation_component.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
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 
49  /** Copy construct a unit animation component, for use when copy constructing a unit. */
51  u_(my_unit),
52  anim_(nullptr),
54  state_(o.state_),
55  next_idling_(0),
59  unit_halo_() {}
60 
61  /** Chooses an appropriate animation from the list of known animations. */
62  const unit_animation* choose_animation(const display& disp,
63  const map_location& loc, const std::string& event,
64  const map_location& second_loc = map_location::null_location(),
65  const int damage=0,
66  const unit_animation::hit_type hit_type = unit_animation::hit_type::INVALID,
67  const_attack_ptr attack=nullptr,const_attack_ptr second_attack = nullptr,
68  int swing_num =0);
69 
70  /** Sets the animation state to standing. */
71  void set_standing(bool with_bars = true);
72 
73  /** Sets the animation state to ghosted. (For use with whiteboard / planning mode.) */
74  void set_ghosted(bool with_bars = true);
75 
76  /** Whiteboard related somehow. TODO: Figure out exactly what this does. */
77  void set_disabled_ghosted(bool with_bars = true);
78 
79  /** Sets the animation state to idling. */
80  void set_idling();
81 
82  /** Sets the animation state to that when the unit is selected */
83  void set_selecting();
84 
85  /** Begin an animation. */
86  void start_animation (int start_time, const unit_animation *animation,
87  bool with_bars, const std::string &text = "",
88  color_t text_color = {}, STATE state = STATE_ANIM);
89 
90  /** Invalidates an animation with respect to a display object, preparing it for redraw. */
91  bool invalidate(const display & disp);
92 
93  /** Intermittently activates the idling animations in place of the standing animations. Used by display object. */
94  void refresh();
95 
96  /** Clear the haloes associated to the unit */
97  void clear_haloes();
98 
99  /** Resets the animations list after the unit is advanced. */
100  void reset_after_advance(const unit_type * newtype = nullptr);
101 
102  /** Adds an animation described by a config. Uses an internal cache to avoid redoing work. */
103  void apply_new_animation_effect(const config & effect);
104 
105  /** Get a pointer to the current animation. */
106  unit_animation* get_animation() const { return anim_.get(); }
107 
108  /** Get the flags of all registered animations. */
109  std::vector<std::string> get_flags();
110 
111  friend class unit;
112  friend class unit_drawer;
113 private:
114  /** A reference to the unit that owns this object. It does so with a scoped pointer, so this reference should not dangle. */
115  const unit & u_;
116 
117  /** The current animation. */
118  std::unique_ptr<unit_animation> anim_;
119  /** List of registered animations for this unit. */
120  std::vector<unit_animation> animations_;
121 
122  /** animation state */
124 
125  /** time for next idle animation */
127  /** time for the frame to begin */
129 
130  /** bool indicating whether to draw bars with the unit */
132  /** avoid infinite recursion. flag used for drawing / animation */
134 
135  /** handle to the halo of this unit */
137 };
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.
bool refreshing_
avoid infinite recursion.
const unit & u_
A reference to the unit that owns this object.
This class represents a single unit of a specific type.
Definition: unit.hpp:121
void set_disabled_ghosted(bool with_bars=true)
Whiteboard related somehow.
std::unique_ptr< unit_animation > anim_
The current animation.
void set_selecting()
Sets the animation state to that when the unit is selected.
animation will be automatically replaced by a standing anim when finished
bool draw_bars_
bool indicating whether to draw bars with the unit
void refresh()
Intermittently activates the idling animations in place of the standing animations.
unit_animation * get_animation() const
Get a pointer to the current animation.
int frame_begin_time_
time for the frame to begin
A single unit type that the player may recruit.
Definition: types.hpp:45
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 clear_haloes()
Clear the haloes associated to the unit.
const unit_animation * choose_animation(const display &disp, const map_location &loc, const std::string &event, const map_location &second_loc=map_location::null_location(), const int damage=0, const unit_animation::hit_type hit_type=unit_animation::hit_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.
STATE state_
animation state
bool invalidate(const display &disp)
Invalidates an animation with respect to a display object, preparing it for redraw.
STATE
States for animation.
Encapsulates the map of the game.
Definition: location.hpp:38
void set_idling()
Sets the animation state to idling.
std::vector< std::string > get_flags()
Get the flags of all registered animations.
void apply_new_animation_effect(const config &effect)
Adds an animation described by a config.
std::vector< unit_animation > animations_
List of registered animations for this unit.
unit_animation_component(unit &my_unit)
normal anims
static const map_location & null_location()
Definition: location.hpp:81
halo::handle unit_halo_
handle to the halo of this unit
void set_standing(bool with_bars=true)
Sets the animation state to standing.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
std::shared_ptr< halo_record > handle
Definition: halo.hpp:30
void set_ghosted(bool with_bars=true)
Sets the animation state to ghosted.
void reset_after_advance(const unit_type *newtype=nullptr)
Resets the animations list after the unit is advanced.
int next_idling_
time for next idle animation