Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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
27 #define  LIFETIME 5
28 #define  MOVETIME 0.75
29 #define  BASE_SPEED 200
30 #define  RAND_SPEED 150
31
32 static const float X_OFFSCREEN_DISTANCE = 1600;
33 static const float Y_OFFSCREEN_DISTANCE = 1200;
34
35 Kugelblitz::Kugelblitz(const lisp::Lisp& reader)
36     : groundhit_pos_set(false)
37 {
38   reader.get("x", start_position.x);
39   start_position.y = 0; //place above visible area
40   bbox.set_size(63.8, 63.8);
41   sprite = sprite_manager->create("images/creatures/kugelblitz/kugelblitz.sprite");
42   sprite->set_action("falling");
43   physic.enable_gravity(false);
44 }
45
46 void
47 Kugelblitz::write(lisp::Writer& writer)
48 {
49   writer.start_list("kugelblitz");
50
51   writer.write_float("x", start_position.x);
52
53   writer.end_list("kugelblitz");
54 }
55
56 void
57 Kugelblitz::activate()
58 {
59   physic.set_velocity_y(-300);
60   physic.set_velocity_x(-20); //fall a little to the left
61   direction = 1;
62   dying = false;
63 }
64
65 HitResponse
66 Kugelblitz::collision_solid(GameObject& , const CollisionHit& chit)
67 {
68   return hit(chit);
69 }
70
71 HitResponse
72 Kugelblitz::collision_player(Player& player, const CollisionHit& )
73 {
74   if(player.is_invincible()) {
75     explode();
76     return ABORT_MOVE;
77   }
78   // hit from above?
79   if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
80       (get_bbox().p1.y + get_bbox().p2.y) / 2) {
81     // if it's not is it possible to squish us, then this will hurt
82     if(!collision_squished(player))
83       player.kill(Player::SHRINK);
84       explode();
85     return FORCE_MOVE;
86   }
87   player.kill(Player::SHRINK);
88   explode();
89   return FORCE_MOVE;
90 }
91
92 HitResponse
93 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
94 {
95   //Let the Kugelblitz explode, too? The problem with that is that
96   //two Kugelblitzes would cancel each other out on contact...
97   other.kill_fall();
98   return hit(chit);
99 }
100
101 HitResponse
102 Kugelblitz::hit(const CollisionHit& chit)
103 {
104   // hit floor?
105   if(chit.normal.y < -.5) {
106     if (!groundhit_pos_set)
107     {
108       pos_groundhit = get_pos();
109       groundhit_pos_set = true;
110     }
111     sprite->set_action("flying");
112     physic.set_velocity_y(0);
113     //Set random initial speed and direction
114     if ((rand() % 2) == 1) direction = 1; else direction = -1;
115     int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
116     physic.set_velocity_x(speed);
117     movement_timer.start(MOVETIME);
118     lifetime.start(LIFETIME);
119
120   } else if(chit.normal.y < .5) { // bumped on roof
121     physic.set_velocity_y(0);
122   }
123
124   return CONTINUE;
125 }
126
127 void
128 Kugelblitz::active_update(float elapsed_time)
129 {
130   if (lifetime.check()) {
131     explode();
132   }
133   else {
134     if (groundhit_pos_set) {
135       if (movement_timer.check()) {
136         if (direction == 1) direction = -1; else direction = 1;
137         int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
138         physic.set_velocity_x(speed);
139         movement_timer.start(MOVETIME);
140       }
141     }
142     if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
143       //HIT WATER
144       Sector::current()->add_object(new Electrifier(75,1421,1.5));
145       Sector::current()->add_object(new Electrifier(76,1422,1.5));
146       explode();
147     }
148     if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
149       //HIT ELECTRIFIED WATER
150       explode();
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")