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 "trigger/trigger_base.hpp" 00018 00019 #include "object/player.hpp" 00020 #include "sprite/sprite.hpp" 00021 00022 TriggerBase::TriggerBase() : 00023 sprite(), 00024 lasthit(false), 00025 hit(false), 00026 losetouch_listeners() 00027 { 00028 set_group(COLGROUP_TOUCHABLE); 00029 } 00030 00031 TriggerBase::~TriggerBase() 00032 { 00033 // unregister remove_listener hooks, so nobody will try to call us after we've been destroyed 00034 for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) { 00035 Player* p = *i; 00036 p->del_remove_listener(this); 00037 } 00038 losetouch_listeners.clear(); 00039 } 00040 00041 void 00042 TriggerBase::update(float ) 00043 { 00044 if (lasthit && !hit) { 00045 for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) { 00046 Player* p = *i; 00047 event(*p, EVENT_LOSETOUCH); 00048 p->del_remove_listener(this); 00049 } 00050 losetouch_listeners.clear(); 00051 } 00052 lasthit = hit; 00053 hit = false; 00054 } 00055 00056 void 00057 TriggerBase::draw(DrawingContext& context) 00058 { 00059 if(!sprite.get()) 00060 return; 00061 00062 sprite->draw(context, get_pos(), LAYER_TILES+1); 00063 } 00064 00065 HitResponse 00066 TriggerBase::collision(GameObject& other, const CollisionHit& ) 00067 { 00068 Player* player = dynamic_cast<Player*> (&other); 00069 if(player) { 00070 hit = true; 00071 if(!lasthit) { 00072 losetouch_listeners.push_back(player); 00073 player->add_remove_listener(this); 00074 event(*player, EVENT_TOUCH); 00075 } 00076 } 00077 00078 return ABORT_MOVE; 00079 } 00080 00081 void 00082 TriggerBase::object_removed(GameObject* object) 00083 { 00084 for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) { 00085 Player* p = *i; 00086 if (p == object) { 00087 losetouch_listeners.erase(i); 00088 break; 00089 } 00090 } 00091 } 00092 00093 /* EOF */