Files
OpenRA/OpenRA.Mods.Common/Traits/Render/FloatingSpriteEmitter.cs
let5sne.win10 9cf6ebb986
Some checks failed
Continuous Integration / Linux (.NET 8.0) (push) Has been cancelled
Continuous Integration / Windows (.NET 8.0) (push) Has been cancelled
Initial commit: OpenRA game engine
Fork from OpenRA/OpenRA with one-click launch script (start-ra.cmd)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 21:46:54 +08:00

127 lines
3.8 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* 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.Immutable;
using OpenRA.Mods.Common.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Spawns moving sprite effects.")]
public class FloatingSpriteEmitterInfo : ConditionalTraitInfo, IRulesetLoaded
{
[FieldLoader.Require]
[Desc("The time between individual particle creation. Two values mean actual lifetime will vary between them.")]
public readonly ImmutableArray<int> Lifetime;
[FieldLoader.Require]
[Desc("The time in ticks until stop spawning. -1 means forever.")]
public readonly int Duration = -1;
[Desc("Should the duration been reset when taken damage")]
public readonly bool ResetOnDamaged = true;
[Desc("Randomised offset for the particle emitter.")]
public readonly ImmutableArray<WVec> Offset = [WVec.Zero];
[Desc("Randomized particle forward movement.")]
public readonly ImmutableArray<WDist> Speed = [WDist.Zero];
[Desc("Randomized particle gravity.")]
public readonly ImmutableArray<WDist> Gravity = [WDist.Zero];
[Desc("Randomize particle facing.")]
public readonly bool RandomFacing = true;
[Desc("Randomize particle turnrate.")]
public readonly int TurnRate = 0;
[Desc("The rate at which particle movement properties are reset.")]
public readonly int RandomRate = 4;
[Desc("How many particles should spawn. Two values for a random range.")]
public readonly ImmutableArray<int> SpawnFrequency = [1];
[Desc("Which image to use.")]
public readonly string Image = "smoke";
[Desc("Which sequence to use.")]
[SequenceReference(nameof(Image))]
public readonly ImmutableArray<string> Sequences = ["particles"];
[Desc("Which palette to use.")]
[PaletteReference(nameof(IsPlayerPalette))]
public readonly string Palette = "effect";
public readonly bool IsPlayerPalette = false;
public override object Create(ActorInitializer init) { return new FloatingSpriteEmitter(init.Self, this); }
}
public class FloatingSpriteEmitter : ConditionalTrait<FloatingSpriteEmitterInfo>, ITick, INotifyDamage
{
WVec offset;
IFacing facing;
int ticks;
int duration;
public FloatingSpriteEmitter(Actor self, FloatingSpriteEmitterInfo info)
: base(info)
{
offset = Util.RandomVector(self.World.SharedRandom, Info.Offset);
}
void INotifyDamage.Damaged(Actor self, AttackInfo e)
{
if (Info.ResetOnDamaged)
{
if (duration < 0)
offset = Util.RandomVector(self.World.SharedRandom, Info.Offset);
duration = Info.Duration;
}
}
protected override void Created(Actor self)
{
facing = self.TraitOrDefault<IFacing>();
base.Created(self);
}
protected override void TraitEnabled(Actor self)
{
base.TraitEnabled(self);
duration = Info.Duration;
offset = Util.RandomVector(self.World.SharedRandom, Info.Offset);
}
void ITick.Tick(Actor self)
{
if (!self.IsInWorld || IsTraitDisabled)
return;
if (Info.Duration > 0 && --duration < 0)
return;
if (--ticks < 0)
{
ticks = Util.RandomInRange(self.World.LocalRandom, Info.SpawnFrequency);
var spawnFacing = (!Info.RandomFacing && facing != null) ? facing.Facing : WAngle.FromFacing(self.World.LocalRandom.Next(256));
self.World.AddFrameEndTask(w => w.Add(new FloatingSprite(self, Info.Image, Info.Sequences, Info.Palette, Info.IsPlayerPalette,
Info.Lifetime, Info.Speed, Info.Gravity, Info.TurnRate, Info.RandomRate, self.CenterPosition + offset, spawnFacing)));
}
}
}
}