Refactor building power-down and Invulnerable effect. Introduces render bugs on state change (effects appearing/dissapearing a frame too late)

This commit is contained in:
Paul Chote
2010-01-08 01:26:30 +13:00
parent 42cfa7ec59
commit f3fd108a23
8 changed files with 87 additions and 45 deletions

View File

@@ -0,0 +1,30 @@
using System.Collections.Generic;
using OpenRa.Game.Graphics;
using OpenRa.Game.Traits;
namespace OpenRa.Game.Effects
{
class InvulnEffect : IEffect
{
Actor a;
IronCurtainable b;
public InvulnEffect(Actor a)
{
this.a = a;
this.b = a.traits.Get<IronCurtainable>();
}
public void Tick()
{
if (a.IsDead || b.GetDamageModifier() > 0)
Game.world.AddFrameEndTask(w => w.Remove(this));
}
public IEnumerable<Renderable> Render()
{
foreach (var r in a.Render())
yield return r.WithPalette(PaletteType.Invuln);
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Collections.Generic;
using OpenRa.Game.Graphics;
using OpenRa.Game.Traits;
namespace OpenRa.Game.Effects
{
class PowerDownIndicator : IEffect
{
Actor a;
Building b;
Animation anim = new Animation("powerdown");
public PowerDownIndicator(Actor a)
{
this.a = a;
this.b = a.traits.Get<Building>();
anim.PlayRepeating("disabled");
}
public void Tick()
{
if (!b.Disabled || a.IsDead)
Game.world.AddFrameEndTask(w => w.Remove(this));
}
public IEnumerable<Renderable> Render()
{
foreach (var r in a.Render())
yield return r.WithPalette(PaletteType.Disabled);
if (b.ManuallyDisabled)
yield return new Renderable(anim.Image,
a.CenterLocation - .5f * anim.Image.size, PaletteType.Chrome);
}
}
}

View File

@@ -83,6 +83,7 @@
<Compile Include="Effects\Corpse.cs" />
<Compile Include="Effects\DelayedAction.cs" />
<Compile Include="Effects\FlashTarget.cs" />
<Compile Include="Effects\InvulnEffect.cs" />
<Compile Include="Effects\MoveFlash.cs" />
<Compile Include="Effects\PowerDownIndicator.cs" />
<Compile Include="Effects\RepairIndicator.cs" />

View File

@@ -26,7 +26,7 @@ namespace OpenRa.Game.Traits
protected override void QueueAttack( Actor self, Order order )
{
if (self.traits.Contains<Building>() && self.traits.Get<Building>().InsuffientPower())
if (self.traits.Contains<Building>() && self.traits.Get<Building>().Disabled)
return;
const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */

View File

@@ -9,14 +9,16 @@ using OpenRa.Game.Graphics;
namespace OpenRa.Game.Traits
{
class Building : INotifyDamage, IOrder, ITick, IRenderModifier
class Building : INotifyDamage, IOrder, ITick
{
readonly Actor self;
public readonly BuildingInfo unitInfo;
bool isRepairing = false;
bool isPoweredDown = false;
public bool IsPoweredDown { get { return isPoweredDown; } }
bool isManuallyDisabled = false;
bool wasManuallyDisabled = false;
public bool ManuallyDisabled { get { return isManuallyDisabled; } }
public bool Disabled { get { return (isManuallyDisabled || (unitInfo.Powered && self.Owner.GetPowerState() != PowerState.Normal)); } }
public Building(Actor self)
{
this.self = self;
@@ -25,14 +27,9 @@ namespace OpenRa.Game.Traits
* ((float2)self.Location + .5f * (float2)unitInfo.Dimensions);
}
public bool InsuffientPower()
{
return (isPoweredDown || (unitInfo.Powered && self.Owner.GetPowerState() != PowerState.Normal));
}
public int GetPowerUsage()
{
if (isPoweredDown)
if (isManuallyDisabled)
return 0;
if (unitInfo.Power > 0) /* todo: is this how real-ra scales it? */
@@ -41,21 +38,6 @@ namespace OpenRa.Game.Traits
return unitInfo.Power;
}
public Animation iconAnim;
public IEnumerable<Renderable>
ModifyRender(Actor self, IEnumerable<Renderable> rs)
{
if (!InsuffientPower())
return rs;
List<Renderable> nrs = new List<Renderable>(rs);
foreach(var r in rs)
{
nrs.Add(r.WithPalette(PaletteType.Disabled));
}
return nrs;
}
public void Damaged(Actor self, AttackInfo e)
{
if (e.DamageState == DamageState.Dead)
@@ -82,9 +64,8 @@ namespace OpenRa.Game.Traits
if (order.OrderString == "PowerDown")
{
isPoweredDown = !isPoweredDown;
if (isPoweredDown) Game.world.AddFrameEndTask(w => w.Add(new PowerDownIndicator(self)));
Sound.Play((isPoweredDown) ? "bleep12.aud" : "bleep11.aud");
isManuallyDisabled = !isManuallyDisabled;
Sound.Play((isManuallyDisabled) ? "bleep12.aud" : "bleep11.aud");
}
}
@@ -92,6 +73,11 @@ namespace OpenRa.Game.Traits
public void Tick(Actor self)
{
// If the disabled state has changed since the last frame
if (Disabled ^ wasManuallyDisabled
&& (wasManuallyDisabled = Disabled)) // Yes, I mean assignment
Game.world.AddFrameEndTask(w => w.Add(new PowerDownIndicator(self)));
if (!isRepairing) return;
if (remainingTicks == 0)

View File

@@ -8,7 +8,7 @@ using OpenRa.Game.Graphics;
namespace OpenRa.Game.Traits
{
class IronCurtainable: IOrder, IDamageModifier, ITick, IRenderModifier
class IronCurtainable: IOrder, IDamageModifier, ITick
{
int RemainingTicks = 0;
@@ -23,6 +23,7 @@ namespace OpenRa.Game.Traits
{
return (RemainingTicks > 0) ? 0.0f : 1.0f;
}
public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor)
{
return null; // Chronoshift order is issued through Chrome.
@@ -33,6 +34,7 @@ namespace OpenRa.Game.Traits
if (order.OrderString == "IronCurtain")
{
Game.controller.CancelInputMode();
Game.world.AddFrameEndTask(w => w.Add(new InvulnEffect(self)));
RemainingTicks = (int)(Rules.General.IronCurtain * 60 * 25);
Sound.Play("ironcur9.aud");
// Play active anim
@@ -41,18 +43,5 @@ namespace OpenRa.Game.Traits
ironCurtain.traits.Get<RenderBuilding>().PlayCustomAnim(ironCurtain, "active");
}
}
public IEnumerable<Renderable> ModifyRender(Actor self, IEnumerable<Renderable> rs)
{
if (RemainingTicks <= 0)
return rs;
List<Renderable> nrs = new List<Renderable>(rs);
foreach(var r in rs)
{
nrs.Add(r.WithPalette(PaletteType.Invuln));
}
return nrs;
}
}
}

View File

@@ -140,7 +140,7 @@ namespace OpenRa.Game.Traits
foreach (var p in primaryProducers)
{
// Ignore buildings that are disabled
if (p.traits.Contains<Building>() && p.traits.Get<Building>().InsuffientPower())
if (p.traits.Contains<Building>() && p.traits.Get<Building>().Disabled)
continue;
producer = p;
break;

View File

@@ -19,7 +19,7 @@ namespace OpenRa.Game.Traits
// Check if powered
var b = self.traits.Get<Building>();
if (b != null && b.InsuffientPower())
if (b != null && b.Disabled)
return false;
return true;