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