/**
* Author: Nicolas Bertoa
*
* File: bullet.cpp
*/
#include "bullet.h"
#include <iostream>
//
// Class cBullet
//
/**
* FUNCTION: cBullet(SDL_Surface* img, int power, int speed)
*
* Constructor
*
* PARAMETERS:
*
* SDL_Surface* img: Bullet’s image.
*
* int power: bullet’s power
*
* int speed: bullet’s speed
*
* RETURN:
*
* none
*/
cBullet::cBullet(SDL_Surface* img, int power, int speed) : cImage(img)
{
if (power < 0)
mPower = 0;
else
mPower = power;
if (speed < 0)
mSpeed = 0;
else
mSpeed = speed;
active(true);
mSound = 0;
}
/**
* FUNCTION: cBullet(SDL_Surface* img, Uint32 colorkey, int power, int speed)
*
* Destructor
*
* PARAMETERS:
*
* none
*
* RETURN:
*
* none
*/
cBullet::~cBullet() {}
/**
* FUNCTION: void draw(SDL_Surface* screen)
*
* Draw the bullet at the screen
*
* PARAMETERS:
*
* SDL_Surface* screen: Window’s image
*
* RETURN:
*
* none
*/
void cBullet::draw(SDL_Surface* screen)
{
// Rectangle where we will draw the bullet
SDL_Rect screenRect;
screenRect.x = getCoordX();
screenRect.y = getCoordY();
screenRect.w = getWidth();
screenRect.h = getHeight();
// If the bullet goes out the screen, we deactivate it
if (screenRect.x < 0 - getWidth())
active(false);
if (screenRect.x >= screen->w)
active(false);
if (screenRect.y < 0 - getHeight())
active(false);
if (screenRect.y >= screen->h)
active(false);
cImage::draw(screen);
}
/**
* FUNCTION: void play(int channel, int times)
*
* Play the explosion’s sound
*
* PARAMETERS:
*
* int channel: Sound reproduction channel. 0 to 99
*
* int times: Reproduction times. Pass 0 for only 1 reproduction and
* pass -1 for an infinite loop reproduction.
*
* RETURN:
*
* none
*/
void cBullet::play(int channel, int times)
{
if (mSound == 0)
return;
if (times >= -1)
Mix_PlayChannel(channel, mSound, times);
}