c1c774270ff66a842c42d915e63f1d3af50c271d
[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
19 #include <math.h>
20
21 #include "math/random_generator.hpp"
22 #include "object/camera.hpp"
23 #include "object/player.hpp"
24 #include "sprite/sprite.hpp"
25 #include "sprite/sprite_manager.hpp"
26 #include "supertux/object_factory.hpp"
27 #include "supertux/sector.hpp"
28 #include "util/reader.hpp"
29
30 #define  LIFETIME 5
31 #define  MOVETIME 0.75
32 #define  BASE_SPEED 200
33 #define  RAND_SPEED 150
34
35 Kugelblitz::Kugelblitz(const Reader& reader) :
36   BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"), 
37   pos_groundhit(),
38   groundhit_pos_set(false),
39   dying(),
40   movement_timer(),
41   lifetime(),
42   direction(),
43   state(),
44   light(0.0f,0.0f,0.0f),
45   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite"))
46 {
47   reader.get("x", start_position.x);
48   sprite->set_action("falling");
49   physic.enable_gravity(false);
50   countMe = false;
51   
52   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
53   lightsprite->set_color(Color(0.2f, 0.1f, 0.0f));  
54 }
55
56 void
57 Kugelblitz::initialize()
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 void
66 Kugelblitz::collision_solid(const CollisionHit& chit)
67 {
68   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(false);
84     explode();
85     return FORCE_MOVE;
86   }
87   player.kill(false);
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& hit)
103 {
104   // hit floor?
105   if(hit.bottom) {
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     direction = gameRandom.rand(2)? 1: -1;
115     int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
116     physic.set_velocity_x(speed);
117     movement_timer.start(MOVETIME);
118     lifetime.start(LIFETIME);
119
120   } else if(hit.top) { // 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 + (gameRandom.rand(RAND_SPEED))) * direction;
138         physic.set_velocity_x(speed);
139         movement_timer.start(MOVETIME);
140       }
141     }
142     /*
143       if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
144       //HIT WATER
145       Sector::current()->add_object(new Electrifier(75,1421,1.5));
146       Sector::current()->add_object(new Electrifier(76,1422,1.5));
147       explode();
148       }
149       if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
150       //HIT ELECTRIFIED WATER
151       explode();
152       }
153     */
154   }
155   BadGuy::active_update(elapsed_time);
156 }
157
158 void
159 Kugelblitz::draw(DrawingContext& context)
160 {
161   sprite->draw(context, get_pos(), layer);
162   
163   //Only draw light in dark areas
164   context.get_light( get_bbox().get_middle(), &light );
165   if (light.red + light.green < 2.0){
166     context.push_target();
167     context.set_target(DrawingContext::LIGHTMAP);
168     sprite->draw(context, get_pos(), layer);
169     lightsprite->draw(context, get_bbox().get_middle(), 0);
170     context.pop_target();
171   }
172 }
173
174 void
175 Kugelblitz::kill_fall()
176 {
177 }
178
179 void
180 Kugelblitz::explode()
181 {
182   if (!dying) {
183     sprite->set_action("pop");
184     lifetime.start(0.2f);
185     dying = true;
186   }
187   else remove_me();
188 }
189
190 void
191 Kugelblitz::try_activate()
192 {
193   // Much smaller offscreen distances to pop out of nowhere and surprise Tux
194   float X_OFFSCREEN_DISTANCE = 400;
195   float Y_OFFSCREEN_DISTANCE = 600;
196
197   Player* player = get_nearest_player();
198   if (!player) return;
199   Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
200   if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
201     set_state(STATE_ACTIVE);
202     if (!is_initialized) {
203
204       // if starting direction was set to AUTO, this is our chance to re-orient the badguy
205       if (start_dir == AUTO) {
206         Player* player = get_nearest_player();
207         if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
208           dir = RIGHT;
209         } else {
210           dir = LEFT;
211         }
212       }
213
214       initialize();
215       is_initialized = true;
216     }
217     activate();
218   }
219 }
220
221 /* EOF */