src/trigger/door.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 "audio/sound_manager.hpp"
00018 #include "object/player.hpp"
00019 #include "sprite/sprite_manager.hpp"
00020 #include "supertux/game_session.hpp"
00021 #include "supertux/object_factory.hpp"
00022 #include "supertux/sector.hpp"
00023 #include "trigger/door.hpp"
00024 #include "util/reader.hpp"
00025 
00026 Door::Door(const Reader& reader) :
00027   state(CLOSED),
00028   target_sector(),
00029   target_spawnpoint(),
00030   script(),
00031   sprite(),
00032   stay_open_timer()
00033 {
00034   reader.get("x", bbox.p1.x);
00035   reader.get("y", bbox.p1.y);
00036   reader.get("sector", target_sector);
00037   reader.get("spawnpoint", target_spawnpoint);
00038 
00039   reader.get("script", script);
00040 
00041   sprite = sprite_manager->create("images/objects/door/door.sprite");
00042   sprite->set_action("closed");
00043   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00044 
00045   sound_manager->preload("sounds/door.wav");
00046 }
00047 
00048 Door::Door(int x, int y, std::string sector, std::string spawnpoint) :
00049   state(CLOSED),
00050   target_sector(),
00051   target_spawnpoint(),
00052   script(),
00053   sprite(),
00054   stay_open_timer()
00055 {
00056   bbox.set_pos(Vector(x, y));
00057   target_sector = sector;
00058   target_spawnpoint = spawnpoint;
00059 
00060   sprite = sprite_manager->create("images/objects/door/door.sprite");
00061   sprite->set_action("closed");
00062   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00063 
00064   sound_manager->preload("sounds/door.wav");
00065 }
00066 
00067 Door::~Door()
00068 {
00069 }
00070 
00071 void
00072 Door::update(float )
00073 {
00074   switch (state) {
00075     case CLOSED:
00076       break;
00077     case OPENING:
00078       // if door has finished opening, start timer and keep door open
00079       if(sprite->animation_done()) {
00080         state = OPEN;
00081         sprite->set_action("open");
00082         stay_open_timer.start(1.0);
00083       }
00084       break;
00085     case OPEN:
00086       // if door was open long enough, start closing it
00087       if (stay_open_timer.check()) {
00088         state = CLOSING;
00089         sprite->set_action("closing", 1);
00090       }
00091       break;
00092     case CLOSING:
00093       // if door has finished closing, keep it shut
00094       if(sprite->animation_done()) {
00095         state = CLOSED;
00096         sprite->set_action("closed");
00097       }
00098       break;
00099   }
00100 }
00101 
00102 void
00103 Door::draw(DrawingContext& context)
00104 {
00105   sprite->draw(context, bbox.p1, LAYER_BACKGROUNDTILES+1);
00106 }
00107 
00108 void
00109 Door::event(Player& , EventType type)
00110 {
00111   switch (state) {
00112     case CLOSED:
00113       // if door was activated, start opening it
00114       if (type == EVENT_ACTIVATE) {
00115         state = OPENING;
00116         sound_manager->play("sounds/door.wav");
00117         sprite->set_action("opening", 1);
00118       }
00119       break;
00120     case OPENING:
00121       break;
00122     case OPEN:
00123       break;
00124     case CLOSING:
00125       break;
00126   }
00127 }
00128 
00129 HitResponse
00130 Door::collision(GameObject& other, const CollisionHit& hit)
00131 {
00132   switch (state) {
00133     case CLOSED:
00134       break;
00135     case OPENING:
00136       break;
00137     case OPEN:
00138     {
00139       // if door is open and was touched by a player, teleport the player
00140       Player* player = dynamic_cast<Player*> (&other);
00141       if (player) {
00142         state = CLOSING;
00143         sprite->set_action("closing", 1);
00144         if(!script.empty()) {
00145           std::istringstream stream(script);
00146           Sector::current()->run_script(stream, "Door");
00147         }
00148 
00149         if(!target_sector.empty()) {
00150           GameSession::current()->respawn(target_sector, target_spawnpoint);
00151         }
00152       }
00153     }
00154     break;
00155     case CLOSING:
00156       break;
00157   }
00158 
00159   return TriggerBase::collision(other, hit);
00160 }
00161 
00162 /* EOF */

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