-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
123 lines (103 loc) · 3.24 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
#include "internal.hpp"
#include "user.hpp"
#include <memory>
#include <algorithm>
#include <raylib-ext.hpp>
#include <raylib.h>
#include <vector>
#include <string>
#include <map>
int main()
{
Vector2 screen_size = { 800, 600 };
InitWindow(screen_size.x, screen_size.y, "Some unnamed game");
SetTargetFPS(60);
Context ctx;
ctx.camera_pos = { 0, 0 };
ctx.time = 0;
ctx.screen_size = screen_size;
ctx.state = GameState::MAIN_MENU;
ctx.textures_storage = std::map<TextureHash, Texture>();
Render heart_render = Render(ctx, "Assets/heart.png", Vector2 {30.0, 30.0});
ctx.heart = std::make_unique<Render>(std::move(heart_render));
std::map<std::string, std::vector<Object>> scenes;
ReadScene(ctx, scenes["game"], "Assets/game.lvl");
Object obj = Object();
obj.gui_draw = DrawStatus;
scenes["game"].push_back(std::move(obj));
ConstructMenuScene(ctx, scenes["menu"]);
obj = Object();
obj.gui_draw = DrawMainScreen;
scenes["menu"].push_back(std::move(obj));
InitAudioDevice();
ctx.scenes = scenes;
ctx.current_scene = scenes["menu"];
while (!WindowShouldClose())
{
float dt = GetFrameTime();
ctx.time += uint64_t(dt * 1000);
UpdateGameState(ctx);
BeginDrawing();
{
ClearBackground(BLACK);
for (auto &obj : ctx.current_scene)
{
if (obj.gui_draw)
{
obj.gui_draw(ctx);
}
if (obj.render.visible)
{
Vector2 pos = local_to_screen(&ctx, obj.position);
Vector2 img_size = {
float(obj.render.width),
float(obj.render.height),
};
pos -= img_size * 0.5;
Texture texture = ctx.textures_storage[obj.render.hash];
DrawTextureV(texture, pos, WHITE);
}
}
UpdateSceneState(ctx);
}
EndDrawing();
if (ctx.state == GameState::MAIN_MENU)
{
continue;
}
Object &player = *find_player(ctx.current_scene);
PlayerControl(ctx, player, dt);
for (Object &obj : ctx.current_scene)
{
ApplyGravity(obj, dt);
if (obj.enemy.enabled)
EnemyAI(obj, ctx.current_scene, dt);
if (obj.bullet.enabled)
UpdateBullet(ctx, obj, dt);
}
FixCollisions(ctx.current_scene, dt);
MoveCameraTowards(ctx, player, dt);
KillEnemies(ctx);
for (GameId id_to_destroy : ctx.to_destroy)
{
ctx.current_scene.erase(
std::remove_if(
ctx.current_scene.begin(),
ctx.current_scene.end(),
[id_to_destroy](Object &c) {
return c.id == id_to_destroy;
}
),
ctx.current_scene.end()
);
}
ctx.to_destroy.clear();
for (Object &obj : ctx.to_spawn)
{
ctx.current_scene.push_back(std::move(obj));
}
ctx.to_spawn.clear();
}
CloseWindow();
return 0;
}