-Started to move stuff from library back to main game
[supertux.git] / src / object / bullet.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include <math.h>
24 #include "bullet.h"
25 #include "resources.h"
26 #include "camera.h"
27 #include "sector.h"
28 #include "app/globals.h"
29 #include "sprite/sprite_manager.h"
30 #include "badguy/badguy.h"
31
32 static const float BULLET_XM = 300;
33 static const float BULLET_STARTING_YM = 0;
34
35 Bullet::Bullet(const Vector& pos, float xm, int dir, int kind_)
36   : kind(kind_), life_count(3), sprite(0)
37 {
38   bbox.set_pos(pos);
39   bbox.set_size(4, 4);
40
41   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
42   physic.set_velocity_x(speed + xm);
43   physic.set_velocity_y(-BULLET_STARTING_YM);
44
45   if (kind == ICE_BULLET) {
46     life_count = 6; //ice-bullets get "extra lives" for bumping off walls
47     sprite = sprite_manager->create("icebullet");
48   } else if(kind == FIRE_BULLET) {
49     sprite = sprite_manager->create("firebullet");
50   }
51 }
52
53 Bullet::~Bullet()
54 {
55   delete sprite;
56 }
57
58 void
59 Bullet::action(float elapsed_time)
60 {
61   if(kind == FIRE_BULLET) {
62     // @not completely framerate independant :-/
63     physic.set_velocity_y(physic.get_velocity_y() - 50 * elapsed_time);
64   }
65   if(physic.get_velocity_y() > 900)
66     physic.set_velocity_y(900);
67   else if(physic.get_velocity_y() < -900)
68     physic.set_velocity_y(-900);
69
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   sprite->draw(context, get_pos(), LAYER_OBJECTS);
90 }
91
92 HitResponse
93 Bullet::collision(GameObject& other, const CollisionHit& hit)
94 {
95   if(other.get_flags() & FLAG_SOLID) {
96     if(fabsf(hit.normal.y) > .5) { // roof or floor bump
97       physic.set_velocity_y(-physic.get_velocity_y());
98       life_count -= 1;
99     } else { // bumped left or right
100       if(kind == FIRE_BULLET)
101         remove_me();
102       else
103         physic.set_velocity_x(-physic.get_velocity_x());
104     }
105     
106     return CONTINUE;
107   }
108
109   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
110   if(badguy) {
111     badguy->kill_fall();
112     remove_me();
113     return FORCE_MOVE;
114   }
115  
116   return FORCE_MOVE;
117 }
118
119