Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / trigger / trigger_base.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "trigger/trigger_base.hpp"
18
19 #include "object/player.hpp"
20 #include "sprite/sprite.hpp"
21
22 TriggerBase::TriggerBase() :
23   sprite(),
24   lasthit(false), 
25   hit(false)
26 {
27   set_group(COLGROUP_TOUCHABLE);
28 }
29
30 TriggerBase::~TriggerBase()
31 {
32   // unregister remove_listener hooks, so nobody will try to call us after we've been destroyed
33   for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
34     Player* p = *i;
35     p->del_remove_listener(this);
36   }
37   losetouch_listeners.clear();
38 }
39
40 void
41 TriggerBase::update(float )
42 {
43   if (lasthit && !hit) {
44     for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
45       Player* p = *i;
46       event(*p, EVENT_LOSETOUCH);
47       p->del_remove_listener(this);
48     }
49     losetouch_listeners.clear();
50   }
51   lasthit = hit;
52   hit = false;
53 }
54
55 void
56 TriggerBase::draw(DrawingContext& context)
57 {
58   if(!sprite.get())
59     return;
60
61   sprite->draw(context, get_pos(), LAYER_TILES+1);
62 }
63
64 HitResponse
65 TriggerBase::collision(GameObject& other, const CollisionHit& )
66 {
67   Player* player = dynamic_cast<Player*> (&other);
68   if(player) {
69     hit = true;
70     if(!lasthit) {
71       losetouch_listeners.push_back(player);
72       player->add_remove_listener(this);
73       event(*player, EVENT_TOUCH);
74     }
75   }
76
77   return ABORT_MOVE;
78 }
79   
80 void 
81 TriggerBase::object_removed(GameObject* object)
82 {
83   for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
84     Player* p = *i;
85     if (p == object) {
86       losetouch_listeners.erase(i);
87       break;
88     }
89   }
90 }
91
92 /* EOF */