little work on the kugelblitz
[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 "object/tilemap.hpp"
24 #include "tile.hpp"
25
26 #define  LIFETIME 5
27 #define  MOVETIME 0.75
28 #define  BASE_SPEED 200
29 #define  RAND_SPEED 150
30
31 Kugelblitz::Kugelblitz(const lisp::Lisp& reader)
32     : groundhit_pos_set(false)
33 {
34   reader.get("x", start_position.x);
35   start_position.y = 0; //place above visible area
36   bbox.set_size(63.8, 63.8);
37   sprite = sprite_manager->create("kugelblitz");
38   sprite->set_action("falling");
39   physic.enable_gravity(false);
40 }
41
42 void
43 Kugelblitz::write(lisp::Writer& writer)
44 {
45   writer.start_list("kugelblitz");
46
47   writer.write_float("x", start_position.x);
48   writer.write_float("y", start_position.y);
49
50   writer.end_list("kugelblitz");
51 }
52
53 void
54 Kugelblitz::activate()
55 {
56   physic.set_velocity_y(-300);
57   physic.set_velocity_x(-20); //fall a little to the left
58   direction = 1;
59   dying = false;
60 }
61
62 HitResponse
63 Kugelblitz::collision_solid(GameObject& , const CollisionHit& chit)
64 {
65   return hit(chit);
66 }
67
68 HitResponse
69 Kugelblitz::collision_player(Player& player, const CollisionHit& )
70 {
71   if(player.is_invincible()) {
72     explode();
73     return ABORT_MOVE;
74   }
75   // hit from above?
76   if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
77       (get_bbox().p1.y + get_bbox().p2.y) / 2) {
78     // if it's not is it possible to squish us, then this will hurt
79     if(!collision_squished(player))
80       player.kill(Player::SHRINK);
81       explode();
82     return FORCE_MOVE;
83   }
84   player.kill(Player::SHRINK);
85   explode();
86   return FORCE_MOVE;
87 }
88
89 HitResponse
90 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
91 {
92   //Let the Kugelblitz explode, too? The problem with that is that
93   //two Kugelblitzes would cancel each other out on contact...
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 (lifetime.check()) {
128     explode();
129   }
130   else {
131     if (groundhit_pos_set) {
132       if (movement_timer.check()) {
133         if (direction == 1) direction = -1; else direction = 1;
134         int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
135         physic.set_velocity_x(speed);
136         movement_timer.start(MOVETIME);
137       }
138     }
139     if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() != 0)
140     std::cout << Sector::current()->solids->get_tile_at(get_pos())->getAttributes() << std::endl;
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   BadGuy::active_update(elapsed_time);  
153 }
154
155 void
156 Kugelblitz::kill_fall()
157 {
158 }
159
160 void
161 Kugelblitz::explode()
162 {
163   if (!dying) {
164     sprite->set_action("pop");
165     lifetime.start(0.2);
166     dying = true;
167   }
168   else remove_me();
169 }
170
171 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz")