The Battle for Wesnoth  1.17.0-dev
animation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2021
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 "utils/make_enum.hpp"
25 
26 class display;
27 class unit;
28 
30 {
31 public:
32  unit_animation() = delete;
33  explicit unit_animation(const config& cfg, const std::string& frame_string = "");
34 
35  enum variation_type {MATCH_FAIL = -10 , DEFAULT_ANIM = -9};
36 
37  MAKE_ENUM(hit_type,
38  (HIT, "hit")
39  (MISS, "miss")
40  (KILL, "kill")
41  (INVALID, "invalid")
42  );
43 
44  static void fill_initial_animations(std::vector<unit_animation>& animations, const config& cfg);
45  static void add_anims(std::vector<unit_animation>& animations, const config& cfg);
46 
47  int matches(const display& disp, const map_location& loc, const map_location& second_loc, unit_const_ptr my_unit, const std::string& event = "", const int value = 0, hit_type hit = hit_type::INVALID, const_attack_ptr attack = nullptr, const_attack_ptr second_attack = nullptr, int value2 = 0) const;
48 
49  const unit_frame& get_last_frame() const
50  {
51  return unit_anim_.get_last_frame();
52  }
53 
54  void add_frame(int duration, const unit_frame& value, bool force_change = false)
55  {
56  unit_anim_.add_frame(duration,value,force_change);
57  }
58 
59  std::vector<std::string> get_flags() const
60  {
61  return event_;
62  }
63 
64  bool need_update() const;
65  bool need_minimal_update() const;
66  bool animation_finished() const;
67  bool animation_finished_potential() const;
68  void update_last_draw_time();
69  int get_begin_time() const;
70  int get_end_time() const;
71 
72  int time_to_tick(int animation_time) const
73  {
74  return unit_anim_.time_to_tick(animation_time);
75  }
76 
77  int get_animation_time() const
78  {
80  }
81 
82  void set_max_animation_time(int time)
83  {
85  }
86 
88  {
90  }
91 
92  void start_animation(int start_time
95  , const std::string& text = ""
96  , const color_t text_color = {0,0,0}
97  , const bool accelerate = true);
98 
99  void update_parameters(const map_location& src, const map_location& dst);
100  void pause_animation();
101  void restart_animation();
103  {
105 
106  }
107  void redraw(frame_parameters& value, halo::manager& halo_man);
108  void clear_haloes();
109  bool invalidate(frame_parameters& value );
110  std::string debug() const;
111  friend std::ostream& operator << (std::ostream& outstream, const unit_animation& u_animation);
112 
113  friend class unit;
114  friend class unit_drawer;
115 
116 protected:
117  // reserved to class unit, for the special case of redrawing the unit base frame
119  {
120  return unit_anim_.parameters(default_val);
121  }
122 
123 private:
124  explicit unit_animation(int start_time
125  , const unit_frame &frame
126  , const std::string& event = ""
127  , const int variation=DEFAULT_ANIM
128  , const frame_builder & builder = frame_builder());
129 
130  class particle : public animated<unit_frame>
131  {
132  public:
133  explicit particle(int start_time = 0, const frame_builder& builder = frame_builder())
134  : animated<unit_frame>(start_time)
135  , accelerate(true)
136  , parameters_(builder)
137  , halo_id_()
139  , cycles_(false)
140  {}
141  explicit particle(const config& cfg, const std::string& frame_string = "frame");
142 
143  virtual ~particle();
144  bool need_update() const;
145  bool need_minimal_update() const;
147  void override(int start_time
148  , int duration
149  , const cycle_state cycles
150  , const std::string& highlight = ""
151  , const std::string& blend_ratio =""
152  , color_t blend_color = {0,0,0}
153  , const std::string& offset = ""
154  , const std::string& layer = ""
155  , const std::string& modifiers = "");
156  void redraw(const frame_parameters& value, const map_location& src, const map_location& dst, halo::manager& halo_man);
157  std::set<map_location> get_overlaped_hex(const frame_parameters& value,const map_location& src, const map_location& dst);
158  void start_animation(int start_time);
159  const frame_parameters parameters(const frame_parameters& default_val) const
160  {
162  }
163  void clear_halo();
165 
166  private:
167  //animation params that can be locally overridden by frames
171  bool cycles_;
172  };
173 
175  std::vector<config> unit_filter_;
176  std::vector<config> secondary_unit_filter_;
177  std::vector<map_location::DIRECTION> directions_;
180  std::vector<std::string> event_;
181  std::vector<int> value_;
182  std::vector<config> primary_attack_filter_;
183  std::vector<config> secondary_attack_filter_;
184  std::vector<hit_type> hits_;
185  std::vector<int> value2_;
186  std::map<std::string,particle> sub_anims_;
188  /* these are drawing parameters, but for efficiency reason they are in the anim and not in the particle */
191  // optimization
194  std::set<map_location> overlaped_hex_;
195 };
196 
198 {
199 public:
201  animated_units_(),
202  start_time_(INT_MIN)
203  {}
204 
205  void add_animation(unit_const_ptr animated_unit
206  , const unit_animation* animation
208  , bool with_bars = false
209  , const std::string& text = ""
210  , const color_t text_color = {0,0,0});
211 
212  void add_animation(unit_const_ptr animated_unit
213  , const std::string& event
216  , const int value = 0
217  , bool with_bars = false
218  , const std::string& text = ""
219  , const color_t text_color = {0,0,0}
220  , const unit_animation::hit_type hit_type =
221  unit_animation::hit_type::INVALID
222  , const_attack_ptr attack = nullptr
223  , const_attack_ptr second_attack = nullptr
224  , int value2 = 0);
225 
226  /** 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]
227  * @return True if the @a animated_unit is present and have animation.
228  * @param animated_unit the unit who is checked.
229  * @param event the animation who is checked([leading_anim] or [resistance_anim].
230  * @param src the location of animated_unit.
231  * @param dst location of unit student(attacker or defender).
232  * @param value value of damage.
233  * @param hit_type type of damage inflicted.
234  * @param attack weapon used by student.
235  * @param second_attack weapon used by opponent.
236  * @param value2 i don't understand myself.but this value is used in choose_animation.
237  */
238  bool has_animation(unit_const_ptr animated_unit
239  , const std::string& event
242  , const int value = 0
243  , const unit_animation::hit_type hit_type =
244  unit_animation::hit_type::INVALID
245  , const_attack_ptr attack = nullptr
246  , const_attack_ptr second_attack = nullptr
247  , int value2 = 0) const;
248 
249  void replace_anim_if_invalid(unit_const_ptr animated_unit
250  , const std::string& event
253  , const int value = 0
254  , bool with_bars = false
255  , const std::string& text = ""
256  , const color_t text_color = {0,0,0}
257  , const unit_animation::hit_type hit_type = unit_animation::hit_type::INVALID
258  , const_attack_ptr attack = nullptr
259  , const_attack_ptr second_attack = nullptr
260  , int value2 = 0);
261  void start_animations();
262  void pause_animation();
263  void restart_animation();
264 
265  void clear()
266  {
267  start_time_ = INT_MIN;
268  animated_units_.clear();
269  }
270 
271  void set_all_standing();
272 
273  bool would_end() const;
274  int get_animation_time() const;
275  int get_animation_time_potential() const;
276  int get_end_time() const;
277  void wait_for_end() const;
278  void wait_until( int animation_time) const;
279 
280 private:
281  struct anim_elem
282  {
284  : my_unit()
285  , animation(0)
286  , text()
287  , text_color()
288  , src()
289  , with_bars(false)
290  {}
291 
294  std::string text;
297  bool with_bars;
298  };
299 
300  std::vector<anim_elem> animated_units_;
302 };
const frame_parameters parameters(int current_time) const
Getters for the different parameters.
Definition: frame.cpp:291
Describes a unit&#39;s animation sequence.
Definition: frame.hpp:201
int time_to_tick(int animation_time) const
Definition: animation.hpp:72
All parameters from a frame at a given instant.
Definition: frame.hpp:35
unit_animation()=delete
int get_animation_time() const
Definition: animation.hpp:77
std::vector< hit_type > hits_
Definition: animation.hpp:184
This class represents a single unit of a specific type.
Definition: unit.hpp:121
bool cycles() const
Definition: animated.hpp:72
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:177
frame_parsed_parameters parameters_
Definition: animation.hpp:168
Frame for unit&#39;s animation sequence.
static void add_anims(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:628
std::vector< config > secondary_attack_filter_
Definition: animation.hpp:183
int get_current_frame_begin_time() const
Definition: animation.hpp:102
void add_frame(int duration, const unit_frame &value, bool force_change=false)
Definition: animation.hpp:54
void clear_haloes()
Definition: animation.cpp:1097
const unit_animation * animation
Definition: animation.hpp:293
std::vector< int > value_
Definition: animation.hpp:181
int get_begin_time() const
Definition: animation.cpp:1023
particle unit_anim_
Definition: animation.hpp:187
bool need_update() const
Definition: animation.cpp:959
bool need_update() const
Definition: animation.cpp:914
Definitions for the interface to Wesnoth Markup Language (WML).
int get_end_time() const
Definition: animation.cpp:1013
bool need_minimal_update() const
Definition: animation.cpp:922
void add_frame(int duration, const T &value, bool force_change=false)
Adds a frame to an animation.
map_location dst_
Definition: animation.hpp:190
std::vector< anim_elem > animated_units_
Definition: animation.hpp:300
void pause_animation()
Definition: animation.cpp:1065
int get_animation_time_potential() const
Definition: animation.hpp:87
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:133
int get_animation_time_potential() const
void start_animation(int start_time)
Definition: animation.cpp:1288
MAKE_ENUM(hit_type,(HIT, "hit")(MISS, "miss")(KILL, "kill")(INVALID, "invalid"))
Animate units.
std::set< map_location > overlaped_hex_
Definition: animation.hpp:194
unit_const_ptr my_unit
Definition: animation.hpp:292
std::vector< config > secondary_unit_filter_
Definition: animation.hpp:176
std::vector< std::string > event_
Definition: animation.hpp:180
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:1247
std::vector< std::string > get_flags() const
Definition: animation.hpp:59
const unit_frame & get_last_frame() const
Definition: animation.hpp:49
void restart_animation()
Definition: animation.cpp:1074
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:1157
map_location src_
Definition: animation.hpp:189
std::vector< int > value2_
Definition: animation.hpp:185
static void fill_initial_animations(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:484
std::vector< config > unit_filter_
Definition: animation.hpp:175
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
std::vector< config > primary_attack_filter_
Definition: animation.hpp:182
void update_parameters(const map_location &src, const map_location &dst)
Definition: animation.cpp:1059
std::map< std::string, particle > sub_anims_
Definition: animation.hpp:186
std::string debug() const
Definition: animation.cpp:1150
const frame_parameters parameters(const frame_parameters &default_val) const
Definition: animation.hpp:159
void set_max_animation_time(int time)
Definition: animation.hpp:82
std::vector< terrain_code > ter_list
Definition: translation.hpp:78
static const map_location & null_location()
Definition: location.hpp:81
void redraw(frame_parameters &value, halo::manager &halo_man)
Definition: animation.cpp:1083
void update_last_draw_time()
Definition: animation.cpp:1004
bool need_minimal_update() const
Definition: animation.cpp:969
int get_end_time() const
bool animation_finished_potential() const
Definition: animation.cpp:994
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
int matches(const display &disp, const map_location &loc, const map_location &second_loc, unit_const_ptr my_unit, const std::string &event="", const int value=0, hit_type hit=hit_type::INVALID, const_attack_ptr attack=nullptr, const_attack_ptr second_attack=nullptr, int value2=0) const
Definition: animation.cpp:378
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
bool play_offscreen_
Definition: animation.hpp:193
std::shared_ptr< halo_record > handle
Definition: halo.hpp:30
t_translation::ter_list terrain_types_
Definition: animation.hpp:174
const frame_parameters get_current_params(const frame_parameters &default_val=frame_parameters()) const
Definition: animation.hpp:118
Defines the MAKE_ENUM macro.
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:751
std::set< map_location > get_overlaped_hex(const frame_parameters &value, const map_location &src, const map_location &dst)
Definition: animation.cpp:1276
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1106
bool animation_finished() const
Definition: animation.cpp:984
int get_animation_time() const