src/object/unstable_tile.cpp

Go to the documentation of this file.
00001 //  SuperTux - Unstable Tile
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
00004 //  Copyright (C) 2010 Florian Forster <supertux at octo.it>
00005 //
00006 //  This program is free software: you can redistribute it and/or modify
00007 //  it under the terms of the GNU General Public License as published by
00008 //  the Free Software Foundation, either version 3 of the License, or
00009 //  (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 #include "object/unstable_tile.hpp"
00020 
00021 #include "object/explosion.hpp"
00022 #include "object/player.hpp"
00023 #include "sprite/sprite.hpp"
00024 #include "supertux/constants.hpp"
00025 #include "supertux/object_factory.hpp"
00026 
00027 UnstableTile::UnstableTile(const Reader& lisp) :
00028   MovingSprite(lisp, LAYER_TILES, COLGROUP_STATIC), 
00029   physic(),
00030   state(STATE_NORMAL),
00031   slowfall_timer()
00032 {
00033   sprite->set_action("normal");
00034   physic.set_gravity_modifier (.98);
00035   physic.enable_gravity (false);
00036 }
00037 
00038 HitResponse
00039 UnstableTile::collision(GameObject& other, const CollisionHit& )
00040 {
00041   if(state == STATE_NORMAL) {
00042     Player* player = dynamic_cast<Player*> (&other);
00043     if(player != NULL &&
00044        player->get_bbox().get_bottom() < get_bbox().get_top() + SHIFT_DELTA) {
00045       shake ();
00046     }
00047 
00048     if (dynamic_cast<Explosion*> (&other)) {
00049       shake ();
00050     }
00051   }
00052   return FORCE_MOVE;
00053 }
00054 
00055 void UnstableTile::shake (void)
00056 {
00057   if (state != STATE_NORMAL)
00058     return;
00059 
00060   if (sprite->has_action ("shake")) {
00061     state = STATE_SHAKE;
00062     this->set_action ("shake", /* loops = */ 1);
00063   }
00064   else {
00065     dissolve ();
00066   }
00067 }
00068 
00069 void UnstableTile::dissolve (void)
00070 {
00071   if ((state != STATE_NORMAL) && (state != STATE_SHAKE))
00072     return;
00073 
00074   if (sprite->has_action ("dissolve")) {
00075     state = STATE_DISSOLVE;
00076     this->set_action ("dissolve", /* loops = */ 1);
00077   }
00078   else {
00079     slow_fall ();
00080   }
00081 }
00082 
00083 void UnstableTile::slow_fall (void)
00084 {
00085   /* Only enter slow-fall if neither shake nor dissolve is available. */
00086   if (state != STATE_NORMAL) {
00087     this->fall_down ();
00088     return;
00089   }
00090 
00091   if (sprite->has_action ("fall-down")) {
00092     state = STATE_SLOWFALL;
00093     this->set_action ("fall-down", /* loops = */ 1);
00094     physic.set_gravity_modifier (.10);
00095     physic.enable_gravity (true);
00096     slowfall_timer = 0.5; /* Fall slowly for half a second. */
00097   }
00098   else {
00099     remove_me ();
00100   }
00101 }
00102 
00103 void UnstableTile::fall_down (void)
00104 {
00105   if ((state != STATE_NORMAL)
00106       && (state != STATE_SHAKE)
00107       && (state != STATE_DISSOLVE)
00108       && (state != STATE_SLOWFALL))
00109     return;
00110 
00111   if (sprite->has_action ("fall-down")) {
00112     state = STATE_FALL;
00113     this->set_action ("fall-down", /* loops = */ 1);
00114     physic.set_gravity_modifier (.98);
00115     physic.enable_gravity (true);
00116   }
00117   else {
00118     remove_me ();
00119   }
00120 }
00121 
00122 void
00123 UnstableTile::update(float elapsed_time)
00124 {
00125   switch (state)
00126   {
00127     case STATE_NORMAL:
00128       break;
00129 
00130     case STATE_SHAKE:
00131       if (sprite->animation_done())
00132         dissolve ();
00133       break;
00134 
00135     case STATE_DISSOLVE:
00136       if (sprite->animation_done()) {
00137         /* dissolving is done. Set to non-solid. */
00138         set_group (COLGROUP_DISABLED);
00139         fall_down ();
00140       }
00141       break;
00142 
00143     case STATE_SLOWFALL:
00144       if (slowfall_timer >= elapsed_time)
00145         slowfall_timer -= elapsed_time;
00146       else /* Switch to normal falling procedure */
00147         fall_down ();
00148       movement = physic.get_movement (elapsed_time);
00149       break;
00150 
00151     case STATE_FALL:
00152       if (sprite->animation_done())
00153         remove_me ();
00154       else
00155         movement = physic.get_movement (elapsed_time);
00156       break;
00157   }
00158 }
00159 
00160 /* EOF */

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