gremlin/src/game.cpp

130 lines
2.9 KiB
C++
Raw Normal View History

2011-01-05 14:00:09 +00:00
#include "game.h"
#include <stdlib.h>
void game_setup_team(team_t *team, uint8_t id) {
team->id = id;
team->color[0] = .5f + .5f * (id & (1 << 0));
team->color[1] = .5f + .5f * (id & (1 << 1));
team->color[2] = .5f + .5f * (id & (1 << 2));
team->color[3] = 1.0f;
team->x = 2000.0 * (id & (1 << 0));
team->y = 2000.0 * (id & (1 << 1));
team->z = 2000.0 * (id & (1 << 2));
}
void game_setup_player(player_t *player) {
player->id = 0;
player->status = 0;
player->team = NULL;
}
void game_setup(game_t *game) {
size_t i;
for (i = 0; i < GAME_TEAM_COUNT; i++)
game_setup_team(&game->team[i], i);
for (i = 0; i < GAME_PLAYER_COUNT; i++)
game_setup_player(&game->player[i]);
game->max_player_id = 0;
}
player_t *_game_free_player(game_t *game) {
size_t i;
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
if (game->player[i].status == 0)
return &game->player[i];
}
return NULL;
}
player_t *game_spawn_player(game_t *game, team_t *team) {
player_t *player = _game_free_player(game);
player->team = team;
player->id = game->max_player_id++;
player->status = 1;
player->x = team->x + 100;
player->y = team->y;
player->z = team->z;
player->vx = 0.;
player->vy = 0.;
player->vz = 0.;
return player;
}
player_t *game_spawn_player_id(game_t *game, team_t *team, uint8_t id) {
player_t *player = _game_free_player(game);
player->team = team;
player->id = id;
if (game->max_player_id < id)
game->max_player_id = id;
player->status = 1;
player->x = team->x + 100;
player->y = team->y;
player->z = team->z;
player->vx = 0.;
player->vy = 0.;
player->vz = 0.;
return player;
}
void game_update_players(game_t *game, double dt) {
size_t i;
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
player_t *player = &game->player[i];
if(player->status == 0)
return;
player->x += player->vx * dt;
player->y += player->vy * dt;
player->z += player->vz * dt;
}
}
size_t game_active_team_players(game_t *game, team_t *team) {
size_t i, count = 0;
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
if(game->player[i].status == 0)
continue;
if (game->player[i].team == team)
count++;
}
return count;
}
team_t *game_team_with_least_players(game_t *game) {
size_t i, count = -1;
team_t *team = NULL;
for (i = 0; i < GAME_TEAM_COUNT; i++) {
size_t players = game_active_team_players(game, &game->team[i]);
if (players < count) {
count = players;
team = &game->team[i];
}
}
return team;
}
team_t *game_team(game_t *game, uint8_t id){
size_t i;
for (i = 0; i < GAME_TEAM_COUNT; i++) {
if (game->team[i].id == id) {
return &game->team[i];
}
}
return NULL;
}
player_t *game_player(game_t *game, uint8_t id){
size_t i;
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
if (game->player[i].id == id) {
return &game->player[i];
}
}
return NULL;
}