//
// bullet.cpp
//
// standard bullet
//
//
// Author: Tomi Belan <tomi.belan@gmail.com>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <iostream> // @debug

#include <SDL.h>

#include "playership.h"
#include "bullet.h"
#include "main.h"

Bullet::Bullet(GameHandler *handler, int playerId)
{
  this->handler = handler;
  this->playerId = playerId;
  type = (playerId == 1 ? handler->shipType1 : handler->shipType2);
  visible = false;
  set_rect_dim(size, 20, 20);
  if(type == 0) { speed = 6; }
  if(type == 1) { speed = 7; }
  set_rect(pos, 0, 0, size.w, size.h);
  set_rect(velocity, 0, 0, 0, 0);
}

Bullet::~Bullet()
{
}

void Bullet::shipsWereInitialized()
{
  myPlayerShip = (playerId == 1 ? handler->ship1 : handler->ship2);
  otherPlayerShip = (playerId == 1 ? handler->ship2 : handler->ship1);
}

void Bullet::update(SDL_Surface *where)
{
  if(!visible)
    return;
  pos.x += velocity.x; pos.y += velocity.y;
  if(pos.x < 0 || pos.x >= handler->world->w ||
     pos.y < 0 || pos.y >= handler->world->h) {
    visible = false; return;
  }
  if(otherPlayerShip->collidesWith(pos)) {
    otherPlayerShip->wasHit();
//    myPlayerShip->setReloading(120-80+myPlayerShip->reloading, 120);
    visible = false; return;
  }
  
  SDL_BlitSurface(imagePool["cball.png"], NULL, where, &pos);
}

void Bullet::show(SDL_Rect npos, SDL_Rect nspeed)
{
  set_rect(pos, npos.x, npos.y, size.w, size.h);
  set_rect_pos(velocity, nspeed.x, nspeed.y);
  visible = true;
}


