Implement TS bridge rendering.
This commit is contained in:
@@ -15,12 +15,13 @@ using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Placeholder actor that transforms into another actor type when repaired.")]
|
||||
[Desc("Placeholder actor used for dead segments and bridge end ramps.")]
|
||||
class BridgePlaceholderInfo : ITraitInfo
|
||||
{
|
||||
public readonly string Type = "GroundLevelBridge";
|
||||
|
||||
[FieldLoader.Require]
|
||||
public readonly DamageState DamageState = DamageState.Undamaged;
|
||||
|
||||
[Desc("Actor type to replace with on repair.")]
|
||||
[ActorReference] public readonly string ReplaceWithActor = null;
|
||||
|
||||
@@ -54,6 +55,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
void IBridgeSegment.Repair(Actor repairer)
|
||||
{
|
||||
if (Info.ReplaceWithActor == null)
|
||||
return;
|
||||
|
||||
self.World.AddFrameEndTask(w =>
|
||||
{
|
||||
self.Dispose();
|
||||
@@ -68,11 +72,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
void IBridgeSegment.Demolish(Actor saboteur)
|
||||
{
|
||||
// Do nothing, we're already dead
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
string IBridgeSegment.Type { get { return Info.Type; } }
|
||||
DamageState IBridgeSegment.DamageState { get { return DamageState.Dead; } }
|
||||
DamageState IBridgeSegment.DamageState { get { return Info.DamageState; } }
|
||||
bool IBridgeSegment.Valid { get { return self.IsInWorld; } }
|
||||
CVec[] IBridgeSegment.NeighbourOffsets { get { return Info.NeighbourOffsets; } }
|
||||
CPos IBridgeSegment.Location { get { return self.Location; } }
|
||||
|
||||
130
OpenRA.Mods.Common/Traits/Render/WithBridgeSpriteBody.cs
Normal file
130
OpenRA.Mods.Common/Traits/Render/WithBridgeSpriteBody.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits.Render
|
||||
{
|
||||
class WithBridgeSpriteBodyInfo : WithSpriteBodyInfo
|
||||
{
|
||||
public readonly string Type = "GroundLevelBridge";
|
||||
|
||||
[Desc("Offset to search for the 'A' neighbour")]
|
||||
public readonly CVec AOffset = CVec.Zero;
|
||||
|
||||
[Desc("Position to search for the 'B' neighbour")]
|
||||
public readonly CVec BOffset = CVec.Zero;
|
||||
|
||||
[SequenceReference]
|
||||
[Desc("Sequences to use when both neighbours are alive.")]
|
||||
public readonly string[] Sequences = { "idle" };
|
||||
|
||||
[SequenceReference]
|
||||
public readonly string[] ADestroyedSequences = { "adestroyed" };
|
||||
|
||||
[SequenceReference]
|
||||
public readonly string[] BDestroyedSequences = { "bdestroyed" };
|
||||
|
||||
[SequenceReference]
|
||||
public readonly string[] ABDestroyedSequences = { "abdestroyed" };
|
||||
|
||||
public override object Create(ActorInitializer init) { return new WithBridgeSpriteBody(init, this); }
|
||||
|
||||
public override IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
|
||||
{
|
||||
var anim = new Animation(init.World, image);
|
||||
anim.PlayFetchIndex(RenderSprites.NormalizeSequence(anim, init.GetDamageState(), Sequences.First()), () => 0);
|
||||
|
||||
yield return new SpriteActorPreview(anim, () => WVec.Zero, () => 0, p, rs.Scale);
|
||||
}
|
||||
}
|
||||
|
||||
class WithBridgeSpriteBody : WithSpriteBody, INotifyRemovedFromWorld
|
||||
{
|
||||
readonly WithBridgeSpriteBodyInfo bridgeInfo;
|
||||
readonly BridgeLayer bridgeLayer;
|
||||
readonly Actor self;
|
||||
|
||||
public WithBridgeSpriteBody(ActorInitializer init, WithBridgeSpriteBodyInfo info)
|
||||
: base(init, info, () => 0)
|
||||
{
|
||||
self = init.Self;
|
||||
bridgeInfo = info;
|
||||
bridgeLayer = init.World.WorldActor.Trait<BridgeLayer>();
|
||||
}
|
||||
|
||||
protected override void OnBuildComplete(Actor self)
|
||||
{
|
||||
if (bridgeInfo.AOffset != CVec.Zero)
|
||||
UpdateNeighbour(bridgeInfo.AOffset);
|
||||
|
||||
if (bridgeInfo.BOffset != CVec.Zero)
|
||||
UpdateNeighbour(bridgeInfo.BOffset);
|
||||
|
||||
SetDirty();
|
||||
}
|
||||
|
||||
void UpdateNeighbour(CVec offset)
|
||||
{
|
||||
var neighbour = bridgeLayer[self.Location + offset];
|
||||
if (neighbour == null)
|
||||
return;
|
||||
|
||||
var body = neighbour.TraitOrDefault<WithBridgeSpriteBody>();
|
||||
if (body != null && body.bridgeInfo.Type == bridgeInfo.Type)
|
||||
body.SetDirty();
|
||||
}
|
||||
|
||||
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
|
||||
{
|
||||
UpdateNeighbour(bridgeInfo.AOffset);
|
||||
UpdateNeighbour(bridgeInfo.BOffset);
|
||||
}
|
||||
|
||||
void SetDirty()
|
||||
{
|
||||
self.World.AddFrameEndTask(w =>
|
||||
{
|
||||
var aDestroyed = bridgeInfo.AOffset != CVec.Zero && NeighbourIsDestroyed(bridgeInfo.AOffset);
|
||||
var bDestroyed = bridgeInfo.BOffset != CVec.Zero && NeighbourIsDestroyed(bridgeInfo.BOffset);
|
||||
|
||||
var sequence = DefaultAnimation.CurrentSequence.Name;
|
||||
if (aDestroyed && bDestroyed && bridgeInfo.ABDestroyedSequences.Any())
|
||||
sequence = bridgeInfo.ABDestroyedSequences.Random(Game.CosmeticRandom);
|
||||
else if (aDestroyed && bridgeInfo.ADestroyedSequences.Any())
|
||||
sequence = bridgeInfo.ADestroyedSequences.Random(Game.CosmeticRandom);
|
||||
else if (bDestroyed && bridgeInfo.BDestroyedSequences.Any())
|
||||
sequence = bridgeInfo.BDestroyedSequences.Random(Game.CosmeticRandom);
|
||||
else
|
||||
sequence = bridgeInfo.Sequences.Random(Game.CosmeticRandom);
|
||||
|
||||
DefaultAnimation.PlayRepeating(NormalizeSequence(self, sequence));
|
||||
});
|
||||
}
|
||||
|
||||
bool NeighbourIsDestroyed(CVec offset)
|
||||
{
|
||||
var neighbour = bridgeLayer[self.Location + offset];
|
||||
if (neighbour == null)
|
||||
return false;
|
||||
|
||||
var segment = neighbour.TraitOrDefault<IBridgeSegment>();
|
||||
if (segment == null)
|
||||
return false;
|
||||
|
||||
return segment.DamageState == DamageState.Dead;
|
||||
}
|
||||
}
|
||||
}
|
||||
100
OpenRA.Mods.Common/Traits/Render/WithDeadBridgeSpriteBody.cs
Normal file
100
OpenRA.Mods.Common/Traits/Render/WithDeadBridgeSpriteBody.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits.Render
|
||||
{
|
||||
class WithDeadBridgeSpriteBodyInfo : WithSpriteBodyInfo
|
||||
{
|
||||
[ActorReference]
|
||||
public readonly string[] RampActors = { };
|
||||
|
||||
[Desc("Offset to search for the 'A' neighbour")]
|
||||
public readonly CVec AOffset = CVec.Zero;
|
||||
|
||||
[Desc("Position to search for the 'B' neighbour")]
|
||||
public readonly CVec BOffset = CVec.Zero;
|
||||
|
||||
[SequenceReference]
|
||||
public readonly string[] ARampSequences = { "aramp" };
|
||||
|
||||
[SequenceReference]
|
||||
public readonly string[] BRampSequences = { "bramp" };
|
||||
|
||||
[SequenceReference]
|
||||
public readonly string[] ABRampSequences = { "abramp" };
|
||||
|
||||
[SequenceReference]
|
||||
[Desc("Placeholder sequence to use in the map editor.")]
|
||||
public readonly string EditorSequence = "editor";
|
||||
|
||||
[PaletteReference]
|
||||
[Desc("Palette to use for the editor placeholder.")]
|
||||
public readonly string EditorPalette = "terrainalpha";
|
||||
|
||||
public override object Create(ActorInitializer init) { return new WithDeadBridgeSpriteBody(init, this); }
|
||||
|
||||
public override IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
|
||||
{
|
||||
var anim = new Animation(init.World, image);
|
||||
var sequence = init.World.Type == WorldType.Editor ? EditorSequence : Sequence;
|
||||
var palette = init.World.Type == WorldType.Editor ? init.WorldRenderer.Palette(EditorPalette) : p;
|
||||
anim.PlayFetchIndex(RenderSprites.NormalizeSequence(anim, init.GetDamageState(), sequence), () => 0);
|
||||
yield return new SpriteActorPreview(anim, () => WVec.Zero, () => 0, palette, rs.Scale);
|
||||
}
|
||||
}
|
||||
|
||||
class WithDeadBridgeSpriteBody : WithSpriteBody
|
||||
{
|
||||
readonly WithDeadBridgeSpriteBodyInfo bridgeInfo;
|
||||
readonly BridgeLayer bridgeLayer;
|
||||
|
||||
public WithDeadBridgeSpriteBody(ActorInitializer init, WithDeadBridgeSpriteBodyInfo info)
|
||||
: base(init, info, () => 0)
|
||||
{
|
||||
bridgeInfo = info;
|
||||
bridgeLayer = init.World.WorldActor.Trait<BridgeLayer>();
|
||||
}
|
||||
|
||||
bool RampExists(Actor self, CVec offset)
|
||||
{
|
||||
var neighbour = bridgeLayer[self.Location + offset];
|
||||
if (neighbour == null)
|
||||
return false;
|
||||
|
||||
return bridgeInfo.RampActors.Contains(neighbour.Info.Name);
|
||||
}
|
||||
|
||||
protected override void OnBuildComplete(Actor self)
|
||||
{
|
||||
self.World.AddFrameEndTask(w =>
|
||||
{
|
||||
var aRamp = bridgeInfo.AOffset != CVec.Zero && RampExists(self, bridgeInfo.AOffset);
|
||||
var bRamp = bridgeInfo.BOffset != CVec.Zero && RampExists(self, bridgeInfo.BOffset);
|
||||
|
||||
var sequence = DefaultAnimation.CurrentSequence.Name;
|
||||
if (aRamp && bRamp && bridgeInfo.ABRampSequences.Any())
|
||||
sequence = bridgeInfo.ABRampSequences.Random(Game.CosmeticRandom);
|
||||
else if (aRamp && bridgeInfo.ARampSequences.Any())
|
||||
sequence = bridgeInfo.ARampSequences.Random(Game.CosmeticRandom);
|
||||
else if (bRamp && bridgeInfo.BRampSequences.Any())
|
||||
sequence = bridgeInfo.BRampSequences.Random(Game.CosmeticRandom);
|
||||
|
||||
DefaultAnimation.PlayRepeating(NormalizeSequence(self, sequence));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user