diff options
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 178 |
1 files changed, 0 insertions, 178 deletions
diff --git a/src/game.cpp b/src/game.cpp deleted file mode 100644 index 673c804..0000000 --- a/src/game.cpp +++ /dev/null | |||
@@ -1,178 +0,0 @@ | |||
1 | #include "game.h" | ||
2 | #include <cstdlib> | ||
3 | #include "renderer.h" | ||
4 | #include "muxer.h" | ||
5 | #include "map.h" | ||
6 | #include "components/user_movement.h" | ||
7 | #include "components/player_physics.h" | ||
8 | #include "components/player_sprite.h" | ||
9 | #include "components/map_render.h" | ||
10 | #include "components/map_collision.h" | ||
11 | #include "consts.h" | ||
12 | |||
13 | Game::Game(const char* mapfile) : world(mapfile) | ||
14 | { | ||
15 | // Set up entities | ||
16 | player = std::make_shared<Entity>(); | ||
17 | player->position = world.getStartingPosition(); | ||
18 | player->size = std::make_pair(10.0,12.0); | ||
19 | |||
20 | auto player_input = std::make_shared<UserMovementComponent>(); | ||
21 | player->addComponent(player_input); | ||
22 | |||
23 | auto player_physics = std::make_shared<PlayerPhysicsComponent>(); | ||
24 | player->addComponent(player_physics); | ||
25 | |||
26 | auto player_anim = std::make_shared<PlayerSpriteComponent>(); | ||
27 | player->addComponent(player_anim); | ||
28 | |||
29 | const Map& startingMap = world.getStartingMap(); | ||
30 | save = {&startingMap, player->position}; | ||
31 | |||
32 | loadMap(startingMap, player->position); | ||
33 | } | ||
34 | |||
35 | void key_callback(GLFWwindow* window, int key, int, int action, int) | ||
36 | { | ||
37 | Game* game = (Game*) glfwGetWindowUserPointer(window); | ||
38 | |||
39 | if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS)) | ||
40 | { | ||
41 | game->shouldQuit = true; | ||
42 | } | ||
43 | |||
44 | for (auto entity : game->entities) | ||
45 | { | ||
46 | entity->input(*game, key, action); | ||
47 | } | ||
48 | } | ||
49 | |||
50 | void Game::execute(GLFWwindow* window) | ||
51 | { | ||
52 | glfwSwapInterval(1); | ||
53 | glfwSetWindowUserPointer(window, this); | ||
54 | glfwSetKeyCallback(window, key_callback); | ||
55 | |||
56 | Texture buffer(GAME_WIDTH, GAME_HEIGHT); | ||
57 | |||
58 | double lastTime = glfwGetTime(); | ||
59 | const double dt = 0.01; | ||
60 | double accumulator = 0.0; | ||
61 | |||
62 | while (!(shouldQuit || glfwWindowShouldClose(window))) | ||
63 | { | ||
64 | double currentTime = glfwGetTime(); | ||
65 | double frameTime = currentTime - lastTime; | ||
66 | lastTime = currentTime; | ||
67 | |||
68 | // Should we load a new world? | ||
69 | if (newWorld) | ||
70 | { | ||
71 | newWorld = false; | ||
72 | entities.clear(); | ||
73 | entities = std::move(nextEntities); | ||
74 | |||
75 | player->position = nextPosition; | ||
76 | } | ||
77 | |||
78 | // Handle input | ||
79 | glfwPollEvents(); | ||
80 | |||
81 | // Tick! | ||
82 | accumulator += frameTime; | ||
83 | while (accumulator >= dt) | ||
84 | { | ||
85 | for (auto entity : entities) | ||
86 | { | ||
87 | entity->tick(*this, dt); | ||
88 | } | ||
89 | |||
90 | accumulator -= dt; | ||
91 | } | ||
92 | |||
93 | // Do any scheduled tasks | ||
94 | for (auto& task : scheduled) | ||
95 | { | ||
96 | task.first -= frameTime; | ||
97 | |||
98 | if (task.first <= 0) | ||
99 | { | ||
100 | task.second(); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | scheduled.remove_if([] (std::pair<double, std::function<void ()>> value) { return value.first <= 0; }); | ||
105 | |||
106 | // Do rendering | ||
107 | buffer.fill(buffer.entirety(), 0, 0, 0); | ||
108 | for (auto entity : entities) | ||
109 | { | ||
110 | entity->render(*this, buffer); | ||
111 | } | ||
112 | |||
113 | buffer.renderScreen(); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | void Game::loadMap(const Map& map, std::pair<double, double> position) | ||
118 | { | ||
119 | auto mapEn = std::make_shared<Entity>(); | ||
120 | |||
121 | auto map_render = std::make_shared<MapRenderComponent>(map); | ||
122 | mapEn->addComponent(map_render); | ||
123 | |||
124 | auto map_collision = std::make_shared<MapCollisionComponent>(map); | ||
125 | mapEn->addComponent(map_collision); | ||
126 | |||
127 | // Map in the back, player on top, rest of entities in between | ||
128 | nextEntities.clear(); | ||
129 | nextEntities.push_back(mapEn); | ||
130 | map.createEntities(nextEntities); | ||
131 | nextEntities.push_back(player); | ||
132 | |||
133 | newWorld = true; | ||
134 | |||
135 | currentMap = ↦ | ||
136 | nextPosition = position; | ||
137 | } | ||
138 | |||
139 | void Game::detectCollision(Entity& collider, std::pair<double, double> old_position) | ||
140 | { | ||
141 | for (auto entity : entities) | ||
142 | { | ||
143 | entity->detectCollision(*this, collider, old_position); | ||
144 | } | ||
145 | } | ||
146 | |||
147 | void Game::saveGame() | ||
148 | { | ||
149 | save = {currentMap, player->position}; | ||
150 | } | ||
151 | |||
152 | void Game::schedule(double time, std::function<void ()> callback) | ||
153 | { | ||
154 | scheduled.emplace_front(time, std::move(callback)); | ||
155 | } | ||
156 | |||
157 | void Game::playerDie() | ||
158 | { | ||
159 | player->send(*this, Message::Type::die); | ||
160 | |||
161 | playSound("res/Hit_Hurt5.wav", 0.25); | ||
162 | |||
163 | schedule(0.75, [&] () { | ||
164 | if (*currentMap != *save.map) | ||
165 | { | ||
166 | loadMap(*save.map, save.position); | ||
167 | } else { | ||
168 | player->position = save.position; | ||
169 | } | ||
170 | |||
171 | player->send(*this, Message::Type::stopDying); | ||
172 | }); | ||
173 | } | ||
174 | |||
175 | const World& Game::getWorld() const | ||
176 | { | ||
177 | return world; | ||
178 | } | ||