home *** CD-ROM | disk | FTP | other *** search
- #include "Shot.h"
-
- #include "Network.h"
- #include "log.h"
- #include "Game.h"
- #include "matrixmath.h"
-
- #include "LaserShot.h"
- #include "ChaingunShot.h"
- #include "RailgunShot.h"
- #include "RocketlauncherShot.h"
- #include "PlasmagunShot.h"
-
- Shot::Shot(int clientId, int weaponId, unsigned int lifetimeMillis): GameObject(){
- this->type = GAME_WEAPON_NO_WEAPON;
- this->weaponId = weaponId;
- this->clientId = clientId;
- this->lifetimeMillis = lifetimeMillis;
- this->spawntimeMillis = SDL_GetTicks();
-
- this->boundingSphereRadius = 1.0f;
- this->inflictedDamage = 0;
- }
-
- Shot::~Shot(){
- }
-
- void Shot::leaveMuzzle(){
-
- // "shoot through wall"-check
- if( Network::server->clients[clientId] != NULL && Network::server->clients[clientId]->vehicle != NULL ){
- Vehicle* v = Network::server->clients[clientId]->vehicle;
-
- unsigned int i;
- trace_t trace;
- trace.ignoreFlags = 0 | COLLISION_FLAG_BACKFACES | COLLISION_FLAG_SHOOT_THROUGH | COLLISION_FLAG_VEHICLES;
-
- traceLinesegment(v->pos, pos, &trace);
-
- for(i=0;i<trace.hits.size();i++){
- if( trace.hits[i].face != NULL ){ // hit a face of the arena
- hitFace( trace.hits[i].face, trace.hits[i].pos );
- break;
- }
- if( trace.hits[i].vehicle != NULL ){ // ignore vehicle-hits
- continue;
- }
- }
- }
-
- // reload animation
- if( Network::server->clients[clientId] != NULL && Network::server->clients[clientId]->vehicle != NULL
- && Network::server->clients[clientId]->vehicle->weapons[weaponId] != NULL && Network::server->clients[clientId]->vehicle->weapons[weaponId]->animator != NULL ){
- Animator* a = Network::server->clients[clientId]->vehicle->weapons[weaponId]->animator;
- a->setCurrentAnimation(ANIMATION_WEAPON_RELOAD);
- // a->setLooping(false);
- a->start();
- }
-
- }
-
- void Shot::move(float deltaT){
-
- vec3_t newPos;
- vectorMA3d(pos, deltaT*moveSpeed, dir, newPos);
-
- unsigned int i;
- trace_t trace;
- trace.ignoreFlags = 0 | COLLISION_FLAG_BACKFACES | COLLISION_FLAG_SHOOT_THROUGH;
-
- traceLinesegment(pos, newPos, &trace);
-
- for(i=0;i<trace.hits.size();i++){
- if( trace.hits[i].face != NULL ){ // hit a face of the arena
- hitFace( trace.hits[i].face, trace.hits[i].pos );
- break;
- }
- if( trace.hits[i].vehicle != NULL ){ // hit a vehicle
- if( trace.hits[i].vehicle == Network::server->clients[clientId]->vehicle )
- continue;
-
- hitVehicle( trace.hits[i].vehicle, trace.hits[i].pos );
- break;
- }
- }
-
- vectorCopy3d(newPos, pos);
- }
-
- void Shot::hitFace(Face* face, vec3_t hp){
- // log("HIT A FACE!\n");
- // Shot::hitVehicle(Network::client->vehicle, hp);
-
- this->lifetimeMillis = 0;
- }
-
- void Shot::hitVehicle(Vehicle* vehicle, vec3_t hp){
- // log("HIT A VEHICLE!\n");
-
- if( !Game::info.var.clientGame ){ // we are the server -> send client Hurt
- if( Network::server->clients[clientId] != NULL ){ // client has not yet disconnected
-
- // do hurt teammate if friendly fire is disabled
- if( !(Game::info.var.mode == GAME_MODE_TEAM_DEATHMATCH && !Game::info.var.enableFriendlyFire && vehicle->client->ci.team == Network::server->clients[clientId]->ci.team) ){
- vehicle->takeDamage(this->inflictedDamage);
- if( vehicle->armor > 0 ){
- Network::server->sendClientHurt(vehicle->client->clientId, this->clientId, this->inflictedDamage);
- }else{
- Network::server->sendClientKill(vehicle->client->clientId, this->clientId, this->type);
- Game::killClient(vehicle->client, Network::server->clients[this->clientId], this->type);
- }
- }
- }
- }
-
- this->lifetimeMillis = 0;
- }
-
- void Shot::render(){
- Renderer::renderBillboard(pos, 1.0f, 1.0f, NULL);
- }
-
-
-
- Shot* Shot::createShotForShotSpawnPacket(shotSpawnPacket_t* ss){
- Shot* ret = NULL;
-
- switch( ss->type ){
- case GAME_WEAPON_LASER:
- ret = new LaserShot(ss->clientId, ss->weaponId);
- break;
-
- case GAME_WEAPON_CHAINGUN:
- ret = new ChaingunShot(ss->clientId, ss->weaponId);
- break;
-
- case GAME_WEAPON_RAILGUN:
- ret = new RailgunShot(ss->clientId, ss->weaponId);
- break;
-
- case GAME_WEAPON_ROCKETLAUNCHER:
- ret = new RocketlauncherShot(ss->clientId, ss->weaponId);
- break;
-
- case GAME_WEAPON_PLASMAGUN:
- ret = new PlasmagunShot(ss->clientId, ss->weaponId);
- break;
-
- case GAME_WEAPON_NO_WEAPON:
- default:
- return NULL;
- }
-
- vectorCopy3d(ss->pos, ret->pos);
- vectorCopy3d(ss->dir, ret->dir);
- vectorCopy3d(ss->up, ret->up);
- vectorCrossP3d(ret->up, ret->dir, ret->left);
-
- return ret;
-
- }
-
- Shot* Shot::createShotForWeapon(Weapon* weapon){
- Shot* ret = NULL;
-
- Vehicle* v = weapon->vehicle;
- int weaponId = -1;
- for(int i=0;i<4;i++){
- if( weapon == v->weapons[i] ){
- weaponId = i;
- break;
- }
- }
-
- switch( weapon->type ){
- case GAME_WEAPON_LASER:
- ret = new LaserShot(v->client->clientId, weaponId);
- break;
-
- case GAME_WEAPON_CHAINGUN:
- ret = new ChaingunShot(v->client->clientId, weaponId);
- break;
-
- case GAME_WEAPON_RAILGUN:
- ret = new RailgunShot(v->client->clientId, weaponId);
- break;
-
- case GAME_WEAPON_ROCKETLAUNCHER:
- ret = new RocketlauncherShot(v->client->clientId, weaponId);
- break;
-
- case GAME_WEAPON_PLASMAGUN:
- ret = new PlasmagunShot(v->client->clientId, weaponId);
- break;
-
- case GAME_WEAPON_NO_WEAPON:
- default:
- return NULL;
- }
-
- vec4_t pos;
- vectorAdd3d(weapon->mountPoint, weapon->muzzlePoint, pos);
- float m[] = { v->left[0], v->left[1], v->left[2], 0.0f,
- v->up[0], v->up[1], v->up[2], 0.0f,
- v->dir[0], v->dir[1], v->dir[2], 0.0f,
- v->pos[0], v->pos[1], v->pos[2], 1.0f
- };
- pos[3] = 1.0f;
- matrixMultVector(m, pos, 4, pos);
-
- vectorCopy3d(pos, ret->pos);
- vectorCopy3d(v->dir, ret->dir);
- vectorCopy3d(v->up, ret->up);
- vectorCopy3d(v->left, ret->left);
- // vectorCopy3d(ss->vel, ret->vel);
-
- return ret;
-
- }
-