src/object/bullet.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   // 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 }
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 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1