00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "object/bullet.hpp"
00018 #include "object/camera.hpp"
00019 #include "sprite/sprite_manager.hpp"
00020 #include "supertux/globals.hpp"
00021 #include "supertux/sector.hpp"
00022
00023 namespace {
00024 const float BULLET_XM = 600;
00025 const float BULLET_STARTING_YM = 0;
00026 }
00027
00028 Bullet::Bullet(const Vector& pos, float xm, int dir, BonusType type) :
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 }
00051
00052 Bullet::~Bullet()
00053 {
00054 }
00055
00056 void
00057 Bullet::update(float elapsed_time)
00058 {
00059
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
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 }
00075
00076 void
00077 Bullet::draw(DrawingContext& context)
00078 {
00079 sprite->draw(context, get_pos(), LAYER_OBJECTS);
00080 }
00081
00082 void
00083 Bullet::collision_solid(const CollisionHit& hit)
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 }
00096
00097 void
00098 Bullet::ricochet(GameObject& , const CollisionHit& hit)
00099 {
00100 collision_solid(hit);
00101 }
00102
00103 HitResponse
00104 Bullet::collision(GameObject& , const CollisionHit& )
00105 {
00106 return FORCE_MOVE;
00107 }
00108
00109