Fix for coverity #29360
[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   light(0.0f,0.0f,0.0f),
44   lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light.sprite"))
45 {
46   reader.get("x", start_position.x);
47   sprite->set_action("falling");
48   physic.enable_gravity(false);
49   countMe = false;
50
51   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
52   lightsprite->set_color(Color(0.2f, 0.1f, 0.0f));
53 }
54
55 void
56 Kugelblitz::initialize()
57 {
58   physic.set_velocity_y(300);
59   physic.set_velocity_x(-20); //fall a little to the left
60   direction = 1;
61   dying = false;
62 }
63
64 void
65 Kugelblitz::collision_solid(const CollisionHit& chit)
66 {
67   hit(chit);
68 }
69
70 HitResponse
71 Kugelblitz::collision_player(Player& player, const CollisionHit& )
72 {
73   if(player.is_invincible()) {
74     explode();
75     return ABORT_MOVE;
76   }
77   // hit from above?
78   if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
79      (get_bbox().p1.y + get_bbox().p2.y) / 2) {
80     // if it's not is it possible to squish us, then this will hurt
81     if(!collision_squished(player))
82       player.kill(false);
83     explode();
84     return FORCE_MOVE;
85   }
86   player.kill(false);
87   explode();
88   return FORCE_MOVE;
89 }
90
91 HitResponse
92 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
93 {
94   //Let the Kugelblitz explode, too? The problem with that is that
95   //two Kugelblitzes would cancel each other out on contact...
96   other.kill_fall();
97   return hit(chit);
98 }
99
100 HitResponse
101 Kugelblitz::hit(const CollisionHit& hit_)
102 {
103   // hit floor?
104   if(hit_.bottom) {
105     if (!groundhit_pos_set)
106     {
107       pos_groundhit = get_pos();
108       groundhit_pos_set = true;
109     }
110     sprite->set_action("flying");
111     physic.set_velocity_y(0);
112     //Set random initial speed and direction
113     direction = gameRandom.rand(2)? 1: -1;
114     int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
115     physic.set_velocity_x(speed);
116     movement_timer.start(MOVETIME);
117     lifetime.start(LIFETIME);
118
119   } else if(hit_.top) { // bumped on roof
120     physic.set_velocity_y(0);
121   }
122
123   return CONTINUE;
124 }
125
126 void
127 Kugelblitz::active_update(float elapsed_time)
128 {
129   if (lifetime.check()) {
130     explode();
131   }
132   else {
133     if (groundhit_pos_set) {
134       if (movement_timer.check()) {
135         if (direction == 1) direction = -1; else direction = 1;
136         int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
137         physic.set_velocity_x(speed);
138         movement_timer.start(MOVETIME);
139       }
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   }
154   BadGuy::active_update(elapsed_time);
155 }
156
157 void
158 Kugelblitz::draw(DrawingContext& context)
159 {
160   sprite->draw(context, get_pos(), layer);
161
162   //Only draw light in dark areas
163   context.get_light( get_bbox().get_middle(), &light );
164   if (light.red + light.green < 2.0){
165     context.push_target();
166     context.set_target(DrawingContext::LIGHTMAP);
167     sprite->draw(context, get_pos(), layer);
168     lightsprite->draw(context, get_bbox().get_middle(), 0);
169     context.pop_target();
170   }
171 }
172
173 void
174 Kugelblitz::kill_fall()
175 {
176 }
177
178 void
179 Kugelblitz::explode()
180 {
181   if (!dying) {
182     sprite->set_action("pop");
183     lifetime.start(0.2f);
184     dying = true;
185   }
186   else remove_me();
187 }
188
189 void
190 Kugelblitz::try_activate()
191 {
192   // Much smaller offscreen distances to pop out of nowhere and surprise Tux
193   float X_OFFSCREEN_DISTANCE = 400;
194   float Y_OFFSCREEN_DISTANCE = 600;
195
196   Player* player_ = get_nearest_player();
197   if (!player_) return;
198   Vector dist = player_->get_bbox().get_middle() - get_bbox().get_middle();
199   if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
200     set_state(STATE_ACTIVE);
201     if (!is_initialized) {
202
203       // if starting direction was set to AUTO, this is our chance to re-orient the badguy
204       if (start_dir == AUTO) {
205         Player* player__ = get_nearest_player();
206         if (player__ && (player__->get_bbox().p1.x > get_bbox().p2.x)) {
207           dir = RIGHT;
208         } else {
209           dir = LEFT;
210         }
211       }
212
213       initialize();
214       is_initialized = true;
215     }
216     activate();
217   }
218 }
219
220 /* EOF */