summary refs log tree commit diff stats
path: root/src/game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp171
1 files changed, 34 insertions, 137 deletions
diff --git a/src/game.cpp b/src/game.cpp index 673c804..b3fa9a8 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -1,60 +1,47 @@
1#include "game.h" 1#include "game.h"
2#include <cstdlib> 2#include "components/animatable.h"
3#include "renderer.h" 3#include "components/transformable.h"
4#include "muxer.h" 4#include "components/controllable.h"
5#include "map.h" 5#include "components/droppable.h"
6#include "components/user_movement.h" 6#include "components/ponderable.h"
7#include "components/player_physics.h" 7#include "systems/rendering.h"
8#include "components/player_sprite.h" 8#include "systems/controlling.h"
9#include "components/map_render.h" 9#include "systems/pondering.h"
10#include "components/map_collision.h"
11#include "consts.h"
12
13Game::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 10
35void key_callback(GLFWwindow* window, int key, int, int action, int) 11void key_callback(GLFWwindow* window, int key, int, int action, int)
36{ 12{
37 Game* game = (Game*) glfwGetWindowUserPointer(window); 13 Game& game = *((Game*) glfwGetWindowUserPointer(window));
38 14
39 if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS)) 15 if ((action == GLFW_PRESS) && (key == GLFW_KEY_ESCAPE))
40 { 16 {
41 game->shouldQuit = true; 17 game.shouldQuit = true;
18
19 return;
42 } 20 }
43 21
44 for (auto entity : game->entities) 22 game.systemManager.getSystem<ControllingSystem>().input(key, action);
45 {
46 entity->input(*game, key, action);
47 }
48} 23}
49 24
50void Game::execute(GLFWwindow* window) 25Game::Game(GLFWwindow* window) : window(window)
51{ 26{
27 systemManager.emplaceSystem<ControllingSystem>(*this);
28 systemManager.emplaceSystem<RenderingSystem>(*this);
29 systemManager.emplaceSystem<PonderingSystem>(*this);
30
31 int player = entityManager.emplaceEntity();
32 entityManager.emplaceComponent<AnimatableComponent>(player, "res/Starla.png", 10, 12, 6);
33 entityManager.emplaceComponent<TransformableComponent>(player, 203, 44, 10, 12);
34 entityManager.emplaceComponent<DroppableComponent>(player);
35 entityManager.emplaceComponent<PonderableComponent>(player);
36 entityManager.emplaceComponent<ControllableComponent>(player);
37
52 glfwSwapInterval(1); 38 glfwSwapInterval(1);
53 glfwSetWindowUserPointer(window, this); 39 glfwSetWindowUserPointer(window, this);
54 glfwSetKeyCallback(window, key_callback); 40 glfwSetKeyCallback(window, key_callback);
55 41}
56 Texture buffer(GAME_WIDTH, GAME_HEIGHT);
57 42
43void Game::execute()
44{
58 double lastTime = glfwGetTime(); 45 double lastTime = glfwGetTime();
59 const double dt = 0.01; 46 const double dt = 0.01;
60 double accumulator = 0.0; 47 double accumulator = 0.0;
@@ -65,114 +52,24 @@ void Game::execute(GLFWwindow* window)
65 double frameTime = currentTime - lastTime; 52 double frameTime = currentTime - lastTime;
66 lastTime = currentTime; 53 lastTime = currentTime;
67 54
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(); 55 glfwPollEvents();
80 56
81 // Tick!
82 accumulator += frameTime; 57 accumulator += frameTime;
83 while (accumulator >= dt) 58 while (accumulator >= dt)
84 { 59 {
85 for (auto entity : entities) 60 systemManager.getSystem<ControllingSystem>().tick(dt);
86 { 61 systemManager.getSystem<PonderingSystem>().tick(dt);
87 entity->tick(*this, dt);
88 }
89 62
90 accumulator -= dt; 63 accumulator -= dt;
91 } 64 }
92 65
93 // Do any scheduled tasks 66 systemManager.getSystem<RenderingSystem>().tick(frameTime);
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
117void 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 = &map;
136 nextPosition = position;
137}
138
139void 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 } 67 }
145} 68}
146 69
147void Game::saveGame() 70EntityManager& Game::getEntityManager()
148{ 71{
149 save = {currentMap, player->position}; 72 return entityManager;
150} 73}
151 74
152void Game::schedule(double time, std::function<void ()> callback)
153{
154 scheduled.emplace_front(time, std::move(callback));
155}
156 75
157void 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
175const World& Game::getWorld() const
176{
177 return world;
178}