Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / kugelblitz.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/kugelblitz.hpp"
18 #include "math/random_generator.hpp"
19 #include "object/camera.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
24
25 #define  LIFETIME 5
26 #define  MOVETIME 0.75
27 #define  BASE_SPEED 200
28 #define  RAND_SPEED 150
29
30 static const float X_OFFSCREEN_DISTANCE = 1600;
31 static const float Y_OFFSCREEN_DISTANCE = 1200;
32
33 Kugelblitz::Kugelblitz(const Reader& reader) :
34   BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"), 
35   pos_groundhit(),
36   groundhit_pos_set(false),
37   dying(),
38   movement_timer(),
39   lifetime(),
40   direction(),
41   state()
42 {
43   reader.get("x", start_position.x);
44   sprite->set_action("falling");
45   physic.enable_gravity(false);
46   countMe = false;
47 }
48
49 void
50 Kugelblitz::initialize()
51 {
52   physic.set_velocity_y(300);
53   physic.set_velocity_x(-20); //fall a little to the left
54   direction = 1;
55   dying = false;
56 }
57
58 void
59 Kugelblitz::collision_solid(const CollisionHit& chit)
60 {
61   hit(chit);
62 }
63
64 HitResponse
65 Kugelblitz::collision_player(Player& player, const CollisionHit& )
66 {
67   if(player.is_invincible()) {
68     explode();
69     return ABORT_MOVE;
70   }
71   // hit from above?
72   if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
73      (get_bbox().p1.y + get_bbox().p2.y) / 2) {
74     // if it's not is it possible to squish us, then this will hurt
75     if(!collision_squished(player))
76       player.kill(false);
77     explode();
78     return FORCE_MOVE;
79   }
80   player.kill(false);
81   explode();
82   return FORCE_MOVE;
83 }
84
85 HitResponse
86 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
87 {
88   //Let the Kugelblitz explode, too? The problem with that is that
89   //two Kugelblitzes would cancel each other out on contact...
90   other.kill_fall();
91   return hit(chit);
92 }
93
94 HitResponse
95 Kugelblitz::hit(const CollisionHit& hit)
96 {
97   // hit floor?
98   if(hit.bottom) {
99     if (!groundhit_pos_set)
100     {
101       pos_groundhit = get_pos();
102       groundhit_pos_set = true;
103     }
104     sprite->set_action("flying");
105     physic.set_velocity_y(0);
106     //Set random initial speed and direction
107     direction = systemRandom.rand(2)? 1: -1;
108     int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
109     physic.set_velocity_x(speed);
110     movement_timer.start(MOVETIME);
111     lifetime.start(LIFETIME);
112
113   } else if(hit.top) { // bumped on roof
114     physic.set_velocity_y(0);
115   }
116
117   return CONTINUE;
118 }
119
120 void
121 Kugelblitz::active_update(float elapsed_time)
122 {
123   if (lifetime.check()) {
124     explode();
125   }
126   else {
127     if (groundhit_pos_set) {
128       if (movement_timer.check()) {
129         if (direction == 1) direction = -1; else direction = 1;
130         int speed = (BASE_SPEED + (systemRandom.rand(RAND_SPEED))) * direction;
131         physic.set_velocity_x(speed);
132         movement_timer.start(MOVETIME);
133       }
134     }
135     /*
136       if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
137       //HIT WATER
138       Sector::current()->add_object(new Electrifier(75,1421,1.5));
139       Sector::current()->add_object(new Electrifier(76,1422,1.5));
140       explode();
141       }
142       if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
143       //HIT ELECTRIFIED WATER
144       explode();
145       }
146     */
147   }
148   BadGuy::active_update(elapsed_time);
149 }
150
151 void
152 Kugelblitz::kill_fall()
153 {
154 }
155
156 void
157 Kugelblitz::explode()
158 {
159   if (!dying) {
160     sprite->set_action("pop");
161     lifetime.start(0.2f);
162     dying = true;
163   }
164   else remove_me();
165 }
166
167 void
168 Kugelblitz::try_activate()
169 {
170   //FIXME: Don't activate Kugelblitz before it's on-screen
171   float scroll_x = Sector::current()->camera->get_translation().x;
172   float scroll_y = Sector::current()->camera->get_translation().y;
173
174   /* Activate badguys if they're just around the screen to avoid
175    * the effect of having badguys suddenly popping up from nowhere.
176    */
177   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
178       start_position.x < scroll_x - bbox.get_width() &&
179       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
180       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
181     dir = RIGHT;
182     set_state(STATE_ACTIVE);
183     activate();
184   } else if (start_position.x > scroll_x &&
185              start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
186              start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
187              start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
188     dir = LEFT;
189     set_state(STATE_ACTIVE);
190     activate();
191   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
192              start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
193              ((start_position.y > scroll_y &&
194                start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
195               (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
196                start_position.y < scroll_y))) {
197     dir = start_position.x < scroll_x ? RIGHT : LEFT;
198     set_state(STATE_ACTIVE);
199     activate();
200   } else if(state == STATE_INIT
201             && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
202             && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
203             && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
204             && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
205     dir = LEFT;
206     set_state(STATE_ACTIVE);
207     activate();
208   }
209 }
210
211 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz");
212
213 /* EOF */