KlassischeKeplerKriege/game/game.cpp

77 lines
1.8 KiB
C++
Raw Normal View History

#include "game.hpp"
#include "state/commands.hpp"
#include "util.hpp"
#include <cmath>
Game::Game()
{
2016-09-27 21:16:25 +00:00
// advance simulation in fixed steps with 100 Hz
2016-09-27 20:35:16 +00:00
m_time_step = 1.0 / 100.0;
m_time_for_next_step = 0.0;
m_state = new game::State();
m_state->init();
}
bool Game::cycle(float dt)
{
2016-09-28 03:52:23 +00:00
#if 0
2016-09-27 21:16:25 +00:00
// XXX the following is just testing code to do things
2016-09-27 21:07:11 +00:00
static float total = 0.0;
static float acc = 0.0;
2016-09-27 21:07:11 +00:00
//if (total == 0.0) {
// float speed = 0.005;
// m_state->players[0]->addCommand(new game::SetSpeedCommand(speed));
// for (int i=0; i<100; i++) {
// float a = 2.0 * M_PI * util::randf_0_1();
// m_state->players[0]->addCommand(new game::ShootCommand(a));
// }
//}
2016-09-28 03:52:23 +00:00
m_state->addPlayer();
2016-09-27 21:07:11 +00:00
2016-09-27 21:16:25 +00:00
acc += dt;
2016-09-27 21:07:11 +00:00
total += dt;
float spawnInterval = 0.1;
2016-09-27 18:56:17 +00:00
while (acc > spawnInterval) {
acc -= spawnInterval;
2016-09-27 21:16:25 +00:00
float angle = 2.0 * M_PI * util::randf_0_1();
float speed = 0.005;
2016-09-27 20:35:16 +00:00
m_state->players[0]->addCommand(new game::SetSpeedCommand(speed));
2016-09-27 21:16:25 +00:00
m_state->players[0]->addCommand(new game::ShootCommand(angle));
2016-09-27 21:07:11 +00:00
//static bool done = false;
//if (total >= 10.0 && !done) {
// done = true;
// m_state->players[0]->addCommand(new game::ClearTracesCommand());
//}
}
2016-09-27 21:16:25 +00:00
#endif
2016-09-27 18:56:17 +00:00
// std::cout<<"adding dt: " << dt << std::endl;
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;
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;
return true;
}