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