Skip to content

Commit

Permalink
Reset the ball if out of navmesh after 2 seconds
Browse files Browse the repository at this point in the history
Plus some code clean up
  • Loading branch information
Benau committed Jan 29, 2016
1 parent ad21b6d commit 40e193e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
79 changes: 40 additions & 39 deletions src/modes/soccer_world.cpp
Expand Up @@ -97,27 +97,30 @@ void SoccerWorld::reset()
m_can_score_points = true;
m_red_goal = 0;
m_blue_goal = 0;

// Reset original positions for the soccer balls
TrackObjectManager* tom = getTrack()->getTrackObjectManager();
assert(tom);
m_red_scorers.clear();
m_red_score_times.clear();
m_blue_scorers.clear();
m_blue_score_times.clear();
m_ball_hitter = -1;
m_ball = NULL;
m_red_defender = -1;
m_blue_defender = -1;
m_ball_invalid_timer = 0.0f;

TrackObjectManager* tom = getTrack()->getTrackObjectManager();
assert(tom);
PtrVector<TrackObject>& objects = tom->getObjects();
for(unsigned int i=0; i<objects.size(); i++)
for (unsigned int i = 0; i < objects.size(); i++)
{
TrackObject* obj = objects.get(i);
if(!obj->isSoccerBall())
continue;

obj->reset();
obj->getPhysicalObject()->reset();
m_ball = obj;
// Handle one ball only
break;
}
if (!m_ball)
Log::fatal("SoccerWorld","Ball is missing in soccer field, abort.");

if (m_goal_sound != NULL &&
m_goal_sound->getStatus() == SFXBase::SFX_PLAYING)
Expand All @@ -128,6 +131,7 @@ void SoccerWorld::reset()
initKartList();
resetAllNodes();
initGoalNodes();
resetBall();

} // reset

Expand All @@ -150,7 +154,7 @@ void SoccerWorld::update(float dt)
WorldWithRank::update(dt);
WorldWithRank::updateTrack(dt);

updateBallPosition();
updateBallPosition(dt);
if (m_track->hasNavMesh())
{
updateKartNodes();
Expand Down Expand Up @@ -240,21 +244,7 @@ void SoccerWorld::onCheckGoalTriggered(bool first_goal)
}
}

// Reset original positions for the soccer balls
TrackObjectManager* tom = getTrack()->getTrackObjectManager();
assert(tom);

PtrVector<TrackObject>& objects = tom->getObjects();
for(unsigned int i=0; i<objects.size(); i++)
{
TrackObject* obj = objects.get(i);
if(!obj->isSoccerBall())
continue;

obj->reset();
obj->getPhysicalObject()->reset();
}

resetBall();
//Resetting the ball triggers the goal check line one more time.
//This ensures that only one goal is counted, and the second is ignored.
m_can_score_points = !m_can_score_points;
Expand Down Expand Up @@ -460,29 +450,33 @@ void SoccerWorld::updateKartNodes()
//-----------------------------------------------------------------------------
/** Localize the ball on the navigation mesh.
*/
void SoccerWorld::updateBallPosition()
void SoccerWorld::updateBallPosition(float dt)
{
if (isRaceOver()) return;

TrackObjectManager* tom = getTrack()->getTrackObjectManager();
assert(tom);

PtrVector<TrackObject>& objects = tom->getObjects();
for (unsigned int i = 0; i < objects.size(); i++)
{
TrackObject* obj = objects.get(i);
if (obj->isSoccerBall())
{
m_ball_position = obj->getPresentation<TrackObjectPresentationMesh>()
->getNode()->getPosition();
break;
}
}
m_ball_position = m_ball->getPresentation<TrackObjectPresentationMesh>()
->getNode()->getPosition();

if (m_track->hasNavMesh())
{
m_ball_on_node = BattleGraph::get()->pointToNode(m_ball_on_node,
m_ball_position, true/*ignore_vertical*/);

if (m_ball_on_node == BattleGraph::UNKNOWN_POLY &&
World::getWorld()->getPhase() == RACE_PHASE)
{
m_ball_invalid_timer += dt;
// Reset the ball and karts if out of navmesh after 2 seconds
if (m_ball_invalid_timer >= 2.0f)
{
m_ball_invalid_timer = 0.0f;
resetBall();
for (unsigned int i = 0; i < m_karts.size(); i++)
moveKartAfterRescue(m_karts[i]);
}
}
else
m_ball_invalid_timer = 0.0f;
}

} // updateBallPosition
Expand Down Expand Up @@ -639,3 +633,10 @@ unsigned int SoccerWorld::getRescuePositionIndex(AbstractKart *kart)
Log::warn("SoccerWorld", "Unknown kart, using default starting position.");
return 0;
} // getRescuePositionIndex

//-----------------------------------------------------------------------------
void SoccerWorld::resetBall()
{
m_ball->reset();
m_ball->getPhysicalObject()->reset();
} // resetBall
10 changes: 8 additions & 2 deletions src/modes/soccer_world.hpp
Expand Up @@ -26,9 +26,9 @@
#include <IMesh.h>
#include <string>

class PhysicalObject;
class AbstractKart;
class Controller;
class TrackObject;

/** An implementation of World, to provide the soccer game mode
* Notice: In soccer world, true goal means blue, false means red.
Expand All @@ -53,6 +53,9 @@ class SoccerWorld : public WorldWithRank
PerPlayerDifficulty difficulty);

private:
/** Keep a pointer to the track object of soccer ball */
TrackObject* m_ball;

/** Number of goals needed to win */
int m_goal_target;
bool m_count_down_reached_zero;
Expand All @@ -64,6 +67,7 @@ class SoccerWorld : public WorldWithRank

/** Timer for displaying goal text*/
float m_goal_timer;
float m_ball_invalid_timer;
int m_ball_hitter;

/** Goals data of each team scored */
Expand Down Expand Up @@ -94,9 +98,11 @@ class SoccerWorld : public WorldWithRank
/** Function to update the locations of all karts on the polygon map */
void updateKartNodes();
/** Function to update the location the ball on the polygon map */
void updateBallPosition();
void updateBallPosition(float dt);
/** Clean up */
void resetAllNodes();
/** Reset the ball to original starting position. */
void resetBall();
/** Function to update the AI which is the closest to its goal to defend. */
void updateDefenders();
/** Get number of teammates in a team, used by starting position assign. */
Expand Down

0 comments on commit 40e193e

Please sign in to comment.