2012-12-27 11:12:10 -05:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2012 Joerg Henrichs
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#include "tracks/check_goal.hpp"
|
|
|
|
|
|
|
|
#include "io/xml_node.hpp"
|
2012-12-27 12:10:02 -05:00
|
|
|
#include "tracks/track.hpp"
|
|
|
|
#include "tracks/track_object_manager.hpp"
|
2012-12-28 09:11:33 -05:00
|
|
|
#include "modes/soccer_world.hpp"
|
2012-12-27 11:12:10 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/** Constructor for a check goal line.
|
2012-12-27 17:09:08 -05:00
|
|
|
* \param node XML node containing the parameters for this goal line.
|
2012-12-27 11:12:10 -05:00
|
|
|
* \param index Index of this check structure in the check manager.
|
|
|
|
*/
|
|
|
|
CheckGoal::CheckGoal(const XMLNode &node, unsigned int index)
|
2012-12-27 17:09:08 -05:00
|
|
|
: CheckStructure(node, index)
|
2012-12-27 11:12:10 -05:00
|
|
|
{
|
2012-12-27 17:09:08 -05:00
|
|
|
// Determine the team for this goal
|
2012-12-27 12:10:02 -05:00
|
|
|
m_first_goal = false;
|
|
|
|
node.get("first_goal", &m_first_goal);
|
2012-12-27 17:09:08 -05:00
|
|
|
|
|
|
|
Vec3 p1, p2;
|
|
|
|
node.get("p1", &p1);
|
|
|
|
node.get("p2", &p2);
|
|
|
|
|
|
|
|
m_line.setLine( core::vector2df(p1.getX(), p1.getZ()),
|
|
|
|
core::vector2df(p2.getX(), p2.getZ()) );
|
2012-12-27 11:12:10 -05:00
|
|
|
} // CheckGoal
|
|
|
|
|
2012-12-27 12:10:02 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Checks the soccer balls to see if they crossed the line and trigger the goal accordingly.
|
|
|
|
*/
|
2013-02-11 17:30:50 -05:00
|
|
|
void CheckGoal::update(float dt)
|
2012-12-27 12:10:02 -05:00
|
|
|
{
|
|
|
|
World *world = World::getWorld();
|
|
|
|
assert(world);
|
2012-12-27 17:09:08 -05:00
|
|
|
|
2012-12-27 12:10:02 -05:00
|
|
|
Track* track = world->getTrack();
|
|
|
|
assert(track);
|
2012-12-27 17:09:08 -05:00
|
|
|
|
2012-12-27 12:10:02 -05:00
|
|
|
TrackObjectManager* tom = track->getTrackObjectManager();
|
|
|
|
assert(tom);
|
2012-12-27 17:09:08 -05:00
|
|
|
|
2012-12-27 12:10:02 -05:00
|
|
|
PtrVector<TrackObject>& objects = tom->getObjects();
|
2013-03-06 18:12:24 -05:00
|
|
|
unsigned int ball_index = 0;
|
2012-12-27 17:09:08 -05:00
|
|
|
for(int i=0; i<objects.size(); i++)
|
2012-12-27 12:10:02 -05:00
|
|
|
{
|
|
|
|
TrackObject* obj = objects.get(i);
|
|
|
|
if(!obj->isSoccerBall())
|
|
|
|
continue;
|
|
|
|
|
2013-04-03 21:15:37 -04:00
|
|
|
const Vec3 &xyz = obj->getPresentation<TrackObjectPresentationMesh>()->getNode()->getPosition();
|
2012-12-27 17:09:08 -05:00
|
|
|
if(isTriggered(m_previous_position[ball_index], xyz, ball_index))
|
2012-12-27 12:10:02 -05:00
|
|
|
{
|
|
|
|
if(UserConfigParams::m_check_debug)
|
|
|
|
printf("CHECK: Goal check structure %d triggered for object %s.\n",
|
2013-04-03 21:15:37 -04:00
|
|
|
m_index, obj->getPresentation<TrackObjectPresentationMesh>()->getNode()->getDebugName());
|
2012-12-27 17:09:08 -05:00
|
|
|
trigger(ball_index);
|
2012-12-27 12:10:02 -05:00
|
|
|
}
|
2012-12-27 17:09:08 -05:00
|
|
|
m_previous_position[ball_index] = xyz;
|
|
|
|
ball_index++;
|
2012-12-27 12:10:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-27 11:12:10 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Called when the check line is triggered. This function creates a cannon
|
|
|
|
* animation object and attaches it to the kart.
|
|
|
|
* \param kart_index The index of the kart that triggered the check line.
|
|
|
|
*/
|
|
|
|
void CheckGoal::trigger(unsigned int kart_index)
|
|
|
|
{
|
2012-12-28 09:11:33 -05:00
|
|
|
SoccerWorld* world = dynamic_cast<SoccerWorld*>(World::getWorld());
|
|
|
|
if(!world)
|
2012-12-27 17:09:08 -05:00
|
|
|
{
|
2012-12-28 09:11:33 -05:00
|
|
|
fprintf(stderr, "WARNING: no soccer world found, cannot count the points\n");
|
|
|
|
return;
|
2012-12-27 17:09:08 -05:00
|
|
|
}
|
2012-12-28 09:11:33 -05:00
|
|
|
|
|
|
|
world->onCheckGoalTriggered(m_first_goal);
|
2012-12-27 11:12:10 -05:00
|
|
|
} // CheckGoal
|
2012-12-27 17:09:08 -05:00
|
|
|
|
2013-03-06 18:12:24 -05:00
|
|
|
bool CheckGoal::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos,
|
|
|
|
unsigned int indx)
|
2012-12-27 17:09:08 -05:00
|
|
|
{
|
|
|
|
core::vector2df cross_point;
|
|
|
|
|
|
|
|
// Check if the finite line was actually crossed:
|
|
|
|
return m_line.intersectWith(core::line2df(old_pos.toIrrVector2d(),
|
|
|
|
new_pos.toIrrVector2d()),
|
|
|
|
cross_point);
|
|
|
|
|
|
|
|
} // isTriggered
|
|
|
|
|
|
|
|
void CheckGoal::reset(const Track &track)
|
|
|
|
{
|
|
|
|
const TrackObjectManager* tom = track.getTrackObjectManager();
|
|
|
|
assert(tom);
|
|
|
|
|
|
|
|
m_previous_position.clear();
|
|
|
|
|
|
|
|
const PtrVector<TrackObject>& objects = tom->getObjects();
|
|
|
|
for(int i=0; i<objects.size(); i++)
|
|
|
|
{
|
|
|
|
const TrackObject* obj = objects.get(i);
|
|
|
|
if(!obj->isSoccerBall())
|
|
|
|
continue;
|
|
|
|
|
2013-04-03 21:15:37 -04:00
|
|
|
const Vec3 &xyz = obj->getPresentation<TrackObjectPresentationMesh>()->getNode()->getPosition();
|
2012-12-27 17:09:08 -05:00
|
|
|
|
|
|
|
m_previous_position.push_back(xyz);
|
|
|
|
}
|
|
|
|
} // reset
|