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
|
#include "game.h"
#include "renderer.h"
Game::Game()
{
window = initRenderer();
glfwSwapInterval(1);
m = new Map("../maps/embarass.txt");
m2 = new Map("../maps/second.txt");
m->setLeftMap(m2);
m2->setRightMap(m);
world = new World();
auto player = std::make_shared<Entity>(world);
auto player_input = std::make_shared<UserMovementComponent>(*player);
player->addComponent(player_input);
auto player_physics = std::make_shared<PlayerPhysicsComponent>(*player);
player_physics->position = std::make_pair(100.0,100.0);
player_physics->size = std::make_pair(10.0,12.0);
player->addComponent(player_physics);
auto player_anim = std::make_shared<PlayerSpriteComponent>(*player, *player_physics);
player->addComponent(player_anim);
world->addEntity(player);
world->player = player;
loadMap(m);
}
Game::~Game()
{
if (world != 0)
{
delete world;
}
if (nextWorld != 0)
{
delete nextWorld;
}
delete m;
delete m2;
destroyRenderer();
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
(void)window;
(void)scancode;
(void)mods;
if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS))
{
Game::getInstance().shouldQuit = true;
}
Game::getInstance().input(key, action);
}
void Game::execute()
{
glfwSetKeyCallback(window, key_callback);
Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT);
while (!(shouldQuit || glfwWindowShouldClose(window)))
{
// Should we load a new world?
if (nextWorld != 0)
{
delete world;
world = nextWorld;
world->player->world = world;
nextWorld = 0;
}
// Handle input
glfwPollEvents();
// Tick!
world->tick();
// Do rendering
world->render(buffer);
renderScreen(buffer);
}
destroyTexture(buffer);
}
void Game::input(int key, int action)
{
if (world != NULL)
{
world->input(key, action);
}
}
void Game::loadMap(Map* map)
{
nextWorld = new World();
nextWorld->player = world->player;
auto mapEn = std::make_shared<Entity>(nextWorld);
auto map_render = std::make_shared<MapRenderComponent>(*mapEn, map);
mapEn->addComponent(map_render);
auto map_collision = std::make_shared<MapCollisionComponent>(*mapEn, map);
mapEn->addComponent(map_collision);
nextWorld->bodies.push_back(map_collision.get());
nextWorld->addEntity(mapEn);
nextWorld->addEntity(nextWorld->player);
}
|