The Battle for Wesnoth  1.19.0-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  const 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:
194  animated_units_(),
195  start_time_(INT_MIN)
196  {}
197 
198  void add_animation(unit_const_ptr animated_unit
199  , const unit_animation* animation
201  , bool with_bars = false
202  , const std::string& text = ""
203  , const color_t text_color = {0,0,0});
204 
205  void add_animation(unit_const_ptr animated_unit
206  , const std::string& event
209  , const int value = 0
210  , bool with_bars = false
211  , const std::string& text = ""
212  , const color_t text_color = {0,0,0}
213  , const strike_result::type hit_type = strike_result::type::invalid
214  , const_attack_ptr attack = nullptr
215  , const_attack_ptr second_attack = nullptr
216  , int value2 = 0);
217 
218  /** 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]
219  * @return True if the @a animated_unit is present and have animation.
220  * @param animated_unit the unit who is checked.
221  * @param event the animation who is checked([leading_anim] or [resistance_anim].
222  * @param src the location of animated_unit.
223  * @param dst location of unit student(attacker or defender).
224  * @param value value of damage.
225  * @param hit_type type of damage inflicted.
226  * @param attack weapon used by student.
227  * @param second_attack weapon used by opponent.
228  * @param value2 i don't understand myself.but this value is used in choose_animation.
229  */
230  bool has_animation(unit_const_ptr animated_unit
231  , const std::string& event
234  , const int value = 0
235  , const strike_result::type hit_type = strike_result::type::invalid
236  , const_attack_ptr attack = nullptr
237  , const_attack_ptr second_attack = nullptr
238  , int value2 = 0) const;
239 
240  void replace_anim_if_invalid(unit_const_ptr animated_unit
241  , const std::string& event
244  , const int value = 0
245  , bool with_bars = false
246  , const std::string& text = ""
247  , const color_t text_color = {0,0,0}
248  , const strike_result::type hit_type = strike_result::type::invalid
249  , const_attack_ptr attack = nullptr
250  , const_attack_ptr second_attack = nullptr
251  , int value2 = 0);
252  void start_animations();
253  void pause_animation();
254  void restart_animation();
255 
256  void clear()
257  {
258  start_time_ = INT_MIN;
259  animated_units_.clear();
260  }
261 
262  void set_all_standing();
263 
264  bool would_end() const;
265  int get_animation_time() const;
266  int get_animation_time_potential() const;
267  int get_end_time() const;
268  void wait_for_end() const;
269  void wait_until( int animation_time) const;
270 
271 private:
272  struct anim_elem
273  {
275  : my_unit()
276  , animation(0)
277  , text()
278  , text_color()
279  , src()
280  , with_bars(false)
281  {}
282 
285  std::string text;
288  bool with_bars;
289  };
290 
291  std::vector<anim_elem> animated_units_;
293 };
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:159
Easily build frame parameters with the serialized constructors.
Definition: frame.hpp:78
Keep most parameters in a separate class to simplify the handling of the large number of parameters b...
Definition: frame.hpp:142
const frame_parameters parameters(int current_time) const
Getters for the different parameters.
Definition: frame.cpp:292
bool need_update() const
Definition: animation.cpp:914
const frame_parameters parameters(const frame_parameters &default_val) const
Definition: animation.hpp:152
void start_animation(int start_time)
Definition: animation.cpp:1288
std::set< map_location > get_overlaped_hex(const frame_parameters &value, const map_location &src, const map_location &dst)
Definition: animation.cpp:1276
frame_parsed_parameters parameters_
Definition: animation.hpp:161
bool need_minimal_update() const
Definition: animation.cpp:922
particle(int start_time=0, const frame_builder &builder=frame_builder())
Definition: animation.hpp:126
void redraw(const frame_parameters &value, const map_location &src, const map_location &dst, halo::manager &halo_man)
Definition: animation.cpp:1247
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1106
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< int > value2_
Definition: animation.hpp:178
bool need_update() const
Definition: animation.cpp:959
map_location src_
Definition: animation.hpp:182
bool need_minimal_update() const
Definition: animation.cpp:969
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:1033
void clear_haloes()
Definition: animation.cpp:1097
int get_animation_time() const
Definition: animation.hpp:70
bool animation_finished_potential() const
Definition: animation.cpp:994
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:377
map_location dst_
Definition: animation.hpp:183
t_translation::ter_list terrain_types_
Definition: animation.hpp:167
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:1150
void update_last_draw_time()
Definition: animation.cpp:1004
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:1059
std::vector< std::string > get_flags() const
Definition: animation.hpp:52
void pause_animation()
Definition: animation.cpp:1065
void restart_animation()
Definition: animation.cpp:1074
int get_begin_time() const
Definition: animation.cpp:1023
unit_animation()=delete
int get_end_time() const
Definition: animation.cpp:1013
const frame_parameters get_current_params(const frame_parameters &default_val=frame_parameters()) const
Definition: animation.hpp:111
void redraw(frame_parameters &value, halo::manager &halo_man)
Definition: animation.cpp:1083
std::set< map_location > overlaped_hex_
Definition: animation.hpp:187
const unit_frame & get_last_frame() const
Definition: animation.hpp:42
std::vector< map_location::DIRECTION > directions_
Definition: animation.hpp:170
friend std::ostream & operator<<(std::ostream &outstream, const unit_animation &u_animation)
Definition: animation.cpp:1157
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: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:628
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:984
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:1361
void wait_until(int animation_time) const
Definition: animation.cpp:1429
void pause_animation()
Definition: animation.cpp:1505
int get_animation_time() const
Definition: animation.cpp:1477
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:1348
std::vector< anim_elem > animated_units_
Definition: animation.hpp:291
void wait_for_end() const
Definition: animation.cpp:1460
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:1325
bool would_end() const
Definition: animation.cpp:1419
void start_animations()
Definition: animation.cpp:1395
void set_all_standing()
Definition: animation.cpp:1523
int get_animation_time_potential() const
Definition: animation.cpp:1485
void restart_animation()
Definition: animation.cpp:1514
int get_end_time() const
Definition: animation.cpp:1493
Describes a unit's animation sequence.
Definition: frame.hpp:201
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:861
This class represents a single unit of a specific type.
Definition: unit.hpp:133
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
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:36
Encapsulates the map of the game.
Definition: location.hpp:38
static const map_location & null_location()
Definition: location.hpp:81
unit_const_ptr my_unit
Definition: animation.hpp:283
const unit_animation * animation
Definition: animation.hpp:284