2161e3be0eeced65fbbc6b79dc2fa7bb00307de6
[supertux.git] / src / object / bullet.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/bullet.hpp"
18 #include "object/camera.hpp"
19 #include "sprite/sprite_manager.hpp"
20 #include "supertux/main.hpp"
21 #include "supertux/sector.hpp"
22
23 namespace {
24 const float BULLET_XM = 600;
25 const float BULLET_STARTING_YM = 0;
26 }
27
28 Bullet::Bullet(const Vector& pos, float xm, int dir, BonusType type) :
29   physic(),
30   life_count(3), 
31   sprite(),
32   type(type)
33 {
34   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
35   physic.set_velocity_x(speed + xm);
36
37   if(type == FIRE_BONUS) {
38     sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
39   } else if(type == ICE_BONUS) {
40     life_count = 10;
41     sprite = sprite_manager->create("images/objects/bullets/icebullet.sprite");
42   } else {
43     log_warning << "Bullet::Bullet called with unknown BonusType" << std::endl;
44     life_count = 10;
45     sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
46   }
47
48   bbox.set_pos(pos);
49   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
50 }
51
52 Bullet::~Bullet()
53 {
54 }
55
56 void
57 Bullet::update(float elapsed_time)
58 {
59   // remove bullet when it's offscreen
60   float scroll_x =
61     Sector::current()->camera->get_translation().x;
62   float scroll_y =
63     Sector::current()->camera->get_translation().y;
64   if (get_pos().x < scroll_x ||
65       get_pos().x > scroll_x + SCREEN_WIDTH ||
66       //     get_pos().y < scroll_y ||
67       get_pos().y > scroll_y + SCREEN_HEIGHT ||
68       life_count <= 0) {
69     remove_me();
70     return;
71   }
72
73   movement = physic.get_movement(elapsed_time);
74 }
75
76 void
77 Bullet::draw(DrawingContext& context)
78 {
79   sprite->draw(context, get_pos(), LAYER_OBJECTS);
80 }
81
82 void
83 Bullet::collision_solid(const CollisionHit& hit)
84 {
85   if(hit.top || hit.bottom) {
86     physic.set_velocity_y(-physic.get_velocity_y());
87     life_count--;
88   } else if(hit.left || hit.right) {
89     if(type == ICE_BONUS) {
90       physic.set_velocity_x(-physic.get_velocity_x());
91       life_count--;
92     } else
93       remove_me();
94   }
95 }
96
97 void
98 Bullet::ricochet(GameObject& , const CollisionHit& hit)
99 {
100   collision_solid(hit);
101 }
102
103 HitResponse
104 Bullet::collision(GameObject& , const CollisionHit& )
105 {
106   return FORCE_MOVE;
107 }
108
109 /* EOF */