From 039eb6733d2b357e6950aa0a7982fe2cf861835d Mon Sep 17 00:00:00 2001 From: hikerstk Date: Sun, 21 Dec 2008 07:43:41 +0000 Subject: [PATCH] Minor refactoring. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@2734 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/karts/kart.hpp | 4 ++++ src/karts/player_kart.cpp | 11 +++++++++ src/karts/player_kart.hpp | 1 + src/modes/linear_world.cpp | 47 +++++++++++++++----------------------- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/karts/kart.hpp b/src/karts/kart.hpp index b096bbb0e..7ec4d3a6b 100644 --- a/src/karts/kart.hpp +++ b/src/karts/kart.hpp @@ -206,6 +206,10 @@ public: const std::string& getName () const {return m_kart_properties->getName();} const std::string& getIdent () const {return m_kart_properties->getIdent();} virtual bool isPlayerKart () const {return false; } + /** Called by world in case of the kart taking a shortcut. The player kart + * will display a message in this case, default behaviour is to do nothing. + */ + virtual void doingShortcut() {}; // addMessages gets called by world to add messages to the gui virtual void addMessages () {}; virtual void collectedItem (const Item &item, int random_attachment); diff --git a/src/karts/player_kart.cpp b/src/karts/player_kart.cpp index 7c706f3a8..cbc21821d 100644 --- a/src/karts/player_kart.cpp +++ b/src/karts/player_kart.cpp @@ -317,3 +317,14 @@ void PlayerKart::collectedItem(const Item &item, int add_info) } } // collectedItem +//----------------------------------------------------------------------------- +/** Called when the kart is doing a shortcut. The player kart will display + * a message for the user. + */ +void PlayerKart::doingShortcut() +{ + RaceGUI* m=(RaceGUI*)menu_manager->getRaceMenu(); + // Can happen if the option menu is called + if(m) + m->addMessage(_("Invalid short-cut!!"), this, 2.0f, 60); +} // doingShortcut \ No newline at end of file diff --git a/src/karts/player_kart.hpp b/src/karts/player_kart.hpp index 571d9f8a8..6d23ea050 100644 --- a/src/karts/player_kart.hpp +++ b/src/karts/player_kart.hpp @@ -61,6 +61,7 @@ public: virtual void crashed (Kart *k); virtual void setPosition (int p); virtual void raceFinished (float time); + virtual void doingShortcut(); bool isPlayerKart () const {return true;} Camera* getCamera () {return m_camera;} void reset(); diff --git a/src/modes/linear_world.cpp b/src/modes/linear_world.cpp index b3b7a12af..a4b06ea42 100644 --- a/src/modes/linear_world.cpp +++ b/src/modes/linear_world.cpp @@ -148,35 +148,29 @@ void LinearWorld::update(float delta) { KartInfo& kart_info = m_kart_info[n]; Kart* kart = m_kart[n]; - + + // Nothing to do for karts that are currently being rescued. + if(kart->isRescue()) continue; + // ---------- deal with sector data --------- // update sector variables int prev_sector = kart_info.m_track_sector; - - if(!kart->isRescue()) - m_track->findRoadSector( kart->getXYZ(), &kart_info.m_track_sector); + m_track->findRoadSector( kart->getXYZ(), &kart_info.m_track_sector); // Check if the kart is taking a shortcut (if it's not already doing one): // ----------------------------------------------------------------------- - if(!kart->isRescue() && kart_info.m_last_valid_sector != Track::UNKNOWN_SECTOR && + if(kart_info.m_last_valid_sector != Track::UNKNOWN_SECTOR && m_track->isShortcut(kart_info.m_last_valid_sector, kart_info.m_track_sector)) { - forceRescue(kart, kart_info, /*is shortcut*/ true); // bring karts back to where they left the track. - if(kart->isPlayerKart()) - { - RaceGUI* m=(RaceGUI*)menu_manager->getRaceMenu(); - // Can happen if the option menu is called - if(m) - m->addMessage(_("Invalid short-cut!!"), kart, 2.0f, 60); - } + forceRescue(kart, kart_info, /*is shortcut*/ true); // bring karts back to where they left the track. } - if(kart_info.m_track_sector != Track::UNKNOWN_SECTOR && !kart->isRescue()) + if(kart_info.m_track_sector != Track::UNKNOWN_SECTOR) kart_info.m_last_valid_sector = kart_info.m_track_sector; // check if kart is on the road - if not, find the closest sector - if (kart_info.m_track_sector == Track::UNKNOWN_SECTOR && !kart->isRescue()) + if (kart_info.m_track_sector == Track::UNKNOWN_SECTOR) { kart_info.m_on_road = false; if( kart_info.m_curr_track_coords[0] > 0.0 ) @@ -192,21 +186,15 @@ void LinearWorld::update(float delta) } else { - // Preserve on_road flag in case of rescue. This allows to pick - // a different rescue position for karts being off road. - if(!kart->isRescue()) - kart_info.m_on_road = true; - } - - if(!kart->isRescue()) - { - // Update track coords (=progression) - m_kart_info[n].m_last_track_coords = m_kart_info[n].m_curr_track_coords; - m_track->spatialToTrack(kart_info.m_curr_track_coords, - kart->getXYZ(), - kart_info.m_track_sector ); + kart_info.m_on_road = true; } + // Update track coords (=progression) + m_kart_info[n].m_last_track_coords = m_kart_info[n].m_curr_track_coords; + m_track->spatialToTrack(kart_info.m_curr_track_coords, + kart->getXYZ(), + kart_info.m_track_sector ); + // Lap counting, based on the new position, but only if the kart // hasn't finished the race (otherwise it would be counted more than // once for the number of finished karts). @@ -565,7 +553,8 @@ void LinearWorld::forceRescue(Kart* kart, KartInfo& kart_info, bool shortcut) kart_info.m_race_lap--; kart_info.m_lap_start_time = -1; // invalidate time so he doesn't get a best time } - } + kart->doingShortcut(); + } // if shortcut kart->forceRescue(); }