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