The Battle for Wesnoth  1.17.12+dev
animation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2022
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 class unit;
27 
29 {
30 public:
31  unit_animation() = delete;
32  explicit unit_animation(const config& cfg, const std::string& frame_string = "");
33 
34  enum variation_type {MATCH_FAIL = -10 , DEFAULT_ANIM = -9};
35 
36  static void fill_initial_animations(std::vector<unit_animation>& animations, const config& cfg);
37  static void add_anims(std::vector<unit_animation>& animations, const config& cfg);
38 
39  int matches(const map_location& loc, const map_location& second_loc, unit_const_ptr my_unit, const std::string& event = "",
40  const int value = 0, strike_result::type hit = strike_result::type::invalid, const_attack_ptr attack = nullptr, const_attack_ptr second_attack = nullptr,
41  int value2 = 0) const;
42 
43  const unit_frame& get_last_frame() const
44  {
45  return unit_anim_.get_last_frame();
46  }
47 
48  void add_frame(int duration, const unit_frame& value, bool force_change = false)
49  {
50  unit_anim_.add_frame(duration,value,force_change);
51  }
52 
53  std::vector<std::string> get_flags() const
54  {
55  return event_;
56  }
57 
58  bool need_update() const;
59  bool need_minimal_update() const;
60  bool animation_finished() const;
61  bool animation_finished_potential() const;
62  void update_last_draw_time();
63  int get_begin_time() const;
64  int get_end_time() const;
65 
66  int time_to_tick(int animation_time) const
67  {
68  return unit_anim_.time_to_tick(animation_time);
69  }
70 
71  int get_animation_time() const
72  {
74  }
75 
76  void set_max_animation_time(int time)
77  {
79  }
80 
82  {
84  }
85 
86  void start_animation(int start_time
89  , const std::string& text = ""
90  , const color_t text_color = {0,0,0}
91  , const bool accelerate = true);
92 
93  void update_parameters(const map_location& src, const map_location& dst);
94  void pause_animation();
95  void restart_animation();
97  {
99 
100  }
101  void redraw(frame_parameters& value, halo::manager& halo_man);
102  void clear_haloes();
103  bool invalidate(frame_parameters& value );
104  std::string debug() const;
105  friend std::ostream& operator << (std::ostream& outstream, const unit_animation& u_animation);
106 
107  friend class unit;
108  friend class unit_drawer;
109 
110 protected:
111  // reserved to class unit, for the special case of redrawing the unit base frame
113  {
114  return unit_anim_.parameters(default_val);
115  }
116 
117 private:
118  explicit unit_animation(int start_time
119  , const unit_frame &frame
120  , const std::string& event = ""
121  , const int variation=DEFAULT_ANIM
122  , const frame_builder & builder = frame_builder());
123 
124  class particle : public animated<unit_frame>
125  {
126  public:
127  explicit particle(int start_time = 0, const frame_builder& builder = frame_builder())
128  : animated<unit_frame>(start_time)
129  , accelerate(true)
130  , parameters_(builder)
131  , halo_id_()
133  , cycles_(false)
134  {}
135  explicit particle(const config& cfg, const std::string& frame_string = "frame");
136 
137  virtual ~particle();
138  bool need_update() const;
139  bool need_minimal_update() const;
141  void override(int start_time
142  , int duration
143  , const cycle_state cycles
144  , const std::string& highlight = ""
145  , const std::string& blend_ratio =""
146  , color_t blend_color = {0,0,0}
147  , const std::string& offset = ""
148  , const std::string& layer = ""
149  , const std::string& modifiers = "");
150  void redraw(const frame_parameters& value, const map_location& src, const map_location& dst, halo::manager& halo_man);
151  std::set<map_location> get_overlaped_hex(const frame_parameters& value,const map_location& src, const map_location& dst);
152  void start_animation(int start_time);
153  const frame_parameters parameters(const frame_parameters& default_val) const
154  {
156  }
157  void clear_halo();
159 
160  private:
161  //animation params that can be locally overridden by frames
165  bool cycles_;
166  };
167 
169  std::vector<config> unit_filter_;
170  std::vector<config> secondary_unit_filter_;
171  std::vector<map_location::DIRECTION> directions_;
174  std::vector<std::string> event_;
175  std::vector<int> value_;
176  std::vector<config> primary_attack_filter_;
177  std::vector<config> secondary_attack_filter_;
178  std::vector<strike_result::type> hits_;
179  std::vector<int> value2_;
180  std::map<std::string,particle> sub_anims_;
182  /* these are drawing parameters, but for efficiency reason they are in the anim and not in the particle */
185  // optimization
188  std::set<map_location> overlaped_hex_;
189 };
190 
192 {
193 public:
195  animated_units_(),
196  start_time_(INT_MIN)
197  {}
198 
199  void add_animation(unit_const_ptr animated_unit
200  , const unit_animation* animation
202  , bool with_bars = false
203  , const std::string& text = ""
204  , const color_t text_color = {0,0,0});
205 
206  void add_animation(unit_const_ptr animated_unit
207  , const std::string& event
210  , const int value = 0
211  , bool with_bars = false
212  , const std::string& text = ""
213  , const color_t text_color = {0,0,0}
214  , const strike_result::type hit_type = strike_result::type::invalid
215  , const_attack_ptr attack = nullptr
216  , const_attack_ptr second_attack = nullptr
217  , int value2 = 0);
218 
219  /** 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]
220  * @return True if the @a animated_unit is present and have animation.
221  * @param animated_unit the unit who is checked.
222  * @param event the animation who is checked([leading_anim] or [resistance_anim].
223  * @param src the location of animated_unit.
224  * @param dst location of unit student(attacker or defender).
225  * @param value value of damage.
226  * @param hit_type type of damage inflicted.
227  * @param attack weapon used by student.
228  * @param second_attack weapon used by opponent.
229  * @param value2 i don't understand myself.but this value is used in choose_animation.
230  */
231  bool has_animation(unit_const_ptr animated_unit
232  , const std::string& event
235  , const int value = 0
236  , const strike_result::type hit_type = strike_result::type::invalid
237  , const_attack_ptr attack = nullptr
238  , const_attack_ptr second_attack = nullptr
239  , int value2 = 0) const;
240 
241  void replace_anim_if_invalid(unit_const_ptr animated_unit
242  , const std::string& event
245  , const int value = 0
246  , bool with_bars = false
247  , const std::string& text = ""
248  , const color_t text_color = {0,0,0}
249  , const strike_result::type hit_type = strike_result::type::invalid
250  , const_attack_ptr attack = nullptr
251  , const_attack_ptr second_attack = nullptr
252  , int value2 = 0);
253  void start_animations();
254  void pause_animation();
255  void restart_animation();
256 
257  void clear()
258  {
259  start_time_ = INT_MIN;
260  animated_units_.clear();
261  }
262 
263  void set_all_standing();
264 
265  bool would_end() const;
266  int get_animation_time() const;
267  int get_animation_time_potential() const;
268  int get_end_time() const;
269  void wait_for_end() const;
270  void wait_until( int animation_time) const;
271 
272 private:
273  struct anim_elem
274  {
276  : my_unit()
277  , animation(0)
278  , text()
279  , text_color()
280  , src()
281  , with_bars(false)
282  {}
283 
286  std::string text;
289  bool with_bars;
290  };
291 
292  std::vector<anim_elem> animated_units_;
294 };
const frame_parameters parameters(int current_time) const
Getters for the different parameters.
Definition: frame.cpp:292
Describes a unit&#39;s animation sequence.
Definition: frame.hpp:201
int time_to_tick(int animation_time) const
Definition: animation.hpp:66
All parameters from a frame at a given instant.
Definition: frame.hpp:35
unit_animation()=delete
int get_animation_time() const
Definition: animation.hpp:71
This class represents a single unit of a specific type.
Definition: unit.hpp:133
bool cycles() const
Definition: animated.hpp:73
Keep most parameters in a separate class to simplify the handling of the large number of parameters b...
Definition: frame.hpp:142
int get_current_frame_begin_time() const
const T & get_last_frame() const
void set_max_animation_time(int time)
std::vector< map_location::DIRECTION > directions_
Definition: animation.hpp:171
frame_parsed_parameters parameters_
Definition: animation.hpp:162
Frame for unit&#39;s animation sequence.
static void add_anims(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:629
std::vector< config > secondary_attack_filter_
Definition: animation.hpp:177
int get_current_frame_begin_time() const
Definition: animation.hpp:96
void add_frame(int duration, const unit_frame &value, bool force_change=false)
Definition: animation.hpp:48
void clear_haloes()
Definition: animation.cpp:1098
const unit_animation * animation
Definition: animation.hpp:285
std::vector< int > value_
Definition: animation.hpp:175
int get_begin_time() const
Definition: animation.cpp:1024
particle unit_anim_
Definition: animation.hpp:181
bool need_update() const
Definition: animation.cpp:960
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:378
bool need_update() const
Definition: animation.cpp:915
Definitions for the interface to Wesnoth Markup Language (WML).
int get_end_time() const
Definition: animation.cpp:1014
bool need_minimal_update() const
Definition: animation.cpp:923
void add_frame(int duration, const T &value, bool force_change=false)
Adds a frame to an animation.
map_location dst_
Definition: animation.hpp:184
std::vector< anim_elem > animated_units_
Definition: animation.hpp:292
void pause_animation()
Definition: animation.cpp:1066
int get_animation_time_potential() const
Definition: animation.hpp:81
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
int get_begin_time() const
int get_current_frame_time() const
particle(int start_time=0, const frame_builder &builder=frame_builder())
Definition: animation.hpp:127
int get_animation_time_potential() const
void start_animation(int start_time)
Definition: animation.cpp:1289
Animate units.
std::set< map_location > overlaped_hex_
Definition: animation.hpp:188
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:58
unit_const_ptr my_unit
Definition: animation.hpp:284
std::vector< config > secondary_unit_filter_
Definition: animation.hpp:170
std::vector< std::string > event_
Definition: animation.hpp:174
int time_to_tick(int animation_time) const
void redraw(const frame_parameters &value, const map_location &src, const map_location &dst, halo::manager &halo_man)
Definition: animation.cpp:1248
std::vector< std::string > get_flags() const
Definition: animation.hpp:53
const unit_frame & get_last_frame() const
Definition: animation.hpp:43
void restart_animation()
Definition: animation.cpp:1075
Encapsulates the map of the game.
Definition: location.hpp:38
const unit_frame & get_current_frame() const
friend std::ostream & operator<<(std::ostream &outstream, const unit_animation &u_animation)
Definition: animation.cpp:1158
map_location src_
Definition: animation.hpp:183
std::vector< int > value2_
Definition: animation.hpp:179
static void fill_initial_animations(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:485
std::vector< config > unit_filter_
Definition: animation.hpp:169
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:1034
std::vector< config > primary_attack_filter_
Definition: animation.hpp:176
void update_parameters(const map_location &src, const map_location &dst)
Definition: animation.cpp:1060
std::map< std::string, particle > sub_anims_
Definition: animation.hpp:180
std::string debug() const
Definition: animation.cpp:1151
const frame_parameters parameters(const frame_parameters &default_val) const
Definition: animation.hpp:153
void set_max_animation_time(int time)
Definition: animation.hpp:76
std::vector< terrain_code > ter_list
Definition: translation.hpp:77
static const map_location & null_location()
Definition: location.hpp:81
void redraw(frame_parameters &value, halo::manager &halo_man)
Definition: animation.cpp:1084
void update_last_draw_time()
Definition: animation.cpp:1005
bool need_minimal_update() const
Definition: animation.cpp:970
int get_end_time() const
std::vector< strike_result::type > hits_
Definition: animation.hpp:178
bool animation_finished_potential() const
Definition: animation.cpp:995
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
bool play_offscreen_
Definition: animation.hpp:187
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
t_translation::ter_list terrain_types_
Definition: animation.hpp:168
const frame_parameters get_current_params(const frame_parameters &default_val=frame_parameters()) const
Definition: animation.hpp:112
const 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
std::set< map_location > get_overlaped_hex(const frame_parameters &value, const map_location &src, const map_location &dst)
Definition: animation.cpp:1277
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1107
bool animation_finished() const
Definition: animation.cpp:985
int get_animation_time() const