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