X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fbadguy%2Fkugelblitz.cpp;h=6b6e81e1c2c608ceb72908b1b2a1e385204c2db2;hb=6fa15ae7e414bb51ba83b7f4af8aca60444d3399;hp=be289ffce77fbc81c0a50042232c4c34f7bae723;hpb=3dd252d40f9d0e213c8ddbad989cd24cf5f0a9b5;p=supertux.git diff --git a/src/badguy/kugelblitz.cpp b/src/badguy/kugelblitz.cpp index be289ffce..6b6e81e1c 100644 --- a/src/badguy/kugelblitz.cpp +++ b/src/badguy/kugelblitz.cpp @@ -1,69 +1,65 @@ -// $Id: Kugelblitz.cpp 2654 2005-06-29 14:16:22Z wansti $ -// // SuperTux -// Copyright (C) 2005 Marek Moeckel +// Copyright (C) 2006 Matthias Braun // -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -// +// // You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. -#include +// along with this program. If not, see . + +#include "badguy/kugelblitz.hpp" -#include "kugelblitz.hpp" -#include "sector.hpp" -#include "object/tilemap.hpp" -#include "tile.hpp" +#include + +#include "math/random_generator.hpp" +#include "object/camera.hpp" +#include "object/player.hpp" +#include "sprite/sprite.hpp" +#include "supertux/object_factory.hpp" +#include "supertux/sector.hpp" +#include "util/reader.hpp" #define LIFETIME 5 #define MOVETIME 0.75 #define BASE_SPEED 200 #define RAND_SPEED 150 -Kugelblitz::Kugelblitz(const lisp::Lisp& reader) - : groundhit_pos_set(false) +Kugelblitz::Kugelblitz(const Reader& reader) : + BadGuy(reader, "images/creatures/kugelblitz/kugelblitz.sprite"), + pos_groundhit(), + groundhit_pos_set(false), + dying(), + movement_timer(), + lifetime(), + direction(), + state() { reader.get("x", start_position.x); - start_position.y = 0; //place above visible area - bbox.set_size(63.8, 63.8); - sprite = sprite_manager->create("kugelblitz"); sprite->set_action("falling"); physic.enable_gravity(false); + countMe = false; } void -Kugelblitz::write(lisp::Writer& writer) +Kugelblitz::initialize() { - writer.start_list("kugelblitz"); - - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); - - writer.end_list("kugelblitz"); -} - -void -Kugelblitz::activate() -{ - physic.set_velocity_y(-300); + physic.set_velocity_y(300); physic.set_velocity_x(-20); //fall a little to the left direction = 1; dying = false; } -HitResponse -Kugelblitz::collision_solid(GameObject& , const CollisionHit& chit) +void +Kugelblitz::collision_solid(const CollisionHit& chit) { - return hit(chit); + hit(chit); } HitResponse @@ -75,14 +71,14 @@ Kugelblitz::collision_player(Player& player, const CollisionHit& ) } // hit from above? if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y < - (get_bbox().p1.y + get_bbox().p2.y) / 2) { + (get_bbox().p1.y + get_bbox().p2.y) / 2) { // if it's not is it possible to squish us, then this will hurt if(!collision_squished(player)) - player.kill(Player::SHRINK); - explode(); + player.kill(false); + explode(); return FORCE_MOVE; } - player.kill(Player::SHRINK); + player.kill(false); explode(); return FORCE_MOVE; } @@ -97,10 +93,10 @@ Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit) } HitResponse -Kugelblitz::hit(const CollisionHit& chit) +Kugelblitz::hit(const CollisionHit& hit) { // hit floor? - if(chit.normal.y < -.5) { + if(hit.bottom) { if (!groundhit_pos_set) { pos_groundhit = get_pos(); @@ -109,13 +105,13 @@ Kugelblitz::hit(const CollisionHit& chit) sprite->set_action("flying"); physic.set_velocity_y(0); //Set random initial speed and direction - if ((rand() % 2) == 1) direction = 1; else direction = -1; - int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction; + direction = gameRandom.rand(2)? 1: -1; + int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction; physic.set_velocity_x(speed); movement_timer.start(MOVETIME); lifetime.start(LIFETIME); - } else if(chit.normal.y < .5) { // bumped on roof + } else if(hit.top) { // bumped on roof physic.set_velocity_y(0); } @@ -125,33 +121,32 @@ Kugelblitz::hit(const CollisionHit& chit) void Kugelblitz::active_update(float elapsed_time) { - if (electrify_timer.check()) { - Sector::current()->solids->change_all(1421,75); - Sector::current()->solids->change_all(1422,76); - explode(); - } - else if (lifetime.check()) { + if (lifetime.check()) { explode(); } else { if (groundhit_pos_set) { if (movement_timer.check()) { if (direction == 1) direction = -1; else direction = 1; - int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction; + int speed = (BASE_SPEED + (gameRandom.rand(RAND_SPEED))) * direction; physic.set_velocity_x(speed); movement_timer.start(MOVETIME); } } - if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) { + /* + if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) { //HIT WATER - Sector::current()->solids->change_all(75,1421); - Sector::current()->solids->change_all(76,1422); - physic.set_velocity_x(0); - physic.set_velocity_y(0); - electrify_timer.start(1); - } + Sector::current()->add_object(new Electrifier(75,1421,1.5)); + Sector::current()->add_object(new Electrifier(76,1422,1.5)); + explode(); + } + if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 48) { + //HIT ELECTRIFIED WATER + explode(); + } + */ } - BadGuy::active_update(elapsed_time); + BadGuy::active_update(elapsed_time); } void @@ -164,10 +159,41 @@ Kugelblitz::explode() { if (!dying) { sprite->set_action("pop"); - lifetime.start(0.2); + lifetime.start(0.2f); dying = true; } else remove_me(); } -IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz") +void +Kugelblitz::try_activate() +{ + // Much smaller offscreen distances to pop out of nowhere and surprise Tux + float X_OFFSCREEN_DISTANCE = 400; + float Y_OFFSCREEN_DISTANCE = 600; + + Player* player = get_nearest_player(); + if (!player) return; + Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle(); + if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) { + set_state(STATE_ACTIVE); + if (!is_initialized) { + + // if starting direction was set to AUTO, this is our chance to re-orient the badguy + if (start_dir == AUTO) { + Player* player = get_nearest_player(); + if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) { + dir = RIGHT; + } else { + dir = LEFT; + } + } + + initialize(); + is_initialized = true; + } + activate(); + } +} + +/* EOF */