The Battle for Wesnoth  1.15.2+dev
animation.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2018 by Jeremy Rosen <jeremy.rosen@enst-bretagne.fr>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "animated.hpp"
18 #include "color.hpp"
19 #include "config.hpp"
20 #include "halo.hpp"
21 #include "units/frame.hpp"
22 #include "units/ptr.hpp"
23 #include "utils/make_enum.hpp"
24 
25 class display;
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  MAKE_ENUM(hit_type,
37  (HIT, "hit")
38  (MISS, "miss")
39  (KILL, "kill")
40  (INVALID, "invalid")
41  )
42 
43  static void fill_initial_animations(std::vector<unit_animation>& animations, const config& cfg);
44  static void add_anims(std::vector<unit_animation>& animations, const config& cfg);
45 
46  int matches(const display& disp, const map_location& loc, const map_location& second_loc, const unit* 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;
47 
48  const unit_frame& get_last_frame() const
49  {
50  return unit_anim_.get_last_frame();
51  }
52 
53  void add_frame(int duration, const unit_frame& value, bool force_change = false)
54  {
55  unit_anim_.add_frame(duration,value,force_change);
56  }
57 
58  std::vector<std::string> get_flags() const
59  {
60  return event_;
61  }
62 
63  bool need_update() const;
64  bool need_minimal_update() const;
65  bool animation_finished() const;
66  bool animation_finished_potential() const;
67  void update_last_draw_time();
68  int get_begin_time() const;
69  int get_end_time() const;
70 
71  int time_to_tick(int animation_time) const
72  {
73  return unit_anim_.time_to_tick(animation_time);
74  }
75 
76  int get_animation_time() const
77  {
79  }
80 
81  void set_max_animation_time(int time)
82  {
84  }
85 
87  {
89  }
90 
91  void start_animation(int start_time
94  , const std::string& text = ""
95  , const color_t text_color = {0,0,0}
96  , const bool accelerate = true);
97 
98  void update_parameters(const map_location& src, const map_location& dst);
99  void pause_animation();
100  void restart_animation();
102  {
104 
105  }
106  void redraw(frame_parameters& value, halo::manager& halo_man);
107  void clear_haloes();
108  bool invalidate(frame_parameters& value );
109  std::string debug() const;
110  friend std::ostream& operator << (std::ostream& outstream, const unit_animation& u_animation);
111 
112  friend class unit;
113  friend class unit_drawer;
114 
115 protected:
116  // reserved to class unit, for the special case of redrawing the unit base frame
118  {
119  return unit_anim_.parameters(default_val);
120  }
121 
122 private:
123  explicit unit_animation(int start_time
124  , const unit_frame &frame
125  , const std::string& event = ""
126  , const int variation=DEFAULT_ANIM
127  , const frame_builder & builder = frame_builder());
128 
129  class particle : public animated<unit_frame>
130  {
131  public:
132  explicit particle(int start_time = 0, const frame_builder& builder = frame_builder())
133  : animated<unit_frame>(start_time)
134  , accelerate(true)
135  , parameters_(builder)
136  , halo_id_()
138  , cycles_(false)
139  {}
140  explicit particle(const config& cfg, const std::string& frame_string = "frame");
141 
142  virtual ~particle();
143  bool need_update() const;
144  bool need_minimal_update() const;
146  void override(int start_time
147  , int duration
148  , const cycle_state cycles
149  , const std::string& highlight = ""
150  , const std::string& blend_ratio =""
151  , color_t blend_color = {0,0,0}
152  , const std::string& offset = ""
153  , const std::string& layer = ""
154  , const std::string& modifiers = "");
155  void redraw(const frame_parameters& value, const map_location& src, const map_location& dst, halo::manager& halo_man);
156  std::set<map_location> get_overlaped_hex(const frame_parameters& value,const map_location& src, const map_location& dst);
157  void start_animation(int start_time);
158  const frame_parameters parameters(const frame_parameters& default_val) const
159  {
161  }
162  void clear_halo();
164 
165  private:
166  //animation params that can be locally overridden by frames
170  bool cycles_;
171  };
172 
174  std::vector<config> unit_filter_;
175  std::vector<config> secondary_unit_filter_;
176  std::vector<map_location::DIRECTION> directions_;
179  std::vector<std::string> event_;
180  std::vector<int> value_;
181  std::vector<config> primary_attack_filter_;
182  std::vector<config> secondary_attack_filter_;
183  std::vector<hit_type> hits_;
184  std::vector<int> value2_;
185  std::map<std::string,particle> sub_anims_;
187  /* these are drawing parameters, but for efficiency reason they are in the anim and not in the particle */
190  // optimization
193  std::set<map_location> overlaped_hex_;
194 };
195 
197 {
198 public:
200  animated_units_(),
201  start_time_(INT_MIN)
202  {}
203 
204  void add_animation(const unit* animated_unit
205  , const unit_animation* animation
207  , bool with_bars = false
208  , const std::string& text = ""
209  , const color_t text_color = {0,0,0});
210 
211  void add_animation(const unit* animated_unit
212  , const std::string& event
215  , const int value = 0
216  , bool with_bars = false
217  , const std::string& text = ""
218  , const color_t text_color = {0,0,0}
219  , const unit_animation::hit_type hit_type =
220  unit_animation::hit_type::INVALID
221  , const_attack_ptr attack = nullptr
222  , const_attack_ptr second_attack = nullptr
223  , int value2 = 0);
224 
225  void replace_anim_if_invalid(const unit* animated_unit
226  , const std::string& event
229  , const int value = 0
230  , bool with_bars = false
231  , const std::string& text = ""
232  , const color_t text_color = {0,0,0}
233  , const unit_animation::hit_type hit_type = unit_animation::hit_type::INVALID
234  , const_attack_ptr attack = nullptr
235  , const_attack_ptr second_attack = nullptr
236  , int value2 = 0);
237  void start_animations();
238  void pause_animation();
239  void restart_animation();
240 
241  void clear()
242  {
243  start_time_ = INT_MIN;
244  animated_units_.clear();
245  }
246 
247  void set_all_standing();
248 
249  bool would_end() const;
250  int get_animation_time() const;
251  int get_animation_time_potential() const;
252  int get_end_time() const;
253  void wait_for_end() const;
254  void wait_until( int animation_time) const;
255 
256 private:
257  struct anim_elem
258  {
260  : my_unit(0)
261  , animation(0)
262  , text()
263  , text_color()
264  , src()
265  , with_bars(false)
266  {}
267 
270  std::string text;
273  bool with_bars;
274  };
275 
276  std::vector<anim_elem> animated_units_;
278 };
const frame_parameters parameters(int current_time) const
Getters for the different parameters.
Definition: frame.cpp:296
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
Describes a unit&#39;s animation sequence.
Definition: frame.hpp:200
int time_to_tick(int animation_time) const
Definition: animation.hpp:71
All parameters from a frame at a given instant.
Definition: frame.hpp:34
unit_animation()=delete
int get_animation_time() const
Definition: animation.hpp:76
std::vector< hit_type > hits_
Definition: animation.hpp:183
This class represents a single unit of a specific type.
Definition: unit.hpp:99
bool cycles() const
Definition: animated.hpp:71
Keep most parameters in a separate class to simplify the handling of the large number of parameters b...
Definition: frame.hpp:141
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:176
frame_parsed_parameters parameters_
Definition: animation.hpp:167
Frame for unit&#39;s animation sequence.
std::vector< config > secondary_attack_filter_
Definition: animation.hpp:182
int get_current_frame_begin_time() const
Definition: animation.hpp:101
void add_frame(int duration, const unit_frame &value, bool force_change=false)
Definition: animation.hpp:53
void clear_haloes()
Definition: animation.cpp:1095
const unit_animation * animation
Definition: animation.hpp:269
std::vector< int > value_
Definition: animation.hpp:180
int get_begin_time() const
Definition: animation.cpp:1021
particle unit_anim_
Definition: animation.hpp:186
bool need_update() const
Definition: animation.cpp:957
bool need_update() const
Definition: animation.cpp:912
Definitions for the interface to Wesnoth Markup Language (WML).
int get_end_time() const
Definition: animation.cpp:1011
bool need_minimal_update() const
Definition: animation.cpp:920
void add_frame(int duration, const T &value, bool force_change=false)
Adds a frame to an animation.
map_location dst_
Definition: animation.hpp:189
std::vector< anim_elem > animated_units_
Definition: animation.hpp:276
void pause_animation()
Definition: animation.cpp:1063
int get_animation_time_potential() const
Definition: animation.hpp:86
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:132
int get_animation_time_potential() const
void start_animation(int start_time)
Definition: animation.cpp:1286
Animate units.
std::set< map_location > overlaped_hex_
Definition: animation.hpp:193
unit_const_ptr my_unit
Definition: animation.hpp:268
std::vector< config > secondary_unit_filter_
Definition: animation.hpp:175
std::vector< std::string > event_
Definition: animation.hpp:179
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:1245
std::vector< std::string > get_flags() const
Definition: animation.hpp:58
const unit_frame & get_last_frame() const
Definition: animation.hpp:48
void restart_animation()
Definition: animation.cpp:1072
Encapsulates the map of the game.
Definition: location.hpp:42
const unit_frame & get_current_frame() const
friend std::ostream & operator<<(std::ostream &outstream, const unit_animation &u_animation)
Definition: animation.cpp:1155
map_location src_
Definition: animation.hpp:188
std::vector< int > value2_
Definition: animation.hpp:184
std::vector< config > unit_filter_
Definition: animation.hpp:174
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:1031
std::vector< config > primary_attack_filter_
Definition: animation.hpp:181
int matches(const display &disp, const map_location &loc, const map_location &second_loc, const unit *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:377
MAKE_ENUM(ALIGNMENT_FEMALE_VARIATION,(LAWFUL, N_("female^lawful"))(FEMALE_NEUTRAL, N_("female^neutral"))(CHAOTIC, N_("female^chaotic"))(LIMINAL, N_("female^liminal"))) std
Implementation detail of unit_type::alignment_description.
Definition: types.cpp:913
void update_parameters(const map_location &src, const map_location &dst)
Definition: animation.cpp:1057
std::map< std::string, particle > sub_anims_
Definition: animation.hpp:185
std::string debug() const
Definition: animation.cpp:1148
const frame_parameters parameters(const frame_parameters &default_val) const
Definition: animation.hpp:158
void set_max_animation_time(int time)
Definition: animation.hpp:81
std::vector< terrain_code > ter_list
Definition: translation.hpp:78
static const map_location & null_location()
Definition: location.hpp:85
void redraw(frame_parameters &value, halo::manager &halo_man)
Definition: animation.cpp:1081
void update_last_draw_time()
Definition: animation.cpp:1002
bool need_minimal_update() const
Definition: animation.cpp:967
int get_end_time() const
bool animation_finished_potential() const
Definition: animation.cpp:992
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:37
bool play_offscreen_
Definition: animation.hpp:192
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
t_translation::ter_list terrain_types_
Definition: animation.hpp:173
const frame_parameters get_current_params(const frame_parameters &default_val=frame_parameters()) const
Definition: animation.hpp:117
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:755
std::set< map_location > get_overlaped_hex(const frame_parameters &value, const map_location &src, const map_location &dst)
Definition: animation.cpp:1274
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1104
bool animation_finished() const
Definition: animation.cpp:982
int get_animation_time() const