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