-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
2108 lines (1758 loc) · 75.3 KB
/
main.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 "splashkit.h"
#include <cmath>
#include <vector>
// increase the value of the input x by an ease out quint, until it reaches 1
double ease_out_quint(double x)
{
return 1 - std::pow(1 - x, 5);
}
// use to increase values by an ease function,
struct ease_data
{
private:
double initial_value; // initial value of the variable (only used for ease_value without variable pointer)
double change_by; // the amount the value needs to be change from initial to end
double increase; // the amount the value has been increased by (x value of the ease function)
double calculate_ease(double time, double delta_time)
{
int max_increase = 1; // increase should only go from 0 to 1
double increment_rate = max_increase / (time / delta_time);
increase += increment_rate; // incrementing the increase by the rate
if (increase >= max_increase)
{
increase = max_increase; // making sure increase does not go over 1
}
return ease_func(increase); // returning the ease function of the increase
}
public:
double value; // the value that is being eased (only used for ease_value without variable pointer) default is 0
// should be changed if the value to start increasing is not 0
// acts as a pointer to the variable that is being eased (syncs the value with the variable)
double time_to_ease; // the time it takes for the value to ease to the end value (used when change_by is positive)
double time_to_release; // the time it takes for the value to release to the initial value (used when change_by is negative)
double (*ease_func)(double); // the ease function to be used
// Constructor
ease_data()
{
increase = 0;
change_by = 0;
initial_value = 0;
value = 0; // default value is 0
time_to_ease = 800; // default time to ease is 800 ms
time_to_release = 800; // default time to release is 800 ms
}
// eases the value of the variable to the to_value directly (using pointers)
void ease_value(double *variable, double to_value, double delta_time)
{
double time;
if (this->change_by != to_value - *variable)
{
increase = 0;
}
if (increase == 0)
{
initial_value = *variable;
change_by = to_value - *variable;
}
if (change_by < 0)
{
time = time_to_release;
}
else if (change_by > 0)
{
time = time_to_ease;
}
double eased = calculate_ease(time, delta_time);
*variable = initial_value + eased * change_by;
}
// returns the eased value of the initial value to the to_value
double ease_value(double to_value, double delta_time)
{
double time;
if (this->change_by != to_value - value)
{
increase = 0;
}
if (increase == 0)
{
this->initial_value = value;
change_by = to_value - value;
}
if (change_by < 0)
{
time = time_to_release;
}
else if (change_by > 0)
{
time = time_to_ease;
}
double eased = calculate_ease(time, delta_time);
value = initial_value + eased * change_by;
return value;
}
};
// data type for coorindates, can be used for both pixel and tile coordinates
// tile coordinates are the coordinates of the tiles in the room (room_data)
struct coordinate
{
double x, y;
// to convert tile coorindates to pixel coordinates
coordinate tile_to_pixel(double tile_size)
{
return {x * tile_size, y * tile_size};
}
// to convert pixel coordinates to tile coordinates of the room
coordinate pixel_to_tile(double tile_size)
{
double pixel_x = ceil(x / tile_size) - 1;
double pixel_y = ceil(y / tile_size) - 1;
return {pixel_x, pixel_y};
}
};
// generate random coordinates
coordinate random_coordinate(const coordinate &max_coords)
{
return {(double)rnd(max_coords.x), (double)rnd(max_coords.y)};
}
coordinate random_coordinate(const coordinate &min_coords, const coordinate &max_coords)
{
return {(double)rnd(min_coords.x, max_coords.x), (double)rnd(min_coords.y, max_coords.y)};
}
// make a game size struct
class game_size_data
{
private:
int screen_height; // the height of the screen
int screen_width; // the width of the screen
int room_width; // the number of tiles in the room on the x axis
int room_height; // the number of tiles in the room on the y axis
double zoom_level; // the zoom level of the game or screen
public:
// Constructor
game_size_data(int screen_width, int screen_height, int room_width, int room_height)
{
this->screen_height = screen_height;
this->screen_width = screen_width;
this->room_width = room_width;
this->room_height = room_height;
this->zoom_level = 1;
}
game_size_data() : game_size_data(1080, 1920, 40, 30) {}
// getters and setters
int get_screen_height() const
{
return screen_height;
}
// screen width is calculated using the screen height and the room width and height
int get_screen_width() const
{
return screen_width;
}
int get_room_width() const
{
return room_width;
}
int get_room_height() const
{
return room_height;
}
double get_zoom_level() const
{
return zoom_level;
}
// set the zoom level of the game
void set_zoom_level(double zoom_level)
{
this->zoom_level = zoom_level;
}
void set_zoom_level(double zoom_level, ease_data &ease, double delta_time)
{
ease.ease_value(&this->zoom_level, zoom_level, delta_time);
}
// get the camera position that would keep the parameter coorindate in the center of the screen
coordinate get_camera_position(coordinate center_position) const
{
return {center_position.x - (get_screen_width() / 2), center_position.y - (screen_height / 2)};
}
};
// class to hold the timing data of the game (mostly for the delta time)
class game_timing_data
{
private:
double delta_time; // time between the last frame and the current frame
double time_rate; // how many seconds the game should load in one second
double time_difference; // delta time wihtout the time rate
double last_update_time; // the time of the last frame
int frame_rate; // the frame rate of the game
double last_frame_update_time; // the time of the last frame update
// get the time it takes for each frame to be displayed to fit frame rate
double get_frame_delay() const
{
return 1000 / frame_rate;
}
public:
// Constructor
game_timing_data()
{
delta_time = 0;
time_rate = 1;
time_difference = 0;
last_update_time = 0;
frame_rate = 60;
last_frame_update_time = 0;
}
game_timing_data(int frame_rate)
{
delta_time = 0;
time_rate = 1;
time_difference = 0;
last_update_time = 0;
last_frame_update_time = 0;
this->frame_rate = frame_rate;
}
// must be ran inside a game loop in order to update the delta time
void update_timing()
{
time_difference = (double)timer_ticks("Main timer") - (double)last_update_time; // getting delta_time
last_update_time = (double)timer_ticks("Main timer"); // setting the last update time
last_frame_update_time += time_difference; // adding the delta time to the last frame update time
delta_time = time_difference * time_rate; // changing the delta time according to the time rate
}
// setters and getters
double get_delta_time() const
{
return delta_time;
}
double get_time_difference() const
{
return time_difference;
}
// set time rate, changes the time rate of the delta time
void set_time_rate(double rate)
{
time_rate = rate;
}
void set_time_rate(double time_rate, ease_data &ease, double delta_time)
{
ease.ease_value(&this->time_rate, time_rate, delta_time);
}
// set frame rate, changes the frame rate of the game, tells when the game should update the frame (calling screen_refresh)
bool update_frame()
{
if (last_frame_update_time >= get_frame_delay())
{
last_frame_update_time = 0;
return true;
}
return false;
// true if the game is ready to update the frame, false if not
}
};
// floor tile struct
struct tile_data
{
color tile_color;
bool passable;
int size;
rectangle tile;
};
// flooring struct, using tiles as arrays
class room_data
{
private:
vector<vector<tile_data>> floor_array; // the floor of the room
// contains the start and end tile coordinates of the walls
// elements are coordinate vectors of two elements, the first element is the start tile, the second element is the end tiles
vector<vector<coordinate>> walls_coords_vector;
vector<rectangle> walls_vector; // rectangles of the walls, can work as hitboxes of walls
color color_pattern[3]; // 0 and 1 is the floor checkers color pattern, 2 is the walls
int size_y; // room height
int size_x; // room width
double tile_size; // size of each tile
coordinate spawn_coords;
double zoom_level; // zoom level of the room
double zoomed_tile_size; // size of each tile after zooming
// a function to construct the room, used in the constructor
void construct_room(int room_width, int room_height, int screen_width, int screen_height, const color &floor_color_1, const color &floor_color_2, const color &wall_color, const coordinate &spawn_tile)
{
color_pattern[0] = floor_color_1;
color_pattern[1] = floor_color_2;
color_pattern[2] = wall_color;
this->size_x = room_width;
this->size_y = room_height;
this->zoom_level = 1;
double size1 = (double)screen_width / (double)room_width;
double size2 = (double)screen_height / (double)room_height;
if (size1 >= size2)
this->tile_size = size1;
else
this->tile_size = size2;
this->spawn_coords = spawn_tile;
// update the zoomed tile size (using tile_size and zoom_level)
update_zoomed_tile_size();
// setting spawn coordinates, it is passed as a tile coordinate
this->spawn_coords = this->spawn_coords.tile_to_pixel(zoomed_tile_size);
// setting the wall (surronding the room)
set_wall({0, 0}, {(double)(size_x - 1), 0}); // top wall
set_wall({0, 1}, {0, (double)(size_y - 2)}); // left wall
set_wall({(double)(size_x - 1), 1}, {(double)(size_x - 1), (double)(size_y - 2)}); // right wall
set_wall({0, (double)(size_y - 1)}, {(double)(size_x - 1), (double)(size_y - 1)}); // bottom wall
// initializing the floor 2d vector
floor_array = vector<vector<tile_data>>(size_y, vector<tile_data>(size_x));
build_room();
}
// function to update the zoomed tile size (depending if zoom level is changed)
void update_zoomed_tile_size()
{
zoomed_tile_size = tile_size * zoom_level;
}
// build the floor of the room (setting floor_array with tiles)
void build_floor()
{
for (int y = 0; y < size_y; y++)
{
for (int x = 0; x < size_x; x++)
{
floor_array[y][x].passable = true;
floor_array[y][x].size = zoomed_tile_size;
coordinate tile_coords = {(double)x, (double)y};
coordinate coords = tile_coords.tile_to_pixel(zoomed_tile_size);
// setting the tile's position and size (rectangle objects)
floor_array[y][x].tile = {coords.x, coords.y, zoomed_tile_size, zoomed_tile_size};
// making the floor as a checked pattern (setting colors for each tile)
color first_color = color_pattern[0];
color second_color = color_pattern[1];
if (y % 2 != 0)
{
first_color = color_pattern[1];
second_color = color_pattern[0];
}
if (x % 2 == 0)
floor_array[y][x].tile_color = first_color;
else
floor_array[y][x].tile_color = second_color;
}
}
}
// set the walls of the room, updating the floor_array with the walls
void build_wall()
{
// creating the walls vector (rectangles) from the walls coordinates vector
walls_vector.clear(); // clearing the walls vector to update it with the new walls (walls can change depending on zoom_level)
for (int i = 0; i < walls_coords_vector.size(); i++)
{
// walls_coords_vector contains vectors contains a 2-element array with the start and end tile coordinates of the walls
coordinate wall_coords_start = walls_coords_vector[i][0].tile_to_pixel(zoomed_tile_size);
coordinate wall_coords_end = walls_coords_vector[i][1].tile_to_pixel(zoomed_tile_size);
// adding the size of the tile to the end tile to get the bottom right corner of the wall
wall_coords_end.x += zoomed_tile_size;
wall_coords_end.y += zoomed_tile_size;
// calculate the width and height of the wall
double wall_width = wall_coords_end.x - wall_coords_start.x;
double wall_height = wall_coords_end.y - wall_coords_start.y;
// add the wall to the walls vector (a rectangle object)
walls_vector.push_back({wall_coords_start.x, wall_coords_start.y, wall_width, wall_height});
// the rectangle is from the top left corner of the start tile to the bottom right corner of the end tile
}
// using the walls_vector to update the floor_array with the walls
for (int y = 0; y < size_y; y++)
{
for (int x = 0; x < size_x; x++)
{
for (int i = 0; i < walls_vector.size(); i++)
{
// any tiles in the floor_array that intersects with the walls_vector will be set as a wall
if (rectangles_intersect(floor_array[y][x].tile, walls_vector[i]))
{
// Splashkit's rectangle_intersection counds touching as a collision, we ignore touching as collision
rectangle intersection_box = intersection(floor_array[y][x].tile, walls_vector[i]);
if (intersection_box.width > 0 && intersection_box.height > 0)
{
// tiles are walls if they are a wall color (color_pattern[2]) and are not passable
floor_array[y][x].tile_color = color_pattern[2];
floor_array[y][x].passable = false;
break;
}
}
}
}
}
}
public:
// Constructor
room_data(int floor_width, int floor_height, int screen_width, int screen_height)
{
// setting room color
color slate_grey = rgb_color(112, 128, 144);
color light_slate_grey = rgb_color(132, 144, 153);
color light_steel_blue = rgb_color(150, 170, 200);
construct_room(floor_width, floor_height, screen_width, screen_height, slate_grey, light_slate_grey, light_steel_blue, {(double)(floor_width - 1) / 2, (double)(floor_height - 1) - 2});
}
room_data(int floor_width, int floor_height, int screen_width, int screen_height, const coordinate &spawn_coords)
{
// setting room color
color slate_grey = rgb_color(112, 128, 144);
color light_slate_grey = rgb_color(132, 144, 153);
color light_steel_blue = rgb_color(150, 170, 200);
construct_room(floor_width, floor_height, screen_width, screen_height, slate_grey, light_slate_grey, light_steel_blue, spawn_coords);
}
room_data(int floor_width, int floor_height, int screen_width, int screen_height, const color &floor_color_1, const color &floor_color_2, const color &wall_color, const coordinate &spawn_coords)
{
construct_room(floor_width, floor_height, screen_width, screen_height, floor_color_1, floor_color_2, wall_color, spawn_coords);
}
// building the room, setting the floor and walls, used when new walls are created (must be called in game loop to watch for changes in zoom_level)
void build_room()
{
// changing the zoomed_tile_size with updated zoom_level
update_zoomed_tile_size();
// building the floor
build_floor();
// building the walls
build_wall();
}
// draw the room onto the screen
void draw() const
{
for (int y = 0; y < size_y; y++)
{
for (int x = 0; x < size_x; x++)
{
tile_data tile = floor_array[y][x];
fill_rectangle(tile.tile_color, tile.tile);
}
}
}
// check if a tile coords is passable
bool is_passable(const coordinate &tile_coords) const
{
return floor_array[(int)tile_coords.y][(int)tile_coords.x].passable;
}
// getters and setters
const coordinate &get_spawn_coords() const
{
return spawn_coords;
}
int get_size_x() const
{
return size_x;
}
int get_size_y() const
{
return size_y;
}
double get_zoomed_tile_size() const
{
return zoomed_tile_size;
}
const vector<rectangle> &get_walls_vector() const
{
return walls_vector;
}
double get_tile_size() const
{
return tile_size;
}
const color *get_color_pattern() const
{
return color_pattern;
}
// sets the wall of the room, using a start and end tile coordinate
//(the wall is the rectangle from the top left corner of start tile to the bottom right corner of end tile)
void set_wall(const coordinate &start_tile, const coordinate &end_tile)
{
coordinate wall_coords_start = start_tile;
// tile_to_pixel returns the top left corner of the tile, so we need to add the size of the tile to get the bottom right corner
coordinate wall_coords_end = end_tile;
vector<coordinate> wall_coords_vector = {wall_coords_start, wall_coords_end};
walls_coords_vector.push_back(wall_coords_vector);
}
void set_color_pattern(const color &floor_color_1, const color &floor_color_2, const color &wall_color)
{
color_pattern[0] = floor_color_1;
color_pattern[1] = floor_color_2;
color_pattern[2] = wall_color;
}
void set_zoom_level(double zoom_level)
{
this->zoom_level = zoom_level;
}
};
class character_data
{
private:
int health;
double speed; // pixels per second
rectangle hurtbox; // the box that determines the player's collision
bitmap character_model;
bool model_facing_right; // models are drawn facing right, this is used to determine if the model should be flipped
double model_scaling; // scaling of the model, character model is scaled by this value (character model is made at 5x10 pixels)
double zoom_level; // zoom level of the screen to adjust the size of the model and posiion of the model
double zoomed_model_scaling; // model scaling after zooming
coordinate position; // position of the character in correlation to the screen (non-zoomed)
coordinate zoomed_position; // position of the character in correlation to the room (after zooming), only used for drawing
void update_zoomed_position()
{
zoomed_position = {position.x * zoom_level, position.y * zoom_level};
}
protected:
// constructor
character_data(int health, double speed, const bitmap &model, bool model_facing_right, double model_size, const coordinate &spawn_coords)
{
// setting player
character_model = model;
this->model_facing_right = model_facing_right; // depending on the drawn model, the player might be facing right or left
this->health = health;
this->speed = speed; // pixels per milisecond
zoom_level = 1;
// setting the model size, calculated by the smallest side of the model
set_model_size(model_size);
position = {spawn_coords.x, spawn_coords.y};
// setting the zoomed model scaling, hurtbox, and zoomed position
update_zoomed_position();
update_zoomed_model_scaling();
update_hurtbox();
}
// update the position of the hurtbox as the character moves (align with character's position)
void update_hurtbox()
{
double model_width = bitmap_width(character_model);
double model_height = bitmap_height(character_model);
hurtbox = {zoomed_position.x, zoomed_position.y, model_width * zoomed_model_scaling, model_height * zoomed_model_scaling};
}
// update the zoomed model scaling (model scaling after zooming)
void update_zoomed_model_scaling()
{
zoomed_model_scaling = model_scaling * zoom_level;
}
// getters and setters
// set the size of the model (scaling of the model)
void set_model_size(double model_size)
{
double model_width = bitmap_width(character_model);
double model_height = bitmap_height(character_model);
// determining the scaling of the model, the smallest side will be scaled to the model_size
if (model_width < model_height)
{
model_scaling = model_size / model_width;
}
else
{
model_scaling = model_size / model_height;
}
}
void set_zoomed_position(const coordinate &zoomed_position)
{
this->zoomed_position = zoomed_position;
}
// return the model (bitmap) of the character
const bitmap &get_model() const
{
return character_model;
}
// get the direction the character is facing (direction of the model)
bool get_is_facing_right() const
{
return model_facing_right;
}
// get the scaling of the model
double get_zoomed_model_scaling() const
{
return zoomed_model_scaling;
}
double get_zoom_level() const
{
return zoom_level;
}
void set_position(const coordinate &position)
{
this->position = position;
}
void set_health(int health)
{
this->health = health;
}
// get position of the character in colleration to the room
const coordinate &get_zoomed_position() const
{
return zoomed_position;
}
public:
// update the character's position and hurtbox, should always be ran inside the game loop
void update()
{
// no need to update if the character is dead
if (get_health() <= 0)
{
return;
}
update_zoomed_position();
update_zoomed_model_scaling();
update_hurtbox();
}
// move the character in a direction by a distance
void move(vector_2d &direction, double distance, const room_data &room)
{
if (direction.x != 0 || direction.y != 0)
{
direction = unit_vector(direction); // convert to unit vector
}
vector_2d movement = vector_multiply(direction, distance);
coordinate new_position = {position.x + movement.x, position.y + movement.y};
// set_position(new_position);
// array with all the walls in the room
const vector<rectangle> &walls_vector = room.get_walls_vector();
// checking the collision of player hurtbox with each walls
for (int i = 0; i < walls_vector.size(); i++)
{
// the box of the collision (intersection of the player hurtbox and the wall)
rectangle collision_box = intersection(hurtbox, walls_vector[i]);
if (collision_box.width != 0 && collision_box.height != 0) // if there is a collision
{
if (collision_box.height < collision_box.width)
{
// checking if player moves by y + collision_box.height will still cause collision
hurtbox.y += collision_box.height;
if (intersection(hurtbox, walls_vector[i]).height == 0)
{
// if no collision, then move the player by y + collision_box.height
new_position = {new_position.x, (new_position.y + collision_box.height)};
}
// hurtbox_copy.y = new_position.y - collision_box.height;
else
{
// if collision, then move the player by y - collision_box.height
new_position = {new_position.x, (new_position.y - collision_box.height)};
}
}
if (collision_box.height > collision_box.width)
{
// checking if player moves by x + collision_box.width will still cause collision
hurtbox.x += collision_box.width;
if (intersection(hurtbox, walls_vector[i]).width != 0)
{
// if no collision, then move the player by x + collision_box.width
new_position = {(new_position.x - collision_box.width), new_position.y};
}
// hurtbox_copy.x = new_position.x - collision_box.width;
else
{
// if collision, then move the player by x - collision_box.width
new_position = {(new_position.x + collision_box.width), new_position.y};
}
}
}
}
set_position(new_position);
}
// draw the character onto the screen
void draw() const
{
if (get_health() <= 0)
{
return;
}
double model_width = bitmap_width(get_model());
double model_height = bitmap_height(get_model());
double zoomed_model_scaling = get_zoomed_model_scaling();
// fixing bitmap scaling position
double pos_x = get_zoomed_position().x + (((model_width * zoomed_model_scaling) - model_width) / 2);
double pos_y = get_zoomed_position().y + (((model_height * zoomed_model_scaling) - model_height) / 2);
// flip when facing opposite direction
if (get_is_facing_right())
{
draw_bitmap(get_model(), pos_x, pos_y, option_scale_bmp(zoomed_model_scaling, zoomed_model_scaling));
}
else
{
draw_bitmap(get_model(), pos_x, pos_y, option_flip_y(option_scale_bmp(zoomed_model_scaling, zoomed_model_scaling)));
}
}
// check if the character's hurtbox is colliding with a hitbox
void check_hitbox_collision(const rectangle &hitbox)
{
rectangle collision_box = intersection(hurtbox, hitbox);
if (collision_box.width != 0 && collision_box.height != 0)
{
// if there is a collision, decrease the health of the character
health--;
}
}
// getters and setters
double get_speed() const
{
return speed;
}
int get_health() const
{
return health;
}
const coordinate &get_position() const
{
return position;
}
// set the direction the character is facing (true is right, false is left)
void set_is_facing_right(bool facing_right)
{
model_facing_right = facing_right;
}
// set character's zoom level (to zoom with the screen)
void set_zoom_level(double zoom_level)
{
this->zoom_level = zoom_level;
}
// get the player's center position on the screen
coordinate get_center_position() const
{
return {zoomed_position.x + (bitmap_width(get_model()) * get_zoomed_model_scaling()) / 2, zoomed_position.y + (bitmap_height(get_model()) * get_zoomed_model_scaling()) / 2};
}
// get the hurtbox of the character
const rectangle &get_hurtbox() const
{
return hurtbox;
}
};
// class for npcs in the game
class npc_data : public character_data
{
private:
coordinate new_position; // the position the npc is moving to
coordinate zoomed_new_position; // the position the npc is moving to after zooming
double auto_move_max_distance; // the range at which the new_position can be from its current position (in a square)
double zoomed_auto_move_max_distance; // the range at which the new_position can be from its current position (in a square) after zooming
int time_since_new_position; // the time since the npc has a new position
int new_position_cooldown; // the time the npc should have a new position
// generates a random position for the npc, that is valid for the npc to move to, or stay at
void update_new_position(const room_data &room, coordinate min_coords, coordinate max_coords)
{
min_coords = min_coords.pixel_to_tile(room.get_zoomed_tile_size());
max_coords = max_coords.pixel_to_tile(room.get_zoomed_tile_size());
coordinate rand_position;
do
{
// generating a random position for the npc to move to converting to tile coordinates
rand_position = random_coordinate(min_coords, max_coords);
// checking if the tile is within the room
if (rand_position.x < 0 || rand_position.x >= room.get_size_x() || rand_position.y < 0 || rand_position.y >= room.get_size_y())
{
continue;
}
// make sure the npc can fit inside the new position
int player_tile_height = (int)ceil(get_hurtbox().height / room.get_zoomed_tile_size());
int player_tile_width = (int)ceil(get_hurtbox().width / room.get_zoomed_tile_size());
vector<coordinate> player_tiles;
for (int i = 0; i < player_tile_height; i++)
{
for (int j = 0; j < player_tile_width; j++)
{
player_tiles.push_back({rand_position.x + j, rand_position.y + i});
}
}
// checking for all tiles the npc will fill in the new position
bool valid_position = true;
for (int i = 0; i < player_tiles.size(); i++)
{
if (!room.is_passable(player_tiles[i]))
{
valid_position = false;
break;
}
}
// checking if the npc can fit in the position
if (valid_position)
{
break;
}
} while (true);
set_new_position(rand_position);
}
// move the npc to a random position within a range automatically
void auto_set_new_position(double delta_time, const room_data &room)
{
coordinate position = get_zoomed_position();
// determines if NPC is at the destination, allows an error of (+-)10 pixel due to the float to int conversion, and other scalings
bool x_at_destination = ((int)position.x >= zoomed_new_position.x - 10) && ((int)position.x <= zoomed_new_position.x + 10);
bool y_at_destination = ((int)position.y >= zoomed_new_position.y - 10) && ((int)position.y <= zoomed_new_position.y + 10);
time_since_new_position += delta_time;
bool cooldown_passed = time_since_new_position >= new_position_cooldown;
// if the npc is at the destination or cooldown for new position passed, generate a new destination
if ((x_at_destination && y_at_destination) || cooldown_passed)
{
coordinate min_coords = {get_zoomed_position().x - zoomed_auto_move_max_distance, get_zoomed_position().y - zoomed_auto_move_max_distance};
coordinate max_coords = {get_zoomed_position().x + zoomed_auto_move_max_distance, get_zoomed_position().y + zoomed_auto_move_max_distance};
update_new_position(room, min_coords, max_coords);
new_position = new_position.tile_to_pixel(room.get_tile_size()); // converting back to pixel coordinates for the npc to move to
time_since_new_position = 0;
}
}
void auto_move(double delta_time, const room_data &room)
{
// calculate the direction and distance the npc should move
vector_2d direction = {0, 0};
direction.x = zoomed_new_position.x - get_zoomed_position().x;
direction.y = zoomed_new_position.y - get_zoomed_position().y;
// set the direction the npc is facing
if (direction.x > 0)
{
set_is_facing_right(true);
}
else
{
set_is_facing_right(false);
}
// the distance the npc should move according to delta_time
double distance = get_speed() * (double)delta_time;
move(direction, distance, room);
}
// update values that are affected by zoom level
void update_zoomed_auto_move_max_distance()
{
zoomed_auto_move_max_distance = auto_move_max_distance * get_zoom_level();
}
// update values that are affected by zoom level
void update_zoomed_new_position()
{
zoomed_new_position = {new_position.x * get_zoom_level(), new_position.y * get_zoom_level()};
}
// setting new position of the npc (that it will auto move to)
void set_new_position(const coordinate &new_position)
{
this->new_position = new_position;
}
public:
// Constructor
npc_data(double tile_size, double model_size, const room_data &room, string bitmap_name)
: character_data(1, 4 * tile_size / 1000, bitmap_named(bitmap_name), true, model_size, {0, 0})
{
auto_move_max_distance = 10 * tile_size;
new_position_cooldown = 5000; // ms
time_since_new_position = 0; // ms
coordinate max = {(double)(room.get_size_x() - 1), (double)(room.get_size_y() - 1)};
coordinate min = {0, 0};
update_new_position(room, min.tile_to_pixel(room.get_zoomed_tile_size()), max.tile_to_pixel(room.get_zoomed_tile_size()));
new_position = new_position.tile_to_pixel(room.get_zoomed_tile_size());
set_position(new_position);
update_zoomed_auto_move_max_distance();
update_zoomed_new_position();