remove outdated comment
[supertux.git] / src / object / bullet.cpp
1 #include <config.h>
2
3 #include <math.h>
4 #include "bullet.h"
5 #include "resources.h"
6 #include "camera.h"
7 #include "sector.h"
8 #include "app/globals.h"
9 #include "special/sprite_manager.h"
10 #include "badguy/badguy.h"
11
12 static const float BULLET_XM = 300;
13 static const float BULLET_STARTING_YM = 0;
14
15 Bullet::Bullet(const Vector& pos, float xm, int dir, int kind_)
16   : kind(kind_), life_count(3), sprite(0)
17 {
18   bbox.set_pos(pos);
19   bbox.set_size(4, 4);
20
21   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
22   physic.set_velocity_x(speed + xm);
23   physic.set_velocity_y(-BULLET_STARTING_YM);
24
25   if (kind == ICE_BULLET) {
26     life_count = 6; //ice-bullets get "extra lives" for bumping off walls
27     sprite = sprite_manager->create("icebullet");
28   } else if(kind == FIRE_BULLET) {
29     sprite = sprite_manager->create("firebullet");
30   }
31 }
32
33 Bullet::~Bullet()
34 {
35   delete sprite;
36 }
37
38 void
39 Bullet::action(float elapsed_time)
40 {
41   if(kind == FIRE_BULLET) {
42     // @not completely framerate independant :-/
43     physic.set_velocity_y(physic.get_velocity_y() - 50 * elapsed_time);
44   }
45   if(physic.get_velocity_y() > 900)
46     physic.set_velocity_y(900);
47   else if(physic.get_velocity_y() < -900)
48     physic.set_velocity_y(-900);
49
50   float scroll_x =
51     Sector::current()->camera->get_translation().x;
52   float scroll_y =
53     Sector::current()->camera->get_translation().y;
54   if (get_pos().x < scroll_x ||
55       get_pos().x > scroll_x + SCREEN_WIDTH ||
56 //     get_pos().y < scroll_y ||
57       get_pos().y > scroll_y + SCREEN_HEIGHT ||
58       life_count <= 0) {
59     remove_me();
60     return;
61   }
62
63   movement = physic.get_movement(elapsed_time);
64 }
65
66 void
67 Bullet::draw(DrawingContext& context)
68 {
69   sprite->draw(context, get_pos(), LAYER_OBJECTS);
70 }
71
72 HitResponse
73 Bullet::collision(GameObject& other, const CollisionHit& hit)
74 {
75   if(other.get_flags() & FLAG_SOLID) {
76     if(fabsf(hit.normal.y) > .5) { // roof or floor bump
77       physic.set_velocity_y(-physic.get_velocity_y());
78       life_count -= 1;
79     } else { // bumped left or right
80       if(kind == FIRE_BULLET)
81         remove_me();
82       else
83         physic.set_velocity_x(-physic.get_velocity_x());
84     }
85     
86     return CONTINUE;
87   }
88
89   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
90   if(badguy) {
91     badguy->kill_fall();
92     remove_me();
93     return FORCE_MOVE;
94   }
95  
96   return FORCE_MOVE;
97 }
98
99