-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine.h
735 lines (554 loc) · 25.1 KB
/
engine.h
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
#pragma once
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#include <numeric>
#include <memory>
#include <filesystem>
#include <functional>
#include <utility>
#include <vector>
#include <set>
#include <magic_enum/magic_enum.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <fmt/core.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#define to_lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define to_upper(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define to_titlecase(s) \
to_lower(s); \
s[0] = toupper(s[0]);
extern std::string generate_uuid();
extern int generate_random_int(int min, int max);
namespace roguely::level_generation {
// Quick and dirty cellular automata that I learned about from YouTube. We
// can do more but currently are just doing the very least to get a playable
// level.
int get_neighbor_wall_count(const std::shared_ptr<boost::numeric::ublas::matrix<int> > &map, int map_width, int map_height,
int x, int y);
void perform_cellular_automaton(const std::shared_ptr<boost::numeric::ublas::matrix<int> > &map, int map_width,
int map_height, int passes);
std::shared_ptr<boost::numeric::ublas::matrix<int> > init_cellular_automata(int map_width, int map_height);
}
namespace roguely::common {
struct Point {
bool eq(Point p) const;
int x{};
int y{};
};
struct Size {
bool eq(Size s) const;
int width{};
int height{};
};
struct Dimension {
bool eq(const Dimension &d) const;
Point point{};
Point supplimental_point{}; // this is a hack for our Janky map drawing optimization
Size size{};
};
struct Sound {
std::string name;
Mix_Chunk *sound;
public:
void play() const {
if (sound != nullptr)
Mix_PlayChannel(-1, sound, 0);
}
};
class Text {
public:
int load_font(const std::string &path, int ptsize);
void draw_text(SDL_Renderer *renderer, int x, int y, const std::string &text);
void draw_text(SDL_Renderer *renderer, int x, int y, const std::string &text, SDL_Color color);
[[nodiscard]] Size get_text_extents(const std::string &text) const;
private:
TTF_Font *font{};
std::string text{};
SDL_Texture *text_texture{};
SDL_Rect text_rect{};
SDL_Color text_color = {255, 255, 255, 255};
SDL_Color text_background_color = {0, 0, 0, 255};
};
}
namespace roguely::ecs {
enum class EntityGroupName {
PLAYER,
MOBS,
ITEMS,
OTHER
};
extern std::string entity_group_name_to_string(roguely::ecs::EntityGroupName group_name);
class Component {
public:
virtual ~Component() = default;
[[nodiscard]] auto get_name() const { return component_name; }
void set_name(const std::string &name) { component_name = name; }
[[nodiscard]] auto get_id() const { return id; }
explicit Component(std::string name, std::string id = generate_uuid()) : component_name(std::move(name)), id(std::move(id)) {
}
private:
std::string component_name{"unnamed component"};
protected:
std::string id{};
};
template<class T>
concept ComponentType = std::is_base_of_v<roguely::ecs::Component, T>;
class Entity {
public:
Entity() : Entity(generate_uuid(), "unnamed entity") {
}
explicit Entity(const std::string &name) : Entity(generate_uuid(), name) {
}
Entity(std::string id, std::string name) : id(std::move(id)), name(std::move(name)) {
components = std::make_unique<std::vector<std::shared_ptr<Component> > >();
}
template<ComponentType T>
std::shared_ptr<T> find_first_component_by_type() {
for (auto &c: *components) {
auto casted = std::dynamic_pointer_cast<T>(c);
if (casted != nullptr) {
return casted;
}
}
return nullptr;
}
template<ComponentType T>
std::shared_ptr<T> find_first_component_by_name(const std::string &name) {
std::vector<std::shared_ptr<T> > matches{};
for (auto &c: *components) {
auto casted = std::dynamic_pointer_cast<T>(c);
if (casted != nullptr && casted->get_name() == name) {
return casted;
}
}
return nullptr;
}
template<ComponentType T>
auto find_components_by_name(const std::string &name) {
std::vector<std::shared_ptr<T> > matches{};
for (auto &c: *components) {
auto casted = std::dynamic_pointer_cast<T>(c);
if (casted != nullptr && casted->get_name() == name) {
matches.emplace_back(casted);
}
}
return matches;
}
template<ComponentType T>
auto find_components_by_type() {
std::vector<std::shared_ptr<T> > matches{};
for (auto &c: *components) {
auto casted = std::dynamic_pointer_cast<T>(c);
if (casted != nullptr) {
matches.emplace_back(casted);
}
}
return matches;
}
template<ComponentType T>
auto find_components_by_type(std::function<bool(std::shared_ptr<T>)> predicate) {
std::vector<std::shared_ptr<T> > matches{};
for (auto &c: *components) {
auto casted = std::dynamic_pointer_cast<T>(c);
if (casted != nullptr && predicate(casted)) {
matches.emplace_back(casted);
}
}
return matches;
}
[[nodiscard]] auto get_name() const { return name; }
[[nodiscard]] auto get_id() const { return id; }
void add_component(const std::shared_ptr<Component>& c) const { components->emplace_back(c); }
void add_components(std::vector<std::shared_ptr<Component> > c) const {
components->insert(components->end(), c.begin(), c.end());
}
template<ComponentType T>
void remove_components(std::vector<std::shared_ptr<T> > c) {
for (auto &component: c) {
auto it = std::find(components->begin(), components->end(), component);
if (it != components->end()) {
components->erase(it);
}
}
}
template<ComponentType T>
void remove_component(std::shared_ptr<T> component) {
auto it = std::find(components->begin(), components->end(), component);
if (it != components->end()) {
components->erase(it);
}
}
void for_each_component(const std::function<void(std::shared_ptr<Component> &)> &fc) const {
for (auto &c: *components) {
fc(c);
}
}
void clear_components() const { components->clear(); }
[[nodiscard]] auto get_component_count() const { return components->size(); }
private:
std::string id{};
public:
template<typename T>
std::vector<std::shared_ptr<T> > find_component_by_type(std::function<bool(std::shared_ptr<T>)> predicate) {
std::vector<std::shared_ptr<T> > results;
for (const auto &component: *components) {
if (auto casted_component = std::dynamic_pointer_cast<T>(component);
casted_component && predicate(casted_component)) {
results.emplace_back(casted_component);
}
}
return results;
}
protected:
std::string name = {"unnamed entity"};
std::unique_ptr<std::vector<std::shared_ptr<Component> > > components{};
};
struct EntityGroup {
std::string name{};
std::shared_ptr<std::vector<std::shared_ptr<Entity> > > entities{};
};
class EntityManager {
public:
explicit EntityManager(sol::this_state s) {
sol::state_view lua(s);
entity_groups = std::make_unique<std::vector<std::shared_ptr<EntityGroup> > >();
lua_entities = lua.create_table();
}
void add_entity_to_group(const std::string &group_name, const std::shared_ptr<Entity>& e, sol::this_state s);
void add_entity_to_group(const EntityGroupName group_name, std::shared_ptr<Entity> e, const sol::this_state s) {
add_entity_to_group(entity_group_name_to_string(group_name), std::move(e), s);
}
[[nodiscard]] std::shared_ptr<EntityGroup> create_entity_group(const std::string &group_name) const;
[[nodiscard]] std::shared_ptr<Entity> create_entity_in_group(const std::string &group_name, const std::string &entity_name) const;
void remove_entity(const std::string &entity_group_name, const std::string &entity_id);
[[nodiscard]] std::vector<std::string> get_entity_group_names() const {
std::vector<std::string> results{};
for (const auto &eg: *entity_groups) {
results.emplace_back(eg->name);
}
return results;
}
[[nodiscard]] std::shared_ptr<EntityGroup> get_entity_group(const std::string &group_name) const;
[[nodiscard]] std::shared_ptr<EntityGroup> get_entity_group(const EntityGroupName group_name) const {
return get_entity_group(entity_group_name_to_string(group_name));
}
[[nodiscard]] std::shared_ptr<std::vector<std::shared_ptr<Entity> > > get_entities_in_group(const std::string &group_name) const;
[[nodiscard]] std::shared_ptr<std::vector<std::shared_ptr<Entity> > > get_entities_in_group(const EntityGroupName group_name) const {
return get_entities_in_group(entity_group_name_to_string(group_name));
}
std::string get_entity_id_by_name(const std::string &group_name, const std::string &entity_name) const;
[[nodiscard]] std::shared_ptr<Entity> get_entity_by_name(const std::string &group_name, const std::string &entity_name) const;
[[nodiscard]] std::shared_ptr<Entity> get_entity_by_name(const EntityGroupName group_name, const std::string &entity_name) const {
return get_entity_by_name(entity_group_name_to_string(group_name), entity_name);
}
[[nodiscard]] std::shared_ptr<Entity> get_entity_by_id(const std::string &group_name, const std::string &entity_id) const;
[[nodiscard]] std::shared_ptr<Entity> get_entity_by_id(const EntityGroupName group_name, const std::string &entity_id) const {
return get_entity_by_id(entity_group_name_to_string(group_name), entity_id);
}
template<typename T>
auto find_entities_by_component_type(const std::string& entity_group, std::function<bool(std::shared_ptr<T>)> predicate) {
auto group = get_entity_group(entity_group);
std::vector<std::shared_ptr<Entity> > matches{};
for (auto &e: *group->entities) {
if (auto result = e->find_component_by_type<T>(predicate); result.size() > 0) {
matches.emplace_back(e);
}
}
return matches;
}
std::shared_ptr<std::vector<std::shared_ptr<Entity> > > find_entities_in_group(
const std::string &entity_group, const std::function<bool(std::shared_ptr<Entity>)> &predicate) const;
std::shared_ptr<Entity> find_entity(const std::string &entity_group,
const std::function<bool(std::shared_ptr<Entity>)> &predicate) const;
std::shared_ptr<Entity> find_entity(const EntityGroupName entity_group,
const std::function<bool(std::shared_ptr<Entity>)> &predicate) const {
return find_entity(entity_group_name_to_string(entity_group), predicate);
}
[[nodiscard]] sol::table get_lua_entities() const { return lua_entities; }
sol::table get_lua_entity(const std::string &entity_group, const std::string &entity_name) {
const sol::table entities = lua_entities[entity_group];
sol::table result = sol::nil;
if (!entities.valid()) {
fmt::println("get_lua_entity: entities is not valid");
return result;
}
entities.for_each([&](const sol::object &key, const sol::table &value) {
if (key.is<std::string>()) {
if (const auto key_str = key.as<std::string>(); key_str.compare(0, entity_name.size(), entity_name) == 0) {
result = value;
return false;
}
}
return true;
});
return result;
}
void remove_lua_component(const std::string &entity_group, const std::string &entity_name,
const std::string &component_name) {
if (auto entity = get_lua_entity(entity_group, entity_name); entity.valid()) {
if (auto components = entity["components"]; components.valid()) {
components[component_name] = sol::nil;
// fmt::println("removed component {} from entity {}", component_name, entity_name);
}
}
}
bool lua_entities_for_each(const std::function<bool(sol::table)> &predicate) const;
[[nodiscard]] bool lua_is_point_unique(roguely::common::Point point) const;
void lua_for_each_overlapping_point(const std::string &entity_name, int x, int y, const sol::function &point_callback) const;
[[nodiscard]] sol::table get_lua_blocked_points(const std::string &entity_group, int x, int y, const std::string &direction,
sol::this_state s) const;
sol::table get_lua_entities_in_viewport(const std::function<bool(int x, int y)> &predicate, sol::this_state s) const;
static sol::table copy_table(const sol::table &original, sol::this_state s) {
sol::state_view lua(original.lua_state());
sol::table copy = lua.create_table();
original.for_each([&](const sol::object &key, const sol::object &value) {
if (value.is<sol::table>()) {
copy[key] = copy_table(value.as<sol::table>(), s);
} else {
copy[key] = value;
}
});
return copy;
}
private:
std::unique_ptr<std::vector<std::shared_ptr<EntityGroup> > > entity_groups{};
sol::table lua_entities{};
};
}
namespace roguely::components {
// For Lua integration we don't need a bunch of custom components. We'll just
// use a simple component that stores everything in a Lua table
class LuaComponent final : public roguely::ecs::Component {
public:
LuaComponent(const std::string &n, const sol::table &props, sol::this_state s)
: Component(n) {
sol::state_view lua(s);
properties = copy_table(props, s);
}
[[nodiscard]] auto get_properties() const { return properties; }
template<typename T>
T get_property(const std::string &name) {
if (properties[name].valid()) {
auto value = properties.get<sol::object>(name);
if (std::is_same_v<T, sol::object>) {
return value;
}
return value.as<T>();
} else {
throw std::runtime_error("Property does not exist: " + name);
}
}
void set_property(const std::string &name, const sol::object& value) { properties.set(name, value); }
void set_properties(const sol::table &props, sol::this_state s) { properties = props; }
static sol::table copy_table(const sol::table &original, const sol::this_state s) {
sol::state_view lua(original.lua_state());
sol::table copy = lua.create_table();
original.for_each([&](const sol::object &key, const sol::object &value) {
if (value.is<sol::table>()) {
copy[key] = copy_table(value.as<sol::table>(), s);
} else {
copy[key] = value;
}
});
return copy;
}
private:
sol::table properties;
};
}
namespace roguely::sprites {
class SpriteSheet {
public:
SpriteSheet(SDL_Renderer *renderer, const std::string &n, const std::string &p, int sw, int sh, int sf);
~SpriteSheet() {
SDL_DestroyTexture(spritesheet_texture);
}
void draw_sprite(SDL_Renderer *renderer, int sprite_id, int x, int y) const;
void draw_sprite(SDL_Renderer *renderer, int sprite_id, int x, int y, int scale_factor) const;
void draw_sprite_sheet(SDL_Renderer *renderer, int x, int y) const;
[[nodiscard]] SDL_Texture *get_spritesheet_texture() const { return spritesheet_texture; }
[[nodiscard]] auto get_name() const { return name; }
[[nodiscard]] auto get_sprite_width() const { return sprite_width; }
[[nodiscard]] auto get_sprite_height() const { return sprite_height; }
[[nodiscard]] auto get_scale_factor() const { return scale_factor; }
[[nodiscard]] sol::table get_sprites_as_lua_table(sol::this_state s) const;
[[nodiscard]] auto get_size_of_sprites() const { return sprites->size(); }
void add_blocked_sprite(const int sprite_id) { blocked_sprite_ids.insert(sprite_id); }
void remove_blocked_sprite(const int sprite_id) { blocked_sprite_ids.erase(sprite_id); }
[[nodiscard]] bool is_sprite_blocked(const int sprite_id) const { return blocked_sprite_ids.contains(sprite_id); }
void set_highlight_color(int r, int g, int b) const {
SDL_SetTextureColorMod(spritesheet_texture, r, g, b);
}
void reset_highlight_color() const {
SDL_SetTextureColorMod(spritesheet_texture, o_red, o_green, o_blue);
}
private:
Uint8 o_red{}, o_green{}, o_blue{};
std::set<int> blocked_sprite_ids{};
std::string name{};
std::string path{};
int sprite_width{};
int sprite_height{};
int scale_factor{};
std::unique_ptr<std::vector<std::shared_ptr<SDL_Rect> > > sprites{};
SDL_Texture *spritesheet_texture{};
};
}
namespace roguely::map {
class Map {
public:
Map() = default;
Map(std::string n, int w, int h, const std::shared_ptr<boost::numeric::ublas::matrix<int> > &m)
: name(std::move(n)), width(w), height(h), map(m),
light_map(std::make_shared<boost::numeric::ublas::matrix<int> >(h, w, 0)) {
};
void draw_map(SDL_Renderer *renderer,
const common::Dimension &dimensions,
const std::shared_ptr<sprites::SpriteSheet> &sprite_sheet,
const std::function<void(int, int, int, int, int, int, int)> &draw_hook);
void draw_map(SDL_Renderer *renderer, const common::Dimension &dimensions, int x, int y, int a,
const std::function<void(int, int, int)> &draw_hook);
void calculate_field_of_view(const common::Dimension &dimensions);
[[nodiscard]] auto get_name() const { return name; }
[[nodiscard]] auto get_width() const { return width; }
[[nodiscard]] auto get_height() const { return height; }
[[nodiscard]] auto get_map() const { return map; }
[[nodiscard]] auto get_light_map() const { return light_map; }
static auto map_to_world(const int x, const int y, const common::Dimension &dimensions,
const std::shared_ptr<sprites::SpriteSheet>& sprite_sheet) {
const int scale_factor = sprite_sheet->get_scale_factor();
const int sprite_width = sprite_sheet->get_sprite_width();
const int sprite_height = sprite_sheet->get_sprite_height();
const int dx = (x * sprite_width * scale_factor) -
(dimensions.point.x * sprite_width * scale_factor);
const int dy = (y * sprite_height * scale_factor) -
(dimensions.point.y * sprite_height * scale_factor);
//fmt::print("map_to_world: x: {}, y: {}, dx: {}, dy: {}\n", x, y, dx, dy);
return common::Point{dx, dy};
}
// auto world_to_map(int x, int y, roguely::common::Dimension dimensions, std::shared_ptr<roguely::sprites::SpriteSheet> sprite_sheet) const
// {
// int scale_factor = sprite_sheet->get_scale_factor();
// int sprite_width = sprite_sheet->get_sprite_width();
// int sprite_height = sprite_sheet->get_sprite_height();
// int dx = (x + dimensions.point.x * sprite_width * scale_factor) /
// (sprite_width * scale_factor);
// int dy = (y + dimensions.point.y * sprite_height * scale_factor) /
// (sprite_height * scale_factor);
// return roguely::common::Point{dx, dy};
// }
[[nodiscard]] common::Point get_random_point(const std::set<int> &off_limit_sprites_ids) const;
void trigger_redraw() { current_map_segment_dimension = {}; }
[[nodiscard]] auto is_point_blocked(const int x, const int y) const { return (*map)(y, x) == 0; }
private:
// This is our jank optimization for preventing us from creating a new
// SDL_Texture every frame if nothing has changed. This is used in draw_map.
roguely::common::Dimension current_map_segment_dimension{};
roguely::common::Dimension current_full_map_dimension{};
SDL_Texture *current_map_segment_texture{};
SDL_Texture *current_full_map_texture{};
std::string name{};
int width{};
int height{};
std::shared_ptr<boost::numeric::ublas::matrix<int> > map{};
std::shared_ptr<boost::numeric::ublas::matrix<int> > light_map{};
};
struct MapInfo {
std::string name{};
std::shared_ptr<roguely::map::Map> map{};
};
class AStar {
public:
AStar() = default;
[[nodiscard]] std::vector<std::pair<int, int> > FindPath(const boost::numeric::ublas::matrix<int> &grid, int start_row,
int start_col, int goal_row, int goal_col) const;
private:
// Heuristic function for estimating the distance between two points
static int heuristic(const int x1, const int y1, const int x2, const int y2) {
return std::abs(x1 - x2) + std::abs(y1 - y2);
}
// Define the possible movements (up, down, left, right)
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
// Define a typedef for the priority queue entry
typedef std::pair<int, std::pair<int, int> > pq_entry;
};
}
namespace roguely::engine {
class Engine {
public:
Engine();
int game_loop();
private:
// Internal functions
int init_sdl(sol::table game_config, sol::this_state s);
void tear_down_sdl();
void setup_lua_api(sol::this_state _s);
static bool check_game_config(sol::table game_config, sol::this_state s);
static sol::function check_if_lua_function_defined(sol::this_state s, const std::string &name);
void play_sound(const std::string &name);
// Drawing functions
void draw_text(const std::string &t, int x, int y) const;
void draw_text(const std::string &t, int x, int y, int r, int g, int b, int a) const;
void draw_sprite(const std::string &spritesheet_name, int sprite_id, int x, int y, int scale_factor) const;
static void set_draw_color(SDL_Renderer *renderer, int r, int g, int b, int a);
static void draw_point(SDL_Renderer *renderer, int x, int y);
static void draw_rect(SDL_Renderer *renderer, int x, int y, int w, int h);
static void draw_filled_rect(SDL_Renderer *renderer, int x, int y, int w, int h);
static void draw_filled_rect_with_color(SDL_Renderer *renderer, int x, int y, int w, int h, int r, int g, int b, int a);
static void draw_graphic(SDL_Renderer *renderer, const std::string &path, int window_width, int x, int y, bool centered,
int scale_factor);
static std::shared_ptr<map::Map> generate_map(const std::string &name, int map_width, int map_height);
common::Dimension update_player_viewport(const common::Point player_position,
const common::Size current_map) {
// fmt::println("BEFORE (update_player_viewport): x: {}, y: {}, width: {}, height: {}", player_position.x, player_position.y, current_map.width, current_map.height);
view_port_x = std::clamp(player_position.x - (VIEW_PORT_WIDTH / 2), 0, current_map.width - VIEW_PORT_WIDTH);
view_port_y = std::clamp(player_position.y - (VIEW_PORT_HEIGHT / 2), 0, current_map.height - VIEW_PORT_HEIGHT);
view_port_width = view_port_x + VIEW_PORT_WIDTH;
view_port_height = view_port_y + VIEW_PORT_HEIGHT;
// fmt::println("(update_player_viewport): view_port_x: {}, view_port_y: {}, view_port_width: {}, view_port_height: {}", view_port_x, view_port_y, view_port_width, view_port_height);
const auto dimensions = roguely::common::Dimension{
view_port_x, view_port_y, player_position.x, player_position.y, view_port_width, view_port_height
};
if (current_map_info.map != nullptr) {
current_map_info.map->calculate_field_of_view(dimensions);
}
return {view_port_x, view_port_y, player_position.x, player_position.y, view_port_width, view_port_height};
}
[[nodiscard]] std::shared_ptr<roguely::map::Map> find_map(const std::string &name) const {
auto it = std::ranges::find_if(*maps, [&name](const std::shared_ptr<roguely::map::Map> &map) {
return map->get_name() == name;
});
return nullptr;
}
[[nodiscard]] bool is_within_viewport(const int x, const int y) const {
if ((x >= view_port_x && x <= view_port_width - 1) &&
(y >= view_port_y && y <= view_port_height - 1))
return true;
return false;
}
int view_port_x{};
int view_port_y{};
int view_port_width{};
int view_port_height{};
int VIEW_PORT_WIDTH{};
int VIEW_PORT_HEIGHT{};
roguely::common::Dimension current_dimension{};
roguely::map::MapInfo current_map_info{};
SDL_Window *window{};
SDL_Renderer *renderer{};
Mix_Music *soundtrack{};
// FIXME: Need to have ability to load multiple fonts
std::weak_ptr<roguely::common::Text> default_font{};
std::unique_ptr<roguely::ecs::EntityManager> entity_manager{};
std::unique_ptr<std::vector<std::shared_ptr<roguely::common::Sound> > > sounds{};
std::unique_ptr<std::unordered_map<std::string, std::shared_ptr<roguely::sprites::SpriteSheet> > > sprite_sheets{};
std::unique_ptr<std::vector<std::shared_ptr<roguely::map::Map> > > maps{};
std::unique_ptr<std::unordered_map<std::string, std::shared_ptr<roguely::common::Text> > > texts{};
std::unique_ptr<std::unordered_map<std::string, sol::function> > systems{};
sol::state lua;
};
}