Hold one kart properties object per player

This commit is contained in:
Flakebi 2015-11-23 01:19:00 +01:00
parent 79c962dd79
commit 6e36c223b9
5 changed files with 33 additions and 22 deletions

View File

@ -41,10 +41,11 @@ AbstractKart::AbstractKart(const std::string& ident,
: Moveable() : Moveable()
{ {
m_world_kart_id = world_kart_id; m_world_kart_id = world_kart_id;
m_kart_properties = kart_properties_manager->getKart(ident); m_kart_properties.reset(new KartProperties());
m_kart_properties->copyForPlayer(kart_properties_manager->getKart(ident));
m_difficulty = difficulty; m_difficulty = difficulty;
m_kart_animation = NULL; m_kart_animation = NULL;
assert(m_kart_properties != NULL); assert(m_kart_properties);
// We have to take a copy of the kart model, since otherwise // We have to take a copy of the kart model, since otherwise
// the animations will be mixed up (i.e. different instances of // the animations will be mixed up (i.e. different instances of

View File

@ -74,7 +74,7 @@ private:
protected: protected:
/** The kart properties. */ /** The kart properties. */
const KartProperties *m_kart_properties; std::unique_ptr<KartProperties> m_kart_properties;
/** The per-player difficulty. */ /** The per-player difficulty. */
PerPlayerDifficulty m_difficulty; PerPlayerDifficulty m_difficulty;
@ -128,10 +128,7 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns the kart properties of this kart. */ /** Returns the kart properties of this kart. */
const KartProperties* getKartProperties() const const KartProperties* getKartProperties() const
{ return m_kart_properties; } { return m_kart_properties.get(); }
// ------------------------------------------------------------------------
/** Sets the kart properties. */
void setKartProperties(const KartProperties *kp) { m_kart_properties=kp; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns the characteristics of this kart. */ /** Returns the characteristics of this kart. */
const AbstractCharacteristic* getCharacteristic() const; const AbstractCharacteristic* getCharacteristic() const;

View File

@ -2455,7 +2455,7 @@ void Kart::loadData(RaceManager::KartType type, bool is_animated_model)
if (!CVS->supportsShadows()) if (!CVS->supportsShadows())
{ {
m_shadow = new Shadow(m_kart_properties, m_node, m_shadow = new Shadow(m_kart_properties.get(), m_node,
-m_kart_model->getLowestPoint()); -m_kart_model->getLowestPoint());
} }
World::getWorld()->kartAdded(this, m_node); World::getWorld()->kartAdded(this, m_node);

View File

@ -94,14 +94,12 @@ KartProperties::KartProperties(const std::string &filename)
// The default constructor for stk_config uses filename="" // The default constructor for stk_config uses filename=""
if (filename != "") if (filename != "")
{ {
for(unsigned int i=0; i<RaceManager::DIFFICULTY_COUNT; i++)
m_ai_properties[i]= NULL;
load(filename, "kart"); load(filename, "kart");
} }
else else
{ {
for(unsigned int i=0; i<RaceManager::DIFFICULTY_COUNT; i++) for (unsigned int i = 0; i < RaceManager::DIFFICULTY_COUNT; i++)
m_ai_properties[i]= new AIProperties((RaceManager::Difficulty)i); m_ai_properties[i].reset(new AIProperties((RaceManager::Difficulty) i));
} }
} // KartProperties } // KartProperties
@ -110,19 +108,18 @@ KartProperties::KartProperties(const std::string &filename)
KartProperties::~KartProperties() KartProperties::~KartProperties()
{ {
delete m_kart_model; delete m_kart_model;
for(unsigned int i=0; i<RaceManager::DIFFICULTY_COUNT; i++)
if(m_ai_properties[i])
delete m_ai_properties[i];
} // ~KartProperties } // ~KartProperties
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Copies this KartProperties to another one. Importnat: if you add any /** Copies this KartProperties to another one. Important: if you add any
* pointers to kart_properties, the data structure they are pointing to * pointers to kart_properties, the data structure they are pointing to
* need to be copied here explicitely! * need to be copied here explicitely!
* The AIProperties won't get cloned here as they don't differ for each player.
* To clone this object for another kart use the copyFrom method.
* \param source The source kart properties from which to copy this objects' * \param source The source kart properties from which to copy this objects'
* values. * values.
*/ */
void KartProperties::copyFrom(const KartProperties *source) void KartProperties::copyForPlayer(const KartProperties *source)
{ {
*this = *source; *this = *source;
@ -136,10 +133,24 @@ void KartProperties::copyFrom(const KartProperties *source)
// this object has other pointers (to m_characteristic). // this object has other pointers (to m_characteristic).
combineCharacteristics(); combineCharacteristics();
} }
} // copyForPlayer
for(unsigned int i=0; i<RaceManager::DIFFICULTY_COUNT; i++) //-----------------------------------------------------------------------------
/** Copies this KartProperties to another one. Important: if you add any
* pointers to kart_properties, the data structure they are pointing to
* need to be copied here explicitely!
* \param source The source kart properties from which to copy this objects'
* values.
*/
void KartProperties::copyFrom(const KartProperties *source)
{
copyForPlayer(source);
// Also copy the AIProperties because they can differ for each car
// (but not for each player).
for (unsigned int i = 0; i < RaceManager::DIFFICULTY_COUNT; i++)
{ {
m_ai_properties[i] = new AIProperties((RaceManager::Difficulty)i); m_ai_properties[i].reset(new AIProperties((RaceManager::Difficulty) i));
assert(m_ai_properties); assert(m_ai_properties);
*m_ai_properties[i] = *source->m_ai_properties[i]; *m_ai_properties[i] = *source->m_ai_properties[i];
} }

View File

@ -19,6 +19,7 @@
#ifndef HEADER_KART_PROPERTIES_HPP #ifndef HEADER_KART_PROPERTIES_HPP
#define HEADER_KART_PROPERTIES_HPP #define HEADER_KART_PROPERTIES_HPP
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -63,7 +64,7 @@ private:
* reduce dependencies (and therefore compile time) when changing * reduce dependencies (and therefore compile time) when changing
* any AI property. There is one separate object for each * any AI property. There is one separate object for each
* difficulty. */ * difficulty. */
AIProperties *m_ai_properties[RaceManager::DIFFICULTY_COUNT]; std::shared_ptr<AIProperties> m_ai_properties[RaceManager::DIFFICULTY_COUNT];
/** The absolute path of the icon texture to use. */ /** The absolute path of the icon texture to use. */
Material *m_icon_material; Material *m_icon_material;
@ -212,6 +213,7 @@ public:
KartProperties (const std::string &filename=""); KartProperties (const std::string &filename="");
~KartProperties (); ~KartProperties ();
void copyForPlayer (const KartProperties *source);
void copyFrom (const KartProperties *source); void copyFrom (const KartProperties *source);
void getAllData (const XMLNode * root); void getAllData (const XMLNode * root);
void checkAllSet (const std::string &filename); void checkAllSet (const std::string &filename);
@ -373,7 +375,7 @@ public:
/** Returns a pointer to the AI properties. */ /** Returns a pointer to the AI properties. */
const AIProperties *getAIPropertiesForDifficulty() const const AIProperties *getAIPropertiesForDifficulty() const
{ {
return m_ai_properties[race_manager->getDifficulty()]; return m_ai_properties[race_manager->getDifficulty()].get();
} // getAIProperties } // getAIProperties
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------