some gameplay fixes

This commit is contained in:
gmueller 2011-01-12 15:28:44 +01:00
parent 90f4b64af8
commit e08d376864
2 changed files with 137 additions and 81 deletions

View File

@ -71,11 +71,21 @@ void game_reset_point(point_t *point) {
point->z = 0.0; point->z = 0.0;
} }
void game_reset_bomb(bomb_t *bomb) {
bomb->status = 0;
bomb->x = 0.0;
bomb->y = 0.0;
bomb->z = 0.0;
}
void game_setup(game_t *game, int master, void(*explosion_callback)(double x, void game_setup(game_t *game, int master, void(*explosion_callback)(double x,
double y, double z)) { double y, double z)) {
size_t i; size_t i;
for (i = 0; i < GAME_TEAM_COUNT; i++) for (i = 0; i < GAME_TEAM_COUNT; i++)
game_setup_team(&game->team[i], i); game_setup_team(&game->team[i], i);
for (i = 0; i < GAME_BOMB_COUNT; i++)
game_reset_bomb(&game->bomb[i]);
if (master) { if (master) {
for (i = 0; i < GAME_PLAYER_COUNT; i++) for (i = 0; i < GAME_PLAYER_COUNT; i++)
@ -84,8 +94,8 @@ void game_setup(game_t *game, int master, void(*explosion_callback)(double x,
for (i = 0; i < GAME_POINT_COUNT; i++) for (i = 0; i < GAME_POINT_COUNT; i++)
game_setup_point(&game->point[i]); game_setup_point(&game->point[i]);
} else { } else {
// for (i = 0; i < GAME_TEAM_COUNT; i++) // for (i = 0; i < GAME_TEAM_COUNT; i++)
// game_reset_team(&game->team[i], i); // game_reset_team(&game->team[i], i);
for (i = 0; i < GAME_PLAYER_COUNT; i++) for (i = 0; i < GAME_PLAYER_COUNT; i++)
game_reset_player(&game->player[i]); game_reset_player(&game->player[i]);
@ -145,12 +155,12 @@ void game_update_players(game_t *game, double dt) {
player_t *player = &game->player[i]; player_t *player = &game->player[i];
if (player->status == 0) if (player->status == 0)
return; return;
if (player->vx > 500.0) if (player->vx > 1000.0)
player->vx = 500.0; player->vx = 1000.0;
if (player->vy > 500.0) if (player->vy > 1000.0)
player->vy = 500.0; player->vy = 1000.0;
if (player->vz > 500.0) if (player->vz > 1000.0)
player->vz = 500.0; player->vz = 1000.0;
player->x += player->vx * dt; player->x += player->vx * dt;
player->y += player->vy * dt; player->y += player->vy * dt;
player->z += player->vz * dt; player->z += player->vz * dt;
@ -161,6 +171,30 @@ void game_update_players(game_t *game, double dt) {
player->team->points += player->points; player->team->points += player->points;
player->points = 0; player->points = 0;
} }
if (player->x < (-1000 + 10)) {
player->x = (-1000 + 10);
player->vx *= -1;
} else if (player->x > (5000 - 10)) {
player->x = (5000 - 10);
player->vx *= -1;
}
if (player->y < (-3000 + 10)) {
player->y = (-3000 + 10);
player->vy *= -1;
} else if (player->y > (3000 - 10)) {
player->y = (3000 - 10);
player->vy *= -1;
}
if (player->z < (-3000 + 10)) {
player->z = (-3000 + 10);
player->vz *= -1;
} else if (player->z > (3000 - 10)) {
player->z = (3000 - 10);
player->vz *= -1;
}
} }
} }
@ -172,7 +206,7 @@ void _explode_bomb(game_t *game, bomb_t *bomb) {
continue; continue;
double distance2 = pow(player->x - bomb->x, 2) + pow(player->y double distance2 = pow(player->x - bomb->x, 2) + pow(player->y
- bomb->y, 2) + pow(player->z - bomb->z, 2); - bomb->y, 2) + pow(player->z - bomb->z, 2);
if (distance2 < 10000.0) { if (distance2 < pow(150 , 2)) {
game->explosion_callback(player->x, player->y, player->z); game->explosion_callback(player->x, player->y, player->z);
player->x = player->team->x + 100; player->x = player->team->x + 100;
player->y = player->team->y; player->y = player->team->y;
@ -201,11 +235,32 @@ void game_update_bombs(game_t *game, double dt) {
bomb->y += bomb->vy * dt; bomb->y += bomb->vy * dt;
bomb->z += bomb->vz * dt; bomb->z += bomb->vz * dt;
bomb->ttl -= dt; bomb->ttl -= dt;
if (bomb->x < (-1000 + 10)) {
bomb->x = (-1000 + 10);
bomb->ttl = -0.1;
} else if (bomb->x > (5000 - 10)) {
bomb->x = (5000 - 10);
bomb->ttl = -0.1;
} else if (bomb->y < (-3000 + 10)) {
bomb->y = (-3000 + 10);
bomb->ttl = -0.1;
} else if (bomb->y > (3000 - 10)) {
bomb->y = (3000 - 10);
bomb->ttl = -0.1;
} else if (bomb->z < (-3000 + 10)) {
bomb->z = (-3000 + 10);
bomb->ttl = -0.1;
} else if (bomb->z > (3000 - 10)) {
bomb->z = (3000 - 10);
bomb->ttl = -0.1;
}
if (bomb->ttl < 0) { if (bomb->ttl < 0) {
if (bomb->status == 1) { if (bomb->status == 1) {
game->explosion_callback(bomb->x, bomb->y, bomb->z); game->explosion_callback(bomb->x, bomb->y, bomb->z);
bomb->status = 2; bomb->status = 2;
} else if (bomb->ttl < 0.8) { } else if (bomb->ttl < -0.2) {
_explode_bomb(game, bomb); _explode_bomb(game, bomb);
bomb->status = 0; bomb->status = 0;
} }
@ -240,9 +295,9 @@ void game_update_points(game_t *game, double dt) {
} }
void game_update(game_t *game, double dt) { void game_update(game_t *game, double dt) {
const double delta = 1. / 60.; const double delta = 1. / 120.;
game->updateTime += dt; game->updateTime += dt;
while (game->updateTime > delta * 0.8) { while (game->updateTime > delta) {
game_update_players(game, delta); game_update_players(game, delta);
game_update_bombs(game, delta); game_update_bombs(game, delta);
game_update_points(game, delta); game_update_points(game, delta);

View File

@ -32,9 +32,9 @@ void setup_light() {
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glEnable( GL_LIGHT0); glEnable(GL_LIGHT0);
glEnable( GL_LIGHTING); glEnable(GL_LIGHTING);
} }
void setup_opengl() { void setup_opengl() {
@ -57,14 +57,14 @@ void setup_opengl() {
glfwEnable(GLFW_STICKY_KEYS); glfwEnable(GLFW_STICKY_KEYS);
// general settings // general settings
glShadeModel( GL_SMOOTH); glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable( GL_CULL_FACE); glEnable(GL_CULL_FACE);
// setup depth buffer // setup depth buffer
glClearDepth(1.0f); glClearDepth(1.0f);
glEnable( GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc( GL_LEQUAL); glDepthFunc(GL_LEQUAL);
// Enable vertical sync (on cards that support it) // Enable vertical sync (on cards that support it)
glfwSwapInterval(1); glfwSwapInterval(1);
@ -73,7 +73,7 @@ void setup_opengl() {
void draw_team(team_t *team) { void draw_team(team_t *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);
glPushMatrix(); glPushMatrix();
glTranslated(team->x, team->y, team->z); glTranslated(team->x, team->y, team->z);
gluSphere(quadratic, 50.f, 32, 32); gluSphere(quadratic, 50.f, 32, 32);
@ -84,7 +84,7 @@ void draw_player(player_t *player) {
if (player->status == 0) if (player->status == 0)
return; return;
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, player->team->color); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, player->team->color);
glMatrixMode( GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
glTranslated(player->x, player->y, player->z); glTranslated(player->x, player->y, player->z);
gluSphere(quadratic, 10.f, 32, 32); gluSphere(quadratic, 10.f, 32, 32);
@ -96,7 +96,7 @@ void draw_bomb(bomb_t *bomb) {
return; return;
GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f }; GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
glMatrixMode( GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
glTranslated(bomb->x, bomb->y, bomb->z); glTranslated(bomb->x, bomb->y, bomb->z);
gluSphere(quadratic, 3.f, 4, 4); gluSphere(quadratic, 3.f, 4, 4);
@ -107,7 +107,7 @@ void draw_point(point_t *point) {
return; return;
GLfloat red[] = { 0.0f, 0.0f, 1.0f, 1.0f }; GLfloat red[] = { 0.0f, 0.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
glMatrixMode( GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
glTranslated(point->x, point->y, point->z); glTranslated(point->x, point->y, point->z);
gluSphere(quadratic, 3.f, 12, 12); gluSphere(quadratic, 3.f, 12, 12);
@ -299,9 +299,9 @@ void dispatch_message(enet_uint8 *data, size_t length, game_t *game) {
case MESSAGE_PLAYER_UPDATE: { case MESSAGE_PLAYER_UPDATE: {
player_update_message_t *um = (player_update_message_t *) data; player_update_message_t *um = (player_update_message_t *) data;
player_t *player = game_player(game, um->player_id); player_t *player = game_player(game, um->player_id);
player->x = um->x; player->x = um->x + um->vx * 0.0001;
player->y = um->y; player->y = um->y + um->vy * 0.0001;
player->z = um->z; player->z = um->z + um->vz * 0.0001;
player->vx = um->vx; player->vx = um->vx;
player->vy = um->vy; player->vy = um->vy;
player->vz = um->vz; player->vz = um->vz;
@ -330,9 +330,9 @@ void dispatch_message(enet_uint8 *data, size_t length, game_t *game) {
bomb_t *bomb = game_spawn_bomb(game); bomb_t *bomb = game_spawn_bomb(game);
if (bomb == NULL) if (bomb == NULL)
return; return;
bomb->x = m->x; bomb->x = m->x + m->vx * 0.0001;
bomb->y = m->y; bomb->y = m->y + m->vy * 0.0001;
bomb->z = m->z; bomb->z = m->z + m->vz * 0.0001;
bomb->vx = m->vx; bomb->vx = m->vx;
bomb->vy = m->vy; bomb->vy = m->vy;
bomb->vz = m->vz; bomb->vz = m->vz;
@ -541,64 +541,64 @@ GLuint wallTex = 0;
void draw_box() { void draw_box() {
glBindTexture(GL_TEXTURE_2D, wallTex); glBindTexture(GL_TEXTURE_2D, wallTex);
glEnable( GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
if (wallTex == 0) { if (wallTex == 0) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glfwLoadTexture2D("data/wall.tga", 0); glfwLoadTexture2D("data/wall.tga", 0);
} }
GLfloat red[] = { 0.1f, 0.2f, 0.3f, 1.0f }; GLfloat red[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
// Enable/Disable features // Enable/Disable features
glPushAttrib( GL_ENABLE_BIT); glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
//glDisable( GL_DEPTH_TEST); //glDisable( GL_DEPTH_TEST);
// glEnable( GL_LIGHTING); // glEnable( GL_LIGHTING);
// glDisable( GL_BLEND); // glDisable( GL_BLEND);
glMatrixMode( GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
glTranslated(2000.0, 0.0, 0.0); glTranslated(2000.0, 0.0, 0.0);
//glScaled(5000.0f, 5000.0f, 5000.0f); //glScaled(5000.0f, 5000.0f, 5000.0f);
float s = 2500.0; float s = 3000.0f, t = 5.0f;
// Just in case we set all vertices to white. // Just in case we set all vertices to white.
glColor4f(1, 1, 1, 1); glColor4f(1, 1, 1, 1);
// Render the front quad // Render the front quad
// glBindTexture(GL_TEXTURE_2D, _skybox[0]); glBegin(GL_QUADS);
glBegin( GL_QUADS); glTexCoord2f(0, t);
glTexCoord2f(0, 0);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(s, -s, -s);
glTexCoord2f(0, 1);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(s, s, -s);
glTexCoord2f(1, 1);
glNormal3f(0.0, 0.0, 1.0); glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-s, s, -s); glVertex3f(-s, s, -s);
glTexCoord2f(1, 0); glTexCoord2f(0, 0);
glNormal3f(0.0, 0.0, 1.0); glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-s, -s, -s); glVertex3f(-s, -s, -s);
glTexCoord2f(t, 0);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(s, -s, -s);
glTexCoord2f(t, t);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(s, s, -s);
glEnd(); glEnd();
// Render the left quad // Render the left quad
// glBindTexture(GL_TEXTURE_2D, _skybox[1]);
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0, 0); glTexCoord2f(0, 0);
glVertex3f(s, -s, s); glVertex3f(s, -s, s);
glTexCoord2f(0, 1);
glTexCoord2f(0, t);
glVertex3f(s, s, s); glVertex3f(s, s, s);
glTexCoord2f(1, 1);
glTexCoord2f(t, t);
glVertex3f(s, s, -s); glVertex3f(s, s, -s);
glTexCoord2f(1, 0);
glTexCoord2f(t, 0);
glVertex3f(s, -s, -s); glVertex3f(s, -s, -s);
glEnd(); glEnd();
@ -607,11 +607,11 @@ void draw_box() {
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0, 0); glTexCoord2f(0, 0);
glVertex3f(-s, -s, s); glVertex3f(-s, -s, s);
glTexCoord2f(0, 1); glTexCoord2f(0, t);
glVertex3f(-s, s, s); glVertex3f(-s, s, s);
glTexCoord2f(1, 1); glTexCoord2f(t, t);
glVertex3f(s, s, s); glVertex3f(s, s, s);
glTexCoord2f(1, 0); glTexCoord2f(t, 0);
glVertex3f(s, -s, s); glVertex3f(s, -s, s);
glEnd(); glEnd();
@ -621,24 +621,24 @@ void draw_box() {
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0, 0); glTexCoord2f(0, 0);
glVertex3f(-s, -s, -s); glVertex3f(-s, -s, -s);
glTexCoord2f(0, 1); glTexCoord2f(0, t);
glVertex3f(-s, s, -s); glVertex3f(-s, s, -s);
glTexCoord2f(1, 1); glTexCoord2f(t, t);
glVertex3f(-s, s, s); glVertex3f(-s, s, s);
glTexCoord2f(1, 0); glTexCoord2f(t, 0);
glVertex3f(-s, -s, s); glVertex3f(-s, -s, s);
glEnd(); glEnd();
// Render the top quad // Render the top quad
//glBindTexture(GL_TEXTURE_2D, _skybox[4]); //glBindTexture(GL_TEXTURE_2D, _skybox[4]);
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0, 1); glTexCoord2f(0, t);
glVertex3f(-s, s, -s); glVertex3f(-s, s, -s);
glTexCoord2f(1, 1); glTexCoord2f(t, t);
glVertex3f(s, s, -s); glVertex3f(s, s, -s);
glTexCoord2f(1, 0); glTexCoord2f(t, 0);
glVertex3f(s, s, s); glVertex3f(s, s, s);
glTexCoord2f(0, 0); glTexCoord2f(0, 0);
@ -651,11 +651,11 @@ void draw_box() {
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0, 0); glTexCoord2f(0, 0);
glVertex3f(-s, -s, -s); glVertex3f(-s, -s, -s);
glTexCoord2f(0, 1); glTexCoord2f(0, t);
glVertex3f(-s, -s, s); glVertex3f(-s, -s, s);
glTexCoord2f(1, 1); glTexCoord2f(t, t);
glVertex3f(s, -s, s); glVertex3f(s, -s, s);
glTexCoord2f(1, 0); glTexCoord2f(t, 0);
glVertex3f(s, -s, -s); glVertex3f(s, -s, -s);
glEnd(); glEnd();
@ -719,12 +719,12 @@ void Application::prepareFrame(double rx, double ry, double rz) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Select and setup the projection matrix // Select and setup the projection matrix
glMatrixMode( GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f, 10000.0f); gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f, 10000.0f);
// Select and setup the modelview matrix // Select and setup the modelview matrix
glMatrixMode( GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
gluLookAt(camX, camY, camZ, camX + rx * 10.0f, camY + ry * 10.0f, camZ + rz gluLookAt(camX, camY, camZ, camX + rx * 10.0f, camY + ry * 10.0f, camZ + rz
* 10.0f, 0.0f, 1.0f, 0.0f); * 10.0f, 0.0f, 1.0f, 0.0f);
@ -768,7 +768,7 @@ void Application::initialize(int argc, char ** argv) {
accelerate_schudule.setInterval(0.05); accelerate_schudule.setInterval(0.05);
player_update_schudule.setExact(true); player_update_schudule.setExact(true);
player_update_schudule.setInterval(0.1); player_update_schudule.setInterval(0.05);
loadConsoleFont(); loadConsoleFont();
} }
@ -791,23 +791,13 @@ void Application::update() {
last_x = x; last_x = x;
last_y = y; last_y = y;
if (local_player) { game_update(&game, dt);
camX = local_player->x;
camY = local_player->y;
camZ = local_player->z;
} else {
camX = 1000.0;
camY = 1000.0;
camZ = 1000.0;
}
service_network(&game); service_network(&game);
if (server && player_update_schudule.next(time)) { if (server && player_update_schudule.next(time)) {
send_player_updates(&game); send_player_updates(&game);
} }
game_update(&game, dt);
double rx = cos(phi) * cos(theta); double rx = cos(phi) * cos(theta);
double ry = sin(theta); double ry = sin(theta);
double rz = sin(phi) * cos(theta); double rz = sin(phi) * cos(theta);
@ -837,13 +827,24 @@ void Application::update() {
drop_bomb(&game, rx, ry, rz, 5.0); drop_bomb(&game, rx, ry, rz, 5.0);
} }
} }
service_network(&game); //service_network(&game);
if (local_player) {
camX = local_player->x;
camY = local_player->y;
camZ = local_player->z;
} else {
camX = 1000.0;
camY = 1000.0;
camZ = 1000.0;
}
prepareFrame(rx, ry, rz); prepareFrame(rx, ry, rz);
setup_light(); setup_light();
glEnable( GL_LIGHT0); glEnable(GL_LIGHT0);
glEnable( GL_LIGHTING); glEnable(GL_LIGHTING);
glEnable( GL_CULL_FACE); glEnable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
for (size_t i = 0; i < GAME_TEAM_COUNT; i++) for (size_t i = 0; i < GAME_TEAM_COUNT; i++)
draw_team(&game.team[i]); draw_team(&game.team[i]);