more oo design

This commit is contained in:
gmueller 2011-01-18 22:50:24 +01:00
parent 1d0dd12b2e
commit b55b687cd9
4 changed files with 47 additions and 44 deletions

View File

@ -62,7 +62,7 @@ void setup_opengl() {
glfwSwapInterval(0); glfwSwapInterval(0);
} }
void draw_team(team_t *team) { void draw_team(Team *team) {
size_t i = 0; size_t i = 0;
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color);
glMatrixMode( GL_MODELVIEW); glMatrixMode( GL_MODELVIEW);
@ -372,7 +372,7 @@ void Client::initialize(Arguments &arg) {
gluQuadricTexture(quadratic, GL_TRUE); gluQuadricTexture(quadratic, GL_TRUE);
if (isMaster()) { if (isMaster()) {
team_t *team = game.getTeam(0); Team *team = game.getTeam(0);
game.local_player = game.spawn_player(team); game.local_player = game.spawn_player(team);
} }
@ -448,8 +448,8 @@ void Client::update() {
glEnable( GL_LIGHTING); glEnable( GL_LIGHTING);
glEnable( GL_CULL_FACE); glEnable( GL_CULL_FACE);
glDisable( GL_TEXTURE_2D); glDisable( GL_TEXTURE_2D);
for (size_t i = 0; i < GAME_TEAM_COUNT; i++) for (size_t i = 0; i < game.teams.size(); i++)
draw_team(&game.team[i]); draw_team(&game.teams[i]);
for (size_t i = 0; i < GAME_PLAYER_COUNT; i++) for (size_t i = 0; i < GAME_PLAYER_COUNT; i++)
draw_player(&game.player[i]); draw_player(&game.player[i]);
@ -476,12 +476,12 @@ void Client::update() {
} }
for (size_t i = 0; i < GAME_TEAM_COUNT; i++) { for (size_t i = 0; i < game.teams.size(); i++) {
std::stringstream sstr; std::stringstream sstr;
if (game.local_player && &game.team[i] == game.local_player->team) { if (game.local_player && &game.teams[i] == game.local_player->team) {
sstr << "Team " << i << " (yours) : " << game.team[i].points; sstr << "Team " << i << " (yours) : " << game.teams[i].points;
} else { } else {
sstr << "Team " << i << " (other) : " << game.team[i].points; sstr << "Team " << i << " (other) : " << game.teams[i].points;
} }
oglf_print(&font, 10, 25 * fy++, sstr.str().c_str()); oglf_print(&font, 10, 25 * fy++, sstr.str().c_str());
} }

View File

@ -9,7 +9,7 @@ double rand2() {
return 2.0 * (u - 0.5); return 2.0 * (u - 0.5);
} }
void reset_team(team_t *team) { void reset_team(Team *team) {
team->points = 0; team->points = 0;
} }
@ -35,7 +35,7 @@ void reset_point(point_t *point) {
point->z = 500.0 * rand2(); point->z = 500.0 * rand2();
} }
void setup_team(team_t *team, uint8_t id) { void setup_team(Team *team, uint8_t id) {
team->id = id; team->id = id;
team->color[0] = .5f + .5f * (id & (1 << 0)); team->color[0] = .5f + .5f * (id & (1 << 0));
team->color[1] = .5f + .5f * (id & (1 << 1)); team->color[1] = .5f + .5f * (id & (1 << 1));
@ -79,8 +79,9 @@ void setup_bomb(bomb_t *bomb) {
void Game::setup() { void Game::setup() {
size_t i; size_t i;
for (i = 0; i < GAME_TEAM_COUNT; i++) teams.resize(2);
setup_team(&team[i], i); for (i = 0; i < teams.size(); i++)
setup_team(&teams[i], i);
for (i = 0; i < GAME_BOMB_COUNT; i++) for (i = 0; i < GAME_BOMB_COUNT; i++)
setup_bomb(&bomb[i]); setup_bomb(&bomb[i]);
@ -101,8 +102,8 @@ void Game::setup() {
void Game::reset() { void Game::reset() {
size_t i; size_t i;
for (i = 0; i < GAME_TEAM_COUNT; i++) for (i = 0; i < teams.size(); i++)
reset_team(&team[i]); reset_team(&teams[i]);
for (i = 0; i < GAME_BOMB_COUNT; i++) for (i = 0; i < GAME_BOMB_COUNT; i++)
setup_bomb(&bomb[i]); setup_bomb(&bomb[i]);
@ -123,7 +124,7 @@ player_t *Game::getFreePlayer() {
return NULL; return NULL;
} }
player_t *Game::spawn_player(team_t *team) { player_t *Game::spawn_player(Team *team) {
player_t *player = getFreePlayer(); player_t *player = getFreePlayer();
player->team = team; player->team = team;
player->status = 1; player->status = 1;
@ -132,7 +133,7 @@ player_t *Game::spawn_player(team_t *team) {
return player; return player;
} }
player_t *Game::spawn_player_id(team_t *team, uint16_t id) { player_t *Game::spawn_player_id(Team *team, uint16_t id) {
player_t *player = getFreePlayer(); player_t *player = getFreePlayer();
player->team = team; player->team = team;
player->id = id; player->id = id;
@ -309,7 +310,7 @@ void Game::update(double dt) {
} }
} }
size_t Game::active_team_players(team_t *team) { size_t Game::active_team_players(Team *team) {
size_t i, count = 0; size_t i, count = 0;
for (i = 0; i < GAME_PLAYER_COUNT; i++) { for (i = 0; i < GAME_PLAYER_COUNT; i++) {
if (player[i].status == 0) if (player[i].status == 0)
@ -320,24 +321,24 @@ size_t Game::active_team_players(team_t *team) {
return count; return count;
} }
team_t *Game::team_with_least_players() { Team *Game::team_with_least_players() {
size_t i, count = -1; size_t i, count = -1;
team_t *t = NULL; Team *team = NULL;
for (i = 0; i < GAME_TEAM_COUNT; i++) { for (i = 0; i < teams.size(); i++) {
size_t players = active_team_players(&team[i]); size_t players = active_team_players(&teams[i]);
if (players < count) { if (players < count) {
count = players; count = players;
t = &team[i]; team = &teams[i];
} }
} }
return t; return team;
} }
team_t *Game::getTeam(uint16_t id) { Team *Game::getTeam(uint16_t id) {
size_t i; size_t i;
for (i = 0; i < GAME_TEAM_COUNT; i++) { for (i = 0; i < teams.size(); i++) {
if (team[i].id == id) { if (teams[i].id == id) {
return &team[i]; return &teams[i];
} }
} }
return NULL; return NULL;

View File

@ -3,11 +3,12 @@
#include "common.h" #include "common.h"
typedef struct team_t team_t; #include <vector>
typedef struct Team Team;
typedef struct player_t player_t; typedef struct player_t player_t;
class Game; class Game;
#define GAME_TEAM_COUNT 2
#define GAME_PLAYER_COUNT 64 #define GAME_PLAYER_COUNT 64
#define GAME_BOMB_COUNT (GAME_PLAYER_COUNT * 5) #define GAME_BOMB_COUNT (GAME_PLAYER_COUNT * 5)
#define GAME_POINT_COUNT 50 #define GAME_POINT_COUNT 50
@ -18,7 +19,7 @@ struct player_t {
double x, y, z; double x, y, z;
double rx, ry, rz, rw; double rx, ry, rz, rw;
double vx, vy, vz; double vx, vy, vz;
team_t *team; Team *team;
uint16_t points; uint16_t points;
}; };
@ -34,7 +35,8 @@ struct point_t {
double x, y, z; double x, y, z;
}; };
struct team_t { class Team {
public:
uint16_t id; uint16_t id;
double x, y, z; double x, y, z;
float color[4]; float color[4];
@ -44,7 +46,7 @@ struct team_t {
class Game { class Game {
public: public:
team_t team[GAME_TEAM_COUNT]; std::vector<Team> teams;
player_t player[GAME_PLAYER_COUNT]; player_t player[GAME_PLAYER_COUNT];
bomb_t bomb[GAME_BOMB_COUNT]; bomb_t bomb[GAME_BOMB_COUNT];
point_t point[GAME_POINT_COUNT]; point_t point[GAME_POINT_COUNT];
@ -57,10 +59,10 @@ public:
void setup(); void setup();
void reset(); void reset();
team_t *team_with_least_players(); Team *team_with_least_players();
player_t *spawn_player(team_t *team); player_t *spawn_player(Team *team);
player_t *spawn_player_id(team_t *team, uint16_t id); player_t *spawn_player_id(Team *team, uint16_t id);
team_t *getTeam(uint16_t id); Team *getTeam(uint16_t id);
player_t *getPlayer(uint16_t id); player_t *getPlayer(uint16_t id);
bomb_t *getBomb(uint16_t index); bomb_t *getBomb(uint16_t index);
void update_players(double dt); void update_players(double dt);
@ -75,7 +77,7 @@ public:
player_t *getFreePlayer(); player_t *getFreePlayer();
void explode_bomb(bomb_t *bomb); void explode_bomb(bomb_t *bomb);
void update_point(point_t *point); void update_point(point_t *point);
size_t active_team_players(team_t *team); size_t active_team_players(Team *team);
}; };
#endif #endif

View File

@ -90,12 +90,12 @@ void Network::sendGameUpdates() {
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0); ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0);
enet_host_broadcast(host, 0, packet); enet_host_broadcast(host, 0, packet);
} }
for (i = 0; i < GAME_TEAM_COUNT; i++) { for (i = 0; i < game->teams.size(); i++) {
team_update_message_t msg; team_update_message_t msg;
msg.msg_id = MESSAGE_TEAM_UPDATE; msg.msg_id = MESSAGE_TEAM_UPDATE;
msg.team_id = game->team[i].id; msg.team_id = game->teams[i].id;
msg.points = game->team[i].points; msg.points = game->teams[i].points;
msg.wins = game->team[i].wins; msg.wins = game->teams[i].wins;
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0); ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0);
enet_host_broadcast(host, 0, packet); enet_host_broadcast(host, 0, packet);
} }
@ -108,7 +108,7 @@ void Network::dispatch(enet_uint8 *data, size_t length) {
switch (msg->msg_id) { switch (msg->msg_id) {
case MESSAGE_PLAYER_SPAWN: { case MESSAGE_PLAYER_SPAWN: {
player_spawn_message_t *sm = (player_spawn_message_t *) data; player_spawn_message_t *sm = (player_spawn_message_t *) data;
team_t *team = game->getTeam(sm->team_id); Team *team = game->getTeam(sm->team_id);
player_t *player = game->spawn_player_id(team, sm->player_id); player_t *player = game->spawn_player_id(team, sm->player_id);
break; break;
} }
@ -195,7 +195,7 @@ void Network::dispatch(enet_uint8 *data, size_t length) {
} }
case MESSAGE_TEAM_UPDATE: { case MESSAGE_TEAM_UPDATE: {
team_update_message_t *m = (team_update_message_t *) data; team_update_message_t *m = (team_update_message_t *) data;
team_t *team = game->getTeam(m->team_id); Team *team = game->getTeam(m->team_id);
if (team == NULL) if (team == NULL)
return; return;
team->points = m->points; team->points = m->points;
@ -249,7 +249,7 @@ void Network::service(uint32_t timeout) {
enet_peer_send(event.peer, 0, packet); enet_peer_send(event.peer, 0, packet);
} }
team_t *team = game->team_with_least_players(); Team *team = game->team_with_least_players();
player_t *player = game->spawn_player(team); player_t *player = game->spawn_player(team);
event.peer->data = player; event.peer->data = player;