The Battle for Wesnoth  1.19.26+dev
animation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2025
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;
60  std::chrono::milliseconds get_begin_time() const;
61  std::chrono::milliseconds get_end_time() const;
62 
63  auto get_elapsed_time() const
64  {
66  }
67 
68  void set_duration_limit(const std::chrono::milliseconds& time)
69  {
71  }
72 
73  void start_animation(const std::chrono::milliseconds& start_time
76  , const std::string& text = ""
77  , const color_t text_color = {0,0,0}
78  , const bool accelerate = true);
79 
80  void update_parameters(const map_location& src, const map_location& dst);
81  void pause_animation();
82  void resume_animation();
84  {
86  }
87  void redraw(frame_parameters& value, halo::manager& halo_man);
88  void clear_haloes();
89  bool invalidate(frame_parameters& value );
90  std::string debug() const;
91  friend std::ostream& operator << (std::ostream& outstream, const unit_animation& u_animation);
92 
93  friend class unit;
94  friend class unit_drawer;
95 
96 protected:
97  // reserved to class unit, for the special case of redrawing the unit base frame
99  {
100  return unit_anim_.parameters(default_val);
101  }
102 
103 private:
104  explicit unit_animation(const std::chrono::milliseconds& start_time
105  , const unit_frame &frame
106  , const std::string& event = ""
107  , const int variation=DEFAULT_ANIM
108  , const frame_builder & builder = frame_builder());
109 
110  class particle : public animated<unit_frame>
111  {
112  public:
113  explicit particle(const std::chrono::milliseconds& start_time = std::chrono::milliseconds{0}, const frame_builder& builder = frame_builder())
114  : animated<unit_frame>(start_time)
115  , parameters_(builder)
116  , halo_id_()
118  , cycles_(false)
119  {
120  set_uses_acceleration(true);
121  }
122  explicit particle(const config& cfg, const std::string& frame_string = "frame");
123 
124  virtual ~particle();
125  bool need_update() const;
126  bool need_minimal_update() const;
128  void override(const std::chrono::milliseconds& start_time
129  , const std::chrono::milliseconds& duration
130  , const cycle_state cycles
131  , const std::string& highlight = ""
132  , const std::string& blend_ratio =""
133  , color_t blend_color = {0,0,0}
134  , const std::string& offset = ""
135  , const std::string& layer = ""
136  , const std::string& modifiers = "");
137  void redraw(const frame_parameters& value, const map_location& src, const map_location& dst, halo::manager& halo_man);
138  std::set<map_location> get_overlaped_hex(const frame_parameters& value,const map_location& src, const map_location& dst);
139  void start_animation(const std::chrono::milliseconds& start_time);
140  frame_parameters parameters(const frame_parameters& default_val) const
141  {
143  }
144  void clear_halo();
145 
146  private:
147  //animation params that can be locally overridden by frames
150  std::chrono::milliseconds last_frame_begin_time_;
151  bool cycles_;
152  };
153 
155  std::vector<config> unit_filter_;
156  std::vector<config> secondary_unit_filter_;
157  std::vector<map_location::direction> directions_;
160  std::vector<std::string> event_;
161  std::vector<int> value_;
162  std::vector<config> primary_attack_filter_;
163  std::vector<config> secondary_attack_filter_;
164  std::vector<strike_result::type> hits_;
165  std::vector<int> value2_;
166  std::map<std::string,particle> sub_anims_;
168  /* these are drawing parameters, but for efficiency reason they are in the anim and not in the particle */
171  // optimization
174  std::set<map_location> overlaped_hex_;
175 };
176 
178 {
179 public:
180  void add_animation(unit_const_ptr animated_unit
181  , const unit_animation* animation
183  , bool with_bars = false
184  , const std::string& text = ""
185  , const color_t text_color = {0,0,0});
186 
187  void add_animation(unit_const_ptr animated_unit
188  , const std::string& event
191  , const int value = 0
192  , bool with_bars = false
193  , const std::string& text = ""
194  , const color_t text_color = {0,0,0}
195  , const strike_result::type hit_type = strike_result::type::invalid
196  , const const_attack_ptr& attack = nullptr
197  , const const_attack_ptr& second_attack = nullptr
198  , int value2 = 0);
199 
200  /** 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]
201  * @return True if the @a animated_unit is present and have animation.
202  * @param animated_unit the unit who is checked.
203  * @param event the animation who is checked([leading_anim] or [resistance_anim].
204  * @param src the location of animated_unit.
205  * @param dst location of unit student(attacker or defender).
206  * @param value value of damage.
207  * @param hit_type type of damage inflicted.
208  * @param attack weapon used by student.
209  * @param second_attack weapon used by opponent.
210  * @param value2 i don't understand myself.but this value is used in choose_animation.
211  */
212  bool has_animation(const unit_const_ptr& animated_unit
213  , const std::string& event
216  , const int value = 0
217  , const strike_result::type hit_type = strike_result::type::invalid
218  , const const_attack_ptr& attack = nullptr
219  , const const_attack_ptr& second_attack = nullptr
220  , int value2 = 0) const;
221 
222  void replace_anim_if_invalid(const unit_const_ptr& animated_unit
223  , const std::string& event
226  , const int value = 0
227  , bool with_bars = false
228  , const std::string& text = ""
229  , const color_t text_color = {0,0,0}
230  , const strike_result::type hit_type = strike_result::type::invalid
231  , const const_attack_ptr& attack = nullptr
232  , const const_attack_ptr& second_attack = nullptr
233  , int value2 = 0);
234  void start_animations();
235  void pause_animation();
236  void resume_animation();
237 
238  void clear()
239  {
240  start_time_ = std::chrono::milliseconds::min();
241  animated_units_.clear();
242  }
243 
244  void set_all_standing();
245 
246  bool would_end() const;
247  std::chrono::milliseconds get_elapsed_time() const;
248  std::chrono::milliseconds get_end_time() const;
249  void wait_for_end() const;
250  void wait_until(const std::chrono::milliseconds& animation_time) const;
251 
252 private:
253  struct anim_elem
254  {
256  const unit_animation* animation = nullptr;
257  std::string text;
260  bool with_bars = false;
261  };
262 
263  std::vector<anim_elem> animated_units_;
264  std::chrono::milliseconds start_time_ = std::chrono::milliseconds::min();
265 };
map_location loc
Definition: move.cpp:172
Frame-based animation system with support for looping, acceleration, and time manipulation.
std::chrono::milliseconds get_time_in_current_frame() const
How far into its own duration the current frame is, clamped to [0, duration].
std::chrono::milliseconds get_begin_time() const
Animation-time offset of the first frame's start.
std::chrono::milliseconds get_current_frame_begin_time() const
Animation-time offset where the current frame starts.
void set_uses_acceleration(bool uses_accel)
Selects which of the two global timelines get_current_animation_tick() reads for this animation: acce...
Definition: animated.hpp:185
void add_frame(const std::chrono::milliseconds &duration, const T &value, bool force_change=false)
Appends a frame, starting where the previous one ends (or at start_time, if first).
const T & get_last_frame() const
std::chrono::milliseconds get_elapsed_time() const
Time since playback began, per get_playback_tick().
const unit_frame & get_current_frame() const
void set_duration_limit(const std::chrono::milliseconds &time)
bool cycles() const
True if the animation loops back to the first frame instead of stopping at the last.
Definition: animated.hpp:112
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
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:930
std::set< map_location > get_overlaped_hex(const frame_parameters &value, const map_location &src, const map_location &dst)
Definition: animation.cpp:1287
void start_animation(const std::chrono::milliseconds &start_time)
Definition: animation.cpp:1299
frame_parsed_parameters parameters_
Definition: animation.hpp:148
std::chrono::milliseconds last_frame_begin_time_
Definition: animation.hpp:150
bool need_minimal_update() const
Definition: animation.cpp:938
particle(const std::chrono::milliseconds &start_time=std::chrono::milliseconds{0}, const frame_builder &builder=frame_builder())
Definition: animation.hpp:113
frame_parameters parameters(const frame_parameters &default_val) const
Definition: animation.hpp:140
void redraw(const frame_parameters &value, const map_location &src, const map_location &dst, halo::manager &halo_man)
Definition: animation.cpp:1258
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1117
bool play_offscreen_
Definition: animation.hpp:173
std::vector< config > primary_attack_filter_
Definition: animation.hpp:162
std::vector< map_location::direction > directions_
Definition: animation.hpp:157
void resume_animation()
Definition: animation.cpp:1085
std::vector< int > value2_
Definition: animation.hpp:165
bool need_update() const
Definition: animation.cpp:976
map_location src_
Definition: animation.hpp:169
bool need_minimal_update() const
Definition: animation.cpp:986
void clear_haloes()
Definition: animation.cpp:1108
std::vector< config > secondary_unit_filter_
Definition: animation.hpp:156
void try_advance_to_current_frame()
Definition: animation.cpp:1012
map_location dst_
Definition: animation.hpp:170
t_translation::ter_list terrain_types_
Definition: animation.hpp:154
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:1044
frame_parameters get_current_params(const frame_parameters &default_val=frame_parameters()) const
Definition: animation.hpp:98
std::chrono::milliseconds get_begin_time() const
Definition: animation.cpp:1034
std::vector< std::string > event_
Definition: animation.hpp:160
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:379
std::vector< int > value_
Definition: animation.hpp:161
void set_duration_limit(const std::chrono::milliseconds &time)
Definition: animation.hpp:68
std::string debug() const
Definition: animation.cpp:1161
std::map< std::string, particle > sub_anims_
Definition: animation.hpp:166
particle unit_anim_
Definition: animation.hpp:167
void update_parameters(const map_location &src, const map_location &dst)
Definition: animation.cpp:1070
std::chrono::milliseconds get_end_time() const
Definition: animation.cpp:1024
std::vector< std::string > get_flags() const
Definition: animation.hpp:51
auto get_current_frame_begin_time() const
Definition: animation.hpp:83
void pause_animation()
Definition: animation.cpp:1076
auto get_elapsed_time() const
Definition: animation.hpp:63
unit_animation()=delete
void redraw(frame_parameters &value, halo::manager &halo_man)
Definition: animation.cpp:1094
std::set< map_location > overlaped_hex_
Definition: animation.hpp:174
const unit_frame & get_last_frame() const
Definition: animation.hpp:41
friend std::ostream & operator<<(std::ostream &outstream, const unit_animation &u_animation)
Definition: animation.cpp:1168
static void fill_initial_animations(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:486
std::vector< config > secondary_attack_filter_
Definition: animation.hpp:163
std::vector< config > unit_filter_
Definition: animation.hpp:155
static void add_anims(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:644
std::vector< strike_result::type > hits_
Definition: animation.hpp:164
bool animation_finished() const
Definition: animation.cpp:1001
std::chrono::milliseconds get_elapsed_time() const
Definition: animation.cpp:1463
void pause_animation()
Definition: animation.cpp:1483
void resume_animation()
Definition: animation.cpp:1492
std::chrono::milliseconds start_time_
Definition: animation.hpp:264
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:1343
std::vector< anim_elem > animated_units_
Definition: animation.hpp:263
void wait_for_end() const
Definition: animation.cpp:1448
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:1356
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:1330
bool would_end() const
Definition: animation.cpp:1406
void start_animations()
Definition: animation.cpp:1382
void set_all_standing()
Definition: animation.cpp:1501
std::chrono::milliseconds get_end_time() const
Definition: animation.cpp:1471
void wait_until(const std::chrono::milliseconds &animation_time) const
Definition: animation.cpp:1416
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:922
This class represents a single unit of a specific type.
Definition: unit.hpp:39
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
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:51
All parameters from a frame at a given instant.
Definition: frame.hpp:44
Encapsulates the map of the game.
Definition: location.hpp:46
static const map_location & null_location()
Definition: location.hpp:103
unit_const_ptr my_unit
Definition: animation.hpp:255
const unit_animation * animation
Definition: animation.hpp:256