forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsprite_actor.cpp
331 lines (277 loc) · 8.63 KB
/
sprite_actor.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player 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 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
// Headers
#include "battle_animation.h"
#include "game_enemy.h"
#include "sprite_actor.h"
#include "game_battler.h"
#include "game_actor.h"
#include "game_screen.h"
#include "bitmap.h"
#include "cache.h"
#include "main_data.h"
#include "player.h"
#include <lcf/reader_util.h>
#include "output.h"
Sprite_Actor::Sprite_Actor(Game_Actor* actor)
: Sprite_Battler(actor, actor->GetId())
{
CreateSprite();
}
Sprite_Actor::~Sprite_Actor() {
}
Game_Actor* Sprite_Actor::GetBattler() const {
return static_cast<Game_Actor*>(Sprite_Battler::GetBattler());
}
void Sprite_Actor::Update() {
auto* battler = GetBattler();
if (!battler->IsHidden() && old_hidden != battler->IsHidden()) {
DoIdleAnimation();
}
old_hidden = battler->IsHidden();
++cycle;
if (anim_state > 0) {
// Animations for allies
if (Player::IsRPG2k3()) {
UpdatePosition();
if (animation) {
// Is a battle animation
animation->SetInvert(battler->IsDirectionFlipped());
animation->Update();
if (animation->IsDone()) {
if (loop_state == LoopState_DefaultAnimationAfterFinish) {
DoIdleAnimation();
} else if (loop_state == LoopState_LoopAnimation) {
animation->SetFrame(0);
} else if (loop_state == LoopState_WaitAfterFinish) {
if (animation->GetFrames() > 0) {
animation->SetFrame(animation->GetFrames() - 1);
}
idling = true;
}
}
return;
}
// Is a battle charset animation
static const int frames[] = {0,1,2,1,0};
int frame = (battler->IsDefending() ? 0 : (normal_attacking ? std::min(2, cycle / 10) : frames[cycle / 10]));
if (battler->IsDirectionFlipped()) {
frame = 2 - frame;
}
if (frame == sprite_frame)
return;
const lcf::rpg::BattlerAnimation* anim = lcf::ReaderUtil::GetElement(lcf::Data::battleranimations, battler->GetBattleAnimationId());
if (!anim) {
Output::Warning("Invalid battler animation ID {}", battler->GetBattleAnimationId());
return;
}
const auto* ext = lcf::ReaderUtil::GetElement(anim->poses, anim_state);
if (!ext) {
Output::Warning("Animation {}: Invalid battler anim-extension state {}", anim->ID, anim_state);
return;
}
SetSrcRect(Rect(frame * 48, ext->battler_index * 48, 48, 48));
if (cycle == ((idling || normal_attacking || anim_state == AnimationState_WalkingLeft || anim_state == AnimationState_WalkingRight || anim_state == AnimationState_Victory) ? 40 : 30)) {
switch (loop_state) {
case LoopState_DefaultAnimationAfterFinish:
DoIdleAnimation();
break;
case LoopState_WaitAfterFinish:
--cycle; // incremented to last cycle next update
idling = true;
break;
case LoopState_LoopAnimation:
cycle = 0;
break;
default:
assert(false && "Bad loop state");
}
}
}
}
if (animation) {
animation->SetVisible(IsVisible());
}
const bool flip = battler->IsDirectionFlipped();
SetFlipX(flip);
}
void Sprite_Actor::SetAnimationState(int state, LoopState loop, int animation_id) {
// Default value is 100 (function called with val+1)
// 100 maps all states to "Bad state" (7)
if (state == 101) {
state = 7;
}
anim_state = state;
loop_state = loop;
cycle = 0;
idling = false;
auto* battler = GetBattler();
if (battler->GetBattleAnimationId() > 0) {
const lcf::rpg::BattlerAnimation* anim = lcf::ReaderUtil::GetElement(lcf::Data::battleranimations, battler->GetBattleAnimationId());
if (!anim) {
Output::Warning("Invalid battler animation ID {}", battler->GetBattleAnimationId());
return;
}
const auto* ext = lcf::ReaderUtil::GetElement(anim->poses, anim_state);
if (!ext) {
Output::Warning("Animation {}: Invalid battler anim-extension state {}", anim->ID, anim_state);
return;
}
StringView sprite_file = ext->battler_name;
if (ext->animation_type == lcf::rpg::BattlerAnimationPose::AnimType_battle) {
do_not_draw = false;
SetBitmap(BitmapRef());
if (animation_id == 0) {
animation_id = ext->battle_animation_id;
}
lcf::rpg::Animation* battle_anim = lcf::ReaderUtil::GetElement(lcf::Data::animations, animation_id);
if (!battle_anim) {
Output::Warning("Invalid battle animation ID {}", animation_id);
animation.reset();
} else {
animation.reset(new BattleAnimationBattler(*battle_anim, { battler }));
animation->SetZ(GetZ());
}
animation->SetInvert(battler->IsDirectionFlipped());
}
else {
do_not_draw = sprite_file.empty();
animation.reset();
if (!sprite_file.empty()) {
FileRequestAsync* request = AsyncHandler::RequestFile("BattleCharSet", sprite_file);
request->SetGraphicFile(true);
request_id = request->Bind(&Sprite_Actor::OnBattlercharsetReady, this, ext->battler_index);
request->Start();
}
}
}
}
void Sprite_Actor::SetAnimationLoop(LoopState loop) {
loop_state = loop;
}
void Sprite_Actor::DetectStateChange() {
if (idling) {
DoIdleAnimation();
}
}
bool Sprite_Actor::IsIdling() {
return idling;
}
int Sprite_Actor::GetWidth() const {
if (animation) {
return animation->GetAnimationCellWidth() / 2;
}
return Sprite::GetWidth();
}
int Sprite_Actor::GetHeight() const {
if (animation) {
return animation->GetAnimationCellHeight() / 2;
}
return Sprite::GetHeight();
}
void Sprite_Actor::CreateSprite() {
auto* battler = GetBattler();
images = {{battler->GetDisplayX(), battler->GetDisplayY()}};
SetX(images.back().x);
SetY(images.back().y);
// Not animated -> Monster
SetOx(24);
SetOy(24);
ResetZ();
SetAnimationState(anim_state);
idling = true;
}
void Sprite_Actor::DoIdleAnimation() {
auto* battler = GetBattler();
if (battler->IsDefending()) {
SetAnimationState(AnimationState_Defending);
idling = true;
return;
}
const lcf::rpg::State* state = battler->GetSignificantState();
int idling_anim;
if (battler->GetBattleAnimationId() <= 0) {
// Monster
// Only visually different state is Death
if (state && state->ID == 1) {
idling_anim = AnimationState_Dead;
} else {
idling_anim = AnimationState_Idle;
}
} else {
idling_anim = state ? state->battler_animation_id + 1 : AnimationState_Idle;
}
if (idling_anim == 101)
idling_anim = 7;
if (idling_anim != anim_state || loop_state == LoopState_DefaultAnimationAfterFinish)
SetAnimationState(idling_anim, idling_anim == AnimationState_Dead ? LoopState_WaitAfterFinish : LoopState_LoopAnimation);
idling = true;
}
void Sprite_Actor::OnBattlercharsetReady(FileRequestResult* result, int32_t battler_index) {
SetBitmap(Cache::Battlecharset(result->file));
SetSrcRect(Rect((battler->IsDirectionFlipped() ? 96 : 0), battler_index * 48, 48, 48));
}
void Sprite_Actor::Draw(Bitmap& dst) {
auto* battler = GetBattler();
// "do_not_draw" is set to true if the CBA battler name is empty, this
// makes the sprite not being drawn. This fixes issue #1708.
if (battler->IsHidden() || do_not_draw) {
return;
}
SetTone(Main_Data::game_screen->GetTone());
SetFlashEffect(battler->GetFlashColor());
int steps = static_cast<int>(256 / images.size());
int opacity = steps;
for (auto it = images.crbegin(); it != images.crend(); ++it) {
Sprite_Battler::SetX(it->x);
Sprite_Battler::SetY(it->y);
Sprite_Battler::SetOpacity(std::min(opacity, 255));
Sprite_Battler::Draw(dst);
opacity += steps;
}
}
void Sprite_Actor::UpdatePosition() {
assert(!images.empty());
images.pop_back();
images.insert(images.begin(), {battler->GetDisplayX(), battler->GetDisplayY()});
if (afterimage_fade >= 0) {
++afterimage_fade;
if (afterimage_fade >= images.size()) {
// CBA finished, remove afterimages
images.resize(1);
afterimage_fade = -1;
}
}
}
void Sprite_Actor::ResetZ() {
Sprite_Battler::ResetZ();
if (animation) {
animation->SetZ(GetZ());
}
}
void Sprite_Actor::SetNormalAttacking(bool nnormal_attacking) {
normal_attacking = nnormal_attacking;
}
void Sprite_Actor::SetAfterimageAmount(unsigned amount) {
images.resize(1 + amount);
std::fill(images.begin() + 1, images.end(), images.front());
}
void Sprite_Actor::DoAfterimageFade() {
if (images.size() > 1) {
afterimage_fade = 0;
}
}