The Battle for Wesnoth  1.19.5+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 
26 
28 {
29 public:
30  unit_animation() = delete;
31  explicit unit_animation(const config& cfg, const std::string& frame_string = "");
32 
33  enum variation_type {MATCH_FAIL = -10 , DEFAULT_ANIM = -9};
34 
35  static void fill_initial_animations(std::vector<unit_animation>& animations, const config& cfg);
36  static void add_anims(std::vector<unit_animation>& animations, const config& cfg);
37 
38  int matches(const map_location& loc, const map_location& second_loc, unit_const_ptr my_unit, const std::string& event = "",
39  const int value = 0, strike_result::type hit = strike_result::type::invalid, const_attack_ptr attack = nullptr, const_attack_ptr second_attack = nullptr,
40  int value2 = 0) const;
41 
42  const unit_frame& get_last_frame() const
43  {
44  return unit_anim_.get_last_frame();
45  }
46 
47  void add_frame(int duration, const unit_frame& value, bool force_change = false)
48  {
49  unit_anim_.add_frame(duration,value,force_change);
50  }
51 
52  std::vector<std::string> get_flags() const
53  {
54  return event_;
55  }
56 
57  bool need_update() const;
58  bool need_minimal_update() const;
59  bool animation_finished() const;
60  bool animation_finished_potential() const;
61  void update_last_draw_time();
62  int get_begin_time() const;
63  int get_end_time() const;
64 
65  int time_to_tick(int animation_time) const
66  {
67  return unit_anim_.time_to_tick(animation_time);
68  }
69 
70  int get_animation_time() const
71  {
73  }
74 
75  void set_max_animation_time(int time)
76  {
78  }
79 
81  {
83  }
84 
85  void start_animation(int start_time
88  , const std::string& text = ""
89  , const color_t text_color = {0,0,0}
90  , const bool accelerate = true);
91 
92  void update_parameters(const map_location& src, const map_location& dst);
93  void pause_animation();
94  void restart_animation();
96  {
98 
99  }
100  void redraw(frame_parameters& value, halo::manager& halo_man);
101  void clear_haloes();
102  bool invalidate(frame_parameters& value );
103  std::string debug() const;
104  friend std::ostream& operator << (std::ostream& outstream, const unit_animation& u_animation);
105 
106  friend class unit;
107  friend class unit_drawer;
108 
109 protected:
110  // reserved to class unit, for the special case of redrawing the unit base frame
112  {
113  return unit_anim_.parameters(default_val);
114  }
115 
116 private:
117  explicit unit_animation(int start_time
118  , const unit_frame &frame
119  , const std::string& event = ""
120  , const int variation=DEFAULT_ANIM
121  , const frame_builder & builder = frame_builder());
122 
123  class particle : public animated<unit_frame>
124  {
125  public:
126  explicit particle(int start_time = 0, const frame_builder& builder = frame_builder())
127  : animated<unit_frame>(start_time)
128  , accelerate(true)
129  , parameters_(builder)
130  , halo_id_()
132  , cycles_(false)
133  {}
134  explicit particle(const config& cfg, const std::string& frame_string = "frame");
135 
136  virtual ~particle();
137  bool need_update() const;
138  bool need_minimal_update() const;
140  void override(int start_time
141  , int duration
142  , const cycle_state cycles
143  , const std::string& highlight = ""
144  , const std::string& blend_ratio =""
145  , color_t blend_color = {0,0,0}
146  , const std::string& offset = ""
147  , const std::string& layer = ""
148  , const std::string& modifiers = "");
149  void redraw(const frame_parameters& value, const map_location& src, const map_location& dst, halo::manager& halo_man);
150  std::set<map_location> get_overlaped_hex(const frame_parameters& value,const map_location& src, const map_location& dst);
151  void start_animation(int start_time);
152  frame_parameters parameters(const frame_parameters& default_val) const
153  {
155  }
156  void clear_halo();
158 
159  private:
160  //animation params that can be locally overridden by frames
164  bool cycles_;
165  };
166 
168  std::vector<config> unit_filter_;
169  std::vector<config> secondary_unit_filter_;
170  std::vector<map_location::direction> directions_;
173  std::vector<std::string> event_;
174  std::vector<int> value_;
175  std::vector<config> primary_attack_filter_;
176  std::vector<config> secondary_attack_filter_;
177  std::vector<strike_result::type> hits_;
178  std::vector<int> value2_;
179  std::map<std::string,particle> sub_anims_;
181  /* these are drawing parameters, but for efficiency reason they are in the anim and not in the particle */
184  // optimization
187  std::set<map_location> overlaped_hex_;
188 };
189 
191 {
192 public:
193  void add_animation(unit_const_ptr animated_unit
194  , const unit_animation* animation
196  , bool with_bars = false
197  , const std::string& text = ""
198  , const color_t text_color = {0,0,0});
199 
200  void add_animation(unit_const_ptr animated_unit
201  , const std::string& event
204  , const int value = 0
205  , bool with_bars = false
206  , const std::string& text = ""
207  , const color_t text_color = {0,0,0}
208  , const strike_result::type hit_type = strike_result::type::invalid
209  , const_attack_ptr attack = nullptr
210  , const_attack_ptr second_attack = nullptr
211  , int value2 = 0);
212 
213  /** 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]
214  * @return True if the @a animated_unit is present and have animation.
215  * @param animated_unit the unit who is checked.
216  * @param event the animation who is checked([leading_anim] or [resistance_anim].
217  * @param src the location of animated_unit.
218  * @param dst location of unit student(attacker or defender).
219  * @param value value of damage.
220  * @param hit_type type of damage inflicted.
221  * @param attack weapon used by student.
222  * @param second_attack weapon used by opponent.
223  * @param value2 i don't understand myself.but this value is used in choose_animation.
224  */
225  bool has_animation(unit_const_ptr animated_unit
226  , const std::string& event
229  , const int value = 0
230  , const strike_result::type hit_type = strike_result::type::invalid
231  , const_attack_ptr attack = nullptr
232  , const_attack_ptr second_attack = nullptr
233  , int value2 = 0) const;
234 
235  void replace_anim_if_invalid(unit_const_ptr animated_unit
236  , const std::string& event
239  , const int value = 0
240  , bool with_bars = false
241  , const std::string& text = ""
242  , const color_t text_color = {0,0,0}
243  , const strike_result::type hit_type = strike_result::type::invalid
244  , const_attack_ptr attack = nullptr
245  , const_attack_ptr second_attack = nullptr
246  , int value2 = 0);
247  void start_animations();
248  void pause_animation();
249  void restart_animation();
250 
251  void clear()
252  {
253  start_time_ = std::numeric_limits<int>::min();
254  animated_units_.clear();
255  }
256 
257  void set_all_standing();
258 
259  bool would_end() const;
260  int get_animation_time() const;
261  int get_animation_time_potential() const;
262  int get_end_time() const;
263  void wait_for_end() const;
264  void wait_until( int animation_time) const;
265 
266 private:
267  struct anim_elem
268  {
270  const unit_animation* animation = nullptr;
271  std::string text;
274  bool with_bars = false;
275  };
276 
277  std::vector<anim_elem> animated_units_;
278  int start_time_ = std::numeric_limits<int>::min();
279 };
Animate units.
int get_current_frame_begin_time() const
int time_to_tick(int animation_time) const
int get_animation_time_potential() const
void set_max_animation_time(int time)
int get_begin_time() const
const T & get_last_frame() const
int get_current_frame_time() const
const unit_frame & get_current_frame() const
int get_animation_time() const
void add_frame(int duration, const T &value, bool force_change=false)
Adds a frame to an animation.
bool cycles() const
Definition: animated.hpp:71
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:82
Keep most parameters in a separate class to simplify the handling of the large number of parameters b...
Definition: frame.hpp:146
frame_parameters parameters(int current_time) const
Getters for the different parameters.
Definition: frame.cpp:274
bool need_update() const
Definition: animation.cpp:910
void start_animation(int start_time)
Definition: animation.cpp:1284
std::set< map_location > get_overlaped_hex(const frame_parameters &value, const map_location &src, const map_location &dst)
Definition: animation.cpp:1272
frame_parsed_parameters parameters_
Definition: animation.hpp:161
bool need_minimal_update() const
Definition: animation.cpp:918
particle(int start_time=0, const frame_builder &builder=frame_builder())
Definition: animation.hpp:126
frame_parameters parameters(const frame_parameters &default_val) const
Definition: animation.hpp:152
void redraw(const frame_parameters &value, const map_location &src, const map_location &dst, halo::manager &halo_man)
Definition: animation.cpp:1243
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1102
void set_max_animation_time(int time)
Definition: animation.hpp:75
int get_animation_time_potential() const
Definition: animation.hpp:80
bool play_offscreen_
Definition: animation.hpp:186
std::vector< config > primary_attack_filter_
Definition: animation.hpp:175
std::vector< map_location::direction > directions_
Definition: animation.hpp:170
std::vector< int > value2_
Definition: animation.hpp:178
bool need_update() const
Definition: animation.cpp:955
map_location src_
Definition: animation.hpp:182
bool need_minimal_update() const
Definition: animation.cpp:965
void start_animation(int 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:1029
void clear_haloes()
Definition: animation.cpp:1093
int get_animation_time() const
Definition: animation.hpp:70
bool animation_finished_potential() const
Definition: animation.cpp:990
std::vector< config > secondary_unit_filter_
Definition: animation.hpp:169
void add_frame(int duration, const unit_frame &value, bool force_change=false)
Definition: animation.hpp:47
int matches(const map_location &loc, const map_location &second_loc, unit_const_ptr my_unit, const std::string &event="", const int value=0, strike_result::type hit=strike_result::type::invalid, const_attack_ptr attack=nullptr, const_attack_ptr second_attack=nullptr, int value2=0) const
Definition: animation.cpp:373
map_location dst_
Definition: animation.hpp:183
t_translation::ter_list terrain_types_
Definition: animation.hpp:167
frame_parameters get_current_params(const frame_parameters &default_val=frame_parameters()) const
Definition: animation.hpp:111
std::vector< std::string > event_
Definition: animation.hpp:173
int get_current_frame_begin_time() const
Definition: animation.hpp:95
std::vector< int > value_
Definition: animation.hpp:174
std::string debug() const
Definition: animation.cpp:1146
void update_last_draw_time()
Definition: animation.cpp:1000
std::map< std::string, particle > sub_anims_
Definition: animation.hpp:179
particle unit_anim_
Definition: animation.hpp:180
void update_parameters(const map_location &src, const map_location &dst)
Definition: animation.cpp:1055
std::vector< std::string > get_flags() const
Definition: animation.hpp:52
void pause_animation()
Definition: animation.cpp:1061
void restart_animation()
Definition: animation.cpp:1070
int get_begin_time() const
Definition: animation.cpp:1019
unit_animation()=delete
int get_end_time() const
Definition: animation.cpp:1009
void redraw(frame_parameters &value, halo::manager &halo_man)
Definition: animation.cpp:1079
std::set< map_location > overlaped_hex_
Definition: animation.hpp:187
const unit_frame & get_last_frame() const
Definition: animation.hpp:42
friend std::ostream & operator<<(std::ostream &outstream, const unit_animation &u_animation)
Definition: animation.cpp:1153
static void fill_initial_animations(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:480
std::vector< config > secondary_attack_filter_
Definition: animation.hpp:176
std::vector< config > unit_filter_
Definition: animation.hpp:168
static void add_anims(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:624
std::vector< strike_result::type > hits_
Definition: animation.hpp:177
int time_to_tick(int animation_time) const
Definition: animation.hpp:65
bool animation_finished() const
Definition: animation.cpp:980
void replace_anim_if_invalid(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_attack_ptr attack=nullptr, const_attack_ptr second_attack=nullptr, int value2=0)
Definition: animation.cpp:1341
void wait_until(int animation_time) const
Definition: animation.cpp:1401
void pause_animation()
Definition: animation.cpp:1477
int get_animation_time() const
Definition: animation.cpp:1449
bool has_animation(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_attack_ptr attack=nullptr, 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:1328
std::vector< anim_elem > animated_units_
Definition: animation.hpp:277
void wait_for_end() const
Definition: animation.cpp:1432
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:1315
bool would_end() const
Definition: animation.cpp:1391
void start_animations()
Definition: animation.cpp:1367
void set_all_standing()
Definition: animation.cpp:1495
int get_animation_time_potential() const
Definition: animation.cpp:1457
void restart_animation()
Definition: animation.cpp:1486
int get_end_time() const
Definition: animation.cpp:1465
Describes a unit's animation sequence.
Definition: frame.hpp:205
frame_parameters merge_parameters(int 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:914
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:42
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:269
const unit_animation * animation
Definition: animation.hpp:270