More -Weffc++ cleanup
[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   losetouch_listeners()
27 {
28   set_group(COLGROUP_TOUCHABLE);
29 }
30
31 TriggerBase::~TriggerBase()
32 {
33   // unregister remove_listener hooks, so nobody will try to call us after we've been destroyed
34   for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
35     Player* p = *i;
36     p->del_remove_listener(this);
37   }
38   losetouch_listeners.clear();
39 }
40
41 void
42 TriggerBase::update(float )
43 {
44   if (lasthit && !hit) {
45     for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
46       Player* p = *i;
47       event(*p, EVENT_LOSETOUCH);
48       p->del_remove_listener(this);
49     }
50     losetouch_listeners.clear();
51   }
52   lasthit = hit;
53   hit = false;
54 }
55
56 void
57 TriggerBase::draw(DrawingContext& context)
58 {
59   if(!sprite.get())
60     return;
61
62   sprite->draw(context, get_pos(), LAYER_TILES+1);
63 }
64
65 HitResponse
66 TriggerBase::collision(GameObject& other, const CollisionHit& )
67 {
68   Player* player = dynamic_cast<Player*> (&other);
69   if(player) {
70     hit = true;
71     if(!lasthit) {
72       losetouch_listeners.push_back(player);
73       player->add_remove_listener(this);
74       event(*player, EVENT_TOUCH);
75     }
76   }
77
78   return ABORT_MOVE;
79 }
80   
81 void 
82 TriggerBase::object_removed(GameObject* object)
83 {
84   for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
85     Player* p = *i;
86     if (p == object) {
87       losetouch_listeners.erase(i);
88       break;
89     }
90   }
91 }
92
93 /* EOF */