Merge pull request #4148 from Mailaender/rank-hardening

Hardened the new Rank effect against crashes
This commit is contained in:
Paul Chote
2013-11-21 23:53:21 -08:00
7 changed files with 21 additions and 18 deletions

View File

@@ -170,7 +170,8 @@ namespace OpenRA.Traits
{ {
public static bool IsDead(this Actor self) public static bool IsDead(this Actor self)
{ {
if (self.Destroyed) return true; if (self.Destroyed)
return true;
var health = self.TraitOrDefault<Health>(); var health = self.TraitOrDefault<Health>();
return (health == null) ? false : health.IsDead; return (health == null) ? false : health.IsDead;
@@ -178,7 +179,8 @@ namespace OpenRA.Traits
public static DamageState GetDamageState(this Actor self) public static DamageState GetDamageState(this Actor self)
{ {
if (self.Destroyed) return DamageState.Dead; if (self.Destroyed)
return DamageState.Dead;
var health = self.TraitOrDefault<Health>(); var health = self.TraitOrDefault<Health>();
return (health == null) ? DamageState.Undamaged : health.DamageState; return (health == null) ? DamageState.Undamaged : health.DamageState;

View File

@@ -181,7 +181,7 @@ namespace OpenRA
public void Add(IEffect b) { effects.Add(b); } public void Add(IEffect b) { effects.Add(b); }
public void Remove(IEffect b) { effects.Remove(b); } public void Remove(IEffect b) { effects.Remove(b); }
public void AddFrameEndTask( Action<World> a ) { frameEndActions.Enqueue( a ); } public void AddFrameEndTask(Action<World> a) { frameEndActions.Enqueue(a); }
public event Action<Actor> ActorAdded = _ => { }; public event Action<Actor> ActorAdded = _ => { };
public event Action<Actor> ActorRemoved = _ => { }; public event Action<Actor> ActorRemoved = _ => { };

View File

@@ -479,7 +479,7 @@ namespace OpenRA.Mods.RA.AI
{ {
squads.RemoveAll(s => !s.IsValid); squads.RemoveAll(s => !s.IsValid);
foreach (var s in squads) foreach (var s in squads)
s.units.RemoveAll(a => a.Destroyed || a.IsDead() || a.Owner != p); s.units.RemoveAll(a => a.IsDead() || a.Owner != p);
} }
// Use of this function requires that one squad of this type. Hence it is a piece of shit // Use of this function requires that one squad of this type. Hence it is a piece of shit
@@ -506,8 +506,8 @@ namespace OpenRA.Mods.RA.AI
void AssignRolesToIdleUnits(Actor self) void AssignRolesToIdleUnits(Actor self)
{ {
CleanSquads(); CleanSquads();
activeUnits.RemoveAll(a => a.Destroyed || a.IsDead() || a.Owner != p); activeUnits.RemoveAll(a => a.IsDead() || a.Owner != p);
unitsHangingAroundTheBase.RemoveAll(a => a.Destroyed || a.IsDead() || a.Owner != p); unitsHangingAroundTheBase.RemoveAll(a => a.IsDead() || a.Owner != p);
if (--rushTicks <= 0) if (--rushTicks <= 0)
{ {

View File

@@ -87,7 +87,7 @@ namespace OpenRA.Mods.RA.Effects
world.AddFrameEndTask(w => w.Remove(this)); world.AddFrameEndTask(w => w.Remove(this));
show = false; show = false;
if (!self.IsInWorld || self.Destroyed || self.IsDead() || self.World.RenderPlayer == null) if (!self.IsInWorld || self.IsDead() || self.World.RenderPlayer == null)
return; return;
var gps = watcher[self.World.RenderPlayer]; var gps = watcher[self.World.RenderPlayer];

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.RA.Effects
public void Tick( World world ) public void Tick( World world )
{ {
if (a.Destroyed || a.IsDead() || b.GetDamageModifier(null, null) > 0) if (a.IsDead() || b.GetDamageModifier(null, null) > 0)
world.AddFrameEndTask(w => w.Remove(this)); world.AddFrameEndTask(w => w.Remove(this));
} }

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Effects; using OpenRA.Effects;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Effects namespace OpenRA.Mods.RA.Effects
{ {
@@ -18,20 +19,19 @@ namespace OpenRA.Mods.RA.Effects
{ {
Actor self; Actor self;
Animation anim = new Animation("rank"); Animation anim = new Animation("rank");
GainsExperience xp;
public Rank(Actor self) public Rank(Actor self)
{ {
this.self = self; this.self = self;
xp = self.Trait<GainsExperience>(); var xp = self.Trait<GainsExperience>();
anim.PlayRepeating("rank"); anim.PlayRepeating("rank");
anim.PlayFetchIndex("rank", () => xp.Level - 1); anim.PlayFetchIndex("rank", () => xp.Level == 0 ? 0 : xp.Level - 1);
} }
public void Tick(World world) public void Tick(World world)
{ {
if (self.Destroyed) if (self.IsDead())
world.AddFrameEndTask(w => w.Remove(this)); world.AddFrameEndTask(w => w.Remove(this));
else else
anim.Tick(); anim.Tick();
@@ -42,10 +42,7 @@ namespace OpenRA.Mods.RA.Effects
if (!self.IsInWorld) if (!self.IsInWorld)
yield break; yield break;
if (self.Destroyed) if (self.IsDead())
yield break;
if (xp.Level < 1)
yield break; yield break;
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer)) if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))

View File

@@ -76,8 +76,12 @@ namespace OpenRA.Mods.RA
Sound.PlayNotification(self.Owner, "Sounds", "LevelUp", self.Owner.Country.Race); Sound.PlayNotification(self.Owner, "Sounds", "LevelUp", self.Owner.Country.Race);
self.World.AddFrameEndTask(w => w.Add(new CrateEffect(self, "levelup"))); self.World.AddFrameEndTask(w => w.Add(new CrateEffect(self, "levelup")));
if (Level == 1 && !self.Destroyed) if (Level == 1)
self.World.AddFrameEndTask(w => w.Add(new Rank(self))); self.World.AddFrameEndTask(w =>
{
if (!self.IsDead())
w.Add(new Rank(self));
});
} }
} }