The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
animation_component.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 
16 
17 #include "config.hpp"
18 #include "display.hpp"
19 #include "map/map.hpp"
20 #include "preferences.hpp"
21 #include "units/animation.hpp"
22 #include "units/unit.hpp"
23 #include "units/types.hpp"
24 
26  const map_location& second_loc,const int value,const unit_animation::hit_type hit,
27  const attack_type* attack, const attack_type* second_attack, int swing_num)
28 {
29  // Select one of the matching animations at random
30  std::vector<const unit_animation*> options;
31  int max_val = unit_animation::MATCH_FAIL;
32  for(std::vector<unit_animation>::const_iterator i = animations_.begin(); i != animations_.end(); ++i) {
33  int matching = i->matches(disp,loc,second_loc,&u_,event,value,hit,attack,second_attack,swing_num);
34  if(matching > unit_animation::MATCH_FAIL && matching == max_val) {
35  options.push_back(&*i);
36  } else if(matching > max_val) {
37  max_val = matching;
38  options.clear();
39  options.push_back(&*i);
40  }
41  }
42 
43  if(max_val == unit_animation::MATCH_FAIL) {
44  return nullptr;
45  }
46  return options[rand()%options.size()];
47 }
48 
50 {
53  start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "standing"),
54  with_bars, "", 0, STATE_STANDING);
55  } else {
56  start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "_disabled_"),
57  with_bars, "", 0, STATE_STANDING);
58  }
59 }
60 
62 {
64  start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "ghosted"),
65  with_bars);
66  anim_->pause_animation();
67 }
68 
70 {
72  start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "disabled_ghosted"),
73  with_bars);
74 }
75 
77 {
79  start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "idling"),
80  true, "", 0, STATE_FORGET);
81 }
82 
84 {
85  const display *disp = display::get_singleton();
87  start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "selected"),
88  true, "", 0, STATE_FORGET);
89  } else {
90  start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "_disabled_selected_"),
91  true, "", 0, STATE_FORGET);
92  }
93 }
94 
95 void unit_animation_component::start_animation (int start_time, const unit_animation *animation,
96  bool with_bars, const std::string &text, Uint32 text_color, STATE state)
97 {
98  const display * disp = display::get_singleton();
99  if (!animation) {
100  if (state == STATE_STANDING)
101  state_ = state;
102  if (!anim_ && state_ != STATE_STANDING)
103  set_standing(with_bars);
104  return ;
105  }
106  state_ = state;
107  // everything except standing select and idle
108  bool accelerate = (state != STATE_FORGET && state != STATE_STANDING);
109  draw_bars_ = with_bars;
110  anim_.reset(new unit_animation(*animation));
111  const int real_start_time = start_time == INT_MAX ? anim_->get_begin_time() : start_time;
112  anim_->start_animation(real_start_time, u_.loc_, u_.loc_.get_direction(u_.facing_),
113  text, text_color, accelerate);
114  frame_begin_time_ = anim_->get_begin_time() -1;
115  if (disp->idle_anim()) {
117  + static_cast<int>((20000 + rand() % 20000) * disp->idle_anim_rate());
118  } else {
119  next_idling_ = INT_MAX;
120  }
121 }
122 
124 {
125  if (state_ == STATE_FORGET && anim_ && anim_->animation_finished_potential())
126  {
127  set_standing();
128  return;
129  }
130  display &disp = *display::get_singleton();
133  {
134  return;
135  }
137  {
138  // prevent all units animating at the same time
139  if (disp.idle_anim()) {
141  + static_cast<int>((20000 + rand() % 20000) * disp.idle_anim_rate());
142  } else {
143  next_idling_ = INT_MAX;
144  }
145  } else {
146  set_idling();
147  }
148 }
149 
151 {
152  unit_halo_ = halo::handle(); //halo::NO_HALO; <-- Removes it from the halo manager automatically.
153  if(anim_ ) anim_->clear_haloes();
154 }
155 
157 {
158  bool result = false;
159 
160  // Very early calls, anim not initialized yet
161  if(get_animation()) {
163  const gamemap & map = disp.get_map();
165  const terrain_type& terrain_info = map.get_terrain_info(terrain);
166 
167  int height_adjust = static_cast<int>(terrain_info.unit_height_adjust() * disp.get_zoom_factor());
168  if (u_.is_flying() && height_adjust < 0) {
169  height_adjust = 0;
170  }
171  params.y -= height_adjust;
172  params.halo_y -= height_adjust;
173  params.image_mod = u_.image_mods();
174  params.halo_mod = u_.TC_image_mods();
175  params.image= u_.default_anim_image();
176 
177  result |= get_animation()->invalidate(params);
178  }
179 
180  return result;
181 
182 }
183 
185 {
186  if (newtype) {
187  animations_ = newtype->animations();
188  }
189 
190  refreshing_ = false;
191  anim_.reset();
192 }
193 
195  if(effect["id"].empty()) {
197  } else {
198  static std::map< std::string, std::vector<unit_animation> > animation_cache;
199  std::vector<unit_animation> &built = animation_cache[effect["id"]];
200  if(built.empty()) {
201  unit_animation::add_anims(built, effect);
202  }
203  animations_.insert(animations_.end(),built.begin(),built.end());
204  }
205 }
std::string image_mods() const
Definition: unit.cpp:2430
bool refreshing_
avoid infinite recursion. flag used for drawing / animation
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:88
const unit & u_
A reference to the unit that owns this object.
image::locator image
Definition: frame.hpp:76
std::string TC_image_mods() const
Definition: unit.cpp:2423
void set_disabled_ghosted(bool with_bars=true)
Whiteboard related somehow.
static void add_anims(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:618
void set_selecting()
Sets the animation state to that when the unit is selected.
map_location::DIRECTION facing_
Definition: unit.hpp:501
bool tile_nearly_on_screen(const map_location &loc) const
Checks if location loc or one of the adjacent tiles is visible on screen.
Definition: display.cpp:2345
bool draw_bars_
bool indicating whether to draw bars with the unit
double get_zoom_factor() const
Returns the current zoom factor.
Definition: display.hpp:269
const GLfloat * params
Definition: glew.h:1499
const std::vector< unit_animation > & animations() const
Definition: types.cpp:476
All parameters from a frame at a given instant.
Definition: frame.hpp:71
double idle_anim_rate() const
Definition: display.hpp:519
Definitions for the interface to Wesnoth Markup Language (WML).
void refresh()
Intermittently activates the idling animations in place of the standing animations.
bool idle_anim() const
Definition: display.hpp:517
int frame_begin_time_
time for the frame to begin
int unit_height_adjust() const
Definition: terrain.hpp:58
const config & options()
bool incapacitated() const
Definition: unit.hpp:215
void clear_haloes()
Clear the haloes associated to the unit.
GLuint64EXT * result
Definition: glew.h:10727
unit_animation * get_animation() const
Get a pointer to the current animation.
GLsizei const GLfloat * value
Definition: glew.h:1817
std::string halo_mod
Definition: frame.hpp:82
Encapsulates the map of the game.
Definition: map.hpp:37
STATE state_
animation state
int get_current_animation_tick()
bool invalidate(const display &disp)
Invalidates an animation with respect to a display object, preparing it for redraw.
map_display and display: classes which take care of displaying the map and game-data on the screen...
bool show_standing_animations()
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:135
bool is_flying() const
Definition: unit.hpp:305
boost::scoped_ptr< unit_animation > anim_
The current animation.
STATE
States for animation.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
Encapsulates the map of the game.
Definition: location.hpp:38
const terrain_type & get_terrain_info(const t_translation::t_terrain &terrain) const
Definition: map.cpp:100
void set_idling()
Sets the animation state to idling.
std::string default_anim_image() const
The default image to use for animation frames with no defined image.
Definition: unit.cpp:2267
std::string image_mod
Definition: frame.hpp:78
void start_animation(int start_time, const unit_animation *animation, bool with_bars, const std::string &text="", Uint32 text_color=0, STATE state=STATE_ANIM)
Begin an animation.
size_t i
Definition: function.cpp:1057
void apply_new_animation_effect(const config &effect)
Adds an animation described by a config.
t_translation::t_terrain get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:341
std::vector< unit_animation > animations_
List of registered animations for this unit.
const gamemap & get_map() const
Definition: display.hpp:92
const unit_animation * choose_animation(const display &disp, const map_location &loc, const std::string &event, const map_location &second_loc=map_location::null_location(), const int damage=0, const unit_animation::hit_type hit_type=unit_animation::INVALID, const attack_type *attack=nullptr, const attack_type *second_attack=nullptr, int swing_num=0)
Chooses an appropriate animation from the list of known animations.
cl_event event
Definition: glew.h:3070
map_location get_direction(DIRECTION d, unsigned int n=1u) const
Definition: location.hpp:232
halo::handle unit_halo_
handle to the halo of this unit
void set_standing(bool with_bars=true)
Sets the animation state to standing.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
map_location loc_
Definition: unit.hpp:440
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void set_ghosted(bool with_bars=true)
Sets the animation state to ghosted.
void reset_after_advance(const unit_type *newtype=nullptr)
Resets the animations list after the unit is advanced.
int next_idling_
time for next idle animation
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1049
boost::shared_ptr< halo_record > handle
Definition: halo.hpp:34