-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
unit_animation_component.cpp
204 lines (181 loc) · 6.31 KB
/
unit_animation_component.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Copyright (C) 2014 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "unit_animation_component.hpp"
#include "config.hpp"
#include "display.hpp"
#include "map.hpp"
#include "preferences.hpp"
#include "unit_animation.hpp"
#include "unit.hpp"
#include "unit_types.hpp"
const unit_animation* unit_animation_component::choose_animation(const display& disp, const map_location& loc,const std::string& event,
const map_location& second_loc,const int value,const unit_animation::hit_type hit,
const attack_type* attack, const attack_type* second_attack, int swing_num)
{
// Select one of the matching animations at random
std::vector<const unit_animation*> options;
int max_val = unit_animation::MATCH_FAIL;
for(std::vector<unit_animation>::const_iterator i = animations_.begin(); i != animations_.end(); ++i) {
int matching = i->matches(disp,loc,second_loc,&u_,event,value,hit,attack,second_attack,swing_num);
if(matching > unit_animation::MATCH_FAIL && matching == max_val) {
options.push_back(&*i);
} else if(matching > max_val) {
max_val = matching;
options.clear();
options.push_back(&*i);
}
}
if(max_val == unit_animation::MATCH_FAIL) {
return NULL;
}
return options[rand()%options.size()];
}
void unit_animation_component::set_standing(bool with_bars)
{
display *disp = display::get_singleton();
if (preferences::show_standing_animations()&& !u_.incapacitated()) {
start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "standing"),
with_bars, "", 0, STATE_STANDING);
} else {
start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "_disabled_"),
with_bars, "", 0, STATE_STANDING);
}
}
void unit_animation_component::set_ghosted(bool with_bars)
{
display *disp = display::get_singleton();
start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "ghosted"),
with_bars);
}
void unit_animation_component::set_disabled_ghosted(bool with_bars)
{
display *disp = display::get_singleton();
start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "disabled_ghosted"),
with_bars);
}
void unit_animation_component::set_idling()
{
display *disp = display::get_singleton();
start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "idling"),
true, "", 0, STATE_FORGET);
}
void unit_animation_component::set_selecting()
{
const display *disp = display::get_singleton();
if (preferences::show_standing_animations() && !u_.incapacitated()) {
start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "selected"),
true, "", 0, STATE_FORGET);
} else {
start_animation(INT_MAX, choose_animation(*disp, u_.loc_, "_disabled_selected_"),
true, "", 0, STATE_FORGET);
}
}
void unit_animation_component::start_animation (int start_time, const unit_animation *animation,
bool with_bars, const std::string &text, Uint32 text_color, STATE state)
{
const display * disp = display::get_singleton();
if (!animation) {
if (state == STATE_STANDING)
state_ = state;
if (!anim_ && state_ != STATE_STANDING)
set_standing(with_bars);
return ;
}
state_ = state;
// everything except standing select and idle
bool accelerate = (state != STATE_FORGET && state != STATE_STANDING);
draw_bars_ = with_bars;
anim_.reset(new unit_animation(*animation));
const int real_start_time = start_time == INT_MAX ? anim_->get_begin_time() : start_time;
anim_->start_animation(real_start_time, u_.loc_, u_.loc_.get_direction(u_.facing_),
text, text_color, accelerate);
frame_begin_time_ = anim_->get_begin_time() -1;
if (disp->idle_anim()) {
next_idling_ = get_current_animation_tick()
+ static_cast<int>((20000 + rand() % 20000) * disp->idle_anim_rate());
} else {
next_idling_ = INT_MAX;
}
}
void unit_animation_component::refresh()
{
if (state_ == STATE_FORGET && anim_ && anim_->animation_finished_potential())
{
set_standing();
return;
}
display &disp = *display::get_singleton();
if (state_ != STATE_STANDING || get_current_animation_tick() < next_idling_ ||
!disp.tile_nearly_on_screen(u_.loc_) || u_.incapacitated())
{
return;
}
if (get_current_animation_tick() > next_idling_ + 1000)
{
// prevent all units animating at the same time
if (disp.idle_anim()) {
next_idling_ = get_current_animation_tick()
+ static_cast<int>((20000 + rand() % 20000) * disp.idle_anim_rate());
} else {
next_idling_ = INT_MAX;
}
} else {
set_idling();
}
}
void unit_animation_component::clear_haloes ()
{
unit_halo_ = halo::handle(); //halo::NO_HALO; <-- Removes it from the halo manager automatically.
if(anim_ ) anim_->clear_haloes();
}
bool unit_animation_component::invalidate (const display & disp)
{
bool result = false;
// Very early calls, anim not initialized yet
if(get_animation()) {
frame_parameters params;
const gamemap & map = disp.get_map();
const t_translation::t_terrain terrain = map.get_terrain(u_.loc_);
const terrain_type& terrain_info = map.get_terrain_info(terrain);
int height_adjust = static_cast<int>(terrain_info.unit_height_adjust() * disp.get_zoom_factor());
if (u_.is_flying() && height_adjust < 0) {
height_adjust = 0;
}
params.y -= height_adjust;
params.halo_y -= height_adjust;
params.image_mod = u_.image_mods();
params.halo_mod = u_.TC_image_mods();
params.image= u_.absolute_image();
result |= get_animation()->invalidate(params);
}
return result;
}
void unit_animation_component::reset_after_advance(const unit_type * newtype)
{
if (newtype) {
animations_ = newtype->animations();
}
refreshing_ = false;
anim_.reset();
}
void unit_animation_component::apply_new_animation_effect(const config & effect) {
if(effect["id"].empty()) {
unit_animation::add_anims(animations_, effect);
} else {
static std::map< std::string, std::vector<unit_animation> > animation_cache;
std::vector<unit_animation> &built = animation_cache[effect["id"]];
if(built.empty()) {
unit_animation::add_anims(built, effect);
}
animations_.insert(animations_.end(),built.begin(),built.end());
}
}