summary refs log tree commit diff stats
path: root/src/game.cpp
blob: e2c1b9c8ef136b174058d46094fd362d46f65e23 (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "game.h"
#include "renderer.h"
#include "components.h"
#include "muxer.h"

Game::Game()
{ 
  m.setLeftMap(&m2);
  m2.setRightMap(&m);
  
  player = std::make_shared<Entity>();
  player->position = std::make_pair(100.0,100.0);
  player->size = std::make_pair(10.0,12.0);
  
  auto player_input = std::make_shared<UserMovementComponent>();
  player->addComponent(player_input);
  
  auto player_physics = std::make_shared<PlayerPhysicsComponent>();
  player->addComponent(player_physics);
  
  auto player_anim = std::make_shared<PlayerSpriteComponent>();
  player->addComponent(player_anim);
  
  save = {&m, player->position};
  
  loadMap(m);
}

void key_callback(GLFWwindow* window, int key, int, int action, int)
{
  Game* game = (Game*) glfwGetWindowUserPointer(window);
  
  if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS))
  {
    game->shouldQuit = true;
  }
  
  for (auto entity : game->entities)
  {
    entity->input(*game, key, action);
  }
}

void Game::execute(GLFWwindow* window)
{
  glfwSwapInterval(1);
  glfwSetWindowUserPointer(window, this);
  glfwSetKeyCallback(window, key_callback);
  
  Texture buffer(GAME_WIDTH, GAME_HEIGHT);

  double lastTime = glfwGetTime();
  const double dt = 0.01;
  double accumulator = 0.0;
  
  while (!(shouldQuit || glfwWindowShouldClose(window)))
  {
    double currentTime = glfwGetTime();
    double frameTime = currentTime - lastTime;
    lastTime = currentTime;
    
    // Should we load a new world?
    if (newWorld)
    {
      newWorld = false;
      entities.clear();
      entities = std::move(nextEntities);
    }
    
    // Handle input
    glfwPollEvents();
    
    // Tick!
    accumulator += frameTime;
    while (accumulator >= dt)
    {
      for (auto entity : entities)
      {
        entity->tick(*this, dt);
      }
      
      accumulator -= dt;
    }
    
    // Do any scheduled tasks
    for (auto& task : scheduled)
    {
      task.first -= frameTime;
      
      if (task.first <= 0)
      {
        task.second();
      }
    }
    
    scheduled.remove_if([] (std::pair<double, std::function<void ()>> value) { return value.first <= 0; });
  
    // Do rendering
    buffer.fill(buffer.entirety(), 0, 0, 0);
    for (auto entity : entities)
    {
      entity->render(*this, buffer);
    }

    buffer.renderScreen();
  }
}

void Game::loadMap(const Map& map)
{
  auto mapEn = std::make_shared<Entity>();
  
  auto map_render = std::make_shared<MapRenderComponent>(map);
  mapEn->addComponent(map_render);
  
  auto map_collision = std::make_shared<MapCollisionComponent>(map);
  mapEn->addComponent(map_collision);
  
  nextEntities.clear();
  nextEntities.push_back(mapEn);
  nextEntities.push_back(player);
  
  // this is cheating but is just for testing
  if (&map == &m2)
  {
    auto saveEn = std::make_shared<Entity>();
    saveEn->position = std::make_pair(257.0, 160.0);
    saveEn->size = std::make_pair(8.0, 11.0);
    
    auto save_render = std::make_shared<StaticImageComponent>("../res/keyring.png");
    saveEn->addComponent(save_render);
    
    auto save_physics = std::make_shared<PhysicsBodyComponent>();
    saveEn->addComponent(save_physics);
    
    auto save_collide = std::make_shared<SimpleColliderComponent>([&] (Entity& collider) {
      playSound("../res/Pickup_Coin23.wav", 0.25);
      
      saveGame(map, collider.position);
    });
    saveEn->addComponent(save_collide);
    
    nextEntities.push_back(saveEn);
  }
  
  newWorld = true;
}

void Game::detectCollision(Entity& collider, std::pair<double, double> old_position)
{
  for (auto entity : entities)
  {
    entity->detectCollision(*this, collider, old_position);
  }
}

void Game::saveGame(const Map& map, std::pair<double, double> position)
{
  save = {&map, position};
}

void Game::schedule(double time, std::function<void ()>&& callback)
{
  scheduled.emplace_front(time, callback);
}

void Game::playerDie(Entity& player, const Map& curMap)
{
  Message msg(Message::Type::die);
  player.send(*this, msg);
  
  playSound("../res/Hit_Hurt5.wav", 0.25);
  
  schedule(0.75, [&] () {
    if (&curMap != save.map)
    {
      loadMap(*(save.map));
    }
    
    player.position = save.position;
    
    Message msg2(Message::Type::stopDying);
    player.send(*this, msg2);
  });
}