server files

This commit is contained in:
j3d1 2016-09-27 20:51:03 +02:00
parent f8798d7c0c
commit fe6779da7e
2 changed files with 88 additions and 0 deletions

67
game/server.cpp Normal file
View file

@ -0,0 +1,67 @@
#include "server.hpp"
#include "state/commands.hpp"
#include "util.hpp"
#include <cmath>
Game::Game()
{
// 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);
//m_state->addPlayer(2);
}
bool Game::cycle(float dt)
{
static float acc = 0.0;
acc += dt;
float spawnInterval = 0.1;
while(acc > spawnInterval) {
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) {
//std::cout<<"time to big: " << dt << std::endl;
dt = m_time_step;
}
//std::cout<<"adding dt: " << dt << std::endl;
m_time_for_next_step += dt;
int steps = 0;
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++;
}
//std::cout << m_time_for_next_step << " s remaining time, " << steps << " steps taken." << std::endl;
return true;
#else
(void) dt;
m_state->advance(dt);
return true;
#endif
}

21
game/server.hpp Normal file
View file

@ -0,0 +1,21 @@
#pragma once
#include "state/state.hpp"
class Server {
public:
Server();
// main method of the game. run this regulary
// return false if want to exit.
// bool cycle(float dt);
// for rendering
// const game::State *state() const { return m_state; }
private:
game::State *m_state;
float m_time_for_next_step;
float m_time_step;
};