2016-09-27 16:23:58 +00:00
|
|
|
#include "game.hpp"
|
|
|
|
|
|
|
|
#include "state/commands.hpp"
|
|
|
|
|
|
|
|
#include "util.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
Game::Game() {
|
2016-09-27 16:23:58 +00:00
|
|
|
// advance simulation with 100 Hz
|
|
|
|
m_time_step = 1.0 / 100.0;
|
|
|
|
m_time_for_next_step = 0.0;
|
|
|
|
|
|
|
|
m_state = new game::State();
|
|
|
|
m_state->init();
|
|
|
|
|
|
|
|
m_state->addPlayer(0);
|
|
|
|
m_state->addPlayer(1);
|
|
|
|
m_state->addPlayer(2);
|
|
|
|
m_state->addPlayer(3);
|
2016-09-27 18:56:17 +00:00
|
|
|
// m_state->addPlayer(2);
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
bool Game::cycle(float dt) {
|
2016-09-27 16:23:58 +00:00
|
|
|
static float acc = 0.0;
|
|
|
|
acc += dt;
|
|
|
|
|
|
|
|
float spawnInterval = 0.1;
|
2016-09-27 18:56:17 +00:00
|
|
|
while (acc > spawnInterval) {
|
2016-09-27 16:23:58 +00:00
|
|
|
acc -= spawnInterval;
|
|
|
|
|
|
|
|
float a = 2.0 * M_PI * util::randf_0_1();
|
|
|
|
float speed = 0.002;
|
|
|
|
m_state->players[0]->addCommand(new game::ShootCommand(a, speed));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
if (dt >= 10.0) {
|
2016-09-27 18:56:17 +00:00
|
|
|
// std::cout<<"time to big: " << dt << std::endl;
|
2016-09-27 16:23:58 +00:00
|
|
|
dt = m_time_step;
|
|
|
|
}
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
// std::cout<<"adding dt: " << dt << std::endl;
|
2016-09-27 16:23:58 +00:00
|
|
|
m_time_for_next_step += dt;
|
|
|
|
|
|
|
|
int steps = 0;
|
2016-09-27 18:56:17 +00:00
|
|
|
while (m_time_for_next_step >= m_time_step) {
|
|
|
|
// std::cout<<"time now: " << m_time_for_next_step << std::endl;
|
2016-09-27 16:23:58 +00:00
|
|
|
m_time_for_next_step -= m_time_step;
|
|
|
|
|
|
|
|
m_state->advance(m_time_step);
|
|
|
|
steps++;
|
|
|
|
}
|
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
// std::cout << m_time_for_next_step << " s remaining time, " << steps << "
|
|
|
|
// steps taken." << std::endl;
|
2016-09-27 16:23:58 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
#else
|
2016-09-27 18:56:17 +00:00
|
|
|
(void)dt;
|
2016-09-27 16:23:58 +00:00
|
|
|
|
|
|
|
m_state->advance(dt);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|