networking addPlayer
This commit is contained in:
parent
0e8e41e4fb
commit
ef38da703e
8 changed files with 50 additions and 37 deletions
|
@ -15,11 +15,7 @@ Game::Game()
|
|||
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);
|
||||
m_state->addPlayer();
|
||||
}
|
||||
|
||||
bool Game::cycle(float dt)
|
||||
|
|
|
@ -11,7 +11,7 @@ public:
|
|||
bool cycle(float dt);
|
||||
|
||||
// for rendering
|
||||
const game::State *state() const { return m_state; }
|
||||
game::State *state() const { return m_state; }
|
||||
|
||||
private:
|
||||
game::State *m_state;
|
||||
|
|
|
@ -27,30 +27,30 @@ protected:
|
|||
//if (!once) {
|
||||
// once = true;
|
||||
// for (int i=0; i<1000; i++) {
|
||||
// m_game.cycle(time);
|
||||
// m_game->cycle(time);
|
||||
// }
|
||||
//}
|
||||
|
||||
if (!m_game.cycle(static_cast<float>(time/1000000000.0))) {
|
||||
if (!m_game->cycle(static_cast<float>(time/1000000000.0))) {
|
||||
std::cout<<"stopping the game..." << std::endl;
|
||||
stop();
|
||||
}
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
for (const game::Planet *planet : m_game.state()->planets) {
|
||||
for (const game::Planet *planet : m_game->state()->planets) {
|
||||
drawPlanet(planet->position, planet->radius);
|
||||
}
|
||||
|
||||
for (const game::Trace *trace : m_game.state()->traces) {
|
||||
for (const game::Trace *trace : m_game->state()->traces) {
|
||||
drawTrace(trace);
|
||||
}
|
||||
|
||||
for (const game::Ship *ship : m_game.state()->ships) {
|
||||
for (const game::Ship *ship : m_game->state()->ships) {
|
||||
drawShip(ship->position);
|
||||
}
|
||||
|
||||
for (const game::Player *player : m_game.state()->players) {
|
||||
for (const game::Player *player : m_game->state()->players) {
|
||||
for (const game::Missile *missile : player->missiles) {
|
||||
drawMissile(missile->position);
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ protected:
|
|||
//std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl;
|
||||
glm::vec3 color = glm::vec3(0.2, 1.0, 0.3);
|
||||
|
||||
float radius = m_game.state()->shipRadius();
|
||||
float radius = m_game->state()->shipRadius();
|
||||
|
||||
m_renderer.drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z, 12);
|
||||
}
|
||||
|
@ -93,11 +93,11 @@ protected:
|
|||
}
|
||||
|
||||
public:
|
||||
GameWindow(unsigned int width, unsigned int height)
|
||||
: endofthejedi::GLWindow(width, height) {}
|
||||
GameWindow(unsigned int width, unsigned int height, Game* game)
|
||||
: endofthejedi::GLWindow(width, height) { m_game = game; }
|
||||
|
||||
private:
|
||||
Game m_game;
|
||||
Game *m_game;
|
||||
endofthejedi::Renderer m_renderer;
|
||||
};
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ int main(int argc, char *argv[]) {
|
|||
// {"add", no_argument, 0, 'a'},
|
||||
// {"append", no_argument, 0, 'b'},
|
||||
// {"delete", required_argument, 0, 'd'},
|
||||
// {"create", required_argument, 0, 'c'},
|
||||
{"port", required_argument, 0, 'p'},
|
||||
{"fps", no_argument, 0, 'f'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
@ -39,7 +39,7 @@ int main(int argc, char *argv[]) {
|
|||
int option_index = 0;
|
||||
|
||||
while(1){
|
||||
c = getopt_long (argc, argv, "abc:d:f",
|
||||
c = getopt_long (argc, argv, "p:f",
|
||||
long_options, &option_index);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
@ -49,10 +49,10 @@ int main(int argc, char *argv[]) {
|
|||
case 'f':
|
||||
SET_FLAG(SHOW_FPS,true);
|
||||
break;
|
||||
/*case 'c':
|
||||
cvalue = optarg;
|
||||
break;
|
||||
*/
|
||||
case 'p':
|
||||
strcpy(port,optarg);
|
||||
break;
|
||||
|
||||
|
||||
case '?':
|
||||
/* getopt_long already printed an error message. */
|
||||
|
@ -63,10 +63,12 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
}
|
||||
|
||||
asio::io_service io_service;
|
||||
Server s(io_service, atoi(port) );
|
||||
Game game;
|
||||
|
||||
GameWindow window(500, 500);
|
||||
asio::io_service io_service;
|
||||
Server s(io_service, game.state(), atoi(port) );
|
||||
|
||||
GameWindow window(500, 500, &game);
|
||||
window.set_maxfps(60.0);
|
||||
|
||||
while(window.running()){
|
||||
|
|
|
@ -12,10 +12,11 @@ using asio::ip::tcp;
|
|||
class Server
|
||||
{
|
||||
public:
|
||||
Server(asio::io_service& io_service, short port)
|
||||
Server(asio::io_service& io_service,game::State* s, short port)
|
||||
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
|
||||
socket_(io_service)
|
||||
{
|
||||
state=s;
|
||||
do_accept();
|
||||
}
|
||||
|
||||
|
@ -27,7 +28,7 @@ private:
|
|||
{
|
||||
if (!ec)
|
||||
{
|
||||
std::make_shared<Session>(std::move(socket_))->start();
|
||||
std::make_shared<Session>(std::move(socket_),state)->start();
|
||||
}
|
||||
|
||||
do_accept();
|
||||
|
@ -36,4 +37,5 @@ private:
|
|||
|
||||
tcp::acceptor acceptor_;
|
||||
tcp::socket socket_;
|
||||
game::State* state;
|
||||
};
|
||||
|
|
|
@ -11,16 +11,22 @@ class Session
|
|||
: public std::enable_shared_from_this<Session>
|
||||
{
|
||||
public:
|
||||
Session(tcp::socket socket)
|
||||
: socket_(std::move(socket))
|
||||
Session(tcp::socket socket, game::State* state)
|
||||
: m_socket(std::move(socket))
|
||||
{
|
||||
m_state = state;
|
||||
char c[]="> ";
|
||||
strcpy(m_snd_data, c);
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(m_started) return;
|
||||
m_started=true;
|
||||
m_id = m_state->addPlayer();
|
||||
auto self(shared_from_this());
|
||||
char hello[] = "\nUse \"n name\" to change name, \"v velocity\" to change velocity, \"c\" to clear past shots or \"q\" to close the connection.\nEverything else is interpreted as a shooting angle.\n\n>";
|
||||
asio::async_write(socket_, asio::buffer(hello, strlen(hello)),
|
||||
char hello[] = "\nUse \"n name\" to change name, \"v velocity\" to change velocity, \"c\" to clear past shots or \"q\" to close the connection.\nEverything else is interpreted as a shooting angle.\n\n> ";
|
||||
asio::async_write(m_socket, asio::buffer(hello, strlen(hello)),
|
||||
[this, self](std::error_code ec, std::size_t /*length*/)
|
||||
{
|
||||
if (!ec)
|
||||
|
@ -34,7 +40,7 @@ private:
|
|||
void do_read()
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
socket_.async_read_some(asio::buffer(data_, max_length),
|
||||
m_socket.async_read_some(asio::buffer(m_rcv_data, max_length),
|
||||
[this, self](std::error_code ec, std::size_t length)
|
||||
{
|
||||
if (!ec)
|
||||
|
@ -47,7 +53,7 @@ private:
|
|||
void do_write(std::size_t length)
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
asio::async_write(socket_, asio::buffer(data_, length),
|
||||
asio::async_write(m_socket, asio::buffer(m_snd_data, length),
|
||||
[this, self](std::error_code ec, std::size_t /*length*/)
|
||||
{
|
||||
if (!ec)
|
||||
|
@ -57,7 +63,11 @@ private:
|
|||
});
|
||||
}
|
||||
|
||||
tcp::socket socket_;
|
||||
tcp::socket m_socket;
|
||||
enum { max_length = 1024 };
|
||||
char data_[max_length];
|
||||
char m_snd_data[max_length];
|
||||
char m_rcv_data[max_length];
|
||||
game::State* m_state;
|
||||
bool m_started = false;
|
||||
int m_id;
|
||||
};
|
||||
|
|
|
@ -62,10 +62,12 @@ namespace game {
|
|||
return true;
|
||||
}
|
||||
|
||||
void State::addPlayer(int playerId)
|
||||
int State::addPlayer()
|
||||
{
|
||||
int playerId = m_nextId++;
|
||||
Player *player = new Player(playerId);
|
||||
players.push_back(player);
|
||||
return playerId;
|
||||
}
|
||||
|
||||
void State::playerLeft(int playerId)
|
||||
|
|
|
@ -36,7 +36,7 @@ namespace game {
|
|||
|
||||
// The upper layer (network/renderer) calling these three functions
|
||||
// should keep id's unique and give one (network) input an id.
|
||||
void addPlayer(int playerId);
|
||||
int addPlayer();
|
||||
void playerLeft(int playerId);
|
||||
void commandForPlayer(int playerId, Command *cmd);
|
||||
|
||||
|
@ -89,5 +89,6 @@ namespace game {
|
|||
float m_playerRespawnTime;
|
||||
float m_shipRadius;
|
||||
float m_defaultEnergy;
|
||||
int m_nextId=0;
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue