kugelblitz electrifies water on contact - electrified water is baaaaaaad
[supertux.git] / src / badguy / kugelblitz.cpp
1 //  $Id: Kugelblitz.cpp 2654 2005-06-29 14:16:22Z wansti $
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Marek Moeckel <wansti@gmx.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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "kugelblitz.hpp"
23 #include "sector.hpp"
24 #include "object/tilemap.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 Kugelblitz::Kugelblitz(const lisp::Lisp& reader)
33     : groundhit_pos_set(false)
34 {
35   reader.get("x", start_position.x);
36   start_position.y = 0; //place above visible area
37   bbox.set_size(63.8, 63.8);
38   sprite = sprite_manager->create("kugelblitz");
39   sprite->set_action("falling");
40   physic.enable_gravity(false);
41 }
42
43 void
44 Kugelblitz::write(lisp::Writer& writer)
45 {
46   writer.start_list("kugelblitz");
47
48   writer.write_float("x", start_position.x);
49   writer.write_float("y", start_position.y);
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 HitResponse
64 Kugelblitz::collision_solid(GameObject& , const CollisionHit& chit)
65 {
66   return 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(Player::SHRINK);
82       explode();
83     return FORCE_MOVE;
84   }
85   player.kill(Player::SHRINK);
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?
94   other.kill_fall();
95   return hit(chit);
96 }
97
98 HitResponse
99 Kugelblitz::hit(const CollisionHit& chit)
100 {
101   // hit floor?
102   if(chit.normal.y < -.5) {
103     if (!groundhit_pos_set)
104     {
105       pos_groundhit = get_pos();
106       groundhit_pos_set = true;
107     }
108     sprite->set_action("flying");
109     physic.set_velocity_y(0);
110     //Set random initial speed and direction
111     if ((rand() % 2) == 1) direction = 1; else direction = -1;
112     int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
113     physic.set_velocity_x(speed);
114     movement_timer.start(MOVETIME);
115     lifetime.start(LIFETIME);
116
117   } else if(chit.normal.y < .5) { // bumped on roof
118     physic.set_velocity_y(0);
119   }
120
121   return CONTINUE;
122 }
123
124 void
125 Kugelblitz::active_update(float elapsed_time)
126 {
127   if (electrify_timer.check()) {
128     Sector::current()->solids->change_all(1421,75);
129     Sector::current()->solids->change_all(1422,76);
130     explode();
131   }
132   else if (lifetime.check()) {
133     explode();
134   }
135   else {
136     if (groundhit_pos_set) {
137       if (movement_timer.check()) {
138         if (direction == 1) direction = -1; else direction = 1;
139         int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
140         physic.set_velocity_x(speed);
141         movement_timer.start(MOVETIME);
142       }
143     }
144     if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
145       //HIT WATER
146       Sector::current()->solids->change_all(75,1421);
147       Sector::current()->solids->change_all(76,1422);
148       physic.set_velocity_x(0);
149       physic.set_velocity_y(0);
150       electrify_timer.start(1);
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 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz")