src/badguy/kugelblitz.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "badguy/kugelblitz.hpp"
00018 
00019 #include <math.h>
00020 
00021 #include "math/random_generator.hpp"
00022 #include "object/camera.hpp"
00023 #include "object/player.hpp"
00024 #include "sprite/sprite.hpp"
00025 #include "supertux/object_factory.hpp"
00026 #include "supertux/sector.hpp"
00027 #include "util/reader.hpp"
00028 
00029 #define  LIFETIME 5
00030 #define  MOVETIME 0.75
00031 #define  BASE_SPEED 200
00032 #define  RAND_SPEED 150
00033 
00034 Kugelblitz::Kugelblitz(const Reader& reader) :
00035   BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"), 
00036   pos_groundhit(),
00037   groundhit_pos_set(false),
00038   dying(),
00039   movement_timer(),
00040   lifetime(),
00041   direction(),
00042   state()
00043 {
00044   reader.get("x", start_position.x);
00045   sprite->set_action("falling");
00046   physic.enable_gravity(false);
00047   countMe = false;
00048 }
00049 
00050 void
00051 Kugelblitz::initialize()
00052 {
00053   physic.set_velocity_y(300);
00054   physic.set_velocity_x(-20); //fall a little to the left
00055   direction = 1;
00056   dying = false;
00057 }
00058 
00059 void
00060 Kugelblitz::collision_solid(const CollisionHit& chit)
00061 {
00062   hit(chit);
00063 }
00064 
00065 HitResponse
00066 Kugelblitz::collision_player(Player& player, const CollisionHit& )
00067 {
00068   if(player.is_invincible()) {
00069     explode();
00070     return ABORT_MOVE;
00071   }
00072   // hit from above?
00073   if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
00074      (get_bbox().p1.y + get_bbox().p2.y) / 2) {
00075     // if it's not is it possible to squish us, then this will hurt
00076     if(!collision_squished(player))
00077       player.kill(false);
00078     explode();
00079     return FORCE_MOVE;
00080   }
00081   player.kill(false);
00082   explode();
00083   return FORCE_MOVE;
00084 }
00085 
00086 HitResponse
00087 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
00088 {
00089   //Let the Kugelblitz explode, too? The problem with that is that
00090   //two Kugelblitzes would cancel each other out on contact...
00091   other.kill_fall();
00092   return hit(chit);
00093 }
00094 
00095 HitResponse
00096 Kugelblitz::hit(const CollisionHit& hit)
00097 {
00098   // hit floor?
00099   if(hit.bottom) {
00100     if (!groundhit_pos_set)
00101     {
00102       pos_groundhit = get_pos();
00103       groundhit_pos_set = true;
00104     }
00105     sprite->set_action("flying");
00106     physic.set_velocity_y(0);
00107     //Set random initial speed and direction
00108     direction = gameRandom.rand(2)? 1: -1;
00109     int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
00110     physic.set_velocity_x(speed);
00111     movement_timer.start(MOVETIME);
00112     lifetime.start(LIFETIME);
00113 
00114   } else if(hit.top) { // bumped on roof
00115     physic.set_velocity_y(0);
00116   }
00117 
00118   return CONTINUE;
00119 }
00120 
00121 void
00122 Kugelblitz::active_update(float elapsed_time)
00123 {
00124   if (lifetime.check()) {
00125     explode();
00126   }
00127   else {
00128     if (groundhit_pos_set) {
00129       if (movement_timer.check()) {
00130         if (direction == 1) direction = -1; else direction = 1;
00131         int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction;
00132         physic.set_velocity_x(speed);
00133         movement_timer.start(MOVETIME);
00134       }
00135     }
00136     /*
00137       if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
00138       //HIT WATER
00139       Sector::current()->add_object(new Electrifier(75,1421,1.5));
00140       Sector::current()->add_object(new Electrifier(76,1422,1.5));
00141       explode();
00142       }
00143       if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) {
00144       //HIT ELECTRIFIED WATER
00145       explode();
00146       }
00147     */
00148   }
00149   BadGuy::active_update(elapsed_time);
00150 }
00151 
00152 void
00153 Kugelblitz::kill_fall()
00154 {
00155 }
00156 
00157 void
00158 Kugelblitz::explode()
00159 {
00160   if (!dying) {
00161     sprite->set_action("pop");
00162     lifetime.start(0.2f);
00163     dying = true;
00164   }
00165   else remove_me();
00166 }
00167 
00168 void
00169 Kugelblitz::try_activate()
00170 {
00171   // Much smaller offscreen distances to pop out of nowhere and surprise Tux
00172   float X_OFFSCREEN_DISTANCE = 400;
00173   float Y_OFFSCREEN_DISTANCE = 600;
00174 
00175   Player* player = get_nearest_player();
00176   if (!player) return;
00177   Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
00178   if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
00179     set_state(STATE_ACTIVE);
00180     if (!is_initialized) {
00181 
00182       // if starting direction was set to AUTO, this is our chance to re-orient the badguy
00183       if (start_dir == AUTO) {
00184         Player* player = get_nearest_player();
00185         if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
00186           dir = RIGHT;
00187         } else {
00188           dir = LEFT;
00189         }
00190       }
00191 
00192       initialize();
00193       is_initialized = true;
00194     }
00195     activate();
00196   }
00197 }
00198 
00199 /* EOF */

Generated on Mon Jun 9 03:38:17 2014 for SuperTux by  doxygen 1.5.1