Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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   : life_count(3), type(type)
30 {
31   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
32   physic.set_velocity_x(speed + xm);
33
34   if(type == FIRE_BONUS) {
35     sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
36   } else if(type == ICE_BONUS) {
37     life_count = 10;
38     sprite = sprite_manager->create("images/objects/bullets/icebullet.sprite");
39   } else {
40     log_warning << "Bullet::Bullet called with unknown BonusType" << std::endl;
41     life_count = 10;
42     sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
43   }
44
45   bbox.set_pos(pos);
46   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
47 }
48
49 Bullet::~Bullet()
50 {
51 }
52
53 void
54 Bullet::update(float elapsed_time)
55 {
56   // remove bullet when it's offscreen
57   float scroll_x =
58     Sector::current()->camera->get_translation().x;
59   float scroll_y =
60     Sector::current()->camera->get_translation().y;
61   if (get_pos().x < scroll_x ||
62       get_pos().x > scroll_x + SCREEN_WIDTH ||
63       //     get_pos().y < scroll_y ||
64       get_pos().y > scroll_y + SCREEN_HEIGHT ||
65       life_count <= 0) {
66     remove_me();
67     return;
68   }
69
70   movement = physic.get_movement(elapsed_time);
71 }
72
73 void
74 Bullet::draw(DrawingContext& context)
75 {
76   sprite->draw(context, get_pos(), LAYER_OBJECTS);
77 }
78
79 void
80 Bullet::collision_solid(const CollisionHit& hit)
81 {
82   if(hit.top || hit.bottom) {
83     physic.set_velocity_y(-physic.get_velocity_y());
84     life_count--;
85   } else if(hit.left || hit.right) {
86     if(type == ICE_BONUS) {
87       physic.set_velocity_x(-physic.get_velocity_x());
88       life_count--;
89     } else
90       remove_me();
91   }
92 }
93
94 void
95 Bullet::ricochet(GameObject& , const CollisionHit& hit)
96 {
97   collision_solid(hit);
98 }
99
100 HitResponse
101 Bullet::collision(GameObject& , const CollisionHit& )
102 {
103   return FORCE_MOVE;
104 }
105
106 /* EOF */