30e6dfaa0e0cc005ed0e85af97129a9d36c3a3b9
[supertux.git] / src / object / bullet.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <math.h>
23 #include "bullet.hpp"
24 #include "resources.hpp"
25 #include "camera.hpp"
26 #include "sector.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "badguy/badguy.hpp"
29 #include "main.hpp"
30
31 namespace {
32   const float BULLET_XM = 600;
33   const float BULLET_STARTING_YM = 0;
34 }
35
36 Bullet::Bullet(const Vector& pos, float xm, int dir, BonusType type)
37   : life_count(3), type(type)
38 {
39   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
40   physic.set_velocity_x(speed + xm);
41
42   if(type == FIRE_BONUS) {
43     sprite.reset(sprite_manager->create("images/objects/bullets/firebullet.sprite"));
44   } else if(type == ICE_BONUS) {
45     life_count = 10;
46     sprite.reset(sprite_manager->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.reset(sprite_manager->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   // remove bullet when it's offscreen
65   float scroll_x =
66     Sector::current()->camera->get_translation().x;
67   float scroll_y =
68     Sector::current()->camera->get_translation().y;
69   if (get_pos().x < scroll_x ||
70       get_pos().x > scroll_x + SCREEN_WIDTH ||
71 //     get_pos().y < scroll_y ||
72       get_pos().y > scroll_y + SCREEN_HEIGHT ||
73       life_count <= 0) {
74     remove_me();
75     return;
76   }
77
78   movement = physic.get_movement(elapsed_time);
79 }
80
81 void
82 Bullet::draw(DrawingContext& context)
83 {
84   sprite->draw(context, get_pos(), LAYER_OBJECTS);
85 }
86
87 void
88 Bullet::collision_solid(const CollisionHit& hit)
89 {
90   if(hit.top || hit.bottom) {
91     physic.set_velocity_y(-physic.get_velocity_y());
92     life_count--;
93   } else if(hit.left || hit.right) {
94     if(type == ICE_BONUS) {
95       physic.set_velocity_x(-physic.get_velocity_x());
96       life_count--;
97     } else
98       remove_me();
99   }
100 }
101
102 void
103 Bullet::ricochet(GameObject& , const CollisionHit& hit)
104 {
105   collision_solid(hit);
106 }
107
108 HitResponse
109 Bullet::collision(GameObject& , const CollisionHit& )
110 {
111   return FORCE_MOVE;
112 }