forked from flareteam/flare-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Avatar.cpp
1080 lines (890 loc) · 32.8 KB
/
Avatar.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright © 2011-2012 Clint Bellanger
Copyright © 2012 Igor Paliychuk
Copyright © 2012 Stefan Beller
Copyright © 2013 Henrik Andersson
Copyright © 2012-2016 Justin Jacobs
This file is part of FLARE.
FLARE 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.
FLARE 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
FLARE. If not, see http://www.gnu.org/licenses/
*/
/**
* class Avatar
*
* Contains logic and rendering routines for the player avatar.
*/
#include "Animation.h"
#include "AnimationManager.h"
#include "AnimationSet.h"
#include "Avatar.h"
#include "CommonIncludes.h"
#include "CursorManager.h"
#include "EnemyGroupManager.h"
#include "Entity.h"
#include "EntityManager.h"
#include "EngineSettings.h"
#include "FileParser.h"
#include "InputState.h"
#include "MapRenderer.h"
#include "MenuActionBar.h"
#include "MenuExit.h"
#include "MenuGameOver.h"
#include "MenuInventory.h"
#include "MenuManager.h"
#include "MessageEngine.h"
#include "MenuMiniMap.h"
#include "ModManager.h"
#include "PowerManager.h"
#include "RenderDevice.h"
#include "SaveLoad.h"
#include "Settings.h"
#include "SharedGameResources.h"
#include "SharedResources.h"
#include "SoundManager.h"
#include "Utils.h"
#include "UtilsFileSystem.h"
#include "UtilsMath.h"
#include "UtilsParsing.h"
Avatar::Avatar()
: Entity()
, attack_cursor(false)
, mm_key(settings->mouse_move_swap ? Input::MAIN2 : Input::MAIN1)
, mm_is_distant(false)
, hero_stats(NULL)
, charmed_stats(NULL)
, act_target()
, drag_walking(false)
, respawn(false)
, close_menus(false)
, allow_movement(true)
, cursor_enemy(NULL)
, lock_enemy(NULL)
, time_played(0)
, questlog_dismissed(false)
, using_main1(false)
, using_main2(false)
, prev_hp(0)
, playing_lowhp(false)
, teleport_camera_lock(false)
, feet_index(-1)
{
init();
// TODO
// set cooldown_hit to duration of hit animation if undefined
if (!stats.cooldown_hit_enabled) {
Animation *hit_anim = animationSet->getAnimation("hit");
if (hit_anim) {
stats.cooldown_hit.setDuration(hit_anim->getDuration());
delete hit_anim;
}
else {
stats.cooldown_hit.setDuration(0);
}
}
loadLayerDefinitions();
// load foot-step definitions
// @CLASS Avatar: Step sounds|Description of items/step_sounds.txt
FileParser infile;
if (infile.open("items/step_sounds.txt", FileParser::MOD_FILE, FileParser::ERROR_NONE)) {
while (infile.next()) {
if (infile.key == "id") {
// @ATTR id|string|An identifier name for a set of step sounds.
step_def.push_back(Step_sfx());
step_def.back().id = infile.val;
}
if (step_def.empty()) continue;
if (infile.key == "step") {
// @ATTR step|filename|Filename of a step sound effect.
step_def.back().steps.push_back(infile.val);
}
}
infile.close();
}
loadStepFX(stats.sfx_step);
}
void Avatar::init() {
// name, base, look are set by GameStateNew so don't reset it here
// other init
sprites = 0;
stats.cur_state = StatBlock::ENTITY_STANCE;
if (mapr->hero_pos_enabled) {
stats.pos.x = mapr->hero_pos.x;
stats.pos.y = mapr->hero_pos.y;
}
current_power = 0;
newLevelNotification = false;
stats.hero = true;
stats.humanoid = true;
stats.level = 1;
stats.xp = 0;
for (size_t i = 0; i < eset->primary_stats.list.size(); ++i) {
stats.primary[i] = stats.primary_starting[i] = 1;
stats.primary_additional[i] = 0;
}
stats.speed = 0.2f;
stats.recalc();
while (!log_msg.empty()) {
log_msg.pop();
}
respawn = false;
stats.cooldown.reset(Timer::END);
body = -1;
transform_triggered = false;
setPowers = false;
revertPowers = false;
last_transform = "";
power_cooldown_timers.clear();
power_cast_timers.clear();
// Find untransform power index to use for manual untransfrom ability
untransform_power = 0;
std::map<PowerID, Power>::iterator power_it;
for (power_it = powers->powers.begin(); power_it != powers->powers.end(); ++power_it) {
if (untransform_power == 0 && power_it->second.required_items.empty() && power_it->second.spawn_type == "untransform") {
untransform_power = power_it->first;
}
power_cooldown_timers[power_it->first] = Timer();
power_cast_timers[power_it->first] = Timer();
}
stats.animations = "animations/hero.txt";
loadAnimations();
}
void Avatar::handleNewMap() {
cursor_enemy = NULL;
lock_enemy = NULL;
playing_lowhp = false;
stats.target_corpse = NULL;
stats.target_nearest = NULL;
stats.target_nearest_corpse = NULL;
}
/**
* Load avatar sprite layer definitions into vector.
*/
void Avatar::loadLayerDefinitions() {
if (!stats.layer_reference_order.empty())
return;
Utils::logError("Avatar: Loading render layers from engine/hero_layers.txt is deprecated! Render layers should be loaded in the 'render_layers' section of engine/stats.txt.");
FileParser infile;
if (infile.open("engine/hero_layers.txt", FileParser::MOD_FILE, FileParser::ERROR_NORMAL)) {
while(infile.next()) {
// hero_layers.txt doesn't have sections, but we now expect the layer key to be under one called "render_layers"
if (infile.section.empty())
infile.section = "render_layers";
if (!stats.loadRenderLayerStat(&infile)) {
infile.error("Avatar: '%s' is not a valid key.", infile.key.c_str());
}
}
infile.close();
}
}
/**
* Walking/running steps sound depends on worn armor
*/
void Avatar::loadStepFX(const std::string& stepname) {
std::string filename = stats.sfx_step;
if (stepname != "") {
filename = stepname;
}
// clear previous sounds
for (unsigned i=0; i<sound_steps.size(); i++) {
snd->unload(sound_steps[i]);
}
sound_steps.clear();
if (filename == "") return;
// A literal "NULL" means we don't want to load any new sounds
// This is used when transforming, since creatures don't have step sound effects
if (stepname == "NULL") return;
// load new sounds
for (unsigned i=0; i<step_def.size(); i++) {
if (step_def[i].id == filename) {
sound_steps.resize(step_def[i].steps.size());
for (unsigned j=0; j<sound_steps.size(); j++) {
sound_steps[j] = snd->load(step_def[i].steps[j], "Avatar loading foot steps");
}
return;
}
}
// Could not find step sound fx
Utils::logError("Avatar: Could not find footstep sounds for '%s'.", filename.c_str());
}
bool Avatar::pressing_move() {
if (!allow_movement || teleport_camera_lock) {
return false;
}
else if (stats.effects.knockback_speed != 0) {
return false;
}
else if (settings->mouse_move) {
return inpt->pressing[mm_key] && !inpt->pressing[Input::SHIFT] && mm_is_distant;
}
else {
return (inpt->pressing[Input::UP] && !inpt->lock[Input::UP]) ||
(inpt->pressing[Input::DOWN] && !inpt->lock[Input::DOWN]) ||
(inpt->pressing[Input::LEFT] && !inpt->lock[Input::LEFT]) ||
(inpt->pressing[Input::RIGHT] && !inpt->lock[Input::RIGHT]);
}
}
void Avatar::set_direction() {
if (teleport_camera_lock || !set_dir_timer.isEnd())
return;
int old_dir = stats.direction;
// handle direction changes
if (settings->mouse_move) {
if (mm_is_distant) {
FPoint target = Utils::screenToMap(inpt->mouse.x, inpt->mouse.y, mapr->cam.pos.x, mapr->cam.pos.y);
stats.direction = Utils::calcDirection(stats.pos.x, stats.pos.y, target.x, target.y);
}
}
else {
// movement keys take top priority for setting direction
bool press_up = inpt->pressing[Input::UP] && !inpt->lock[Input::UP];
bool press_down = inpt->pressing[Input::DOWN] && !inpt->lock[Input::DOWN];
bool press_left = inpt->pressing[Input::LEFT] && !inpt->lock[Input::LEFT];
bool press_right = inpt->pressing[Input::RIGHT] && !inpt->lock[Input::RIGHT];
// aiming keys can set direction as well
if (!press_up && !press_down && !press_left && !press_right) {
press_up = inpt->pressing[Input::AIM_UP] && !inpt->lock[Input::AIM_UP];
press_down = inpt->pressing[Input::AIM_DOWN] && !inpt->lock[Input::AIM_DOWN];
press_left = inpt->pressing[Input::AIM_LEFT] && !inpt->lock[Input::AIM_LEFT];
press_right = inpt->pressing[Input::AIM_RIGHT] && !inpt->lock[Input::AIM_RIGHT];
}
if (press_up && press_left) stats.direction = 1;
else if (press_up && press_right) stats.direction = 3;
else if (press_down && press_right) stats.direction = 5;
else if (press_down && press_left) stats.direction = 7;
else if (press_left) stats.direction = 0;
else if (press_up) stats.direction = 2;
else if (press_right) stats.direction = 4;
else if (press_down) stats.direction = 6;
// Adjust for ORTHO tilesets
if (eset->tileset.orientation == eset->tileset.TILESET_ORTHOGONAL && (press_up || press_down || press_left || press_right))
stats.direction = static_cast<unsigned char>((stats.direction == 7) ? 0 : stats.direction + 1);
}
// give direction changing a 100ms cooldown
// this allows the player to quickly change direction on their own without becoming overly "jittery"
// the cooldown can be ended by releasing the move button, but the cooldown is so fast that it doesn't matter much (maybe a speed run tactic?)
if (stats.direction != old_dir)
set_dir_timer.setDuration(settings->max_frames_per_sec / 10);
}
/**
* logic()
* Handle a single frame. This includes:
* - move the avatar based on buttons pressed
* - calculate the next frame of animation
* - calculate camera position based on avatar position
*/
void Avatar::logic() {
bool restrict_power_use = false;
if (settings->mouse_move) {
if(inpt->pressing[mm_key] && !inpt->pressing[Input::SHIFT] && !menu->act->isWithinSlots(inpt->mouse) && !menu->act->isWithinMenus(inpt->mouse)) {
restrict_power_use = true;
}
}
// clear current space to allow correct movement
mapr->collider.unblock(stats.pos.x, stats.pos.y);
// turn on all passive powers
if ((stats.hp > 0 || stats.effects.triggered_death) && !respawn && !transform_triggered)
powers->activatePassives(&stats);
if (transform_triggered)
transform_triggered = false;
// handle when the player stops blocking
if (stats.effects.triggered_block && !stats.blocking) {
stats.cur_state = StatBlock::ENTITY_STANCE;
stats.effects.triggered_block = false;
stats.effects.clearTriggerEffects(Power::TRIGGER_BLOCK);
stats.refresh_stats = true;
stats.block_power = 0;
}
stats.logic();
// alert on low health
if (isDroppedToLowHp()) {
// show message if set
if (isLowHpMessageEnabled()) {
logMsg(msg->get("Your health is low!"), MSG_NORMAL);
}
// play a sound if set in settings
if (isLowHpSoundEnabled() && !playing_lowhp) {
// if looping, then do not cleanup
snd->play(sound_lowhp, "lowhp", snd->NO_POS, stats.sfx_lowhp_loop, !stats.sfx_lowhp_loop);
playing_lowhp = true;
}
}
// if looping, stop sounds when HP recovered above threshold
if (isLowHpSoundEnabled() && !isLowHp() && playing_lowhp && stats.sfx_lowhp_loop) {
snd->pauseChannel("lowhp");
playing_lowhp = false;
}
else if (isLowHpSoundEnabled() && isLowHp() && !playing_lowhp && stats.sfx_lowhp_loop) {
snd->play(sound_lowhp, "lowhp", snd->NO_POS, stats.sfx_lowhp_loop, !stats.sfx_lowhp_loop);
playing_lowhp = true;
}
else if (!isLowHpSoundEnabled() && playing_lowhp) {
snd->pauseChannel("lowhp");
playing_lowhp = false;
}
// we can not use stats.prev_hp here
prev_hp = stats.hp;
// check level up
if (stats.level < eset->xp.getMaxLevel() && stats.xp >= eset->xp.getLevelXP(stats.level + 1)) {
stats.level_up = true;
stats.level = eset->xp.getLevelFromXP(stats.xp);
logMsg(msg->getv("Congratulations, you have reached level %d!", stats.level), MSG_NORMAL);
if (pc->stats.stat_points_per_level > 0) {
logMsg(msg->get("You may increase one or more attributes through the Character Menu."), MSG_NORMAL);
newLevelNotification = true;
}
if (pc->stats.power_points_per_level > 0) {
logMsg(msg->get("You may unlock one or more abilities through the Powers Menu."), MSG_NORMAL);
}
stats.recalc();
snd->play(sound_levelup, snd->DEFAULT_CHANNEL, snd->NO_POS, !snd->LOOP);
// if the player managed to level up while dead (e.g. via a bleeding creature), restore to life
if (stats.cur_state == StatBlock::ENTITY_DEAD) {
stats.cur_state = StatBlock::ENTITY_STANCE;
}
}
// assist mouse movement
mm_key = settings->mouse_move_swap ? Input::MAIN2 : Input::MAIN1;
if (!inpt->pressing[mm_key]) {
drag_walking = false;
}
// block some interactions when attacking/moving
using_main1 = inpt->pressing[Input::MAIN1] && !inpt->lock[Input::MAIN1];
using_main2 = inpt->pressing[Input::MAIN2] && !inpt->lock[Input::MAIN2];
// handle animation
if (!stats.effects.stun) {
if (activeAnimation)
activeAnimation->advanceFrame();
for (size_t i = 0; i < anims.size(); ++i) {
if (anims[i])
anims[i]->advanceFrame();
}
}
// save a valid tile position in the event that we untransform on an invalid tile
if (stats.transformed && mapr->collider.isValidPosition(stats.pos.x, stats.pos.y, MapCollision::MOVE_NORMAL, MapCollision::COLLIDE_HERO)) {
transform_pos = stats.pos;
transform_map = mapr->getFilename();
}
PowerID mm_attack_id = (settings->mouse_move_swap ? menu->act->getSlotPower(MenuActionBar::SLOT_MAIN2) : menu->act->getSlotPower(MenuActionBar::SLOT_MAIN1));
bool mm_can_use_power = true;
if (settings->mouse_move) {
if (!inpt->pressing[mm_key]) {
lock_enemy = NULL;
}
else {
// prevents erratic behavior when mouse move is too close to player
FPoint target = Utils::screenToMap(inpt->mouse.x, inpt->mouse.y, mapr->cam.pos.x, mapr->cam.pos.y);
if (stats.cur_state == StatBlock::ENTITY_MOVE) {
mm_is_distant = Utils::calcDist(stats.pos, target) >= eset->misc.mouse_move_deadzone_moving;
}
else {
mm_is_distant = Utils::calcDist(stats.pos, target) >= eset->misc.mouse_move_deadzone_not_moving;
}
}
if (lock_enemy && lock_enemy->stats.hp <= 0) {
lock_enemy = NULL;
}
if (mm_attack_id > 0) {
if (!stats.canUsePower(mm_attack_id, !StatBlock::CAN_USE_PASSIVE)) {
lock_enemy = NULL;
mm_can_use_power = false;
}
else if (!power_cooldown_timers[mm_attack_id].isEnd()) {
lock_enemy = NULL;
mm_can_use_power = false;
}
else if (lock_enemy && powers->powers[mm_attack_id].requires_los && !mapr->collider.lineOfSight(stats.pos.x, stats.pos.y, lock_enemy->stats.pos.x, lock_enemy->stats.pos.y)) {
lock_enemy = NULL;
mm_can_use_power = false;
}
}
}
if (teleport_camera_lock && Utils::calcDist(stats.pos, mapr->cam.pos) < 0.5f) {
teleport_camera_lock = false;
}
set_dir_timer.tick();
if (!pressing_move()) {
set_dir_timer.reset(Timer::END);
}
if (!stats.effects.stun) {
bool allowed_to_move;
bool allowed_to_turn;
bool allowed_to_use_power = true;
switch(stats.cur_state) {
case StatBlock::ENTITY_STANCE:
setAnimation("stance");
// allowed to move or use powers?
if (settings->mouse_move) {
allowed_to_move = restrict_power_use && (!inpt->lock[mm_key] || drag_walking) && !lock_enemy;
allowed_to_turn = allowed_to_move;
allowed_to_use_power = true;
if ((inpt->pressing[mm_key] && inpt->pressing[Input::SHIFT]) || lock_enemy) {
inpt->lock[mm_key] = false;
}
}
else if (!settings->mouse_aim) {
allowed_to_move = !inpt->pressing[Input::SHIFT];
allowed_to_turn = true;
allowed_to_use_power = true;
}
else {
allowed_to_move = true;
allowed_to_turn = true;
allowed_to_use_power = true;
}
// handle transitions to RUN
if (allowed_to_turn)
set_direction();
if (pressing_move() && allowed_to_move) {
if (move()) { // no collision
if (settings->mouse_move && inpt->pressing[mm_key]) {
inpt->lock[mm_key] = true;
drag_walking = true;
}
stats.cur_state = StatBlock::ENTITY_MOVE;
}
}
if (settings->mouse_move && settings->mouse_move_attack && cursor_enemy && !cursor_enemy->stats.hero_ally && mm_can_use_power && powers->checkCombatRange(mm_attack_id, &stats, cursor_enemy->stats.pos)) {
stats.cur_state = StatBlock::ENTITY_STANCE;
lock_enemy = cursor_enemy;
}
break;
case StatBlock::ENTITY_MOVE:
setAnimation("run");
if (!sound_steps.empty()) {
int stepfx = rand() % static_cast<int>(sound_steps.size());
if (activeAnimation->isFirstFrame() || activeAnimation->isActiveFrame())
snd->play(sound_steps[stepfx], snd->DEFAULT_CHANNEL, snd->NO_POS, !snd->LOOP);
}
// handle direction changes
set_direction();
// handle transition to STANCE
if (!pressing_move()) {
stats.cur_state = StatBlock::ENTITY_STANCE;
break;
}
else if (!move()) { // collide with wall
stats.cur_state = StatBlock::ENTITY_STANCE;
break;
}
else if ((settings->mouse_move || !settings->mouse_aim) && inpt->pressing[Input::SHIFT]) {
// Shift should stop movement in some cases.
// With mouse_move, it allows the player to stop moving and begin attacking.
// With mouse_aim disabled, it allows the player to aim their attacks without having to move.
stats.cur_state = StatBlock::ENTITY_STANCE;
break;
}
if (activeAnimation->getName() != "run")
stats.cur_state = StatBlock::ENTITY_STANCE;
if (settings->mouse_move && settings->mouse_move_attack && cursor_enemy && !cursor_enemy->stats.hero_ally && mm_can_use_power && powers->checkCombatRange(mm_attack_id, &stats, cursor_enemy->stats.pos)) {
stats.cur_state = StatBlock::ENTITY_STANCE;
lock_enemy = cursor_enemy;
}
break;
case StatBlock::ENTITY_POWER:
setAnimation(attack_anim);
if (attack_cursor) {
curs->setCursor(CursorManager::CURSOR_ATTACK);
}
if (activeAnimation->isFirstFrame()) {
float attack_speed = (stats.effects.getAttackSpeed(attack_anim) * powers->powers[current_power].attack_speed) / 100.0f;
activeAnimation->setSpeed(attack_speed);
for (size_t i=0; i<anims.size(); ++i) {
if (anims[i])
anims[i]->setSpeed(attack_speed);
}
playAttackSound(attack_anim);
power_cast_timers[current_power].setDuration(activeAnimation->getDuration());
}
// do power
if (activeAnimation->isActiveFrame() && !stats.hold_state) {
// some powers check if the caster is blocking a tile
// so we block the player tile prematurely here
mapr->collider.block(stats.pos.x, stats.pos.y, !MapCollision::IS_ALLY);
powers->activate(current_power, &stats, act_target);
power_cooldown_timers[current_power].setDuration(powers->powers[current_power].cooldown);
if (!stats.state_timer.isEnd())
stats.hold_state = true;
}
// animation is done, switch back to normal stance
if ((activeAnimation->isLastFrame() && stats.state_timer.isEnd()) || activeAnimation->getName() != attack_anim) {
stats.cur_state = StatBlock::ENTITY_STANCE;
stats.cooldown.reset(Timer::BEGIN);
allowed_to_use_power = false;
stats.prevent_interrupt = false;
if (settings->mouse_move) {
drag_walking = true;
}
}
if (settings->mouse_move && lock_enemy && !powers->checkCombatRange(mm_attack_id, &stats, lock_enemy->stats.pos)) {
lock_enemy = NULL;
}
break;
case StatBlock::ENTITY_BLOCK:
setAnimation("block");
stats.blocking = false;
break;
case StatBlock::ENTITY_HIT:
setAnimation("hit");
if (activeAnimation->isFirstFrame()) {
stats.effects.triggered_hit = true;
if (stats.block_power != 0) {
power_cooldown_timers[stats.block_power].setDuration(powers->powers[stats.block_power].cooldown);
stats.block_power = 0;
}
}
if (activeAnimation->getTimesPlayed() >= 1 || activeAnimation->getName() != "hit") {
stats.cur_state = StatBlock::ENTITY_STANCE;
if (settings->mouse_move) {
drag_walking = true;
}
}
break;
case StatBlock::ENTITY_DEAD:
allowed_to_use_power = false;
if (stats.effects.triggered_death) break;
if (stats.transformed) {
stats.transform_duration = 0;
untransform();
}
setAnimation("die");
if (!stats.corpse && activeAnimation->isFirstFrame() && activeAnimation->getTimesPlayed() < 1) {
stats.effects.clearEffects();
stats.powers_passive.clear();
// reset power cooldowns
std::map<size_t, Timer>::iterator pct_it;
for (pct_it = power_cooldown_timers.begin(); pct_it != power_cooldown_timers.end(); ++pct_it) {
pct_it->second.reset(Timer::END);
power_cast_timers[pct_it->first].reset(Timer::END);
}
// close menus in GameStatePlay
close_menus = true;
playSound(Entity::SOUND_DIE);
logMsg(msg->get("You are defeated."), MSG_NORMAL);
if (stats.permadeath) {
// ignore death penalty on permadeath and instead delete the player's saved game
stats.death_penalty = false;
Utils::removeSaveDir(save_load->getGameSlot());
menu->exit->disableSave();
menu->game_over->disableSave();
}
else {
// raise the death penalty flag. This is handled in MenuInventory
stats.death_penalty = true;
}
// if the player is attacking, we need to block further input
if (inpt->pressing[Input::MAIN1])
inpt->lock[Input::MAIN1] = true;
}
if (!stats.corpse && (activeAnimation->getTimesPlayed() >= 1 || activeAnimation->getName() != "die")) {
stats.corpse = true;
menu->game_over->visible = true;
}
// allow respawn with Accept if not permadeath
if (menu->game_over->visible && menu->game_over->continue_clicked) {
menu->game_over->close();
mapr->teleportation = true;
mapr->teleport_mapname = mapr->respawn_map;
if (stats.permadeath) {
// set these positions so it doesn't flash before jumping to Title
mapr->teleport_destination.x = stats.pos.x;
mapr->teleport_destination.y = stats.pos.y;
}
else {
respawn = true;
// set teleportation variables. GameEngine acts on these.
mapr->teleport_destination.x = mapr->respawn_point.x;
mapr->teleport_destination.y = mapr->respawn_point.y;
}
}
break;
default:
break;
}
// handle power usage
if (allowed_to_use_power) {
bool blocking = false;
for (unsigned i=0; i<action_queue.size(); i++) {
ActionData &action = action_queue[i];
PowerID power_id = powers->checkReplaceByEffect(action.power, &stats);
const Power &power = powers->powers[power_id];
if (power.type == Power::TYPE_BLOCK)
blocking = true;
if (power_id != 0 && (stats.cooldown.isEnd() || action.instant_item)) {
FPoint target = action.target;
// check requirements
if ((stats.cur_state == StatBlock::ENTITY_POWER || stats.cur_state == StatBlock::ENTITY_HIT) && !action.instant_item)
continue;
if (!stats.canUsePower(power_id, !StatBlock::CAN_USE_PASSIVE))
continue;
if (power.requires_los && !mapr->collider.lineOfSight(stats.pos.x, stats.pos.y, target.x, target.y))
continue;
if (power.requires_empty_target && !mapr->collider.isEmpty(target.x, target.y))
continue;
if (!power_cooldown_timers[power_id].isEnd())
continue;
if (!powers->hasValidTarget(power_id, &stats, target))
continue;
// automatically target the selected enemy with melee attacks
if (inpt->usingMouse() && power.type == Power::TYPE_FIXED && power.starting_pos == Power::STARTING_POS_MELEE && cursor_enemy) {
target = cursor_enemy->stats.pos;
}
// is this a power that requires changing direction?
if (power.face) {
stats.direction = Utils::calcDirection(stats.pos.x, stats.pos.y, target.x, target.y);
}
if (power.new_state != Power::STATE_INSTANT) {
current_power = power_id;
act_target = target;
attack_anim = power.attack_anim;
}
if (power.state_duration > 0)
stats.state_timer.setDuration(power.state_duration);
if (power.charge_speed != 0.0f)
stats.charge_speed = power.charge_speed;
stats.prevent_interrupt = power.prevent_interrupt;
for (size_t j = 0; j < power.chain_powers.size(); ++j) {
const ChainPower& chain_power = power.chain_powers[j];
if (chain_power.type == ChainPower::TYPE_PRE && Math::percentChanceF(chain_power.chance)) {
powers->activate(chain_power.id, &stats, target);
}
}
switch (power.new_state) {
case Power::STATE_ATTACK: // handle attack powers
stats.cur_state = StatBlock::ENTITY_POWER;
break;
case Power::STATE_INSTANT: // handle instant powers
powers->activate(power_id, &stats, target);
power_cooldown_timers[power_id].setDuration(power.cooldown);
break;
default:
if (power.type == Power::TYPE_BLOCK) {
stats.cur_state = StatBlock::ENTITY_BLOCK;
powers->activate(power_id, &stats, target);
stats.refresh_stats = true;
}
break;
}
// if the player is attacking, show the attack cursor
attack_cursor = (
stats.cur_state == StatBlock::ENTITY_POWER &&
!power.buff && !power.buff_teleport &&
power.type != Power::TYPE_TRANSFORM &&
power.type != Power::TYPE_BLOCK &&
!(power.starting_pos == Power::STARTING_POS_SOURCE && power.speed == 0)
);
}
}
for (size_t i = action_queue.size(); i > 0; i--) {
if (action_queue[i-1].activated_from_inventory)
action_queue.erase(action_queue.begin()+(i-1));
}
stats.blocking = blocking;
}
}
// update camera
mapr->cam.setTarget(stats.pos);
// check for map events
mapr->checkEvents(stats.pos);
// decrement all cooldowns
std::map<size_t, Timer>::iterator pct_it;
for (pct_it = power_cooldown_timers.begin(); pct_it != power_cooldown_timers.end(); ++pct_it) {
pct_it->second.tick();
power_cast_timers[pct_it->first].tick();
}
// make the current square solid
mapr->collider.block(stats.pos.x, stats.pos.y, !MapCollision::IS_ALLY);
if (stats.state_timer.isEnd() && stats.hold_state)
stats.hold_state = false;
if (stats.cur_state != StatBlock::ENTITY_POWER && stats.charge_speed != 0.0f)
stats.charge_speed = 0.0f;
}
void Avatar::transform() {
// dead players can't transform
if (stats.hp <= 0)
return;
// calling a transform power locks the actionbar, so we unlock it here
inpt->unlockActionBar();
delete charmed_stats;
charmed_stats = NULL;
Enemy_Level el = enemyg->getRandomEnemy(stats.transform_type, 0, 0);
if (el.type != "") {
charmed_stats = new StatBlock();
charmed_stats->load(el.type);
}
else {
Utils::logError("Avatar: Could not transform into creature type '%s'", stats.transform_type.c_str());
stats.transform_type = "";
return;
}
transform_triggered = true;
stats.transformed = true;
setPowers = true;
// temporary save hero stats
delete hero_stats;
hero_stats = new StatBlock();
*hero_stats = stats;
// do not allow two copies of the summons list
hero_stats->summons.clear();
// replace some hero stats
stats.speed = charmed_stats->speed;
stats.flying = charmed_stats->flying;
stats.intangible = charmed_stats->intangible;
stats.humanoid = charmed_stats->humanoid;
stats.animations = charmed_stats->animations;
stats.powers_list = charmed_stats->powers_list;
stats.powers_passive = charmed_stats->powers_passive;
stats.effects.clearEffects();
stats.animations = charmed_stats->animations;
stats.layer_reference_order = charmed_stats->layer_reference_order;
stats.layer_def = charmed_stats->layer_def;
stats.animation_slots = charmed_stats->animation_slots;
anim->decreaseCount(hero_stats->animations);
animationSet = NULL;
loadAnimations();
stats.cur_state = StatBlock::ENTITY_STANCE;
// base stats
for (int i=0; i<Stats::COUNT; ++i) {
stats.starting[i] = std::max(stats.starting[i], charmed_stats->starting[i]);
}
loadSoundsFromStatBlock(charmed_stats);
loadStepFX("NULL");
stats.applyEffects();
transform_pos = stats.pos;
transform_map = mapr->getFilename();
}
void Avatar::untransform() {
// calling a transform power locks the actionbar, so we unlock it here
inpt->unlockActionBar();
// For timed transformations, move the player to the last valid tile when untransforming
mapr->collider.unblock(stats.pos.x, stats.pos.y);
if (!mapr->collider.isValidPosition(stats.pos.x, stats.pos.y, MapCollision::MOVE_NORMAL, MapCollision::COLLIDE_HERO)) {
logMsg(msg->get("Transformation expired. You have been moved back to a safe place."), MSG_NORMAL);
if (transform_map != mapr->getFilename()) {
mapr->teleportation = true;
mapr->teleport_mapname = transform_map;
mapr->teleport_destination.x = floorf(transform_pos.x) + 0.5f;
mapr->teleport_destination.y = floorf(transform_pos.y) + 0.5f;
transform_map = "";
}
else {
stats.pos.x = floorf(transform_pos.x) + 0.5f;
stats.pos.y = floorf(transform_pos.y) + 0.5f;
}
}
mapr->collider.block(stats.pos.x, stats.pos.y, !MapCollision::IS_ALLY);
stats.transformed = false;
transform_triggered = true;
stats.transform_type = "";
revertPowers = true;
stats.effects.clearEffects();
// revert some hero stats to last saved
stats.speed = hero_stats->speed;
stats.flying = hero_stats->flying;
stats.intangible = hero_stats->intangible;
stats.humanoid = hero_stats->humanoid;
stats.animations = hero_stats->animations;
stats.effects = hero_stats->effects;
stats.powers_list = hero_stats->powers_list;
stats.powers_passive = hero_stats->powers_passive;
stats.animations = hero_stats->animations;
stats.layer_reference_order = hero_stats->layer_reference_order;
stats.layer_def = hero_stats->layer_def;
stats.animation_slots = hero_stats->animation_slots;
anim->decreaseCount(charmed_stats->animations);
animationSet = NULL;
loadAnimations();
stats.cur_state = StatBlock::ENTITY_STANCE;
// This is a bit of a hack.
// In order to switch to the stance animation, we can't already be in a stance animation
setAnimation("run");
for (int i=0; i<Stats::COUNT; ++i) {
stats.starting[i] = hero_stats->starting[i];
}
loadSounds();
loadStepFX(stats.sfx_step);
delete charmed_stats;
delete hero_stats;
charmed_stats = NULL;
hero_stats = NULL;
stats.applyEffects();
stats.untransform_on_hit = false;
}
void Avatar::checkTransform() {
// handle transformation
if (stats.transform_type != "" && stats.transform_type != "untransform" && stats.transformed == false)
transform();
if (stats.transform_type != "" && stats.transform_duration == 0)
untransform();
}