The Battle for Wesnoth  1.19.7+dev
animation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2024
3  by Jeremy Rosen <jeremy.rosen@enst-bretagne.fr>
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 #pragma once
17 
18 #include "animated.hpp"
19 #include "color.hpp"
20 #include "config.hpp"
21 #include "halo.hpp"
22 #include "units/frame.hpp"
23 #include "units/ptr.hpp"
24 #include "units/strike_result.hpp"
25 
27 {
28 public:
29  unit_animation() = delete;
30  explicit unit_animation(const config& cfg, const std::string& frame_string = "");
31 
32  enum variation_type {MATCH_FAIL = -10 , DEFAULT_ANIM = -9};
33 
34  static void fill_initial_animations(std::vector<unit_animation>& animations, const config& cfg);
35  static void add_anims(std::vector<unit_animation>& animations, const config& cfg);
36 
37  int matches(const map_location& loc, const map_location& second_loc, const unit_const_ptr& my_unit, const std::string& event = "",
38  const int value = 0, strike_result::type hit = strike_result::type::invalid, const const_attack_ptr& attack = nullptr, const const_attack_ptr& second_attack = nullptr,
39  int value2 = 0) const;
40 
41  const unit_frame& get_last_frame() const
42  {
43  return unit_anim_.get_last_frame();
44  }
45 
46  void add_frame(const std::chrono::milliseconds& duration, const unit_frame& value, bool force_change = false)
47  {
48  unit_anim_.add_frame(duration,value,force_change);
49  }
50 
51  std::vector<std::string> get_flags() const
52  {
53  return event_;
54  }
55 
56  bool need_update() const;
57  bool need_minimal_update() const;
58  bool animation_finished() const;
59  bool animation_finished_potential() const;
60  void update_last_draw_time();
61  std::chrono::milliseconds get_begin_time() const;
62  std::chrono::milliseconds get_end_time() const;
63 
64  auto time_to_tick(const std::chrono::milliseconds& animation_time) const
65  {
66  return unit_anim_.time_to_tick(animation_time);
67  }
68 
69  auto get_animation_time() const
70  {
72  }
73 
74  void set_max_animation_time(const std::chrono::milliseconds& time)
75  {
77  }
78 
80  {
82  }
83 
84  void start_animation(const std::chrono::milliseconds& start_time
87  , const std::string& text = ""
88  , const color_t text_color = {0,0,0}
89  , const bool accelerate = true);
90 
91  void update_parameters(const map_location& src, const map_location& dst);
92  void pause_animation();
93  void restart_animation();
95  {
97  }
98  void redraw(frame_parameters& value, halo::manager& halo_man);
99  void clear_haloes();
100  bool invalidate(frame_parameters& value );
101  std::string debug() const;
102  friend std::ostream& operator << (std::ostream& outstream, const unit_animation& u_animation);
103 
104  friend class unit;
105  friend class unit_drawer;
106 
107 protected:
108  // reserved to class unit, for the special case of redrawing the unit base frame
110  {
111  return unit_anim_.parameters(default_val);
112  }
113 
114 private:
115  explicit unit_animation(const std::chrono::milliseconds& start_time
116  , const unit_frame &frame
117  , const std::string& event = ""
118  , const int variation=DEFAULT_ANIM
119  , const frame_builder & builder = frame_builder());
120 
121  class particle : public animated<unit_frame>
122  {
123  public:
124  explicit particle(const std::chrono::milliseconds& start_time = std::chrono::milliseconds{0}, const frame_builder& builder = frame_builder())
125  : animated<unit_frame>(start_time)
126  , accelerate(true)
127  , parameters_(builder)
128  , halo_id_()
130  , cycles_(false)
131  {}
132  explicit particle(const config& cfg, const std::string& frame_string = "frame");
133 
134  virtual ~particle();
135  bool need_update() const;
136  bool need_minimal_update() const;
138  void override(const std::chrono::milliseconds& start_time
139  , const std::chrono::milliseconds& duration
140  , const cycle_state cycles
141  , const std::string& highlight = ""
142  , const std::string& blend_ratio =""
143  , color_t blend_color = {0,0,0}
144  , const std::string& offset = ""
145  , const std::string& layer = ""
146  , const std::string& modifiers = "");
147  void redraw(const frame_parameters& value, const map_location& src, const map_location& dst, halo::manager& halo_man);
148  std::set<map_location> get_overlaped_hex(const frame_parameters& value,const map_location& src, const map_location& dst);
149  void start_animation(const std::chrono::milliseconds& start_time);
150  frame_parameters parameters(const frame_parameters& default_val) const
151  {
153  }
154  void clear_halo();
156 
157  private:
158  //animation params that can be locally overridden by frames
161  std::chrono::milliseconds last_frame_begin_time_;
162  bool cycles_;
163  };
164 
166  std::vector<config> unit_filter_;
167  std::vector<config> secondary_unit_filter_;
168  std::vector<map_location::direction> directions_;
171  std::vector<std::string> event_;
172  std::vector<int> value_;
173  std::vector<config> primary_attack_filter_;
174  std::vector<config> secondary_attack_filter_;
175  std::vector<strike_result::type> hits_;
176  std::vector<int> value2_;
177  std::map<std::string,particle> sub_anims_;
179  /* these are drawing parameters, but for efficiency reason they are in the anim and not in the particle */
182  // optimization
185  std::set<map_location> overlaped_hex_;
186 };
187 
189 {
190 public:
191  void add_animation(unit_const_ptr animated_unit
192  , const unit_animation* animation
194  , bool with_bars = false
195  , const std::string& text = ""
196  , const color_t text_color = {0,0,0});
197 
198  void add_animation(unit_const_ptr animated_unit
199  , const std::string& event
202  , const int value = 0
203  , bool with_bars = false
204  , const std::string& text = ""
205  , const color_t text_color = {0,0,0}
206  , const strike_result::type hit_type = strike_result::type::invalid
207  , const const_attack_ptr& attack = nullptr
208  , const const_attack_ptr& second_attack = nullptr
209  , int value2 = 0);
210 
211  /** has_animation : return an boolean value if animated unit present and have animation specified, used for verify prensence of [leading_anim] or [resistance_anim] for playability of [teaching_anim]
212  * @return True if the @a animated_unit is present and have animation.
213  * @param animated_unit the unit who is checked.
214  * @param event the animation who is checked([leading_anim] or [resistance_anim].
215  * @param src the location of animated_unit.
216  * @param dst location of unit student(attacker or defender).
217  * @param value value of damage.
218  * @param hit_type type of damage inflicted.
219  * @param attack weapon used by student.
220  * @param second_attack weapon used by opponent.
221  * @param value2 i don't understand myself.but this value is used in choose_animation.
222  */
223  bool has_animation(const unit_const_ptr& animated_unit
224  , const std::string& event
227  , const int value = 0
228  , const strike_result::type hit_type = strike_result::type::invalid
229  , const const_attack_ptr& attack = nullptr
230  , const const_attack_ptr& second_attack = nullptr
231  , int value2 = 0) const;
232 
233  void replace_anim_if_invalid(const unit_const_ptr& animated_unit
234  , const std::string& event
237  , const int value = 0
238  , bool with_bars = false
239  , const std::string& text = ""
240  , const color_t text_color = {0,0,0}
241  , const strike_result::type hit_type = strike_result::type::invalid
242  , const const_attack_ptr& attack = nullptr
243  , const const_attack_ptr& second_attack = nullptr
244  , int value2 = 0);
245  void start_animations();
246  void pause_animation();
247  void restart_animation();
248 
249  void clear()
250  {
251  start_time_ = std::chrono::milliseconds::min();
252  animated_units_.clear();
253  }
254 
255  void set_all_standing();
256 
257  bool would_end() const;
258  std::chrono::milliseconds get_animation_time() const;
259  std::chrono::milliseconds get_animation_time_potential() const;
260  std::chrono::milliseconds get_end_time() const;
261  void wait_for_end() const;
262  void wait_until(const std::chrono::milliseconds& animation_time) const;
263 
264 private:
265  struct anim_elem
266  {
268  const unit_animation* animation = nullptr;
269  std::string text;
272  bool with_bars = false;
273  };
274 
275  std::vector<anim_elem> animated_units_;
276  std::chrono::milliseconds start_time_ = std::chrono::milliseconds::min();
277 };
map_location loc
Definition: move.cpp:172
Animate units.
std::chrono::milliseconds get_animation_time_potential() const
std::chrono::milliseconds get_begin_time() const
std::chrono::milliseconds get_current_frame_begin_time() const
std::chrono::milliseconds get_animation_time() const
void set_max_animation_time(const std::chrono::milliseconds &time)
void add_frame(const std::chrono::milliseconds &duration, const T &value, bool force_change=false)
Adds a frame to an animation.
const T & get_last_frame() const
std::chrono::steady_clock::time_point time_to_tick(const std::chrono::milliseconds &animation_time) const
const unit_frame & get_current_frame() const
std::chrono::milliseconds get_current_frame_time() const
bool cycles() const
Definition: animated.hpp:75
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Easily build frame parameters with the serialized constructors.
Definition: frame.hpp:84
Keep most parameters in a separate class to simplify the handling of the large number of parameters b...
Definition: frame.hpp:148
frame_parameters parameters(const std::chrono::milliseconds &current_time) const
Getters for the different parameters.
Definition: frame.cpp:277
bool need_update() const
Definition: animation.cpp:914
std::set< map_location > get_overlaped_hex(const frame_parameters &value, const map_location &src, const map_location &dst)
Definition: animation.cpp:1276
void start_animation(const std::chrono::milliseconds &start_time)
Definition: animation.cpp:1288
frame_parsed_parameters parameters_
Definition: animation.hpp:159
std::chrono::milliseconds last_frame_begin_time_
Definition: animation.hpp:161
bool need_minimal_update() const
Definition: animation.cpp:922
particle(const std::chrono::milliseconds &start_time=std::chrono::milliseconds{0}, const frame_builder &builder=frame_builder())
Definition: animation.hpp:124
frame_parameters parameters(const frame_parameters &default_val) const
Definition: animation.hpp:150
void redraw(const frame_parameters &value, const map_location &src, const map_location &dst, halo::manager &halo_man)
Definition: animation.cpp:1247
auto get_animation_time_potential() const
Definition: animation.hpp:79
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1106
bool play_offscreen_
Definition: animation.hpp:184
auto time_to_tick(const std::chrono::milliseconds &animation_time) const
Definition: animation.hpp:64
std::vector< config > primary_attack_filter_
Definition: animation.hpp:173
std::vector< map_location::direction > directions_
Definition: animation.hpp:168
std::vector< int > value2_
Definition: animation.hpp:176
bool need_update() const
Definition: animation.cpp:959
map_location src_
Definition: animation.hpp:180
bool need_minimal_update() const
Definition: animation.cpp:969
void clear_haloes()
Definition: animation.cpp:1097
bool animation_finished_potential() const
Definition: animation.cpp:994
std::vector< config > secondary_unit_filter_
Definition: animation.hpp:167
map_location dst_
Definition: animation.hpp:181
t_translation::ter_list terrain_types_
Definition: animation.hpp:165
void add_frame(const std::chrono::milliseconds &duration, const unit_frame &value, bool force_change=false)
Definition: animation.hpp:46
void start_animation(const std::chrono::milliseconds &start_time, const map_location &src=map_location::null_location(), const map_location &dst=map_location::null_location(), const std::string &text="", const color_t text_color={0, 0, 0}, const bool accelerate=true)
Definition: animation.cpp:1033
frame_parameters get_current_params(const frame_parameters &default_val=frame_parameters()) const
Definition: animation.hpp:109
std::chrono::milliseconds get_begin_time() const
Definition: animation.cpp:1023
std::vector< std::string > event_
Definition: animation.hpp:171
int matches(const map_location &loc, const map_location &second_loc, const unit_const_ptr &my_unit, const std::string &event="", const int value=0, strike_result::type hit=strike_result::type::invalid, const const_attack_ptr &attack=nullptr, const const_attack_ptr &second_attack=nullptr, int value2=0) const
Definition: animation.cpp:377
std::vector< int > value_
Definition: animation.hpp:172
std::string debug() const
Definition: animation.cpp:1150
void update_last_draw_time()
Definition: animation.cpp:1004
std::map< std::string, particle > sub_anims_
Definition: animation.hpp:177
particle unit_anim_
Definition: animation.hpp:178
void update_parameters(const map_location &src, const map_location &dst)
Definition: animation.cpp:1059
std::chrono::milliseconds get_end_time() const
Definition: animation.cpp:1013
std::vector< std::string > get_flags() const
Definition: animation.hpp:51
auto get_current_frame_begin_time() const
Definition: animation.hpp:94
void pause_animation()
Definition: animation.cpp:1065
void restart_animation()
Definition: animation.cpp:1074
unit_animation()=delete
void redraw(frame_parameters &value, halo::manager &halo_man)
Definition: animation.cpp:1083
std::set< map_location > overlaped_hex_
Definition: animation.hpp:185
const unit_frame & get_last_frame() const
Definition: animation.hpp:41
auto get_animation_time() const
Definition: animation.hpp:69
friend std::ostream & operator<<(std::ostream &outstream, const unit_animation &u_animation)
Definition: animation.cpp:1157
void set_max_animation_time(const std::chrono::milliseconds &time)
Definition: animation.hpp:74
static void fill_initial_animations(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:484
std::vector< config > secondary_attack_filter_
Definition: animation.hpp:174
std::vector< config > unit_filter_
Definition: animation.hpp:166
static void add_anims(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:628
std::vector< strike_result::type > hits_
Definition: animation.hpp:175
bool animation_finished() const
Definition: animation.cpp:984
std::chrono::milliseconds get_animation_time_potential() const
Definition: animation.cpp:1460
void pause_animation()
Definition: animation.cpp:1480
std::chrono::milliseconds get_animation_time() const
Definition: animation.cpp:1452
std::chrono::milliseconds start_time_
Definition: animation.hpp:276
bool has_animation(const unit_const_ptr &animated_unit, const std::string &event, const map_location &src=map_location::null_location(), const map_location &dst=map_location::null_location(), const int value=0, const strike_result::type hit_type=strike_result::type::invalid, const const_attack_ptr &attack=nullptr, const const_attack_ptr &second_attack=nullptr, int value2=0) const
has_animation : return an boolean value if animated unit present and have animation specified,...
Definition: animation.cpp:1332
std::vector< anim_elem > animated_units_
Definition: animation.hpp:275
void wait_for_end() const
Definition: animation.cpp:1437
void replace_anim_if_invalid(const unit_const_ptr &animated_unit, const std::string &event, const map_location &src=map_location::null_location(), const map_location &dst=map_location::null_location(), const int value=0, bool with_bars=false, const std::string &text="", const color_t text_color={0, 0, 0}, const strike_result::type hit_type=strike_result::type::invalid, const const_attack_ptr &attack=nullptr, const const_attack_ptr &second_attack=nullptr, int value2=0)
Definition: animation.cpp:1345
void add_animation(unit_const_ptr animated_unit, const unit_animation *animation, const map_location &src=map_location::null_location(), bool with_bars=false, const std::string &text="", const color_t text_color={0, 0, 0})
Definition: animation.cpp:1319
bool would_end() const
Definition: animation.cpp:1395
void start_animations()
Definition: animation.cpp:1371
void set_all_standing()
Definition: animation.cpp:1498
std::chrono::milliseconds get_end_time() const
Definition: animation.cpp:1468
void restart_animation()
Definition: animation.cpp:1489
void wait_until(const std::chrono::milliseconds &animation_time) const
Definition: animation.cpp:1405
Describes a unit's animation sequence.
Definition: frame.hpp:208
frame_parameters merge_parameters(const std::chrono::milliseconds &current_time, const frame_parameters &animation_val, const frame_parameters &engine_val=frame_parameters()) const
This function merges the value provided by:
Definition: frame.cpp:917
This class represents a single unit of a specific type.
Definition: unit.hpp:133
Definitions for the interface to Wesnoth Markup Language (WML).
Frame for unit's animation sequence.
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
std::vector< terrain_code > ter_list
Definition: translation.hpp:77
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
All parameters from a frame at a given instant.
Definition: frame.hpp:44
Encapsulates the map of the game.
Definition: location.hpp:45
static const map_location & null_location()
Definition: location.hpp:102
unit_const_ptr my_unit
Definition: animation.hpp:267
const unit_animation * animation
Definition: animation.hpp:268