Removed trailing whitespace from all *.?pp files
[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 "math/random_generator.hpp"
18 #include "object/bullet.hpp"
19 #include "object/camera.hpp"
20 #include "sprite/sprite.hpp"
21 #include "sprite/sprite_manager.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/sector.hpp"
24
25 namespace {
26 const float BULLET_XM = 600;
27 const float BULLET_STARTING_YM = 0;
28 }
29
30 Bullet::Bullet(const Vector& pos, float xm, int dir, BonusType type) :
31   physic(),
32   life_count(3),
33   sprite(),
34   light(0.0f,0.0f,0.0f),
35   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-small.sprite")),
36   type(type)
37 {
38   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
39   physic.set_velocity_x(speed + xm);
40
41   if(type == FIRE_BONUS) {
42     sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
43     lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
44     lightsprite->set_color(Color(0.3f, 0.1f, 0.0f));
45  } else if(type == ICE_BONUS) {
46     life_count = 10;
47     sprite = sprite_manager->create("images/objects/bullets/icebullet.sprite");
48   } else {
49     log_warning << "Bullet::Bullet called with unknown BonusType" << std::endl;
50     life_count = 10;
51     sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
52   }
53
54   bbox.set_pos(pos);
55   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
56 }
57
58 Bullet::~Bullet()
59 {
60 }
61
62 void
63 Bullet::update(float elapsed_time)
64 {
65   // cause fireball color to flicker randomly
66   if (gameRandom.rand(5) != 0) {
67     lightsprite->set_color(Color(0.3f + gameRandom.rand(10)/100.0f, 0.1f + gameRandom.rand(20)/100.0f, gameRandom.rand(10)/100.0f));
68   } else
69     lightsprite->set_color(Color(0.3f, 0.1f, 0.0f));
70   // remove bullet when it's offscreen
71   float scroll_x =
72     Sector::current()->camera->get_translation().x;
73   float scroll_y =
74     Sector::current()->camera->get_translation().y;
75   if (get_pos().x < scroll_x ||
76       get_pos().x > scroll_x + SCREEN_WIDTH ||
77       //     get_pos().y < scroll_y ||
78       get_pos().y > scroll_y + SCREEN_HEIGHT ||
79       life_count <= 0) {
80     remove_me();
81     return;
82   }
83
84   movement = physic.get_movement(elapsed_time);
85 }
86
87 void
88 Bullet::draw(DrawingContext& context)
89 {
90   //Draw the Sprite.
91   sprite->draw(context, get_pos(), LAYER_OBJECTS);
92   //Draw the light if fire and dark
93   if(type == FIRE_BONUS){
94     context.get_light( get_bbox().get_middle(), &light );
95     if (light.red + light.green < 2.0){
96       context.push_target();
97       context.set_target(DrawingContext::LIGHTMAP);
98       sprite->draw(context, get_pos(), LAYER_OBJECTS);
99       lightsprite->draw(context, get_bbox().get_middle(), 0);
100       context.pop_target();
101     }
102   }
103 }
104
105 void
106 Bullet::collision_solid(const CollisionHit& hit)
107 {
108   if(hit.top || hit.bottom) {
109     physic.set_velocity_y(-physic.get_velocity_y());
110     life_count--;
111   } else if(hit.left || hit.right) {
112     if(type == ICE_BONUS) {
113       physic.set_velocity_x(-physic.get_velocity_x());
114       life_count--;
115     } else
116       remove_me();
117   }
118 }
119
120 void
121 Bullet::ricochet(GameObject& , const CollisionHit& hit)
122 {
123   collision_solid(hit);
124 }
125
126 HitResponse
127 Bullet::collision(GameObject& , const CollisionHit& )
128 {
129   return FORCE_MOVE;
130 }
131
132 /* EOF */