1 //  SuperTuxKart - a fun racing game with go-kart
2 //  Copyright (C) 2019 SuperTuxKart-Team
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License
6 //  as published by the Free Software Foundation; either version 3
7 //  of the License, or (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, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 
18 #include "tracks/check_trigger.hpp"
19 #include "karts/abstract_kart.hpp"
20 #include "modes/world.hpp"
21 #include "utils/time.hpp"
22 
23 /** Constructor for a check trigger.
24  *  \param center Center point of this trigger
25  *  \param distance Kart within it between center will trigger
26  *  \param triggering_function callback function to be used when triggered
27  */
CheckTrigger(const Vec3 & center,float distance,std::function<void (int)> triggering_function)28 CheckTrigger::CheckTrigger(const Vec3& center, float distance,
29                            std::function<void(int)> triggering_function)
30             : CheckStructure(),
31               m_center(center), m_distance2(distance * distance),
32               m_triggering_function(triggering_function)
33 {
34     m_last_triggered_time = StkTime::getMonoTimeMs();
35 }   // CheckSphere
36 
37 // ----------------------------------------------------------------------------
38 /** Copied from item state.
39  */
isTriggered(const Vec3 & old_pos,const Vec3 & new_pos,int kart_id)40 bool CheckTrigger::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos,
41                                int kart_id)
42 {
43     // kart_id will be -1 if called by CheckManager::getChecklineTriggering
44     if (kart_id < 0 || kart_id >= (int)World::getWorld()->getNumKarts())
45         return false;
46     if (m_last_triggered_time + 2000 > StkTime::getMonoTimeMs())
47         return false;
48     AbstractKart* k = World::getWorld()->getKart(kart_id);
49     if ((k->getXYZ() - m_center).length2() < m_distance2)
50     {
51         m_last_triggered_time = StkTime::getMonoTimeMs();
52         return true;
53     }
54     return false;
55 }   // isTriggered
56