1
0
Fork 0

Restructured cSplashPotionEntity code.

The callback doesn't need declaration in the header.
Renamed PotionName to PotionParticleType.
This commit is contained in:
madmaxoft 2014-07-15 22:41:42 +02:00
parent f5259d7651
commit cc452f51c8
5 changed files with 103 additions and 71 deletions

View File

@ -7,7 +7,7 @@ class cEntityEffect
{
public:
/** All types of entity effects (numbers correspond to IDs) */
/** All types of entity effects (numbers correspond to protocol / storage types) */
enum eType
{
effNoEffect = 0,
@ -66,21 +66,30 @@ public:
static cEntityEffect * CreateEntityEffect(cEntityEffect::eType a_EffectType, int a_Duration, short a_Intensity, double a_DistanceModifier);
/** Returns how many ticks this effect has been active for */
int GetTicks() { return m_Ticks; }
int GetTicks(void) const { return m_Ticks; }
/** Returns the duration of the effect */
int GetDuration() { return m_Duration; }
int GetDuration(void) const { return m_Duration; }
/** Returns how strong the effect will be applied */
short GetIntensity() { return m_Intensity; }
short GetIntensity(void) const { return m_Intensity; }
/** Returns the distance modifier for affecting potency */
double GetDistanceModifier() { return m_DistanceModifier; }
double GetDistanceModifier(void) const { return m_DistanceModifier; }
void SetTicks(int a_Ticks) { m_Ticks = a_Ticks; }
void SetDuration(int a_Duration) { m_Duration = a_Duration; }
void SetIntensity(short a_Intensity) { m_Intensity = a_Intensity; }
void SetDistanceModifier(double a_DistanceModifier) { m_DistanceModifier = a_DistanceModifier; }
/** Called on each tick.
By default increases the m_Ticks, descendants may override to provide additional processing. */
virtual void OnTick(cPawn & a_Target);
/** Called when the effect is first added to an entity */
virtual void OnActivate(cPawn & a_Target) { }
/** Called when the effect is removed from an entity */
virtual void OnDeactivate(cPawn & a_Target) { }
protected:

View File

@ -7,11 +7,77 @@
cSplashPotionEntity::cSplashPotionEntity(cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed, cEntityEffect::eType a_EntityEffectType, cEntityEffect a_EntityEffect, int a_PotionName) :
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cSplashPotionEntityCallback:
/** Used to distribute the splashed potion effect among nearby entities */
class cSplashPotionCallback :
public cEntityCallback
{
public:
/** Creates the callback.
@param a_HitPos The position where the splash potion has splashed
@param a_EntityEffectType The effect type of the potion
@param a_EntityEffect The effect description */
cSplashPotionCallback(const Vector3d & a_HitPos, cEntityEffect::eType a_EntityEffectType, const cEntityEffect & a_EntityEffect) :
m_HitPos(a_HitPos),
m_EntityEffectType(a_EntityEffectType),
m_EntityEffect(a_EntityEffect)
{
}
/** Called by cWorld::ForEachEntity(), adds the stored entity effect to the entity, if it is close enough. */
virtual bool Item(cEntity * a_Entity) override
{
double SplashDistance = (a_Entity->GetPosition() - m_HitPos).Length();
if (SplashDistance >= 20)
{
// Too far away
return false;
}
if (!a_Entity->IsPawn())
{
// Not an entity that can take effects
return false;
}
// y = -0.25x + 1, where x is the distance from the player. Approximation for potion splash.
// TODO: better equation
double Reduction = -0.25 * SplashDistance + 1.0;
if (Reduction < 0)
{
Reduction = 0;
}
((cPawn *) a_Entity)->AddEntityEffect(m_EntityEffectType, m_EntityEffect.GetDuration(), m_EntityEffect.GetIntensity(), Reduction);
return false;
}
private:
const Vector3d & m_HitPos;
cEntityEffect::eType m_EntityEffectType;
const cEntityEffect & m_EntityEffect;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cSplashPotionEntity:
cSplashPotionEntity::cSplashPotionEntity(
cEntity * a_Creator,
double a_X, double a_Y, double a_Z,
const Vector3d & a_Speed,
cEntityEffect::eType a_EntityEffectType,
cEntityEffect a_EntityEffect,
int a_PotionParticleType
) :
super(pkSplashPotion, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25),
m_EntityEffectType(a_EntityEffectType),
m_EntityEffect(a_EntityEffect),
m_PotionName(a_PotionName)
m_PotionParticleType(a_PotionParticleType)
{
SetSpeed(a_Speed);
}
@ -46,42 +112,7 @@ void cSplashPotionEntity::Splash(const Vector3d & a_HitPos)
cSplashPotionCallback Callback(a_HitPos, m_EntityEffectType, m_EntityEffect);
m_World->ForEachEntity(Callback);
m_World->BroadcastSoundParticleEffect(2002, a_HitPos.x, a_HitPos.y, a_HitPos.z, m_PotionName);
}
cSplashPotionEntity::cSplashPotionCallback::cSplashPotionCallback(const Vector3d & a_HitPos, cEntityEffect::eType &a_EntityEffectType, cEntityEffect &a_EntityEffect):
m_HitPos(a_HitPos),
m_EntityEffectType(a_EntityEffectType),
m_EntityEffect(a_EntityEffect)
{
}
bool cSplashPotionEntity::cSplashPotionCallback::Item(cEntity * a_Entity)
{
double SplashDistance = (a_Entity->GetPosition() - m_HitPos).Length();
if (SplashDistance < 20 && a_Entity->IsPawn())
{
// y = -0.25x + 1, where x is the distance from the player. Approximation for potion splash.
// TODO: better equation
double Reduction = -0.25 * SplashDistance + 1.0;
if (Reduction < 0)
{
Reduction = 0;
}
m_EntityEffect.SetDistanceModifier(Reduction);
((cPawn *) a_Entity)->AddEntityEffect(m_EntityEffectType, m_EntityEffect.GetDuration(), m_EntityEffect.GetIntensity(), Reduction);
}
return false;
m_World->BroadcastSoundParticleEffect(2002, a_HitPos.x, a_HitPos.y, a_HitPos.z, m_PotionParticleType);
}

View File

@ -25,43 +25,35 @@ public:
CLASS_PROTODEF(cSplashPotionEntity);
cSplashPotionEntity(cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d & a_Speed, cEntityEffect::eType a_EntityEffectType, cEntityEffect a_EntityEffect, int a_PotionName);
cSplashPotionEntity(
cEntity * a_Creator,
double a_X, double a_Y, double a_Z,
const Vector3d & a_Speed,
cEntityEffect::eType a_EntityEffectType,
cEntityEffect a_EntityEffect,
int a_PotionParticleType
);
cEntityEffect::eType GetEntityEffectType() { return m_EntityEffectType; }
cEntityEffect GetEntityEffect() { return m_EntityEffect; }
int GetPotionName() { return m_PotionName; }
cEntityEffect::eType GetEntityEffectType (void) const { return m_EntityEffectType; }
cEntityEffect GetEntityEffect (void) const { return m_EntityEffect; }
int GetPotionParticleType(void) const { return m_PotionParticleType; }
void SetEntityEffectType(cEntityEffect::eType a_EntityEffectType) { m_EntityEffectType = a_EntityEffectType; }
void SetEntityEffect(cEntityEffect a_EntityEffect) { m_EntityEffect = a_EntityEffect; }
void SetPotionName(int a_PotionName) { m_PotionName = a_PotionName; }
void SetPotionParticleType(int a_PotionParticleType) { m_PotionParticleType = a_PotionParticleType; }
protected:
cEntityEffect::eType m_EntityEffectType;
cEntityEffect m_EntityEffect;
int m_PotionParticleType;
// cProjectileEntity overrides:
virtual void OnHitSolidBlock(const Vector3d & a_HitPos, eBlockFace a_HitFace) override;
virtual void OnHitEntity (cEntity & a_EntityHit, const Vector3d & a_HitPos) override;
/** Splashes the potion, fires its particle effects and sounds
* @param a_HitPos The position where the potion will splash
*/
@param a_HitPos The position where the potion will splash */
void Splash(const Vector3d & a_HitPos);
cEntityEffect::eType m_EntityEffectType;
cEntityEffect m_EntityEffect;
int m_PotionName;
class cSplashPotionCallback :
public cEntityCallback
{
public:
cSplashPotionCallback(const Vector3d & a_HitPos, cEntityEffect::eType &a_EntityEffectType, cEntityEffect &a_EntityEffect);
virtual bool Item(cEntity *a_Entity) override;
private:
const Vector3d &m_HitPos;
cEntityEffect::eType &m_EntityEffectType;
cEntityEffect &m_EntityEffect;
};
} ; // tolua_export

View File

@ -613,7 +613,7 @@ void cNBTChunkSerializer::AddProjectileEntity(cProjectileEntity * a_Projectile)
m_Writer.AddInt("EffectDuration", (Int16)Potion->GetEntityEffect().GetDuration());
m_Writer.AddShort("EffectIntensity", Potion->GetEntityEffect().GetIntensity());
m_Writer.AddDouble("EffectDistanceModifier", Potion->GetEntityEffect().GetDistanceModifier());
m_Writer.AddInt("PotionName", Potion->GetPotionName());
m_Writer.AddInt("PotionName", Potion->GetPotionParticleType());
}
case cProjectileEntity::pkGhastFireball:
{

View File

@ -1681,7 +1681,7 @@ void cWSSAnvil::LoadSplashPotionFromNBT(cEntityList & a_Entities, const cParsedN
SplashPotion->SetEntityEffectType((cEntityEffect::eType) a_NBT.FindChildByName(a_TagIdx, "EffectType"));
SplashPotion->SetEntityEffect(cEntityEffect(EffectDuration, EffectIntensity, EffectDistanceModifier));
SplashPotion->SetPotionName(a_NBT.FindChildByName(a_TagIdx, "PotionName"));
SplashPotion->SetPotionParticleType(a_NBT.FindChildByName(a_TagIdx, "PotionName"));
// Store the new splash potion in the entities list:
a_Entities.push_back(SplashPotion.release());