changed worldmap format a bit to be more consistent with level format
[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 #include <config.h>
21
22 #include <math.h>
23 #include "bullet.h"
24 #include "resources.h"
25 #include "camera.h"
26 #include "sector.h"
27 #include "sprite/sprite_manager.h"
28 #include "badguy/badguy.h"
29 #include "main.h"
30
31 static const float BULLET_XM = 300;
32 static const float BULLET_STARTING_YM = 0;
33
34 Bullet::Bullet(const Vector& pos, float xm, int dir, int kind_)
35   : kind(kind_), life_count(3), sprite(0)
36 {
37   bbox.set_pos(pos);
38   bbox.set_size(4, 4);
39
40   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
41   physic.set_velocity_x(speed + xm);
42   physic.set_velocity_y(-BULLET_STARTING_YM);
43
44   if (kind == ICE_BULLET) {
45     life_count = 6; //ice-bullets get "extra lives" for bumping off walls
46     sprite = sprite_manager->create("icebullet");
47   } else if(kind == FIRE_BULLET) {
48     sprite = sprite_manager->create("firebullet");
49   }
50 }
51
52 Bullet::~Bullet()
53 {
54   delete sprite;
55 }
56
57 void
58 Bullet::update(float elapsed_time)
59 {
60   if(kind == FIRE_BULLET) {
61     // @not completely framerate independant :-/
62     physic.set_velocity_y(physic.get_velocity_y() - 50 * elapsed_time);
63   }
64   if(physic.get_velocity_y() > 900)
65     physic.set_velocity_y(900);
66   else if(physic.get_velocity_y() < -900)
67     physic.set_velocity_y(-900);
68
69   float scroll_x =
70     Sector::current()->camera->get_translation().x;
71   float scroll_y =
72     Sector::current()->camera->get_translation().y;
73   if (get_pos().x < scroll_x ||
74       get_pos().x > scroll_x + SCREEN_WIDTH ||
75 //     get_pos().y < scroll_y ||
76       get_pos().y > scroll_y + SCREEN_HEIGHT ||
77       life_count <= 0) {
78     remove_me();
79     return;
80   }
81
82   movement = physic.get_movement(elapsed_time);
83 }
84
85 void
86 Bullet::draw(DrawingContext& context)
87 {
88   sprite->draw(context, get_pos(), LAYER_OBJECTS);
89 }
90
91 HitResponse
92 Bullet::collision(GameObject& other, const CollisionHit& hit)
93 {
94   if(other.get_flags() & FLAG_SOLID) {
95     if(fabsf(hit.normal.y) > .5) { // roof or floor bump
96       physic.set_velocity_y(-physic.get_velocity_y());
97       life_count -= 1;
98     } else { // bumped left or right
99       if(kind == FIRE_BULLET)
100         remove_me();
101       else
102         physic.set_velocity_x(-physic.get_velocity_x());
103     }
104     
105     return CONTINUE;
106   }
107
108   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
109   if(badguy) {
110     badguy->kill_fall();
111     remove_me();
112     return FORCE_MOVE;
113   }
114  
115   return FORCE_MOVE;
116 }
117
118