00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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", 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", 1);
00077 }
00078 else {
00079 slow_fall ();
00080 }
00081 }
00082
00083 void UnstableTile::slow_fall (void)
00084 {
00085
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", 1);
00094 physic.set_gravity_modifier (.10);
00095 physic.enable_gravity (true);
00096 slowfall_timer = 0.5;
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", 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
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
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