src/badguy/stumpy.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/stumpy.hpp"
00018 
00019 #include "audio/sound_manager.hpp"
00020 #include "math/random_generator.hpp"
00021 #include "object/player.hpp"
00022 #include "object/sprite_particle.hpp"
00023 #include "supertux/object_factory.hpp"
00024 #include "supertux/sector.hpp"
00025 
00026 #include <math.h>
00027 
00028 static const float STUMPY_SPEED = 120;
00029 static const float INVINCIBLE_TIME = 1;
00030 
00031 Stumpy::Stumpy(const Reader& reader) :
00032   WalkingBadguy(reader, "images/creatures/mr_tree/stumpy.sprite","left","right"), 
00033   mystate(STATE_NORMAL),
00034   invincible_timer()
00035 {
00036   walk_speed = STUMPY_SPEED;
00037   max_drop_height = 16;
00038   sound_manager->preload("sounds/mr_treehit.ogg");
00039 }
00040 
00041 Stumpy::Stumpy(const Vector& pos, Direction d) :
00042   WalkingBadguy(pos, d, "images/creatures/mr_tree/stumpy.sprite","left","right"), 
00043   mystate(STATE_INVINCIBLE),
00044   invincible_timer()
00045 {
00046   walk_speed = STUMPY_SPEED;
00047   max_drop_height = 16;
00048   sound_manager->preload("sounds/mr_treehit.ogg");
00049   invincible_timer.start(INVINCIBLE_TIME);
00050 }
00051 
00052 void
00053 Stumpy::initialize()
00054 {
00055   switch (mystate) {
00056     case STATE_INVINCIBLE:
00057       sprite->set_action(dir == LEFT ? "dizzy-left" : "dizzy-right");
00058       bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00059       physic.set_velocity_x(0);
00060       break;
00061     case STATE_NORMAL:
00062       WalkingBadguy::initialize();
00063       break;
00064   }
00065 }
00066 
00067 void
00068 Stumpy::active_update(float elapsed_time)
00069 {
00070   switch (mystate) {
00071     case STATE_INVINCIBLE:
00072       if (invincible_timer.check()) {
00073         mystate = STATE_NORMAL;
00074         WalkingBadguy::initialize();
00075       }
00076       BadGuy::active_update(elapsed_time);
00077       break;
00078     case STATE_NORMAL:
00079       WalkingBadguy::active_update(elapsed_time);
00080       break;
00081   }
00082 }
00083 
00084 bool
00085 Stumpy::collision_squished(GameObject& object)
00086 {
00087 
00088   // if we're still invincible, we ignore the hit
00089   if (mystate == STATE_INVINCIBLE) {
00090     sound_manager->play("sounds/mr_treehit.ogg", get_pos());
00091     Player* player = dynamic_cast<Player*>(&object);
00092     if (player) player->bounce(*this);
00093     return true;
00094   }
00095 
00096   // if we can die, we do
00097   if (mystate == STATE_NORMAL) {
00098     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
00099     set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00100     kill_squished(object);
00101     // spawn some particles
00102     // TODO: provide convenience function in MovingSprite or MovingObject?
00103     for (int i = 0; i < 25; i++) {
00104       Vector ppos = bbox.get_middle();
00105       float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
00106       float velocity = graphicsRandom.randf(45, 90);
00107       float vx = sin(angle)*velocity;
00108       float vy = -cos(angle)*velocity;
00109       Vector pspeed = Vector(vx, vy);
00110       Vector paccel = Vector(0, 100);
00111       Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
00112     }
00113 
00114     return true;
00115 
00116   }
00117 
00118   //TODO: exception?
00119   return true;
00120 }
00121 
00122 void
00123 Stumpy::collision_solid(const CollisionHit& hit)
00124 {
00125   update_on_ground_flag(hit);
00126 
00127   switch (mystate) {
00128     case STATE_INVINCIBLE:
00129       if(hit.top || hit.bottom) {
00130         physic.set_velocity_y(0);
00131       }
00132       if(hit.left || hit.right) {
00133         physic.set_velocity_x(0);
00134       }
00135       break;
00136     case STATE_NORMAL:
00137       WalkingBadguy::collision_solid(hit);
00138       break;
00139   }
00140 }
00141 
00142 HitResponse
00143 Stumpy::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
00144 {
00145   switch (mystate) {
00146     case STATE_INVINCIBLE:
00147       if(hit.top || hit.bottom) {
00148         physic.set_velocity_y(0);
00149       }
00150       if(hit.left || hit.right) {
00151         physic.set_velocity_x(0);
00152       }
00153       return CONTINUE;
00154       break;
00155     case STATE_NORMAL:
00156       return WalkingBadguy::collision_badguy(badguy, hit);
00157       break;
00158   }
00159   return CONTINUE;
00160 }
00161 
00162 /* EOF */

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