Merge pull request #11112 from reaperrr/improve-LeavesTrails

Add facings support and other controls to LeavesTrails
This commit is contained in:
Oliver Brakmann
2016-05-21 12:36:13 +02:00
2 changed files with 55 additions and 13 deletions

View File

@@ -23,14 +23,14 @@ namespace OpenRA.Effects
readonly bool visibleThroughFog; readonly bool visibleThroughFog;
readonly bool scaleSizeWithZoom; readonly bool scaleSizeWithZoom;
public SpriteEffect(WPos pos, World world, string image, string sequence, string palette, bool visibleThroughFog = false, bool scaleSizeWithZoom = false) public SpriteEffect(WPos pos, World world, string image, string sequence, string palette, bool visibleThroughFog = false, bool scaleSizeWithZoom = false, int facing = 0)
{ {
this.world = world; this.world = world;
this.pos = pos; this.pos = pos;
this.palette = palette; this.palette = palette;
this.scaleSizeWithZoom = scaleSizeWithZoom; this.scaleSizeWithZoom = scaleSizeWithZoom;
this.visibleThroughFog = visibleThroughFog; this.visibleThroughFog = visibleThroughFog;
anim = new Animation(world, image); anim = new Animation(world, image, () => facing);
anim.PlayThen(sequence, () => world.AddFrameEndTask(w => w.Remove(this))); anim.PlayThen(sequence, () => world.AddFrameEndTask(w => w.Remove(this)));
} }

View File

@@ -49,41 +49,83 @@ namespace OpenRA.Mods.Common.Traits.Render
[Desc("Delay between trail updates when moving.")] [Desc("Delay between trail updates when moving.")]
public readonly int MovingInterval = 0; public readonly int MovingInterval = 0;
[Desc("Delay before first trail.")]
public readonly int StartDelay = 0;
[Desc("Trail spawn positions relative to actor position. (forward, right, up) triples")]
public readonly WVec[] Offsets = { WVec.Zero };
[Desc("Should the trail spawn relative to last position or current position?")]
public readonly bool SpawnAtLastPosition = true;
public override object Create(ActorInitializer init) { return new LeavesTrails(init.Self, this); } public override object Create(ActorInitializer init) { return new LeavesTrails(init.Self, this); }
} }
public class LeavesTrails : UpgradableTrait<LeavesTrailsInfo>, ITick public class LeavesTrails : UpgradableTrait<LeavesTrailsInfo>, ITick, INotifyCreated
{ {
BodyOrientation body;
IFacing facing;
int cachedFacing;
int cachedInterval;
public LeavesTrails(Actor self, LeavesTrailsInfo info) public LeavesTrails(Actor self, LeavesTrailsInfo info)
: base(info) { } : base(info)
{
cachedInterval = Info.StartDelay;
}
WPos cachedPosition; WPos cachedPosition;
public void Created(Actor self)
{
body = self.Trait<BodyOrientation>();
facing = self.TraitOrDefault<IFacing>();
cachedFacing = facing != null ? facing.Facing : 0;
cachedPosition = self.CenterPosition;
}
int ticks; int ticks;
int offset;
bool wasStationary;
bool isMoving;
public void Tick(Actor self) public void Tick(Actor self)
{ {
if (IsTraitDisabled) if (IsTraitDisabled)
return; return;
var isMoving = self.CenterPosition != cachedPosition; wasStationary = !isMoving;
isMoving = self.CenterPosition != cachedPosition;
if ((isMoving && !Info.TrailWhileMoving) || (!isMoving && !Info.TrailWhileStationary)) if ((isMoving && !Info.TrailWhileMoving) || (!isMoving && !Info.TrailWhileStationary))
return; return;
var interval = isMoving ? Info.MovingInterval : Info.StationaryInterval; if (isMoving && wasStationary)
if (++ticks >= interval) cachedInterval = Info.StartDelay;
{
var cachedCell = self.World.Map.CellContaining(cachedPosition);
var type = self.World.Map.GetTerrainInfo(cachedCell).Type;
var pos = Info.Type == TrailType.CenterPosition ? cachedPosition : if (++ticks >= cachedInterval)
self.World.Map.CenterOfCell(cachedCell); {
var spawnCell = Info.SpawnAtLastPosition ? self.World.Map.CellContaining(cachedPosition) : self.World.Map.CellContaining(self.CenterPosition);
var type = self.World.Map.GetTerrainInfo(spawnCell).Type;
if (++offset >= Info.Offsets.Length)
offset = 0;
var offsetRotation = Info.Offsets[offset].Rotate(body.QuantizeOrientation(self, self.Orientation));
var spawnPosition = Info.SpawnAtLastPosition ? cachedPosition : self.CenterPosition;
var pos = Info.Type == TrailType.CenterPosition ? spawnPosition + body.LocalToWorld(offsetRotation) :
self.World.Map.CenterOfCell(spawnCell);
var spawnFacing = Info.SpawnAtLastPosition ? cachedFacing : (facing != null ? facing.Facing : 0);
if (Info.TerrainTypes.Contains(type) && !string.IsNullOrEmpty(Info.Image)) if (Info.TerrainTypes.Contains(type) && !string.IsNullOrEmpty(Info.Image))
self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, self.World, Info.Image, self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, self.World, Info.Image,
Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, Info.VisibleThroughFog))); Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, Info.VisibleThroughFog, false, spawnFacing)));
cachedPosition = self.CenterPosition; cachedPosition = self.CenterPosition;
cachedFacing = facing != null ? facing.Facing : 0;
ticks = 0; ticks = 0;
cachedInterval = isMoving && !wasStationary ? Info.MovingInterval : Info.StationaryInterval;
} }
} }