From 14f7f080c0a938fe5fe91baf2f23de745a327347 Mon Sep 17 00:00:00 2001 From: funto66 Date: Thu, 27 Dec 2012 22:09:08 +0000 Subject: [PATCH] CheckGoal now is just a CheckStructure and not a CheckLine anymore. This fixes bugs due to arrays sized with the number of karts instead of the number of soccer balls git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/christmas@12313 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/tracks/check_goal.cpp | 94 +++++++++++++++++++++++++++++----- src/tracks/check_goal.hpp | 17 ++++-- src/tracks/check_structure.hpp | 1 + src/tracks/track.hpp | 2 +- 4 files changed, 97 insertions(+), 17 deletions(-) diff --git a/src/tracks/check_goal.cpp b/src/tracks/check_goal.cpp index e8ce02c61..34a560274 100644 --- a/src/tracks/check_goal.cpp +++ b/src/tracks/check_goal.cpp @@ -25,14 +25,22 @@ #include /** Constructor for a check goal line. - * \param node XML node containing the parameters for this checkline. + * \param node XML node containing the parameters for this goal line. * \param index Index of this check structure in the check manager. */ CheckGoal::CheckGoal(const XMLNode &node, unsigned int index) - : CheckLine(node, index) + : CheckStructure(node, index) { + // Determine the team for this goal m_first_goal = false; node.get("first_goal", &m_first_goal); + + 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()) ); } // CheckGoal // ---------------------------------------------------------------------------- @@ -43,6 +51,46 @@ void CheckGoal::update(float dt) OVERRIDE { World *world = World::getWorld(); assert(world); + + Track* track = world->getTrack(); + assert(track); + + TrackObjectManager* tom = track->getTrackObjectManager(); + assert(tom); + + PtrVector& objects = tom->getObjects(); + int ball_index = 0; + for(int i=0; iisSoccerBall()) + continue; + + const Vec3 &xyz = obj->getNode()->getPosition(); + if(isTriggered(m_previous_position[ball_index], xyz, ball_index)) + { + if(UserConfigParams::m_check_debug) + printf("CHECK: Goal check structure %d triggered for object %s.\n", + m_index, obj->getDebugName()); + trigger(ball_index); + } + m_previous_position[ball_index] = xyz; + ball_index++; + } +} + +// ---------------------------------------------------------------------------- +/** 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) +{ + printf("*** TODO: GOOOOOOOOOAAAAAAALLLLLL!!!! ***\n"); + + /* + World *world = World::getWorld(); + assert(world); Track* track = world->getTrack(); assert(track); TrackObjectManager* tom = track->getTrackObjectManager(); @@ -65,14 +113,36 @@ void CheckGoal::update(float dt) OVERRIDE } m_previous_position[i] = xyz; } -} - -// ---------------------------------------------------------------------------- -/** 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) -{ - printf("*** TODO: GOOOOOOOOOAAAAAAALLLLLL!!!! ***\n"); + */ } // CheckGoal + +bool CheckGoal::isTriggered(const Vec3 &old_pos, const Vec3 &new_pos, int indx) +{ + 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& objects = tom->getObjects(); + for(int i=0; iisSoccerBall()) + continue; + + const Vec3 &xyz = obj->getNode()->getPosition(); + + m_previous_position.push_back(xyz); + } +} // reset diff --git a/src/tracks/check_goal.hpp b/src/tracks/check_goal.hpp index bb2280c0a..8e23512b4 100644 --- a/src/tracks/check_goal.hpp +++ b/src/tracks/check_goal.hpp @@ -19,12 +19,14 @@ #ifndef HEADER_CHECK_GOAL_HPP #define HEADER_CHECK_GOAL_HPP -#include "animations/animation_base.hpp" -#include "tracks/check_line.hpp" +#include "tracks/check_structure.hpp" #include "utils/cpp2011.h" +#include +using namespace irr; class CheckManager; class XMLNode; +class Track; /** * \brief Implements a simple checkline that will score a point when the @@ -32,15 +34,22 @@ class XMLNode; * * \ingroup tracks */ -class CheckGoal : public CheckLine +class CheckGoal : public CheckStructure { private: - bool m_first_goal; + /** Which team is this goal for? */ + bool m_first_goal; + + /** The line that is tested for being crossed. */ + core::line2df m_line; + public: CheckGoal(const XMLNode &node, unsigned int index); virtual ~CheckGoal() {} virtual void update(float dt) OVERRIDE; virtual void trigger(unsigned int kart_index); + virtual bool isTriggered(const Vec3 &old_pos, const Vec3 &new_pos, int indx) OVERRIDE; + virtual void reset(const Track &track) OVERRIDE; }; // CheckLine #endif diff --git a/src/tracks/check_structure.hpp b/src/tracks/check_structure.hpp index 6c95b3377..10ff68d06 100644 --- a/src/tracks/check_structure.hpp +++ b/src/tracks/check_structure.hpp @@ -41,6 +41,7 @@ class CheckManager; * CT_TOGGLE: Toggles the specified other check structures (active to * inactive and vice versa. * CT_CANNON: A check line that 'shoots' the kart to a specified location. + * CT_GOAL: A goal line in soccer mode. * Each check structure can be active or inactive. Only lap counters are * initialised to be active, all other check structures are inactive. * diff --git a/src/tracks/track.hpp b/src/tracks/track.hpp index cb1722b19..8f89bc1f6 100644 --- a/src/tracks/track.hpp +++ b/src/tracks/track.hpp @@ -531,7 +531,7 @@ public: /** Returns true if the normals of this track can be smoothed. */ bool smoothNormals() const { return m_smooth_normals; } // ------------------------------------------------------------------------ - TrackObjectManager* getTrackObjectManager() {return m_track_object_manager;} + TrackObjectManager* getTrackObjectManager() const {return m_track_object_manager;} /** Get list of challenges placed on that world. Works only for overworld. */ const std::vector& getChallengeList() const