diff options
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..2db0a2c --- /dev/null +++ b/src/game.cpp | |||
@@ -0,0 +1,124 @@ | |||
1 | #include "game.h" | ||
2 | #include "renderer.h" | ||
3 | |||
4 | Game::Game() | ||
5 | { | ||
6 | window = initRenderer(); | ||
7 | glfwSwapInterval(1); | ||
8 | |||
9 | m = new Map("../maps/embarass.txt"); | ||
10 | m2 = new Map("../maps/second.txt"); | ||
11 | |||
12 | m->setLeftMap(m2); | ||
13 | m2->setRightMap(m); | ||
14 | |||
15 | world = new World(); | ||
16 | |||
17 | auto player = std::make_shared<Entity>(world); | ||
18 | |||
19 | auto player_input = std::make_shared<UserMovementComponent>(*player); | ||
20 | player->addComponent(player_input); | ||
21 | |||
22 | auto player_physics = std::make_shared<PlayerPhysicsComponent>(*player); | ||
23 | player_physics->position = std::make_pair(100.0,100.0); | ||
24 | player_physics->size = std::make_pair(10.0,12.0); | ||
25 | player->addComponent(player_physics); | ||
26 | |||
27 | auto player_anim = std::make_shared<PlayerSpriteComponent>(*player, *player_physics); | ||
28 | player->addComponent(player_anim); | ||
29 | |||
30 | world->addEntity(player); | ||
31 | world->player = player; | ||
32 | |||
33 | loadMap(m); | ||
34 | } | ||
35 | |||
36 | Game::~Game() | ||
37 | { | ||
38 | if (world != 0) | ||
39 | { | ||
40 | delete world; | ||
41 | } | ||
42 | |||
43 | if (nextWorld != 0) | ||
44 | { | ||
45 | delete nextWorld; | ||
46 | } | ||
47 | |||
48 | delete m; | ||
49 | delete m2; | ||
50 | |||
51 | destroyRenderer(); | ||
52 | } | ||
53 | |||
54 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) | ||
55 | { | ||
56 | (void)window; | ||
57 | (void)scancode; | ||
58 | (void)mods; | ||
59 | |||
60 | if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS)) | ||
61 | { | ||
62 | Game::getInstance().shouldQuit = true; | ||
63 | } | ||
64 | |||
65 | Game::getInstance().input(key, action); | ||
66 | } | ||
67 | |||
68 | void Game::execute() | ||
69 | { | ||
70 | glfwSetKeyCallback(window, key_callback); | ||
71 | |||
72 | Texture* buffer = createTexture(GAME_WIDTH, GAME_HEIGHT); | ||
73 | |||
74 | while (!(shouldQuit || glfwWindowShouldClose(window))) | ||
75 | { | ||
76 | // Should we load a new world? | ||
77 | if (nextWorld != 0) | ||
78 | { | ||
79 | delete world; | ||
80 | world = nextWorld; | ||
81 | world->player->world = world; | ||
82 | nextWorld = 0; | ||
83 | } | ||
84 | |||
85 | // Handle input | ||
86 | glfwPollEvents(); | ||
87 | |||
88 | // Tick! | ||
89 | world->tick(); | ||
90 | |||
91 | // Do rendering | ||
92 | world->render(buffer); | ||
93 | renderScreen(buffer); | ||
94 | } | ||
95 | |||
96 | destroyTexture(buffer); | ||
97 | } | ||
98 | |||
99 | void Game::input(int key, int action) | ||
100 | { | ||
101 | if (world != NULL) | ||
102 | { | ||
103 | world->input(key, action); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | void Game::loadMap(Map* map) | ||
108 | { | ||
109 | nextWorld = new World(); | ||
110 | |||
111 | nextWorld->player = world->player; | ||
112 | |||
113 | auto mapEn = std::make_shared<Entity>(nextWorld); | ||
114 | |||
115 | auto map_render = std::make_shared<MapRenderComponent>(*mapEn, map); | ||
116 | mapEn->addComponent(map_render); | ||
117 | |||
118 | auto map_collision = std::make_shared<MapCollisionComponent>(*mapEn, map); | ||
119 | mapEn->addComponent(map_collision); | ||
120 | nextWorld->bodies.push_back(map_collision.get()); | ||
121 | |||
122 | nextWorld->addEntity(mapEn); | ||
123 | nextWorld->addEntity(nextWorld->player); | ||
124 | } | ||