The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
animation.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2006 - 2025
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"
25
27{
28public:
29 unit_animation() = delete;
30 explicit unit_animation(const config& cfg, const std::string& frame_string = "");
31
33
34 static void fill_initial_animations(std::vector<unit_animation>& animations, const config& cfg);
35 static void add_anims(std::vector<unit_animation>& animations, const config& cfg);
36
37 int matches(const map_location& loc, const map_location& second_loc, const unit_const_ptr& my_unit, const std::string& event = "",
38 const int value = 0, strike_result::type hit = strike_result::type::invalid, const const_attack_ptr& attack = nullptr, const const_attack_ptr& second_attack = nullptr,
39 int value2 = 0) const;
40
42 {
44 }
45
46 void add_frame(const std::chrono::milliseconds& duration, const unit_frame& value, bool force_change = false)
47 {
48 unit_anim_.add_frame(duration,value,force_change);
49 }
50
51 std::vector<std::string> get_flags() const
52 {
53 return event_;
54 }
55
56 bool need_update() const;
57 bool need_minimal_update() const;
58 bool animation_finished() const;
60 std::chrono::milliseconds get_begin_time() const;
61 std::chrono::milliseconds get_end_time() const;
62
63 auto get_elapsed_time() const
64 {
66 }
67
68 void set_duration_limit(const std::chrono::milliseconds& time)
69 {
71 }
72
73 void start_animation(const std::chrono::milliseconds& start_time
76 , const std::string& text = ""
77 , const color_t text_color = {0,0,0}
78 , const bool accelerate = true);
79
81 void pause_animation();
82 void resume_animation();
88 void clear_haloes();
90 std::string debug() const;
91 friend std::ostream& operator << (std::ostream& outstream, const unit_animation& u_animation);
92
93 friend class unit;
94 friend class unit_drawer;
95
96protected:
97 // reserved to class unit, for the special case of redrawing the unit base frame
99 {
100 return unit_anim_.parameters(default_val);
101 }
102
103private:
104 explicit unit_animation(const std::chrono::milliseconds& start_time
105 , const unit_frame &frame
106 , const std::string& event = ""
107 , const int variation=DEFAULT_ANIM
108 , const frame_builder & builder = frame_builder());
109
110 class particle : public animated<unit_frame>
111 {
112 public:
113 explicit particle(const std::chrono::milliseconds& start_time = std::chrono::milliseconds{0}, const frame_builder& builder = frame_builder())
114 : animated<unit_frame>(start_time)
115 , parameters_(builder)
116 , halo_id_()
118 , cycles_(false)
119 {
121 }
122 explicit particle(const config& cfg, const std::string& frame_string = "frame");
123
124 virtual ~particle();
125 bool need_update() const;
126 bool need_minimal_update() const;
128 void override(const std::chrono::milliseconds& start_time
129 , const std::chrono::milliseconds& duration
130 , const cycle_state cycles
131 , const std::string& highlight = ""
132 , const std::string& blend_ratio =""
133 , color_t blend_color = {0,0,0}
134 , const std::string& offset = ""
135 , const std::string& layer = ""
136 , const std::string& modifiers = "");
137 void redraw(const frame_parameters& value, const map_location& src, const map_location& dst, halo::manager& halo_man);
138 std::set<map_location> get_overlaped_hex(const frame_parameters& value,const map_location& src, const map_location& dst);
139 void start_animation(const std::chrono::milliseconds& start_time);
144 void clear_halo();
145
146 private:
147 //animation params that can be locally overridden by frames
150 std::chrono::milliseconds last_frame_begin_time_;
152 };
153
155 std::vector<config> unit_filter_;
156 std::vector<config> secondary_unit_filter_;
157 std::vector<map_location::direction> directions_;
160 std::vector<std::string> event_;
161 std::vector<int> value_;
162 std::vector<config> primary_attack_filter_;
163 std::vector<config> secondary_attack_filter_;
164 std::vector<strike_result::type> hits_;
165 std::vector<int> value2_;
166 std::map<std::string,particle> sub_anims_;
168 /* these are drawing parameters, but for efficiency reason they are in the anim and not in the particle */
171 // optimization
174 std::set<map_location> overlaped_hex_;
175};
176
178{
179public:
180 void add_animation(unit_const_ptr animated_unit
181 , const unit_animation* animation
183 , bool with_bars = false
184 , const std::string& text = ""
185 , const color_t text_color = {0,0,0});
186
187 void add_animation(unit_const_ptr animated_unit
188 , const std::string& event
191 , const int value = 0
192 , bool with_bars = false
193 , const std::string& text = ""
194 , const color_t text_color = {0,0,0}
195 , const strike_result::type hit_type = strike_result::type::invalid
196 , const const_attack_ptr& attack = nullptr
197 , const const_attack_ptr& second_attack = nullptr
198 , int value2 = 0);
199
200 /** 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]
201 * @return True if the @a animated_unit is present and have animation.
202 * @param animated_unit the unit who is checked.
203 * @param event the animation who is checked([leading_anim] or [resistance_anim].
204 * @param src the location of animated_unit.
205 * @param dst location of unit student(attacker or defender).
206 * @param value value of damage.
207 * @param hit_type type of damage inflicted.
208 * @param attack weapon used by student.
209 * @param second_attack weapon used by opponent.
210 * @param value2 i don't understand myself.but this value is used in choose_animation.
211 */
212 bool has_animation(const unit_const_ptr& animated_unit
213 , const std::string& event
216 , const int value = 0
217 , const strike_result::type hit_type = strike_result::type::invalid
218 , const const_attack_ptr& attack = nullptr
219 , const const_attack_ptr& second_attack = nullptr
220 , int value2 = 0) const;
221
222 void replace_anim_if_invalid(const unit_const_ptr& animated_unit
223 , const std::string& event
226 , const int value = 0
227 , bool with_bars = false
228 , const std::string& text = ""
229 , const color_t text_color = {0,0,0}
230 , const strike_result::type hit_type = strike_result::type::invalid
231 , const const_attack_ptr& attack = nullptr
232 , const const_attack_ptr& second_attack = nullptr
233 , int value2 = 0);
234 void start_animations();
235 void pause_animation();
236 void resume_animation();
237
238 void clear()
239 {
240 start_time_ = std::chrono::milliseconds::min();
241 animated_units_.clear();
242 }
243
244 void set_all_standing();
245
246 bool would_end() const;
247 std::chrono::milliseconds get_elapsed_time() const;
248 std::chrono::milliseconds get_end_time() const;
249 void wait_for_end() const;
250 void wait_until(const std::chrono::milliseconds& animation_time) const;
251
252private:
262
263 std::vector<anim_elem> animated_units_;
264 std::chrono::milliseconds start_time_ = std::chrono::milliseconds::min();
265};
map_location loc
Definition move.cpp:172
Frame-based animation system with support for looping, acceleration, and time manipulation.
std::chrono::milliseconds get_time_in_current_frame() const
How far into its own duration the current frame is, clamped to [0, duration].
std::chrono::milliseconds get_begin_time() const
Animation-time offset of the first frame's start.
std::chrono::milliseconds get_current_frame_begin_time() const
Animation-time offset where the current frame starts.
void set_uses_acceleration(bool uses_accel)
Selects which of the two global timelines get_current_animation_tick() reads for this animation: acce...
Definition animated.hpp:185
void add_frame(const std::chrono::milliseconds &duration, const T &value, bool force_change=false)
Appends a frame, starting where the previous one ends (or at start_time, if first).
std::chrono::milliseconds get_elapsed_time() const
Time since playback began, per get_playback_tick().
const unit_frame & get_current_frame() const
void set_duration_limit(const std::chrono::milliseconds &time)
const T & get_last_frame() const
bool cycles() const
True if the animation loops back to the first frame instead of stopping at the last.
Definition animated.hpp:112
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
Easily build frame parameters with the serialized constructors.
Definition frame.hpp:84
Keep most parameters in a separate class to simplify the handling of the large number of parameters b...
Definition frame.hpp:148
frame_parameters parameters(const std::chrono::milliseconds &current_time) const
Getters for the different parameters.
Definition frame.cpp:277
std::set< map_location > get_overlaped_hex(const frame_parameters &value, const map_location &src, const map_location &dst)
void start_animation(const std::chrono::milliseconds &start_time)
frame_parsed_parameters parameters_
std::chrono::milliseconds last_frame_begin_time_
bool need_minimal_update() const
particle(const std::chrono::milliseconds &start_time=std::chrono::milliseconds{0}, const frame_builder &builder=frame_builder())
frame_parameters parameters(const frame_parameters &default_val) const
void redraw(const frame_parameters &value, const map_location &src, const map_location &dst, halo::manager &halo_man)
bool invalidate(frame_parameters &value)
std::vector< config > primary_attack_filter_
std::vector< map_location::direction > directions_
void resume_animation()
std::vector< int > value2_
bool need_update() const
map_location src_
bool need_minimal_update() const
std::vector< std::string > get_flags() const
Definition animation.hpp:51
std::vector< config > secondary_unit_filter_
void try_advance_to_current_frame()
map_location dst_
t_translation::ter_list terrain_types_
void add_frame(const std::chrono::milliseconds &duration, const unit_frame &value, bool force_change=false)
Definition animation.hpp:46
void start_animation(const std::chrono::milliseconds &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)
frame_parameters get_current_params(const frame_parameters &default_val=frame_parameters()) const
Definition animation.hpp:98
std::chrono::milliseconds get_begin_time() const
std::vector< std::string > event_
int matches(const map_location &loc, const map_location &second_loc, const unit_const_ptr &my_unit, const std::string &event="", const int value=0, strike_result::type hit=strike_result::type::invalid, const const_attack_ptr &attack=nullptr, const const_attack_ptr &second_attack=nullptr, int value2=0) const
std::vector< int > value_
void set_duration_limit(const std::chrono::milliseconds &time)
Definition animation.hpp:68
std::string debug() const
const unit_frame & get_last_frame() const
Definition animation.hpp:41
std::map< std::string, particle > sub_anims_
particle unit_anim_
void update_parameters(const map_location &src, const map_location &dst)
friend std::ostream & operator<<(std::ostream &outstream, const unit_animation &u_animation)
std::chrono::milliseconds get_end_time() const
auto get_current_frame_begin_time() const
Definition animation.hpp:83
void pause_animation()
auto get_elapsed_time() const
Definition animation.hpp:63
unit_animation()=delete
void redraw(frame_parameters &value, halo::manager &halo_man)
std::set< map_location > overlaped_hex_
static void fill_initial_animations(std::vector< unit_animation > &animations, const config &cfg)
std::vector< config > secondary_attack_filter_
std::vector< config > unit_filter_
static void add_anims(std::vector< unit_animation > &animations, const config &cfg)
std::vector< strike_result::type > hits_
bool animation_finished() const
std::chrono::milliseconds get_elapsed_time() const
void pause_animation()
void resume_animation()
std::chrono::milliseconds start_time_
bool has_animation(const 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 const_attack_ptr &attack=nullptr, const const_attack_ptr &second_attack=nullptr, int value2=0) const
has_animation : return an boolean value if animated unit present and have animation specified,...
std::vector< anim_elem > animated_units_
void wait_for_end() const
void replace_anim_if_invalid(const 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 const_attack_ptr &attack=nullptr, const const_attack_ptr &second_attack=nullptr, int value2=0)
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})
bool would_end() const
void start_animations()
void set_all_standing()
std::chrono::milliseconds get_end_time() const
void wait_until(const std::chrono::milliseconds &animation_time) const
Describes a unit's animation sequence.
Definition frame.hpp:208
frame_parameters merge_parameters(const std::chrono::milliseconds &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:922
This class represents a single unit of a specific type.
Definition unit.hpp:39
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
Frame for unit's animation sequence.
std::shared_ptr< halo_record > handle
Definition halo.hpp:33
std::vector< terrain_code > ter_list
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
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
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:51
All parameters from a frame at a given instant.
Definition frame.hpp:44
Encapsulates the map of the game.
Definition location.hpp:46
static const map_location & null_location()
Definition location.hpp:103
const unit_animation * animation