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