- fix debugging functions for killing player
[supertux.git] / src / badguy / kugelblitz.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 "kugelblitz.hpp"
23 #include "object/tilemap.hpp"
24 #include "object/camera.hpp"
25 #include "tile.hpp"
26 #include "random_generator.hpp"
27
28 #define  LIFETIME 5
29 #define  MOVETIME 0.75
30 #define  BASE_SPEED 200
31 #define  RAND_SPEED 150
32
33 static const float X_OFFSCREEN_DISTANCE = 1600;
34 static const float Y_OFFSCREEN_DISTANCE = 1200;
35
36 Kugelblitz::Kugelblitz(const lisp::Lisp& reader)
37     : groundhit_pos_set(false)
38 {
39   reader.get("x", start_position.x);
40   start_position.y = 0; //place above visible area
41   bbox.set_size(63.8, 63.8);
42   sprite = sprite_manager->create("images/creatures/kugelblitz/kugelblitz.sprite");
43   sprite->set_action("falling");
44   physic.enable_gravity(false);
45 }
46
47 void
48 Kugelblitz::write(lisp::Writer& writer)
49 {
50   writer.start_list("kugelblitz");
51
52   writer.write_float("x", start_position.x);
53
54   writer.end_list("kugelblitz");
55 }
56
57 void
58 Kugelblitz::activate()
59 {
60   physic.set_velocity_y(-300);
61   physic.set_velocity_x(-20); //fall a little to the left
62   direction = 1;
63   dying = false;
64 }
65
66 HitResponse
67 Kugelblitz::collision_solid(GameObject& , const CollisionHit& chit)
68 {
69   return hit(chit);
70 }
71
72 HitResponse
73 Kugelblitz::collision_player(Player& player, const CollisionHit& )
74 {
75   if(player.is_invincible()) {
76     explode();
77     return ABORT_MOVE;
78   }
79   // hit from above?
80   if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
81       (get_bbox().p1.y + get_bbox().p2.y) / 2) {
82     // if it's not is it possible to squish us, then this will hurt
83     if(!collision_squished(player))
84       player.kill(false);
85       explode();
86     return FORCE_MOVE;
87   }
88   player.kill(false);
89   explode();
90   return FORCE_MOVE;
91 }
92
93 HitResponse
94 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
95 {
96   //Let the Kugelblitz explode, too? The problem with that is that
97   //two Kugelblitzes would cancel each other out on contact...
98   other.kill_fall();
99   return hit(chit);
100 }
101
102 HitResponse
103 Kugelblitz::hit(const CollisionHit& chit)
104 {
105   // hit floor?
106   if(chit.normal.y < -.5) {
107     if (!groundhit_pos_set)
108     {
109       pos_groundhit = get_pos();
110       groundhit_pos_set = true;
111     }
112     sprite->set_action("flying");
113     physic.set_velocity_y(0);
114     //Set random initial speed and direction
115     direction = systemRandom.rand(2)? 1: -1;
116     int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
117     physic.set_velocity_x(speed);
118     movement_timer.start(MOVETIME);
119     lifetime.start(LIFETIME);
120
121   } else if(chit.normal.y < .5) { // bumped on roof
122     physic.set_velocity_y(0);
123   }
124
125   return CONTINUE;
126 }
127
128 void
129 Kugelblitz::active_update(float elapsed_time)
130 {
131   if (lifetime.check()) {
132     explode();
133   }
134   else {
135     if (groundhit_pos_set) {
136       if (movement_timer.check()) {
137         if (direction == 1) direction = -1; else direction = 1;
138         int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
139         physic.set_velocity_x(speed);
140         movement_timer.start(MOVETIME);
141       }
142     }
143     if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
144       //HIT WATER
145       Sector::current()->add_object(new Electrifier(75,1421,1.5));
146       Sector::current()->add_object(new Electrifier(76,1422,1.5));
147       explode();
148     }
149     if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
150       //HIT ELECTRIFIED WATER
151       explode();
152     }
153   }
154   BadGuy::active_update(elapsed_time);  
155 }
156
157 void
158 Kugelblitz::kill_fall()
159 {
160 }
161
162 void
163 Kugelblitz::explode()
164 {
165   if (!dying) {
166     sprite->set_action("pop");
167     lifetime.start(0.2);
168     dying = true;
169   }
170   else remove_me();
171 }
172
173 void
174 Kugelblitz::try_activate()
175 {
176   //FIXME: Don't activate Kugelblitz before it's on-screen
177   float scroll_x = Sector::current()->camera->get_translation().x;
178   float scroll_y = Sector::current()->camera->get_translation().y;
179
180   /* Activate badguys if they're just around the screen to avoid
181    * the effect of having badguys suddenly popping up from nowhere.
182    */
183   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
184       start_position.x < scroll_x - bbox.get_width() &&
185       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
186       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
187     dir = RIGHT;
188     set_state(STATE_ACTIVE);
189     activate();
190   } else if (start_position.x > scroll_x &&
191       start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
192       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
193       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
194     dir = LEFT;
195     set_state(STATE_ACTIVE);
196     activate();
197   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
198       start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
199       ((start_position.y > scroll_y &&
200         start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
201        (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
202         start_position.y < scroll_y))) {
203     dir = start_position.x < scroll_x ? RIGHT : LEFT;
204     set_state(STATE_ACTIVE);
205     activate();
206   } else if(state == STATE_INIT
207       && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
208       && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
209       && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
210       && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
211     dir = LEFT;
212     set_state(STATE_ACTIVE);
213     activate();
214   }
215 }
216
217 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz")