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;
27 const float BULLET_STARTING_YM = 0;
30 Bullet::Bullet(const Vector& pos, float xm, int dir, BonusType type) :
34 light(0.0f,0.0f,0.0f),
35 lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-small.sprite")),
38 float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
39 physic.set_velocity_x(speed + xm);
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) {
47 sprite = sprite_manager->create("images/objects/bullets/icebullet.sprite");
49 log_warning << "Bullet::Bullet called with unknown BonusType" << std::endl;
51 sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
55 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
63 Bullet::update(float elapsed_time)
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));
69 lightsprite->set_color(Color(0.3f, 0.1f, 0.0f));
70 // remove bullet when it's offscreen
72 Sector::current()->camera->get_translation().x;
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 ||
84 movement = physic.get_movement(elapsed_time);
88 Bullet::draw(DrawingContext& context)
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();
106 Bullet::collision_solid(const CollisionHit& hit)
108 if(hit.top || hit.bottom) {
109 physic.set_velocity_y(-physic.get_velocity_y());
111 } else if(hit.left || hit.right) {
112 if(type == ICE_BONUS) {
113 physic.set_velocity_x(-physic.get_velocity_x());
121 Bullet::ricochet(GameObject& , const CollisionHit& hit)
123 collision_solid(hit);
127 Bullet::collision(GameObject& , const CollisionHit& )