Refactoring: removed code duplication in some init() and restartRace()

functions of game modes (caused by restartRace only called in case of
a restart, not the first time a race is started). The function restartRace()
was renamed to reset(), which is now called always before a race is started
or restarted.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@12591 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2013-04-03 06:05:34 +00:00
parent 4cd55d0dc5
commit 820ebd2673
21 changed files with 282 additions and 280 deletions

View File

@ -424,14 +424,6 @@ void CutsceneWorld::terminateRace()
World::terminateRace(); World::terminateRace();
} // terminateRace } // terminateRace
//-----------------------------------------------------------------------------
/** Called then a battle is restarted.
*/
void CutsceneWorld::restartRace()
{
World::restartRace();
} // restartRace
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the data to display in the race gui. /** Returns the data to display in the race gui.
*/ */

View File

@ -74,9 +74,6 @@ public:
{ {
m_parts = parts; m_parts = parts;
} }
// overriding World methods
virtual void restartRace() OVERRIDE;
virtual void getKartsDisplayInfo( virtual void getKartsDisplayInfo(
std::vector<RaceGUIBase::KartIconDisplayInfo> *info) OVERRIDE; std::vector<RaceGUIBase::KartIconDisplayInfo> *info) OVERRIDE;

View File

@ -158,14 +158,14 @@ bool EasterEggHunt::isRaceOver()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Called then a battle is restarted. /** Called then a battle is restarted.
*/ */
void EasterEggHunt::restartRace() void EasterEggHunt::reset()
{ {
WorldWithRank::restartRace(); WorldWithRank::reset();
for(unsigned int i=0; i<m_eggs_collected.size(); i++) for(unsigned int i=0; i<m_eggs_collected.size(); i++)
m_eggs_collected[i] = 0; m_eggs_collected[i] = 0;
m_eggs_found = 0; m_eggs_found = 0;
} // restartRace } // reset
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the data to display in the race gui. /** Returns the data to display in the race gui.

View File

@ -51,7 +51,7 @@ public:
virtual bool isRaceOver(); virtual bool isRaceOver();
// overriding World methods // overriding World methods
virtual void restartRace(); virtual void reset();
virtual bool raceHasLaps(){ return false; } virtual bool raceHasLaps(){ return false; }
virtual void moveKartAfterRescue(AbstractKart* kart); virtual void moveKartAfterRescue(AbstractKart* kart);

View File

@ -47,11 +47,9 @@ FollowTheLeaderRace::FollowTheLeaderRace() : LinearWorld()
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
FollowTheLeaderRace::~FollowTheLeaderRace() /** Called immediately after the constructor. Here functions that use
{ * World::getWorld() as well as overridden functions.
} */
//-----------------------------------------------------------------------------
void FollowTheLeaderRace::init() void FollowTheLeaderRace::init()
{ {
LinearWorld::init(); LinearWorld::init();
@ -62,6 +60,26 @@ void FollowTheLeaderRace::init()
#pragma mark - #pragma mark -
#pragma mark clock events #pragma mark clock events
#endif #endif
//-----------------------------------------------------------------------------
FollowTheLeaderRace::~FollowTheLeaderRace()
{
}
//-----------------------------------------------------------------------------
/** Called just before a race is started.
*/
void FollowTheLeaderRace::reset()
{
LinearWorld::reset();
m_leader_intervals.clear();
m_leader_intervals = stk_config->m_leader_intervals;
for(unsigned int i=0; i<m_leader_intervals.size(); i++)
m_leader_intervals[i] +=
stk_config->m_leader_time_per_kart*race_manager->getNumberOfKarts();
WorldStatus::setClockMode(WorldStatus::CLOCK_COUNTDOWN,
m_leader_intervals[0]);
} // reset
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the original time at which the countdown timer started. This is /** Returns the original time at which the countdown timer started. This is
* used by the race_gui to display the music credits in FTL mode correctly. * used by the race_gui to display the music credits in FTL mode correctly.
@ -193,19 +211,6 @@ bool FollowTheLeaderRace::isRaceOver()
return getCurrentNumKarts()==2 || getCurrentNumPlayers()==0; return getCurrentNumKarts()==2 || getCurrentNumPlayers()==0;
} // isRaceOver } // isRaceOver
//-----------------------------------------------------------------------------
void FollowTheLeaderRace::restartRace()
{
LinearWorld::restartRace();
m_leader_intervals.clear();
m_leader_intervals = stk_config->m_leader_intervals;
for(unsigned int i=0; i<m_leader_intervals.size(); i++)
m_leader_intervals[i] +=
stk_config->m_leader_time_per_kart*race_manager->getNumberOfKarts();
WorldStatus::setClockMode(WorldStatus::CLOCK_COUNTDOWN,
m_leader_intervals[0]);
} // restartRace
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the internal identifier for this kind of race. /** Returns the internal identifier for this kind of race.
*/ */

View File

@ -38,7 +38,7 @@ public:
virtual void countdownReachedZero() OVERRIDE; virtual void countdownReachedZero() OVERRIDE;
// overriding World methods // overriding World methods
virtual void restartRace() OVERRIDE; virtual void reset() OVERRIDE;
virtual const std::string& getIdent() const OVERRIDE; virtual const std::string& getIdent() const OVERRIDE;
virtual float getClockStartTime(); virtual float getClockStartTime();
virtual bool useFastMusicNearEnd() const OVERRIDE { return false; } virtual bool useFastMusicNearEnd() const OVERRIDE { return false; }

View File

@ -79,11 +79,9 @@ LinearWorld::~LinearWorld()
} // ~LinearWorld } // ~LinearWorld
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void LinearWorld::restartRace() void LinearWorld::reset()
{ {
WorldWithRank::restartRace(); WorldWithRank::reset();
//if(m_last_lap_sfx->getStatus()== SFXManager::SFX_PLAYING)
// m_last_lap_sfx->stop();
m_last_lap_sfx_played = false; m_last_lap_sfx_played = false;
m_last_lap_sfx_playing = false; m_last_lap_sfx_playing = false;
@ -119,7 +117,7 @@ void LinearWorld::restartRace()
} }
#endif #endif
} // restartRace } // reset
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** General update function called once per frame. This updates the kart /** General update function called once per frame. This updates the kart
@ -343,9 +341,9 @@ void LinearWorld::newLap(unsigned int kart_index)
} // newLap } // newLap
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Gets the sector a kart is on. This return UNKNOWN_SECTOR if the kart_id /** Gets the sector a kart is on. This function returns UNKNOWN_SECTOR if the
* is larger than the current kart info. This is necessary in the case that * kart_id is larger than the current kart info. This is necessary in the case
* a collision with the track happens during resetAllKarts: at this time * that a collision with the track happens during resetAllKarts: at this time
* m_kart_info is not initialised (and has size 0), so it would trigger this * m_kart_info is not initialised (and has size 0), so it would trigger this
* assert. While this normally does not happen, it is useful for track * assert. While this normally does not happen, it is useful for track
* designers that STK does not crash. * designers that STK does not crash.

View File

@ -130,7 +130,7 @@ public:
virtual void getKartsDisplayInfo( virtual void getKartsDisplayInfo(
std::vector<RaceGUIBase::KartIconDisplayInfo> *info) OVERRIDE; std::vector<RaceGUIBase::KartIconDisplayInfo> *info) OVERRIDE;
virtual void moveKartAfterRescue(AbstractKart* kart) OVERRIDE; virtual void moveKartAfterRescue(AbstractKart* kart) OVERRIDE;
virtual void restartRace() OVERRIDE; virtual void reset() OVERRIDE;
virtual void newLap(unsigned int kart_index) OVERRIDE; virtual void newLap(unsigned int kart_index) OVERRIDE;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -33,46 +33,6 @@
#include "states_screens/race_gui_overworld.hpp" #include "states_screens/race_gui_overworld.hpp"
#include "tracks/track.hpp" #include "tracks/track.hpp"
//-----------------------------------------------------------------------------
/** Function to simplify the start process */
void OverWorld::enterOverWorld()
{
race_manager->setNumLocalPlayers(1);
race_manager->setMajorMode (RaceManager::MAJOR_MODE_SINGLE);
race_manager->setMinorMode (RaceManager::MINOR_MODE_OVERWORLD);
race_manager->setNumKarts( 1 );
race_manager->setTrack( "overworld" );
race_manager->setDifficulty(RaceManager::DIFFICULTY_HARD);
// Use keyboard 0 by default (FIXME: let player choose?)
InputDevice* device = input_manager->getDeviceList()->getKeyboard(0);
// Create player and associate player with keyboard
StateManager::get()->createActivePlayer(unlock_manager->getCurrentPlayer(),
device);
if (kart_properties_manager->getKart(UserConfigParams::m_default_kart) == NULL)
{
fprintf(stderr, "[MainMenuScreen] WARNING: cannot find kart '%s', will revert to default\n",
UserConfigParams::m_default_kart.c_str());
UserConfigParams::m_default_kart.revertToDefaults();
}
race_manager->setLocalKartInfo(0, UserConfigParams::m_default_kart);
// ASSIGN should make sure that only input from assigned devices
// is read.
input_manager->getDeviceList()->setAssignMode(ASSIGN);
input_manager->getDeviceList()
->setSinglePlayer( StateManager::get()->getActivePlayer(0) );
StateManager::get()->enterGameState();
network_manager->setupPlayerKartInfo();
race_manager->startNew(false);
irr_driver->showPointer(); // the user should be able to click on the minimap
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
OverWorld::OverWorld() : LinearWorld() OverWorld::OverWorld() : LinearWorld()
{ {
@ -105,6 +65,47 @@ OverWorld::~OverWorld()
race_manager->setKartLastPositionOnOverworld(kart_xyz); race_manager->setKartLastPositionOnOverworld(kart_xyz);
} // ~OverWorld } // ~OverWorld
//-----------------------------------------------------------------------------
/** Function to simplify the start process */
void OverWorld::enterOverWorld()
{
race_manager->setNumLocalPlayers(1);
race_manager->setMajorMode (RaceManager::MAJOR_MODE_SINGLE);
race_manager->setMinorMode (RaceManager::MINOR_MODE_OVERWORLD);
race_manager->setNumKarts( 1 );
race_manager->setTrack( "overworld" );
race_manager->setDifficulty(RaceManager::DIFFICULTY_HARD);
// Use keyboard 0 by default (FIXME: let player choose?)
InputDevice* device = input_manager->getDeviceList()->getKeyboard(0);
// Create player and associate player with keyboard
StateManager::get()->createActivePlayer(unlock_manager->getCurrentPlayer(),
device);
if (!kart_properties_manager->getKart(UserConfigParams::m_default_kart))
{
fprintf(stderr, "[MainMenuScreen] WARNING: cannot find kart '%s', "
"will revert to default\n",
UserConfigParams::m_default_kart.c_str());
UserConfigParams::m_default_kart.revertToDefaults();
}
race_manager->setLocalKartInfo(0, UserConfigParams::m_default_kart);
// ASSIGN should make sure that only input from assigned devices
// is read.
input_manager->getDeviceList()->setAssignMode(ASSIGN);
input_manager->getDeviceList()
->setSinglePlayer( StateManager::get()->getActivePlayer(0) );
StateManager::get()->enterGameState();
network_manager->setupPlayerKartInfo();
race_manager->startNew(false);
irr_driver->showPointer(); // User should be able to click on the minimap
} // enterOverWorld
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** General update function called once per frame. /** General update function called once per frame.
* \param dt Time step size. * \param dt Time step size.
@ -121,7 +122,8 @@ void OverWorld::update(float dt)
// so we have to start music 'manually', since we skip all phases. // so we have to start music 'manually', since we skip all phases.
World::getWorld()->getTrack()->startMusic(); World::getWorld()->getTrack()->startMusic();
if (music_manager->getCurrentMusic() != NULL && UserConfigParams::m_music) if (music_manager->getCurrentMusic() != NULL &&
UserConfigParams::m_music)
music_manager->getCurrentMusic()->startMusic(); music_manager->getCurrentMusic()->startMusic();
m_karts[0]->startEngineSFX(); m_karts[0]->startEngineSFX();
} }
@ -161,13 +163,14 @@ void OverWorld::checkForWrongDirection(unsigned int i)
void OverWorld::createRaceGUI() void OverWorld::createRaceGUI()
{ {
m_race_gui = new RaceGUIOverworld(); m_race_gui = new RaceGUIOverworld();
} } // createRaceGUI
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void OverWorld::onFirePressed(Controller* who) void OverWorld::onFirePressed(Controller* who)
{ {
const std::vector<OverworldChallenge>& challenges = m_track->getChallengeList(); const std::vector<OverworldChallenge>& challenges =
m_track->getChallengeList();
AbstractKart* k = getKart(0); AbstractKart* k = getKart(0);
Vec3 kart_xyz = k->getXYZ(); Vec3 kart_xyz = k->getXYZ();
@ -179,9 +182,12 @@ void OverWorld::onFirePressed(Controller* who)
for (unsigned int n=0; n<challenges.size(); n++) for (unsigned int n=0; n<challenges.size(); n++)
{ {
if (challenges[n].isForceFieldSet() && challenges[n].getForceField().m_is_locked) continue; if ( challenges[n].isForceFieldSet() &&
challenges[n].getForceField().m_is_locked )
continue;
if ((kart_xyz - Vec3(challenges[n].m_position)).length2_2d() < CHALLENGE_DISTANCE_SQUARED) if ( (kart_xyz - Vec3(challenges[n].m_position)).length2_2d()
< CHALLENGE_DISTANCE_SQUARED)
{ {
if (challenges[n].m_challenge_id == "tutorial") if (challenges[n].m_challenge_id == "tutorial")
{ {
@ -191,28 +197,22 @@ void OverWorld::onFirePressed(Controller* who)
else else
{ {
race_manager->setKartLastPositionOnOverworld(kart_xyz); race_manager->setKartLastPositionOnOverworld(kart_xyz);
new SelectChallengeDialog(0.8f, 0.8f, challenges[n].m_challenge_id); new SelectChallengeDialog(0.8f, 0.8f,
challenges[n].m_challenge_id);
} }
} // end if } // end if
} // end for } // end for
} } // onFirePressed
//-----------------------------------------------------------------------------
/** Moves a kart to its rescue position.
* \param kart The kart that was rescued.
*/
void OverWorld::moveKartAfterRescue(AbstractKart* kart)
{
moveKartAfterRescue(kart, 0);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
btTransform OverWorld::getClosestStartPoint(float currentKart_x, float currentKart_z) btTransform OverWorld::getClosestStartPoint(float currentKart_x,
float currentKart_z)
{ {
// find closest point to drop kart on // find closest point to drop kart on
World *world = World::getWorld(); World *world = World::getWorld();
const int start_spots_amount = world->getTrack()->getNumberOfStartPositions(); const int start_spots_amount =
world->getTrack()->getNumberOfStartPositions();
assert(start_spots_amount > 0); assert(start_spots_amount > 0);
@ -238,7 +238,16 @@ btTransform OverWorld::getClosestStartPoint(float currentKart_x, float currentKa
assert(closest_id != -1); assert(closest_id != -1);
return world->getTrack()->getStartTransform(closest_id); return world->getTrack()->getStartTransform(closest_id);
} } // getClosestStartPoint
//-----------------------------------------------------------------------------
/** Moves a kart to its rescue position.
* \param kart The kart that was rescued.
*/
void OverWorld::moveKartAfterRescue(AbstractKart* kart)
{
moveKartAfterRescue(kart, 0);
} // moveKartAfterRescue(AbstractKart*)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -246,7 +255,8 @@ void OverWorld::moveKartAfterRescue(AbstractKart* kart, float angle)
{ {
// find closest point to drop kart on // find closest point to drop kart on
World *world = World::getWorld(); World *world = World::getWorld();
const int start_spots_amount = world->getTrack()->getNumberOfStartPositions(); const int start_spots_amount =
world->getTrack()->getNumberOfStartPositions();
assert(start_spots_amount > 0); assert(start_spots_amount > 0);
const float currentKart_x = kart->getXYZ().getX(); const float currentKart_x = kart->getXYZ().getX();
@ -259,7 +269,8 @@ void OverWorld::moveKartAfterRescue(AbstractKart* kart, float angle)
//position kart from same height as in World::resetAllKarts //position kart from same height as in World::resetAllKarts
btTransform pos; btTransform pos;
pos.setOrigin(kart->getXYZ()+btVector3(0, 0.5f*kart->getKartHeight(), 0.0f)); pos.setOrigin( kart->getXYZ()
+btVector3(0, 0.5f*kart->getKartHeight(), 0.0f) );
pos.setRotation( btQuaternion(btVector3(0.0f, 1.0f, 0.0f), angle) ); pos.setRotation( btQuaternion(btVector3(0.0f, 1.0f, 0.0f), angle) );
kart->getBody()->setCenterOfMassTransform(pos); kart->getBody()->setCenterOfMassTransform(pos);
@ -270,13 +281,15 @@ void OverWorld::moveKartAfterRescue(AbstractKart* kart, float angle)
if (kart_over_ground) if (kart_over_ground)
{ {
//add vertical offset so that the kart starts off above the track //add vertical offset so that the kart starts off above the track
float vertical_offset = kart->getKartProperties()->getVertRescueOffset() * float vertical_offset =
kart->getKartHeight(); kart->getKartProperties()->getVertRescueOffset()
* kart->getKartHeight();
kart->getBody()->translate(btVector3(0, vertical_offset, 0)); kart->getBody()->translate(btVector3(0, vertical_offset, 0));
} }
else else
{ {
fprintf(stderr, "WARNING: invalid position after rescue for kart %s on track %s.\n", fprintf(stderr, "WARNING: invalid position after rescue for kart %s "
"on track %s.\n",
(kart->getIdent().c_str()), m_track->getIdent().c_str()); (kart->getIdent().c_str()), m_track->getIdent().c_str());
} }
} // moveKartAfterRescue } // moveKartAfterRescue

View File

@ -57,12 +57,12 @@ public:
virtual void update(float delta) OVERRIDE; virtual void update(float delta) OVERRIDE;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns if this race mode has laps. */ /** Returns if this race mode has laps. */
virtual bool raceHasLaps() OVERRIDE { return false; } virtual bool raceHasLaps() OVERRIDE { return false; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
virtual void checkForWrongDirection(unsigned int i) OVERRIDE; virtual void checkForWrongDirection(unsigned int i) OVERRIDE;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** The overworld is not a race per se so it's never over */ /** The overworld is not a race per se so it's never over */
virtual bool isRaceOver() OVERRIDE { return false; } virtual bool isRaceOver() OVERRIDE { return false; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -75,12 +75,12 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Override base class method */ /** Override base class method */
virtual void onFirePressed(Controller* who) OVERRIDE; virtual void onFirePressed(Controller* who) OVERRIDE;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Override settings from base class */ /** Override settings from base class */
virtual bool useChecklineRequirements() const OVERRIDE { return false; } virtual bool useChecklineRequirements() const OVERRIDE { return false; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void scheduleSelectKart() { m_return_to_garage = true; } void scheduleSelectKart() { m_return_to_garage = true; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
virtual void moveKartAfterRescue(AbstractKart* kart) OVERRIDE; virtual void moveKartAfterRescue(AbstractKart* kart) OVERRIDE;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
virtual void onMouseClick(int x, int y) OVERRIDE; virtual void onMouseClick(int x, int y) OVERRIDE;

View File

@ -50,20 +50,27 @@ void SoccerWorld::init()
{ {
WorldWithRank::init(); WorldWithRank::init();
m_display_rank = false; m_display_rank = false;
m_can_score_points = true;
memset(m_team_goals, 0, sizeof(m_team_goals));
// check for possible problems if AI karts were incorrectly added // check for possible problems if AI karts were incorrectly added
if(getNumKarts() > race_manager->getNumPlayers()) if(getNumKarts() > race_manager->getNumPlayers())
{ {
fprintf(stderr, "No AI exists for this game mode\n"); fprintf(stderr, "No AI exists for this game mode\n");
exit(1); exit(1);
} }
} // init
initKartList();
//-----------------------------------------------------------------------------
/** Called then a battle is restarted.
*/
void SoccerWorld::reset()
{
WorldWithRank::reset();
} // SoccerWorld m_can_score_points = true;
memset(m_team_goals, 0, sizeof(m_team_goals));
initKartList();
} // reset
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the internal identifier for this race. /** Returns the internal identifier for this race.
@ -118,7 +125,7 @@ void SoccerWorld::onCheckGoalTriggered(bool first_goal)
// TODO: rescue the karts // TODO: rescue the karts
// TODO: score a point // TODO: score a point
} } // onCheckGoalTriggered
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** The battle is over if only one kart is left, or no player kart. /** The battle is over if only one kart is left, or no player kart.
@ -146,20 +153,6 @@ void SoccerWorld::terminateRace()
WorldWithRank::terminateRace(); WorldWithRank::terminateRace();
} // terminateRace } // terminateRace
//-----------------------------------------------------------------------------
/** Called then a battle is restarted.
*/
void SoccerWorld::restartRace()
{
WorldWithRank::restartRace();
m_can_score_points = true;
memset(m_team_goals, 0, sizeof(m_team_goals));
initKartList();
} // restartRace
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the data to display in the race gui. /** Returns the data to display in the race gui.
*/ */

View File

@ -58,7 +58,7 @@ public:
virtual void terminateRace(); virtual void terminateRace();
// overriding World methods // overriding World methods
virtual void restartRace(); virtual void reset();
virtual bool useFastMusicNearEnd() const { return false; } virtual bool useFastMusicNearEnd() const { return false; }
virtual void getKartsDisplayInfo( virtual void getKartsDisplayInfo(

View File

@ -36,7 +36,8 @@ public:
virtual ~StandardRace() {}; virtual ~StandardRace() {};
// overriding World methods // overriding World methods
virtual void getDefaultCollectibles(int *collectible_type, int *amount) OVERRIDE; virtual void getDefaultCollectibles(int *collectible_type,
int *amount) OVERRIDE;
virtual bool haveBonusBoxes() OVERRIDE; virtual bool haveBonusBoxes() OVERRIDE;
virtual const std::string& getIdent() const OVERRIDE; virtual const std::string& getIdent() const OVERRIDE;

View File

@ -67,19 +67,9 @@ void ThreeStrikesBattle::init()
{ {
// create the struct that ill hold each player's lives // create the struct that ill hold each player's lives
BattleInfo info; BattleInfo info;
info.m_lives = 3;
m_kart_info.push_back(info); m_kart_info.push_back(info);
} // next kart
// no positions in this mode m_kart_info.resize(kart_amount);
m_karts[n]->setPosition(-1);
}// next kart
BattleEvent evt;
evt.m_time = 0.0f;
evt.m_kart_info = m_kart_info;
m_battle_events.push_back(evt);
} // ThreeStrikesBattle } // ThreeStrikesBattle
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -97,6 +87,59 @@ ThreeStrikesBattle::~ThreeStrikesBattle()
irr_driver->removeMeshFromCache(m_tire); irr_driver->removeMeshFromCache(m_tire);
} // ~ThreeStrikesBattle } // ~ThreeStrikesBattle
//-----------------------------------------------------------------------------
/** Called when a battle is restarted.
*/
void ThreeStrikesBattle::reset()
{
WorldWithRank::reset();
const unsigned int kart_amount = m_karts.size();
for(unsigned int n=0; n<kart_amount; n++)
{
m_kart_info[n].m_lives = 3;
// no positions in this mode
m_karts[n]->setPosition(-1);
scene::ISceneNode* kart_node = m_karts[n]->getNode();
// FIXME: sorry for this ugly const_cast, irrlicht doesn't seem to allow getting a writable list of children, wtf??
core::list<scene::ISceneNode*>& children = const_cast<core::list<scene::ISceneNode*>&>(kart_node->getChildren());
for (core::list<scene::ISceneNode*>::Iterator it = children.begin(); it != children.end(); it++)
{
scene::ISceneNode* curr = *it;
if (core::stringc(curr->getName()) == "tire1")
{
curr->setVisible(true);
}
else if (core::stringc(curr->getName()) == "tire2")
{
curr->setVisible(true);
}
}
}// next kart
// remove old battle events
m_battle_events.clear();
// add initial battle event
BattleEvent evt;
evt.m_time = 0.0f;
evt.m_kart_info = m_kart_info;
m_battle_events.push_back(evt);
PhysicalObject *obj;
for_in(obj, m_tires)
{
m_track->getTrackObjectManager()->removeObject(obj);
}
m_tires.clearWithoutDeleting();
} // reset
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Adds two tires to each of the kart. The tires are used to represent /** Adds two tires to each of the kart. The tires are used to represent
* lifes. * lifes.
@ -396,59 +439,6 @@ void ThreeStrikesBattle::terminateRace()
WorldWithRank::terminateRace(); WorldWithRank::terminateRace();
} // terminateRace } // terminateRace
//-----------------------------------------------------------------------------
/** Called then a battle is restarted.
*/
void ThreeStrikesBattle::restartRace()
{
WorldWithRank::restartRace();
const unsigned int kart_amount = m_karts.size();
for(unsigned int n=0; n<kart_amount; n++)
{
m_kart_info[n].m_lives = 3;
// no positions in this mode
m_karts[n]->setPosition(-1);
scene::ISceneNode* kart_node = m_karts[n]->getNode();
// FIXME: sorry for this ugly const_cast, irrlicht doesn't seem to allow getting a writable list of children, wtf??
core::list<scene::ISceneNode*>& children = const_cast<core::list<scene::ISceneNode*>&>(kart_node->getChildren());
for (core::list<scene::ISceneNode*>::Iterator it = children.begin(); it != children.end(); it++)
{
scene::ISceneNode* curr = *it;
if (core::stringc(curr->getName()) == "tire1")
{
curr->setVisible(true);
}
else if (core::stringc(curr->getName()) == "tire2")
{
curr->setVisible(true);
}
}
}// next kart
// remove old battle events
m_battle_events.clear();
// add initial battle event
BattleEvent evt;
evt.m_time = 0.0f;
evt.m_kart_info = m_kart_info;
m_battle_events.push_back(evt);
PhysicalObject *obj;
for_in(obj, m_tires)
{
m_track->getTrackObjectManager()->removeObject(obj);
}
m_tires.clearWithoutDeleting();
} // restartRace
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the data to display in the race gui. /** Returns the data to display in the race gui.
*/ */

View File

@ -90,7 +90,7 @@ public:
virtual void terminateRace(); virtual void terminateRace();
// overriding World methods // overriding World methods
virtual void restartRace(); virtual void reset();
//virtual void getDefaultCollectibles(int& collectible_type, int& amount); //virtual void getDefaultCollectibles(int& collectible_type, int& amount);
virtual bool useFastMusicNearEnd() const { return false; } virtual bool useFastMusicNearEnd() const { return false; }

View File

@ -10,7 +10,8 @@ void TutorialWorld::moveKartAfterRescue(AbstractKart* kart)
// find closest point to drop kart on // find closest point to drop kart on
World *world = World::getWorld(); World *world = World::getWorld();
const int start_spots_amount = world->getTrack()->getNumberOfStartPositions(); const int start_spots_amount =
world->getTrack()->getNumberOfStartPositions();
assert(start_spots_amount > 0); assert(start_spots_amount > 0);
const float currentKart_x = kart->getXYZ().getX(); const float currentKart_x = kart->getXYZ().getX();
@ -23,7 +24,8 @@ void TutorialWorld::moveKartAfterRescue(AbstractKart* kart)
//position kart from same height as in World::resetAllKarts //position kart from same height as in World::resetAllKarts
btTransform pos; btTransform pos;
pos.setOrigin(kart->getXYZ()+btVector3(0, 0.5f*kart->getKartHeight(), 0.0f)); pos.setOrigin( kart->getXYZ()
+btVector3(0, 0.5f*kart->getKartHeight(), 0.0f));
pos.setRotation( btQuaternion(btVector3(0.0f, 1.0f, 0.0f), angle) ); pos.setRotation( btQuaternion(btVector3(0.0f, 1.0f, 0.0f), angle) );
kart->getBody()->setCenterOfMassTransform(pos); kart->getBody()->setCenterOfMassTransform(pos);
@ -34,24 +36,28 @@ void TutorialWorld::moveKartAfterRescue(AbstractKart* kart)
if (kart_over_ground) if (kart_over_ground)
{ {
//add vertical offset so that the kart starts off above the track //add vertical offset so that the kart starts off above the track
float vertical_offset = kart->getKartProperties()->getVertRescueOffset() * float vertical_offset =
kart->getKartHeight(); kart->getKartProperties()->getVertRescueOffset()
* kart->getKartHeight();
kart->getBody()->translate(btVector3(0, vertical_offset, 0)); kart->getBody()->translate(btVector3(0, vertical_offset, 0));
} }
else else
{ {
fprintf(stderr, "WARNING: invalid position after rescue for kart %s on track %s.\n", fprintf(stderr, "WARNING: invalid position after rescue for kart %s"
"on track %s.\n",
(kart->getIdent().c_str()), m_track->getIdent().c_str()); (kart->getIdent().c_str()), m_track->getIdent().c_str());
} }
} // moveKartAfterRescue } // moveKartAfterRescue
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
btTransform TutorialWorld::getClosestStartPoint(float currentKart_x, float currentKart_z) btTransform TutorialWorld::getClosestStartPoint(float currentKart_x,
float currentKart_z)
{ {
// find closest point to drop kart on // find closest point to drop kart on
World *world = World::getWorld(); World *world = World::getWorld();
const int start_spots_amount = world->getTrack()->getNumberOfStartPositions(); const int start_spots_amount =
world->getTrack()->getNumberOfStartPositions();
assert(start_spots_amount > 0); assert(start_spots_amount > 0);
@ -77,4 +83,4 @@ btTransform TutorialWorld::getClosestStartPoint(float currentKart_x, float curre
assert(closest_id != -1); assert(closest_id != -1);
return world->getTrack()->getStartTransform(closest_id); return world->getTrack()->getStartTransform(closest_id);
} } // getClosestStartPoint

View File

@ -164,17 +164,28 @@ void World::init()
if(ReplayPlay::get()) if(ReplayPlay::get())
ReplayPlay::get()->Load(); ReplayPlay::get()->Load();
resetAllKarts();
// Note: track reset must be called after all karts exist, since check
// objects need to allocate data structures depending on the number
// of karts.
m_track->reset();
if(!history->replayHistory()) history->initRecording();
if(ReplayRecorder::get()) ReplayRecorder::get()->init();
network_manager->worldLoaded(); network_manager->worldLoaded();
powerup_manager->updateWeightsForRace(num_karts); powerup_manager->updateWeightsForRace(num_karts);
} // init
//-----------------------------------------------------------------------------
/** This function is called before a race is started (i.e. either after
* calling init() when starting a race for the first time, or after
* restarting a race, in which case no init() is called.
*/
void World::reset()
{
// If m_saved_race_gui is set, it means that the restart was done
// when the race result gui was being shown. In this case restore the
// race gui (note that the race result gui is cached and so never really
// destroyed).
if(m_saved_race_gui)
{
m_race_gui = m_saved_race_gui;
m_saved_race_gui = NULL;
}
// erase messages left over // erase messages left over
RaceGUIBase* rg = getRaceGUI(); RaceGUIBase* rg = getRaceGUI();
if (rg) if (rg)
@ -183,9 +194,52 @@ void World::init()
rg->clearAllMessages(); rg->clearAllMessages();
} }
m_race_gui->restartRace();
m_race_gui->clearAllMessages();
m_schedule_pause = false;
m_schedule_unpause = false;
WorldStatus::reset();
m_faster_music_active = false;
m_eliminated_karts = 0;
m_eliminated_players = 0;
for ( KartList::iterator i = m_karts.begin(); i != m_karts.end() ; ++i )
{
(*i)->reset();
}
for(unsigned int i=0; i<Camera::getNumCameras(); i++)
{
Camera::getCamera(i)->reset();
}
if(ReplayPlay::get())
ReplayPlay::get()->reset();
resetAllKarts();
// Note: track reset must be called after all karts exist, since check
// objects need to allocate data structures depending on the number
// of karts.
m_track->reset();
// Start music from beginning
music_manager->stopMusic();
// Enable SFX again
sfx_manager->resumeAll();
projectile_manager->cleanup();
race_manager->reset();
// Make sure to overwrite the data from the previous race.
if(!history->replayHistory()) history->initRecording();
if(ReplayRecorder::get()) ReplayRecorder::get()->init();
// Reset all data structures that depend on number of karts. // Reset all data structures that depend on number of karts.
irr_driver->reset(); irr_driver->reset();
} // init
} // reset
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -956,59 +1010,6 @@ void World::getDefaultCollectibles(int *collectible_type, int *amount )
*amount = 0; *amount = 0;
} // getDefaultCollectibles } // getDefaultCollectibles
//-----------------------------------------------------------------------------
void World::restartRace()
{
// If m_saved_race_gui is set, it means that the restart was done
// when the race result gui was being shown. In this case restore the
// race gui (note that the race result gui is cached and so never really
// destroyed).
if(m_saved_race_gui)
{
m_race_gui = m_saved_race_gui;
m_saved_race_gui = NULL;
}
m_race_gui->restartRace();
m_race_gui->clearAllMessages();
m_schedule_pause = false;
m_schedule_unpause = false;
WorldStatus::reset();
m_faster_music_active = false;
m_eliminated_karts = 0;
m_eliminated_players = 0;
for ( KartList::iterator i = m_karts.begin(); i != m_karts.end() ; ++i )
{
(*i)->reset();
}
for(unsigned int i=0; i<Camera::getNumCameras(); i++)
{
Camera::getCamera(i)->reset();
}
if(ReplayPlay::get())
ReplayPlay::get()->reset();
resetAllKarts();
// Start music from beginning
music_manager->stopMusic();
m_track->reset();
// Enable SFX again
sfx_manager->resumeAll();
projectile_manager->cleanup();
race_manager->reset();
// Make sure to overwrite the data from the previous race.
if(!history->replayHistory()) history->initRecording();
if(ReplayRecorder::get()) ReplayRecorder::get()->init();
} // restartRace
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Pauses the music (and then pauses WorldStatus). /** Pauses the music (and then pauses WorldStatus).
*/ */

View File

@ -218,7 +218,7 @@ public:
// ================= // =================
virtual void init(); virtual void init();
virtual void terminateRace() OVERRIDE; virtual void terminateRace() OVERRIDE;
virtual void restartRace(); virtual void reset();
virtual void pause(Phase phase) OVERRIDE; virtual void pause(Phase phase) OVERRIDE;
virtual void unpause() OVERRIDE; virtual void unpause() OVERRIDE;
virtual void getDefaultCollectibles(int *collectible_type, virtual void getDefaultCollectibles(int *collectible_type,

View File

@ -34,10 +34,6 @@
WorldStatus::WorldStatus() WorldStatus::WorldStatus()
{ {
m_clock_mode = CLOCK_CHRONO; m_clock_mode = CLOCK_CHRONO;
m_time = 0.0f;
m_auxiliary_timer = 0.0f;
m_phase = UserConfigParams::m_race_now ? RACE_PHASE : SETUP_PHASE;
m_previous_phase = UNDEFINED_PHASE; // initialise it just in case
m_prestart_sound = sfx_manager->createSoundSource("pre_start_race"); m_prestart_sound = sfx_manager->createSoundSource("pre_start_race");
m_start_sound = sfx_manager->createSoundSource("start_race"); m_start_sound = sfx_manager->createSoundSource("start_race");

View File

@ -131,7 +131,7 @@ void History::updateReplay(float dt)
m_current = 0; m_current = 0;
// Note that for physics replay all physics parameters // Note that for physics replay all physics parameters
// need to be reset, e.g. velocity, ... // need to be reset, e.g. velocity, ...
world->restartRace(); world->reset();
} }
unsigned int num_karts = world->getNumKarts(); unsigned int num_karts = world->getNumKarts();
for(unsigned k=0; k<num_karts; k++) for(unsigned k=0; k<num_karts; k++)

View File

@ -419,7 +419,17 @@ void RaceManager::startNextRace()
assert(0); assert(0);
} }
// A second constructor phase is necessary in order to be able to
// call functions which are overwritten (otherwise polymorphism
// will fail and the results will be incorrect . Also in init() functions
// can be called that use World::getWorld().
World::getWorld()->init(); World::getWorld()->init();
// Now initialise all values that need to be reset from race to race
// Calling this here reduces code duplication in init and restartRace()
// functions.
World::getWorld()->reset();
// Save the current score and set last time to zero. This is necessary // Save the current score and set last time to zero. This is necessary
// if someone presses esc after finishing a gp, and selects restart: // if someone presses esc after finishing a gp, and selects restart:
// The race is rerun, and the points and scores get reset ... but if // The race is rerun, and the points and scores get reset ... but if
@ -659,7 +669,7 @@ void RaceManager::rerunRace()
m_kart_status[i].m_score = m_kart_status[i].m_last_score; m_kart_status[i].m_score = m_kart_status[i].m_last_score;
m_kart_status[i].m_overall_time -= m_kart_status[i].m_last_time; m_kart_status[i].m_overall_time -= m_kart_status[i].m_last_time;
} }
World::getWorld()->restartRace(); World::getWorld()->reset();
} // rerunRace } // rerunRace
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------