reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
#include "Engine.hh"
#include "SinglePlayer.hh"
#include "Network/Reactor.hh"
#include "Config.hh"
#include <iostream>
Engine::Engine (const std::string resource_xml_path) :
terrain(NULL), game_state(NULL), graphics_config(NULL), graphics(NULL), net_server(NULL), net_client_connect(NULL),
is_running(true), resources(resource_xml_path)
{
}
GameState& Engine::setupGame (Terrain *terrain) {
// ensure this isn't called in inappropriate ways
assert(!net_server);
// remember the terrain
this->terrain = terrain;
// create the GameState
game_state = new GameState(*terrain);
// start graphics?
if (graphics_config)
startGraphics();
return *game_state;
}
GameState& Engine::setupGame (void) {
// proxy off to setupGame(Terrain *)
return setupGame(new Terrain(TERRAIN_WIDTH, TERRAIN_HEIGHT, TERRAIN_RANDOM_SEED));
}
void Engine::setupGraphics (const GraphicsConfiguration &config) {
// store config
graphics_config = &config;
// start already?
if (game_state)
startGraphics();
}
void Engine::startGraphics (void) {
// check state
assert(game_state && graphics_config);
// create the graphics
graphics = new Graphics(*this, *game_state, *graphics_config);
}
void Engine::setupNetworkServer (const std::string &listen_port) {
NetworkEndpoint listen_addr(listen_port);
// setup default game
setupGame();
// create the server
net_server = new NetworkServer(*game_state, listen_addr);
}
void Engine::setupNetworkClient (const std::string &connect_host, const std::string &connect_port) {
// connect_to
NetworkEndpoint connect_addr(connect_host, connect_port);
// begin connecting, the client will callback us with setupGame once it's connected
net_client_connect = new NetworkClientConnect(*this, connect_addr);
}
void Engine::setupSinglePlayer (void) {
// setup default game
setupGame();
// create player directly
LocalPlayer* lp = new SinglePlayer(*game_state);
// add to gamestate
game_state->setLocalPlayer(lp);
}
void Engine::stop (void) {
is_running = false;
}
void Engine::run (void) {
// our NetworkReactor
NetworkReactor *reactor = NetworkReactor::current;
// timeout info
timeval timeout;
while (is_running) {
// this does.... magical things
CL_System::keep_alive();
// setup our timeout to ENGINE_TIMEOUT_MS
timeout.tv_sec = 0;
timeout.tv_usec = ENGINE_TIMEOUT_MS * 1000;
/*
* Thursday came and went, I re-wrote clan-event.
*
* We use the NetworkReactor for sleeping, as it handles it effeciently even if we're not using network.
*/
reactor->poll(&timeout);
}
}
CL_ResourceManager* Engine::getResourceManager (void) {
return &resources;
}
Logger Engine::log (enum LogLevel level, const char *type) {
return Logger(level <= WARN ? std::cerr : std::cout, level, type);
}