Add enter-cloak & exit-cloak effect for Cloak

This commit is contained in:
dnqbob
2022-01-14 13:51:43 +08:00
committed by Matthias Mailänder
parent d67f696bd0
commit 831bed2c4d
11 changed files with 89 additions and 25 deletions

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Effects;
using OpenRA.Primitives;
using OpenRA.Traits;
@@ -33,8 +34,8 @@ namespace OpenRA.Mods.Common.Traits
Dock = 256
}
// Type tag for cloaktypes
public class CloakType { }
// Type tag for DetectionTypes
public class DetectionType { }
[Desc("This unit can cloak and uncloak in specific situations.")]
public class CloakInfo : PausableConditionalTraitInfo
@@ -57,12 +58,36 @@ namespace OpenRA.Mods.Common.Traits
public readonly string Palette = "cloak";
public readonly bool IsPlayerPalette = false;
public readonly BitSet<CloakType> CloakTypes = new BitSet<CloakType>("Cloak");
public readonly BitSet<DetectionType> DetectionTypes = new BitSet<DetectionType>("Cloak");
[GrantedConditionReference]
[Desc("The condition to grant to self while cloaked.")]
public readonly string CloakedCondition = null;
[Desc("The type of cloak. Same type of cloaks won't trigger cloaking and uncloaking sound and effect.")]
public readonly string CloakType = null;
[Desc("Which image to use for the effect played when cloaking or uncloaking.")]
public readonly string EffectImage = null;
[Desc("Which effect sequence to play when cloaking.")]
[SequenceReference(nameof(EffectImage), allowNullImage: true)]
public readonly string CloakEffectSequence = null;
[Desc("Which effect sequence to play when uncloaking.")]
[SequenceReference(nameof(EffectImage), allowNullImage: true)]
public readonly string UncloakEffectSequence = null;
[PaletteReference(nameof(EffectPaletteIsPlayerPalette))]
public readonly string EffectPalette = "effect";
public readonly bool EffectPaletteIsPlayerPalette = false;
[Desc("Offset for the effect played when cloaking or uncloaking.")]
public readonly WVec EffectOffset = WVec.Zero;
[Desc("Should the effect track the actor.")]
public readonly bool EffectTracksActor = true;
public override object Create(ActorInitializer init) { return new Cloak(this); }
}
@@ -88,9 +113,12 @@ namespace OpenRA.Mods.Common.Traits
protected override void Created(Actor self)
{
otherCloaks = self.TraitsImplementing<Cloak>()
.Where(c => c != this)
.ToArray();
if (Info.CloakType != null)
{
otherCloaks = self.TraitsImplementing<Cloak>()
.Where(c => c != this && c.Info.CloakType == Info.CloakType)
.ToArray();
}
if (Cloaked)
{
@@ -167,16 +195,50 @@ namespace OpenRA.Mods.Common.Traits
cloakedToken = self.GrantCondition(Info.CloakedCondition);
// Sounds shouldn't play if the actor starts cloaked
if (!(firstTick && Info.InitialDelay == 0) && !otherCloaks.Any(a => a.Cloaked))
if (!(firstTick && Info.InitialDelay == 0) && (otherCloaks == null || !otherCloaks.Any(a => a.Cloaked)))
{
var pos = self.CenterPosition;
Game.Sound.Play(SoundType.World, Info.CloakSound, self.CenterPosition);
Func<WPos> posfunc = () => self.CenterPosition + Info.EffectOffset;
if (!Info.EffectTracksActor)
posfunc = () => pos + Info.EffectOffset;
if (Info.EffectImage != null && Info.CloakEffectSequence != null)
{
var palette = Info.EffectPalette;
if (Info.EffectPaletteIsPlayerPalette)
palette += self.Owner.InternalName;
self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(
posfunc, () => WAngle.Zero, w, Info.EffectImage, Info.CloakEffectSequence, palette)));
}
}
}
else if (!isCloaked && wasCloaked)
{
if (cloakedToken != Actor.InvalidConditionToken)
cloakedToken = self.RevokeCondition(cloakedToken);
if (!(firstTick && Info.InitialDelay == 0) && !otherCloaks.Any(a => a.Cloaked))
Game.Sound.Play(SoundType.World, Info.UncloakSound, self.CenterPosition);
if (!(firstTick && Info.InitialDelay == 0) && (otherCloaks == null || !otherCloaks.Any(a => a.Cloaked)))
{
var pos = self.CenterPosition;
Game.Sound.Play(SoundType.World, Info.CloakSound, pos);
Func<WPos> posfunc = () => self.CenterPosition + Info.EffectOffset;
if (!Info.EffectTracksActor)
posfunc = () => pos + Info.EffectOffset;
if (Info.EffectImage != null && Info.UncloakEffectSequence != null)
{
var palette = Info.EffectPalette;
if (Info.EffectPaletteIsPlayerPalette)
palette += self.Owner.InternalName;
self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(
posfunc, () => WAngle.Zero, w, Info.EffectImage, Info.UncloakEffectSequence, palette)));
}
}
}
wasCloaked = isCloaked;
@@ -196,7 +258,7 @@ namespace OpenRA.Mods.Common.Traits
return true;
return self.World.ActorsWithTrait<DetectCloaked>().Any(a => a.Actor.Owner.IsAlliedWith(viewer)
&& Info.CloakTypes.Overlaps(a.Trait.Info.CloakTypes)
&& Info.DetectionTypes.Overlaps(a.Trait.Info.DetectionTypes)
&& (self.CenterPosition - a.Actor.CenterPosition).LengthSquared <= a.Trait.Range.LengthSquared);
}