src/badguy/sspiky.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/sspiky.hpp"
00018 
00019 #include "object/player.hpp"
00020 #include "sprite/sprite.hpp"
00021 #include "supertux/object_factory.hpp"
00022 
00023 static const float WALKSPEED = 80;
00024 
00025 SSpiky::SSpiky(const Reader& reader)
00026   : WalkingBadguy(reader, "images/creatures/spiky/sleepingspiky.sprite", "left", "right"), state(SSPIKY_SLEEPING)
00027 {
00028   walk_speed = WALKSPEED;
00029   max_drop_height = -1;
00030 }
00031 
00032 void
00033 SSpiky::initialize()
00034 {
00035   state = SSPIKY_SLEEPING;
00036   physic.set_velocity_x(0);
00037   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
00038 }
00039 
00040 void
00041 SSpiky::collision_solid(const CollisionHit& hit)
00042 {
00043   if(state != SSPIKY_WALKING) {
00044     BadGuy::collision_solid(hit);
00045     return;
00046   }
00047   WalkingBadguy::collision_solid(hit);
00048 }
00049 
00050 HitResponse
00051 SSpiky::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
00052 {
00053   if(state != SSPIKY_WALKING) {
00054     return BadGuy::collision_badguy(badguy, hit);
00055   }
00056   return WalkingBadguy::collision_badguy(badguy, hit);
00057 }
00058 
00059 void
00060 SSpiky::active_update(float elapsed_time) {
00061 
00062   if(state == SSPIKY_WALKING) {
00063     WalkingBadguy::active_update(elapsed_time);
00064     return;
00065   }
00066 
00067   if(state == SSPIKY_SLEEPING) {
00068 
00069     Player* player = this->get_nearest_player();
00070     if (player) {
00071       Rectf mb = this->get_bbox();
00072       Rectf pb = player->get_bbox();
00073 
00074       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
00075       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
00076       bool inReach_top = (pb.p2.y >= mb.p1.y);
00077       bool inReach_bottom = (pb.p1.y <= mb.p2.y);
00078 
00079       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
00080         // wake up
00081         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right", 1);
00082         state = SSPIKY_WAKING;
00083       }
00084     }
00085 
00086     BadGuy::active_update(elapsed_time);
00087   }
00088 
00089   if(state == SSPIKY_WAKING) {
00090     if(sprite->animation_done()) {
00091       // start walking
00092       state = SSPIKY_WALKING;
00093       WalkingBadguy::initialize();
00094     }
00095 
00096     BadGuy::active_update(elapsed_time);
00097   }
00098 }
00099 
00100 void
00101 SSpiky::freeze()
00102 {
00103   WalkingBadguy::freeze();
00104   sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
00105   state = SSPIKY_WALKING; // if we get hit while sleeping, wake up :)
00106 }
00107 
00108 bool
00109 SSpiky::is_freezable() const
00110 {
00111   return true;
00112 }
00113 
00114 /* EOF */

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