Files
OpenRA/OpenRA.Mods.Common/Traits/Infantry/ScaredyCat.cs
reaperrr b52d055eec Misc constructor caching
Cache trait look-ups in constructor for various other traits and
activities.
2015-03-27 13:47:04 +01:00

94 lines
2.3 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Makes the unit automatically run around when taking damage.")]
class ScaredyCatInfo : ITraitInfo
{
[Desc("How long (in ticks) the actor should panic for.")]
public readonly int PanicLength = 25 * 10;
[Desc("Panic movement speed as a precentage of the normal speed.")]
public readonly int PanicSpeedModifier = 200;
[Desc("Chance (out of 100) the unit has to enter panic mode when attacked.")]
public readonly int AttackPanicChance = 20;
public object Create(ActorInitializer init) { return new ScaredyCat(init.Self, this); }
}
class ScaredyCat : ITick, INotifyIdle, INotifyDamage, INotifyAttack, ISpeedModifier, ISync, IRenderInfantrySequenceModifier
{
readonly ScaredyCatInfo info;
readonly Mobile mobile;
[Sync] readonly Actor self;
[Sync] int panicStartedTick;
bool Panicking { get { return panicStartedTick > 0; } }
public bool IsModifyingSequence { get { return Panicking; } }
public string SequencePrefix { get { return "panic-"; } }
public ScaredyCat(Actor self, ScaredyCatInfo info)
{
this.self = self;
this.info = info;
mobile = self.Trait<Mobile>();
}
public void Panic()
{
if (!Panicking)
self.CancelActivity();
panicStartedTick = self.World.WorldTick;
}
public void Tick(Actor self)
{
if (!Panicking)
return;
if (self.World.WorldTick >= panicStartedTick + info.PanicLength)
{
self.CancelActivity();
panicStartedTick = 0;
}
}
public void TickIdle(Actor self)
{
if (!Panicking)
return;
mobile.Nudge(self, self, true);
}
public void Damaged(Actor self, AttackInfo e)
{
if (e.Damage > 0)
Panic();
}
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
{
if (self.World.SharedRandom.Next(100 / info.AttackPanicChance) == 0)
Panic();
}
public int GetSpeedModifier()
{
return Panicking ? info.PanicSpeedModifier : 100;
}
}
}