2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
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"
26 const float BULLET_XM = 600;
29 Bullet::Bullet(const Vector& pos, float xm, int dir, BonusType type_) :
33 light(0.0f,0.0f,0.0f),
34 lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite")),
37 float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
38 physic.set_velocity_x(speed + xm);
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) {
46 sprite = SpriteManager::current()->create("images/objects/bullets/icebullet.sprite");
48 log_warning << "Bullet::Bullet called with unknown BonusType" << std::endl;
50 sprite = SpriteManager::current()->create("images/objects/bullets/firebullet.sprite");
54 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
62 Bullet::update(float elapsed_time)
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));
68 lightsprite->set_color(Color(0.3f, 0.1f, 0.0f));
69 // remove bullet when it's offscreen
71 Sector::current()->camera->get_translation().x;
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 ||
83 movement = physic.get_movement(elapsed_time);
87 Bullet::draw(DrawingContext& context)
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);
105 Bullet::collision_solid(const CollisionHit& hit)
107 if(hit.top || hit.bottom) {
108 physic.set_velocity_y(-physic.get_velocity_y());
110 } else if(hit.left || hit.right) {
111 if(type == ICE_BONUS) {
112 physic.set_velocity_x(-physic.get_velocity_x());
120 Bullet::ricochet(GameObject& , const CollisionHit& hit)
122 collision_solid(hit);
126 Bullet::collision(GameObject& , const CollisionHit& )