Bullet Class Reference

#include <bullet.hpp>

Inherits MovingObject.

List of all members.

Public Member Functions

 Bullet (const Vector &pos, float xm, int dir, BonusType type)
 ~Bullet ()
void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.
void collision_solid (const CollisionHit &hit)
 this function is called when the object collided with something solid
HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object
void ricochet (GameObject &other, const CollisionHit &hit)
 Makes bullet bounce off an object (that got hit).
BonusType get_type ()

Private Attributes

Physic physic
int life_count
SpritePtr sprite
BonusType type


Detailed Description

Definition at line 25 of file bullet.hpp.


Constructor & Destructor Documentation

Bullet::Bullet ( const Vector pos,
float  xm,
int  dir,
BonusType  type 
)

Definition at line 28 of file bullet.cpp.

References MovingObject::bbox, BULLET_XM, SpriteManager::create(), FIRE_BONUS, ICE_BONUS, life_count, log_warning, physic, RIGHT, Rectf::set_pos(), Rectf::set_size(), Physic::set_velocity_x(), sprite, and sprite_manager.

00028                                                                    :
00029   physic(),
00030   life_count(3), 
00031   sprite(),
00032   type(type)
00033 {
00034   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
00035   physic.set_velocity_x(speed + xm);
00036 
00037   if(type == FIRE_BONUS) {
00038     sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
00039   } else if(type == ICE_BONUS) {
00040     life_count = 10;
00041     sprite = sprite_manager->create("images/objects/bullets/icebullet.sprite");
00042   } else {
00043     log_warning << "Bullet::Bullet called with unknown BonusType" << std::endl;
00044     life_count = 10;
00045     sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
00046   }
00047 
00048   bbox.set_pos(pos);
00049   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00050 }

Bullet::~Bullet (  ) 

Definition at line 52 of file bullet.cpp.

00053 {
00054 }


Member Function Documentation

void Bullet::update ( float  elapsed_time  )  [virtual]

This function is called once per frame and allows the object to update it's state.

The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)

Implements GameObject.

Definition at line 57 of file bullet.cpp.

References Sector::camera, Sector::current(), Physic::get_movement(), MovingObject::get_pos(), Camera::get_translation(), life_count, MovingObject::movement, physic, GameObject::remove_me(), SCREEN_HEIGHT, SCREEN_WIDTH, Vector::x, and Vector::y.

00058 {
00059   // remove bullet when it's offscreen
00060   float scroll_x =
00061     Sector::current()->camera->get_translation().x;
00062   float scroll_y =
00063     Sector::current()->camera->get_translation().y;
00064   if (get_pos().x < scroll_x ||
00065       get_pos().x > scroll_x + SCREEN_WIDTH ||
00066       //     get_pos().y < scroll_y ||
00067       get_pos().y > scroll_y + SCREEN_HEIGHT ||
00068       life_count <= 0) {
00069     remove_me();
00070     return;
00071   }
00072 
00073   movement = physic.get_movement(elapsed_time);
00074 }

void Bullet::draw ( DrawingContext context  )  [virtual]

The GameObject should draw itself onto the provided DrawingContext if this function is called.

Implements GameObject.

Definition at line 77 of file bullet.cpp.

References MovingObject::get_pos(), LAYER_OBJECTS, and sprite.

00078 {
00079   sprite->draw(context, get_pos(), LAYER_OBJECTS);
00080 }

void Bullet::collision_solid ( const CollisionHit hit  )  [virtual]

this function is called when the object collided with something solid

Reimplemented from MovingObject.

Definition at line 83 of file bullet.cpp.

References CollisionHit::bottom, Physic::get_velocity_x(), Physic::get_velocity_y(), ICE_BONUS, CollisionHit::left, life_count, physic, GameObject::remove_me(), CollisionHit::right, Physic::set_velocity_x(), Physic::set_velocity_y(), CollisionHit::top, and type.

Referenced by ricochet().

00084 {
00085   if(hit.top || hit.bottom) {
00086     physic.set_velocity_y(-physic.get_velocity_y());
00087     life_count--;
00088   } else if(hit.left || hit.right) {
00089     if(type == ICE_BONUS) {
00090       physic.set_velocity_x(-physic.get_velocity_x());
00091       life_count--;
00092     } else
00093       remove_me();
00094   }
00095 }

HitResponse Bullet::collision ( GameObject other,
const CollisionHit hit 
) [virtual]

this function is called when the object collided with any other object

Implements MovingObject.

Definition at line 104 of file bullet.cpp.

References FORCE_MOVE.

00105 {
00106   return FORCE_MOVE;
00107 }

void Bullet::ricochet ( GameObject other,
const CollisionHit hit 
)

Makes bullet bounce off an object (that got hit).

To be called by the collision handler of that object. Note that the hit parameter is filled in as perceived by the object, not by the bullet.

Definition at line 98 of file bullet.cpp.

References collision_solid().

Referenced by Igel::collision_bullet(), and BadGuy::collision_bullet().

00099 {
00100   collision_solid(hit);
00101 }

BonusType Bullet::get_type (  )  [inline]

Definition at line 43 of file bullet.hpp.

Referenced by BadGuy::collision_bullet().

00044   {
00045     return type;
00046   }


Member Data Documentation

Physic Bullet::physic [private]

Definition at line 49 of file bullet.hpp.

Referenced by Bullet(), collision_solid(), and update().

int Bullet::life_count [private]

Definition at line 50 of file bullet.hpp.

Referenced by Bullet(), collision_solid(), and update().

SpritePtr Bullet::sprite [private]

Definition at line 51 of file bullet.hpp.

Referenced by Bullet(), and draw().

BonusType Bullet::type [private]

Definition at line 52 of file bullet.hpp.

Referenced by collision_solid().


The documentation for this class was generated from the following files:
Generated on Mon Apr 21 03:38:22 2014 for SuperTux by  doxygen 1.5.1