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()
{
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_kart_animation = NULL;
assert(m_kart_properties != NULL);
assert(m_kart_properties);
// We have to take a copy of the kart model, since otherwise
// the animations will be mixed up (i.e. different instances of

View File

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

View File

@ -2455,7 +2455,7 @@ void Kart::loadData(RaceManager::KartType type, bool is_animated_model)
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());
}
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=""
if (filename != "")
{
for(unsigned int i=0; i<RaceManager::DIFFICULTY_COUNT; i++)
m_ai_properties[i]= NULL;
load(filename, "kart");
}
else
{
for(unsigned int i=0; i<RaceManager::DIFFICULTY_COUNT; i++)
m_ai_properties[i]= new AIProperties((RaceManager::Difficulty)i);
for (unsigned int i = 0; i < RaceManager::DIFFICULTY_COUNT; i++)
m_ai_properties[i].reset(new AIProperties((RaceManager::Difficulty) i));
}
} // KartProperties
@ -110,19 +108,18 @@ KartProperties::KartProperties(const std::string &filename)
KartProperties::~KartProperties()
{
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
//-----------------------------------------------------------------------------
/** 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
* 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'
* values.
*/
void KartProperties::copyFrom(const KartProperties *source)
void KartProperties::copyForPlayer(const KartProperties *source)
{
*this = *source;
@ -136,10 +133,24 @@ void KartProperties::copyFrom(const KartProperties *source)
// this object has other pointers (to m_characteristic).
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);
*m_ai_properties[i] = *source->m_ai_properties[i];
}
@ -337,7 +348,7 @@ void KartProperties::getAllData(const XMLNode * root)
root->get("shadow-x-offset", &m_shadow_x_offset );
root->get("shadow-z-offset", &m_shadow_z_offset );
root->get("type", &m_kart_type );
root->get("type", &m_kart_type );
if(const XMLNode *dimensions_node = root->getNode("center"))
dimensions_node->get("gravity-shift", &m_gravity_center_shift);

View File

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