-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclassmap.cpp
2442 lines (2078 loc) · 91.5 KB
/
classmap.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
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdlib>
using namespace std;
#include "classmap.h"
#include "objects/object.h"
#include "character/character.h"
#include "collision_detection.h"
#include "file/file_io.h"
#include "game_mediator.h"
#include "aux_tools/exception_manager.h"
extern string FILEPATH;
extern graphicsLib graphLib;
#include "game.h"
extern game gameControl;
#include "inputlib.h"
extern inputLib input;
#include "soundlib.h"
extern soundLib soundManager;
#ifdef ANDROID
#include <android/log.h>
#endif
#include "file/file_io.h"
extern CURRENT_FILE_FORMAT::file_io fio;
extern CURRENT_FILE_FORMAT::file_game game_data;
extern CURRENT_FILE_FORMAT::file_stage stage_data;
extern CURRENT_FILE_FORMAT::st_save game_save;
extern CURRENT_FILE_FORMAT::st_save game_save;
extern struct CURRENT_FILE_FORMAT::st_checkpoint checkpoint;
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
classMap::classMap() : stage_number(-1), number(-1), bg_scroll(st_float_position(0.0, 0.0)), fg_layer_scroll(st_float_position(0.0, 0.0)), _platform_leave_counter(0)
{
for (int i=0; i<MAP_W; i++) {
wall_scroll_lock[i] = false;
}
_water_bubble.pos.x = -1;
_water_bubble.pos.y = -1;
_water_bubble.x_adjust = 0;
_water_bubble.timer = 0;
_water_bubble.x_adjust_direction = ANIM_DIRECTION_LEFT;
_3rd_level_ignore_area = st_rectangle(-1, -1, -1, -1);
_level3_tiles = std::vector<struct st_level3_tile>();
_show_map_pos_x = -1;
graphLib.initSurface(st_size(RES_W+(TILESIZE*2), RES_H), &map_screen);
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
classMap::~classMap()
{
}
void classMap::reset_map()
{
// reset objects
for (std::vector<object>::iterator it=object_list.begin(); it!=object_list.end(); it++) {
object& temp_obj = (*it);
// if object is a player item, remove it
if (temp_obj.get_id() == game_data.player_items[0] || temp_obj.get_id() == game_data.player_items[1]) {
temp_obj.set_finished(true);
} else {
temp_obj.reset();
}
}
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
void classMap::setStageNumber(int setStageN) {
stage_number = setStageN;
}
short classMap::get_number() const
{
return number;
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
void classMap::setMapNumber(int setMapN) {
number = setMapN;
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
void classMap::loadMap()
{
if (stage_number == -1) {
graphLib.show_debug_msg("ERROR::loadStage invalid number[-1]");
cout << "ERROR::map::loadMap - stage number was not set, can't load it before setting the number.\n";
return;
}
if (number == -1) {
graphLib.show_debug_msg("ERROR::loadStage invalid number[>MAX]");
cout << "ERROR::map::loadMap - map number was not set, can't load it before setting the number.\n";
return;
}
object_list.clear();
_npc_list.clear();
animation_list.clear();
_level3_tiles.clear();
for (int i=0; i<MAP_W; i++) {
for (int j=0; j<MAP_H; j++) {
int lvl3_x = GameMediator::get_instance()->map_data[number].tiles[i][j].tile3.x;
int lvl3_y = GameMediator::get_instance()->map_data[number].tiles[i][j].tile3.y;
if (lvl3_x != -1 && lvl3_y != -1) {
struct st_level3_tile temp_tile(st_position(lvl3_x, lvl3_y), st_position(i, j));
_level3_tiles.push_back(temp_tile);
}
}
}
load_map_npcs();
load_map_objects();
bool column_locked = true;
for (int i=0; i<MAP_W; i++) {
column_locked = true;
for (int j=0; j<MAP_H; j++) {
// check if a object-door ocuppies the position
std::vector<object>::iterator object_it;
bool obj_locked = false;
for (object_it = object_list.begin(); object_it != object_list.end(); object_it++) {
object *obj_item = &(*object_it);
if (obj_item->get_type() == OBJ_BOSS_DOOR) {
if ((i >= obj_item->get_start_position().x/TILESIZE && i < obj_item->get_start_position().x/TILESIZE+obj_item->get_size().width/TILESIZE) && (j >= obj_item->get_start_position().y/TILESIZE && j < obj_item->get_start_position().y/TILESIZE+obj_item->get_size().height/TILESIZE)) {
obj_locked = true;
}
}
}
if (obj_locked != true && GameMediator::get_instance()->map_data[number].tiles[i][j].locked != TERRAIN_SOLID && GameMediator::get_instance()->map_data[number].tiles[i][j].locked != TERRAIN_DOOR && GameMediator::get_instance()->map_data[number].tiles[i][j].locked != TERRAIN_SCROLL_LOCK && GameMediator::get_instance()->map_data[number].tiles[i][j].locked != TERRAIN_ICE && GameMediator::get_instance()->map_data[number].tiles[i][j].locked != TERRAIN_SPIKE) {
column_locked = false;
break;
}
}
wall_scroll_lock[i] = column_locked;
}
create_dynamic_background_surfaces();
init_animated_tiles();
#ifdef HANDLELD // portable consoles aren't strong enought for two dynamic backgrounds
GameMediator::get_instance()->map_data[number].backgrounds[0].speed = 0;
#endif
}
void classMap::show_map()
{
draw_dynamic_backgrounds();
if (get_map_gfx_mode() == SCREEN_GFX_MODE_BACKGROUND) {
draw_lib.show_gfx();
}
// TODO: optimization, build a list of visible enemies and one for ghost and other for non-ghost,
// and use those instead in the show-loop
show_ghost_npcs();
// redraw screen, if needed
if (_show_map_pos_x == -1 || abs(_show_map_pos_x - scroll.x) > TILESIZE) {
draw_map_tiles();
// use memory screen
}
int diff_scroll_x = scroll.x - _show_map_pos_x;
graphLib.copyArea(st_rectangle(diff_scroll_x+TILESIZE, 0, RES_W, RES_H), st_position(0, 0), &map_screen, &graphLib.gameScreen);
// draw animated tiles
draw_animated_tiles(graphLib.gameScreen);
if (get_map_gfx_mode() == SCREEN_GFX_MODE_FULLMAP) {
draw_lib.show_gfx();
}
show_objects();
show_npcs();
}
void classMap::get_map_area_surface(graphicsLib_gSurface& mapSurface)
{
graphLib.initSurface(st_size(RES_W, RES_H), &mapSurface);
if (!mapSurface.get_surface()) {
graphLib.show_debug_msg("EXIT #21.MALLOC");
exception_manager::throw_general_exception(std::string("classMap::get_map_area_surface"), "Could not init map surface");
}
graphLib.clear_surface_area(0, 0, RES_W, RES_H, GameMediator::get_instance()->map_data[number].background_color.r, GameMediator::get_instance()->map_data[number].background_color.g, GameMediator::get_instance()->map_data[number].background_color.b, mapSurface);
draw_dynamic_backgrounds_into_surface(mapSurface);
// redraw screen, if needed
if (_show_map_pos_x == -1 || abs(_show_map_pos_x - scroll.x) > TILESIZE) {
draw_map_tiles();
// use memory screen
}
int diff_scroll_x = scroll.x - _show_map_pos_x;
graphLib.copyArea(st_rectangle(diff_scroll_x+TILESIZE, 0, RES_W, RES_H), st_position(0, 0), &map_screen, &mapSurface);
// draw animated tiles
draw_animated_tiles(mapSurface);
}
void classMap::draw_map_tiles()
{
_show_map_pos_x = scroll.x;
int tile_x_ini = scroll.x/TILESIZE-1;
if (tile_x_ini < 0) {
tile_x_ini = 0;
}
graphLib.clear_surface(map_screen);
// draw the tiles of the screen region
struct st_position pos_origin;
struct st_position pos_destiny;
int n = -1;
for (int i=tile_x_ini; i<tile_x_ini+(RES_W/TILESIZE)+3; i++) {
int diff = scroll.x - (tile_x_ini+1)*TILESIZE;
pos_destiny.x = n*TILESIZE - diff + TILESIZE;
for (int j=0; j<MAP_H; j++) {
// don't draw easy-mode blocks if game difficulty not set to easy
if (GameMediator::get_instance()->map_data[number].tiles[i][j].locked == TERRAIN_EASYMODEBLOCK && game_save.difficulty == DIFFICULTY_EASY) {
pos_destiny.y = j*TILESIZE;
graphLib.place_easymode_block_tile(pos_destiny, map_screen);
} else if (GameMediator::get_instance()->map_data[number].tiles[i][j].locked == TERRAIN_HARDMODEBLOCK && game_save.difficulty == DIFFICULTY_HARD) {
pos_destiny.y = j*TILESIZE;
graphLib.place_hardmode_block_tile(pos_destiny, map_screen);
} else {
pos_origin.x = GameMediator::get_instance()->map_data[number].tiles[i][j].tile1.x;
pos_origin.y = GameMediator::get_instance()->map_data[number].tiles[i][j].tile1.y;
if (pos_origin.x >= 0 && pos_origin.y >= 0) {
pos_destiny.y = j*TILESIZE;
graphLib.placeTile(pos_origin, pos_destiny, &map_screen);
}
}
}
n++;
}
}
void classMap::draw_animated_tiles(graphicsLib_gSurface &surface)
{
//scroll.x - dest.x
for (int i=0; i<anim_tile_list.size(); i++) {
int pos_x = anim_tile_list.at(i).dest_x-scroll.x;
if (pos_x >= -TILESIZE && pos_x <= RES_W+1) {
st_position dest_pos(pos_x, anim_tile_list.at(i).dest_y);
graphLib.place_anim_tile(anim_tile_list.at(i).anim_tile_id, dest_pos, &surface);
}
}
graphLib.update_anim_tiles_timers();
}
void classMap::init_animated_tiles()
{
// draw the tiles of the screen region
struct st_position pos_origin;
struct st_position pos_destiny;
for (int i=0; i<MAP_W; i++) {
pos_destiny.x = i*TILESIZE;
for (int j=0; j<MAP_H; j++) {
pos_origin.x = GameMediator::get_instance()->map_data[number].tiles[i][j].tile1.x;
pos_origin.y = GameMediator::get_instance()->map_data[number].tiles[i][j].tile1.y;
if (pos_origin.x < -1 && pos_origin.y == 0) {
int anim_tile_id = (pos_origin.x * -1) - 2;
pos_destiny.y = j*TILESIZE;
anim_tile_list.push_back(anim_tile_desc(anim_tile_id, pos_destiny));
}
}
}
}
// ********************************************************************************************** //
// show the third level of tiles //
// ********************************************************************************************** //
void classMap::showAbove(int scroll_y, int temp_scroll_x, bool show_fg)
{
int scroll_x = scroll.x;
if (temp_scroll_x != -99999) {
scroll_x = temp_scroll_x;
}
// only show pieces that in current screen position
short start_point = scroll_x/TILESIZE;
if (start_point > 0) { start_point--; }
short end_point = (scroll_x+RES_W)/TILESIZE;
if (end_point < MAP_W-1) { end_point++; }
// draw 3rd tile level
std::vector<st_level3_tile>::iterator tile3_it;
for (tile3_it = _level3_tiles.begin(); tile3_it != _level3_tiles.end(); tile3_it++) {
if (_3rd_level_ignore_area.x != -1 && _3rd_level_ignore_area.w > 0 && ((*tile3_it).map_position.x >= _3rd_level_ignore_area.x && (*tile3_it).map_position.x < _3rd_level_ignore_area.x+_3rd_level_ignore_area.w && (*tile3_it).map_position.y >= _3rd_level_ignore_area.y && (*tile3_it).map_position.y < _3rd_level_ignore_area.y+_3rd_level_ignore_area.h)) {
continue;
}
int pos_x = (*tile3_it).tileset_pos.x;
int pos_y = (*tile3_it).tileset_pos.y;
// only show tile if it is on the screen range
graphLib.place_3rd_level_tile(pos_x, pos_y, ((*tile3_it).map_position.x*TILESIZE)-scroll_x, ((*tile3_it).map_position.y*TILESIZE)+scroll_y);
}
if (_water_bubble.pos.x != -1) {
draw_lib.show_bubble(_water_bubble.pos.x+_water_bubble.x_adjust, _water_bubble.pos.y);
int water_lock = getMapPointLock(st_position((_water_bubble.pos.x+2+scroll_x)/TILESIZE, _water_bubble.pos.y/TILESIZE));
_water_bubble.pos.y -= 2;
if (_water_bubble.x_adjust_direction == ANIM_DIRECTION_LEFT) {
_water_bubble.x_adjust -= 0.5;
if (_water_bubble.x_adjust < -4) {
_water_bubble.x_adjust_direction = ANIM_DIRECTION_RIGHT;
}
} else {
_water_bubble.x_adjust += 0.5;
if (_water_bubble.x_adjust >= 0) {
_water_bubble.x_adjust_direction = ANIM_DIRECTION_LEFT;
}
}
if (water_lock != TERRAIN_WATER || _water_bubble.timer < timer.getTimer()) {
_water_bubble.pos.x = -1;
_water_bubble.pos.y = -1;
}
}
// animations
/// @TODO: remove "finished" animations
std::vector<animation>::iterator animation_it;
for (animation_it = animation_list.begin(); animation_it != animation_list.end(); animation_it++) {
if ((*animation_it).finished() == true) {
animation_list.erase(animation_it);
break;
} else {
(*animation_it).execute(); // TODO: must pass scroll map to npcs somwhow...
}
}
if (show_fg) {
draw_foreground_layer(scroll_x, scroll_y);
}
}
bool classMap::is_point_solid(st_position pos) const
{
short int lock_p = getMapPointLock(pos);
if (lock_p == TERRAIN_UNBLOCKED || lock_p != TERRAIN_WATER || lock_p == TERRAIN_CHECKPOINT || lock_p == TERRAIN_SCROLL_LOCK || (lock_p == TERRAIN_EASYMODEBLOCK && game_save.difficulty != 0)) {
return false;
}
return true;
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
int classMap::getMapPointLock(st_position pos) const
{
if (pos.x < 0 || pos.y < 0 || pos.y >= MAP_H || pos.x >= MAP_W) {
return TERRAIN_UNBLOCKED;
}
return GameMediator::get_instance()->map_data[number].tiles[pos.x][pos.y].locked;
}
st_position_int8 classMap::get_map_point_tile1(st_position pos)
{
if (pos.x < 0 || pos.y < 0 || pos.y > RES_H/TILESIZE || pos.x > MAP_W) {
return st_position_int8(-1, -1);
}
return GameMediator::get_instance()->map_data[number].tiles[pos.x][pos.y].tile1;
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
void classMap::changeScrolling(st_float_position pos, bool check_lock)
{
float bg1_speed = (float)GameMediator::get_instance()->map_data[number].backgrounds[0].speed/10 * SharedData::get_instance()->get_movement_multiplier();
float foreground_layer_speed = (float)GameMediator::get_instance()->map_data[number].backgrounds[1].speed/10 * SharedData::get_instance()->get_movement_multiplier();
// moving player to right, screen to left
if (pos.x > 0 && ((scroll.x/TILESIZE+RES_W/TILESIZE)-1 < MAP_W-1)) {
int x_change = pos.x;
if (pos.x >= TILESIZE) { // if change is too big, do not update (TODO: must check all wall until lock)
x_change = 1;
}
int tile_x = (scroll.x+RES_W-TILESIZE+2)/TILESIZE;
if (check_lock == false || wall_scroll_lock[tile_x] == false) {
scroll.x += x_change;
if (GameMediator::get_instance()->map_data[number].backgrounds[0].auto_scroll == BG_SCROLL_MODE_NONE) {
bg_scroll.x -= ((float)x_change*bg1_speed);
}
fg_layer_scroll.x -= ((float)x_change*foreground_layer_speed);
adjust_dynamic_background_position();
adjust_foreground_position();
}
} else if (pos.x < 0) {
int x_change = pos.x;
if (pos.x < -TILESIZE) {
x_change = -1;
}
if (scroll.x/TILESIZE >= 0) { // if change is too big, do not update (TODO: must check all wall until lock)
int tile_x = (scroll.x+TILESIZE-2)/TILESIZE;
if (check_lock == false || wall_scroll_lock[tile_x] == false) {
scroll.x += x_change;
bg_scroll.x -= ((float)x_change*bg1_speed);
fg_layer_scroll.x -= ((float)x_change*foreground_layer_speed);
adjust_dynamic_background_position();
adjust_foreground_position();
}
}
}
scroll.y += pos.y;
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
void classMap::set_scrolling(st_float_position pos)
{
scrolled = pos;
scroll.x = pos.x;
scroll.y = pos.y;
}
void classMap::reset_scrolling()
{
scrolled = st_position(0, 0);
scroll.x = 0;
scroll.y = 0;
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
st_float_position classMap::getMapScrolling() const
{
return scroll;
}
st_float_position *classMap::get_map_scrolling_ref()
{
return &scroll;
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
void classMap::load_map_npcs()
{
// store friends that have already left into a list to avoid re-adding them
for (unsigned int i=0; i<_npc_list.size(); i++) {
if (GameMediator::get_instance()->get_enemy(_npc_list.at(i).get_number())->behavior == NPC_BEHAVIOR_PLAYER_FRIEND && _npc_list.at(i).get_teleport_state() > 0) {
finished_friend_list.insert(std::pair<int, std::string>(_npc_list.at(i).get_number(), _npc_list.at(i).get_name()));
}
}
// remove all elements currently in the list
if (_npc_list.size() > 0) {
_npc_list.back().clean_character_graphics_list();
}
while (!_npc_list.empty()) {
_npc_list.pop_back();
}
for (unsigned int i=0; i<GameMediator::get_instance()->map_npc_data.size(); i++) {
if (GameMediator::get_instance()->map_npc_data[i].difficulty_mode == DIFFICULTY_MODE_GREATER && GameMediator::get_instance()->map_npc_data[i].difficulty_level > game_save.difficulty) {
continue;
} else if (GameMediator::get_instance()->map_npc_data[i].difficulty_mode == DIFFICULTY_MODE_EQUAL && GameMediator::get_instance()->map_npc_data[i].difficulty_level != game_save.difficulty) {
continue;
}
int npc_ic = GameMediator::get_instance()->map_npc_data[i].id_npc;
if (finished_friend_list.find(npc_ic) != finished_friend_list.end()) {
continue;
}
if (npc_ic != -1 && GameMediator::get_instance()->map_npc_data[i].stage_id == stage_number && GameMediator::get_instance()->map_npc_data[i].map_id == number) {
classnpc new_npc = classnpc(stage_number, number, npc_ic, i);
if (stage_data.boss.id_npc == npc_ic) {
new_npc.set_stage_boss(true);
} else if (GameMediator::get_instance()->get_enemy(npc_ic)->is_boss == true) {
new_npc.set_is_boss(true);
// adjust NPC position to ground, if needed
} else if (new_npc.is_able_to_fly() == false && new_npc.hit_ground() == false) {
new_npc.initialize_boss_position_to_ground();
}
new_npc.init_animation();
_npc_list.push_back(new_npc); // insert new npc at the list-end
}
}
}
void classMap::draw_dynamic_backgrounds()
{
// only draw solid background color, if map-heigth is less than RES_H
graphicsLib_gSurface* surface_bg = get_dynamic_bg();
if (surface_bg == NULL || surface_bg->width <= 0) {
graphLib.clear_surface_area(0, 0, RES_W, RES_H, GameMediator::get_instance()->map_data[number].background_color.r, GameMediator::get_instance()->map_data[number].background_color.g, GameMediator::get_instance()->map_data[number].background_color.b, graphLib.gameScreen);
draw_static_background();
return;
}
// if there is no background or it does not cover the whole screen, draw solid color
if (surface_bg->width <= 0 || surface_bg->height < RES_H || GameMediator::get_instance()->map_data[number].backgrounds[0].adjust_y != 0) {
graphLib.clear_surface_area(0, 0, RES_W, RES_H, GameMediator::get_instance()->map_data[number].background_color.r, GameMediator::get_instance()->map_data[number].background_color.g, GameMediator::get_instance()->map_data[number].background_color.b, graphLib.gameScreen);
}
float bg1_speed = (float)GameMediator::get_instance()->map_data[number].backgrounds[0].speed/10 * SharedData::get_instance()->get_movement_multiplier();
int bg1_scroll_mode = GameMediator::get_instance()->map_data[number].backgrounds[0].auto_scroll;
// dynamic background won't work in low-end graphics more
if (SharedData::get_instance()->game_config.graphics_performance_mode != PERFORMANCE_MODE_LOW) {
if (bg1_scroll_mode == BG_SCROLL_MODE_LEFT) {
bg_scroll.x -= bg1_speed;
adjust_dynamic_background_position();
} else if (bg1_scroll_mode == BG_SCROLL_MODE_RIGHT) {
bg_scroll.x += bg1_speed;
adjust_dynamic_background_position();
} else if (bg1_scroll_mode == BG_SCROLL_MODE_UP) {
bg_scroll.y -= bg1_speed;
adjust_dynamic_background_position();
} else if (bg1_scroll_mode == BG_SCROLL_MODE_DOWN) {
bg_scroll.y += bg1_speed;
adjust_dynamic_background_position();
}
}
float x1 = bg_scroll.x;
if (x1 > 0.0) { // moving to right
x1 = (RES_W - x1) * -1;
}
float y1 = bg_scroll.y + GameMediator::get_instance()->map_data[number].backgrounds[0].adjust_y;
if (surface_bg->width > 0) {
if (bg1_scroll_mode == BG_SCROLL_MODE_ANIM_BG) {
// draw leftmost part
graphLib.copyAreaWithAdjustAndAnimFrame(st_position(x1, y1), surface_bg, &graphLib.gameScreen, bg_anim_pos);
// draw rightmost part, if needed
if (abs(bg_scroll.x) > RES_W) {
float bg_pos_x = RES_W - (abs(x1)-RES_W);
graphLib.copyAreaWithAdjustAndAnimFrame(st_position(bg_pos_x, y1), surface_bg, &graphLib.gameScreen, bg_anim_pos);
} else if (surface_bg->width - abs(bg_scroll.x) < RES_W) {
float bg_pos_x = surface_bg->width - (int)abs(bg_scroll.x);
graphLib.copyAreaWithAdjustAndAnimFrame(st_position(bg_pos_x, y1), surface_bg, &graphLib.gameScreen, bg_anim_pos);
}
if (timer.getTimer() > bg_anim_timer) {
if (bg_anim_pos == 0) {
bg_anim_pos = 1;
} else {
bg_anim_pos = 0;
}
bg_anim_timer = timer.getTimer() + 200;
}
} else {
// draw leftmost part
graphLib.copyAreaWithAdjust(st_position(x1, y1), surface_bg, &graphLib.gameScreen);
// draw rightmost part, if needed
if (abs(bg_scroll.x) > RES_W) {
float bg_pos_x = RES_W - (abs(x1)-RES_W);
graphLib.copyAreaWithAdjust(st_position(bg_pos_x, y1), surface_bg, &graphLib.gameScreen);
} else if (surface_bg->width - abs(bg_scroll.x) < RES_W) {
float bg_pos_x = surface_bg->width - (int)abs(bg_scroll.x);
graphLib.copyAreaWithAdjust(st_position(bg_pos_x, y1), surface_bg, &graphLib.gameScreen);
}
}
}
draw_static_background();
}
void classMap::draw_static_background()
{
if (static_bg.is_null() == false && static_bg_pos.x >= scroll.x-1 && static_bg_pos.x < scroll.x+RES_W) {
st_position adjusted_static_bg_pos((static_bg_pos.x-scroll.x), static_bg_pos.y);
graphLib.copyAreaWithAdjust(adjusted_static_bg_pos, &static_bg, &graphLib.gameScreen);
return;
}
}
void classMap::draw_foreground_layer(int scroll_x, int scroll_y)
{
if (strlen(GameMediator::get_instance()->map_data[number].backgrounds[1].filename) > 0) {
float foreground_speed = (float)GameMediator::get_instance()->map_data[number].backgrounds[1].speed/10 * SharedData::get_instance()->get_movement_multiplier();
int scroll_mode = GameMediator::get_instance()->map_data[number].backgrounds[1].auto_scroll;
// dynamic background won't work in low-end graphics more
if (SharedData::get_instance()->game_config.graphics_performance_mode != PERFORMANCE_MODE_LOW) {
if (scroll_mode == BG_SCROLL_MODE_LEFT) {
fg_layer_scroll.x -= ((float)1*foreground_speed);
adjust_foreground_position();
} else if (scroll_mode == BG_SCROLL_MODE_RIGHT) {
fg_layer_scroll.x += ((float)1*foreground_speed);
adjust_foreground_position();
} else if (scroll_mode == BG_SCROLL_MODE_UP) {
fg_layer_scroll.y -= ((float)1*foreground_speed);
adjust_foreground_position();
} else if (scroll_mode == BG_SCROLL_MODE_DOWN) {
fg_layer_scroll.y += ((float)1*foreground_speed);
adjust_foreground_position();
}
}
int x1 = fg_layer_scroll.x;
if (x1 > 0) { // moving to right
x1 = (RES_W - x1) * -1;
}
int y1 = fg_layer_scroll.y + GameMediator::get_instance()->map_data[number].backgrounds[1].adjust_y;
if (get_dynamic_foreground() != NULL && get_dynamic_foreground()->width > 0) {
// draw leftmost part
graphLib.copyAreaWithAdjust(st_position(x1, y1), get_dynamic_foreground(), &graphLib.gameScreen);
// draw rightmost part, if needed
if (abs(fg_layer_scroll.x) > RES_W) {
int bg_pos_x = RES_W - (abs(x1)-RES_W);
graphLib.copyAreaWithAdjust(st_position(bg_pos_x, y1), get_dynamic_foreground(), &graphLib.gameScreen);
} else if (get_dynamic_foreground()->width - abs(fg_layer_scroll.x) < RES_W) {
int foreground_pos_x = get_dynamic_foreground()->width - (int)abs(fg_layer_scroll.x);
// test if there isn't a overlap, so we need to add +1
graphLib.copyAreaWithAdjust(st_position(foreground_pos_x, y1), get_dynamic_foreground(), &graphLib.gameScreen);
}
}
}
}
void classMap::adjust_dynamic_background_position()
{
if (get_dynamic_bg() == NULL) {
return;
}
//int bg_limit = get_dynamic_bg()->width-RES_W;
int bg_limit = get_dynamic_bg()->width;
// esq -> direita: #1 bg_limt[640], scroll.x[-640.799]
if (bg_scroll.x < -bg_limit) {
bg_scroll.x = 0;
} else if (bg_scroll.x > bg_limit) {
bg_scroll.x = 0;
} else if (bg_scroll.x > 0) {
bg_scroll.x = -(get_dynamic_bg()->width); // erro aqui
}
if (bg_scroll.y < -RES_H) {
bg_scroll.y = 0;
} else if (bg_scroll.y > RES_H) {
bg_scroll.y = 0;
}
}
void classMap::adjust_foreground_position()
{
// no need to adjust if no foreground
if (get_dynamic_foreground() == NULL || get_dynamic_foreground()->width == 0) {
return;
}
//int bg_limit = get_dynamic_foreground()->width-RES_W;
int foreground_limit = get_dynamic_foreground()->width;
// esq -> direita: #1 bg_limt[640], scroll.x[-640.799]
if (fg_layer_scroll.x < -foreground_limit) {
fg_layer_scroll.x = 0;
} else if (fg_layer_scroll.x > foreground_limit) {
fg_layer_scroll.x = 0;
} else if (fg_layer_scroll.x > 0) {
fg_layer_scroll.x = -(get_dynamic_foreground()->width); // erro aqui
}
if (fg_layer_scroll.y < -RES_H) {
fg_layer_scroll.y = 0;
} else if (fg_layer_scroll.y > RES_H) {
fg_layer_scroll.y = 0;
}
}
st_float_position classMap::get_foreground_postion()
{
return fg_layer_scroll;
}
void classMap::set_foreground_postion(st_float_position pos)
{
fg_layer_scroll = pos;
}
bool classMap::must_show_static_bg()
{
if (static_bg.is_null() == false && static_bg_pos.x >= scroll.x-1 && static_bg_pos.x < scroll.x+RES_W) {
return true;
}
return false;
}
void classMap::draw_dynamic_backgrounds_into_surface(graphicsLib_gSurface &surface)
{
graphLib.clear_surface_area(0, 0, surface.width, surface.height, GameMediator::get_instance()->map_data[number].background_color.r, GameMediator::get_instance()->map_data[number].background_color.g, GameMediator::get_instance()->map_data[number].background_color.b, surface);
if (get_dynamic_bg() == NULL) {
return;
}
int x1 = bg_scroll.x;
if (x1 > 0) { // moving to right
x1 = (RES_W - x1) * -1;
}
int y1 = bg_scroll.y + GameMediator::get_instance()->map_data[number].backgrounds[0].adjust_y;
if (get_dynamic_bg()->width > 0) {
// draw leftmost part
graphLib.copyAreaWithAdjust(st_position(x1, y1), get_dynamic_bg(), &surface);
// draw rightmost part, if needed
if (abs(bg_scroll.x) > RES_W) {
int bg_pos_x = RES_W - (abs(x1)-RES_W);
graphLib.copyAreaWithAdjust(st_position(bg_pos_x, y1), get_dynamic_bg(), &surface);
} else if (get_dynamic_bg()->width - abs(bg_scroll.x) < RES_W) {
int bg_pos_x = get_dynamic_bg()->width - abs(bg_scroll.x);
graphLib.copyAreaWithAdjust(st_position(bg_pos_x, y1), get_dynamic_bg(), &surface);
}
}
}
void classMap::add_object(object& obj)
{
object_list.push_back(obj);
}
st_position classMap::get_first_lock_in_direction(st_position pos, st_size max_dist, int direction)
{
st_position res;
st_position x_limit_pos;
switch (direction) {
case ANIM_DIRECTION_LEFT:
{
res.y = pos.y;
res.x = pos.x - max_dist.width;
int scroll_lock_left = get_first_lock_on_left(pos.x/TILESIZE);
if (scroll_lock_left < pos.x-max_dist.width) {
for (int pos_i=pos.x; pos_i>(pos.x-max_dist.width); pos_i--) {
int map_lock = gameControl.get_current_map_obj()->getMapPointLock(st_position(pos_i/TILESIZE, pos.y/TILESIZE));
if (map_lock != TERRAIN_UNBLOCKED && map_lock != TERRAIN_WATER) {
res.x = pos_i+1;
break;
}
}
} else {
res.x = scroll_lock_left+TILESIZE;
}
break;
}
case ANIM_DIRECTION_RIGHT:
{
res.y = pos.y;
res.x = pos.x + max_dist.width;
int scroll_lock_right = get_first_lock_on_right(pos.x/TILESIZE);
if (scroll_lock_right > pos.x+max_dist.width) {
for (int pos_i=pos.x; pos_i<(pos.x+max_dist.width); pos_i++) {
int map_lock = gameControl.get_current_map_obj()->getMapPointLock(st_position(pos_i/TILESIZE, pos.y/TILESIZE));
if (map_lock != TERRAIN_UNBLOCKED && map_lock != TERRAIN_WATER) {
res.x = pos_i-1;
break;
}
}
} else {
res.x = scroll_lock_right-TILESIZE;
}
break;
}
case ANIM_DIRECTION_UP:
{
res.y = pos.y - max_dist.height;
res.x = pos.x;
for (int pos_i=pos.y; pos_i>(pos.y-max_dist.height); pos_i--) {
int map_lock = gameControl.get_current_map_obj()->getMapPointLock(st_position(pos.x/TILESIZE, pos_i/TILESIZE));
if (map_lock != TERRAIN_UNBLOCKED && map_lock != TERRAIN_WATER) {
res.y = pos_i+1;
break;
}
}
break;
}
case ANIM_DIRECTION_DOWN:
res.y = pos.y + max_dist.height;
res.x = pos.x;
for (int pos_i=pos.y; pos_i<(pos.y+max_dist.height); pos_i++) {
int map_lock = gameControl.get_current_map_obj()->getMapPointLock(st_position(pos.x/TILESIZE, pos_i/TILESIZE));
if (map_lock != TERRAIN_UNBLOCKED && map_lock != TERRAIN_WATER) {
res.y = pos_i-1;
break;
}
}
break;
case ANIM_DIRECTION_UP_LEFT:
x_limit_pos = get_first_lock_in_direction(pos, max_dist, ANIM_DIRECTION_LEFT);
res = get_first_lock_in_direction(x_limit_pos, max_dist, ANIM_DIRECTION_UP);
break;
case ANIM_DIRECTION_UP_RIGHT:
x_limit_pos = get_first_lock_in_direction(pos, max_dist, ANIM_DIRECTION_RIGHT);
res = get_first_lock_in_direction(x_limit_pos, max_dist, ANIM_DIRECTION_UP);
break;
case ANIM_DIRECTION_DOWN_LEFT:
x_limit_pos = get_first_lock_in_direction(pos, max_dist, ANIM_DIRECTION_LEFT);
res = get_first_lock_in_direction(x_limit_pos, max_dist, ANIM_DIRECTION_DOWN);
break;
case ANIM_DIRECTION_DOWN_RIGHT:
x_limit_pos = get_first_lock_in_direction(pos, max_dist, ANIM_DIRECTION_RIGHT);
res = get_first_lock_in_direction(x_limit_pos, max_dist, ANIM_DIRECTION_DOWN);
break;
default:
break;
}
return res;
}
int classMap::get_first_lock_on_left(int x_tile_pos) const
{
for (int i=x_tile_pos; i>= 0; i--) {
if (wall_scroll_lock[i] == true) {
return i*TILESIZE;
}
}
return -1;
}
int classMap::get_first_lock_on_right(int x_tile_pos) const
{
int limit = (scroll.x+RES_W)/TILESIZE;
x_tile_pos += 1;
for (int i=x_tile_pos; i<=limit; i++) {
if (wall_scroll_lock[i] == true) {
return i*TILESIZE;
}
}
return -1;
}
// gets the first tile locked that have at least 3 tiles unlocked above it
int classMap::get_first_lock_on_bottom(int x_pos, int y_pos)
{
return get_first_lock_on_bottom(x_pos, y_pos, TILESIZE, TILESIZE*3);
}
int classMap::get_first_lock_on_bottom(int x_pos, int y_pos, int w, int h)
{
int tilex = x_pos/TILESIZE;
int above_tiles_to_test = h/TILESIZE;
if (above_tiles_to_test < 2) { // at least two tiles above even for small npcs
above_tiles_to_test = 2;
}
int right_tiles_to_test = w/TILESIZE;
if (right_tiles_to_test < 1) {
right_tiles_to_test = 1;
}
int initial_y = MAP_H-1;
if (y_pos >= 0) {
initial_y = y_pos/TILESIZE;
}
for (int i=initial_y; i>=above_tiles_to_test+1; i--) { // ignore here first tiles, as we need to test them next
int map_lock = getMapPointLock(st_position(tilex, i));
bool found_bad_point = false;
if (map_lock != TERRAIN_UNBLOCKED && map_lock != TERRAIN_WATER && map_lock != TERRAIN_EASYMODEBLOCK && map_lock != TERRAIN_HARDMODEBLOCK) {
// found a stop point, now check above tiles
for (int j=i-1; j>=i-above_tiles_to_test; j--) {
for (int k=0; k<right_tiles_to_test; k++) {
int map_lock2 = getMapPointLock(st_position(tilex+k, j));
if (map_lock2 != TERRAIN_UNBLOCKED && map_lock2 != TERRAIN_WATER) { // found a stop point, now check above ones
found_bad_point = true;
break;
}
}
if (found_bad_point) {
break;
}
}
if (found_bad_point == false) {
return i-1;
}
}
}
return 0;
}
void classMap::drop_item(classnpc* npc_ref)
{
if (npc_ref == NULL) {
return;
}
st_float_position position = st_float_position(npc_ref->getPosition().x + npc_ref->get_size().width/2, npc_ref->getPosition().y + npc_ref->get_size().height/2);
// dying out of screen should not drop item
if (position.y > RES_H) {
return;
}
srand(static_cast<unsigned int>(timer.getTimer()));
int rand_n = static_cast<int> (100.0 * (rand() / (RAND_MAX + 1.0)));
DROP_ITEMS_LIST obj_type;
// sub-bosses always will drop energy big
if (npc_ref->is_subboss()) {
obj_type = DROP_ITEM_ENERGY_BIG;
} else {
// Big Energy (3%), Big Weapon (2%), Small Energy (15)%, Small Weapon (15%), Score Pearl (53%)
int drop_ratio[5];
if (game_save.difficulty == DIFFICULTY_EASY) {
// 5%, 10%, 10%, 20%, 20%, 20% //
drop_ratio[0] = 95;
drop_ratio[1] = 85;
drop_ratio[2] = 75;
drop_ratio[3] = 55;
drop_ratio[4] = 35;
} else if (game_save.difficulty == DIFFICULTY_HARD) {
// 1%, 1%, 1%, 10%, 10%, 10% //
drop_ratio[0] = 99;
drop_ratio[1] = 98;
drop_ratio[2] = 97;
drop_ratio[3] = 87;
drop_ratio[4] = 77;
} else {
// .byt 99, 97, 95, 80, 65, 12 (http://tasvideos.org/RandomGenerators.html)
drop_ratio[0] = 97;
drop_ratio[1] = 95;
drop_ratio[2] = 80;
drop_ratio[3] = 65;