Remove component.Startup calls (#18229)

This commit is contained in:
metalgearsloth
2023-07-23 16:11:13 +10:00
committed by GitHub
parent e755509fc7
commit 5dd4169c51
5 changed files with 102 additions and 93 deletions

View File

@@ -384,7 +384,7 @@ namespace Content.Client.Light.Components
public readonly List<LightBehaviourAnimationTrack> Behaviours = new(); public readonly List<LightBehaviourAnimationTrack> Behaviours = new();
[ViewVariables(VVAccess.ReadOnly)] [ViewVariables(VVAccess.ReadOnly)]
private readonly List<AnimationContainer> _animations = new(); public readonly List<AnimationContainer> Animations = new();
[ViewVariables(VVAccess.ReadOnly)] [ViewVariables(VVAccess.ReadOnly)]
private Dictionary<string, object> _originalPropertyValues = new(); private Dictionary<string, object> _originalPropertyValues = new();
@@ -400,60 +400,11 @@ namespace Content.Client.Light.Components
AnimationTracks = {behaviour} AnimationTracks = {behaviour}
}; };
_animations.Add(new AnimationContainer(key, animation, behaviour)); Animations.Add(new AnimationContainer(key, animation, behaviour));
key++; key++;
} }
} }
protected override void Startup()
{
base.Startup();
// TODO: Do NOT ensure component here. And use eventbus events instead...
Owner.EnsureComponent<AnimationPlayerComponent>();
if (_entMan.TryGetComponent(Owner, out AnimationPlayerComponent? animation))
{
#pragma warning disable 618
animation.AnimationCompleted += OnAnimationCompleted;
#pragma warning restore 618
}
foreach (var container in _animations)
{
container.LightBehaviour.Initialize(Owner, _random, _entMan);
}
// we need to initialize all behaviours before starting any
foreach (var container in _animations)
{
if (container.LightBehaviour.Enabled)
{
StartLightBehaviour(container.LightBehaviour.ID);
}
}
}
private void OnAnimationCompleted(string key)
{
var container = _animations.FirstOrDefault(x => x.FullKey == key);
if (container == null)
{
return;
}
if (container.LightBehaviour.IsLooped)
{
container.LightBehaviour.UpdatePlaybackValues(container.Animation);
if (_entMan.TryGetComponent(Owner, out AnimationPlayerComponent? animation))
{
animation.Play(container.Animation, container.FullKey);
}
}
}
/// <summary> /// <summary>
/// If we disable all the light behaviours we want to be able to revert the light to its original state. /// If we disable all the light behaviours we want to be able to revert the light to its original state.
/// </summary> /// </summary>
@@ -485,7 +436,7 @@ namespace Content.Client.Light.Components
return; return;
} }
foreach (var container in _animations) foreach (var container in Animations)
{ {
if (container.LightBehaviour.ID == id || id == string.Empty) if (container.LightBehaviour.ID == id || id == string.Empty)
{ {
@@ -516,7 +467,7 @@ namespace Content.Client.Light.Components
var toRemove = new List<AnimationContainer>(); var toRemove = new List<AnimationContainer>();
foreach (var container in _animations) foreach (var container in Animations)
{ {
if (container.LightBehaviour.ID == id || id == string.Empty) if (container.LightBehaviour.ID == id || id == string.Empty)
{ {
@@ -534,7 +485,7 @@ namespace Content.Client.Light.Components
foreach (var container in toRemove) foreach (var container in toRemove)
{ {
_animations.Remove(container); Animations.Remove(container);
} }
if (resetToOriginalSettings && _entMan.TryGetComponent(Owner, out PointLightComponent? light)) if (resetToOriginalSettings && _entMan.TryGetComponent(Owner, out PointLightComponent? light))
@@ -559,7 +510,7 @@ namespace Content.Client.Light.Components
return false; return false;
} }
return _animations.Any(container => animation.HasRunningAnimation(KeyPrefix + container.Key)); return Animations.Any(container => animation.HasRunningAnimation(KeyPrefix + container.Key));
} }
/// <summary> /// <summary>
@@ -569,7 +520,7 @@ namespace Content.Client.Light.Components
{ {
var key = 0; var key = 0;
while (_animations.Any(x => x.Key == key)) while (Animations.Any(x => x.Key == key))
{ {
key++; key++;
} }
@@ -582,7 +533,7 @@ namespace Content.Client.Light.Components
behaviour.Initialize(Owner, _random, _entMan); behaviour.Initialize(Owner, _random, _entMan);
var container = new AnimationContainer(key, animation, behaviour); var container = new AnimationContainer(key, animation, behaviour);
_animations.Add(container); Animations.Add(container);
if (playImmediately) if (playImmediately)
{ {

View File

@@ -0,0 +1,55 @@
using System.Linq;
using Content.Client.Light.Components;
using Robust.Client.GameObjects;
using Robust.Shared.Random;
namespace Content.Client.Light.EntitySystems;
public sealed class LightBehaviorSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly AnimationPlayerSystem _player = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LightBehaviourComponent, ComponentStartup>(OnLightStartup);
SubscribeLocalEvent<LightBehaviourComponent, AnimationCompletedEvent>(OnBehaviorAnimationCompleted);
}
private void OnBehaviorAnimationCompleted(EntityUid uid, LightBehaviourComponent component, AnimationCompletedEvent args)
{
var container = component.Animations.FirstOrDefault(x => x.FullKey == args.Key);
if (container == null)
{
return;
}
if (container.LightBehaviour.IsLooped)
{
container.LightBehaviour.UpdatePlaybackValues(container.Animation);
_player.Play(uid, container.Animation, container.FullKey);
}
}
private void OnLightStartup(EntityUid uid, LightBehaviourComponent component, ComponentStartup args)
{
// TODO: Do NOT ensure component here. And use eventbus events instead...
EnsureComp<AnimationPlayerComponent>(uid);
foreach (var container in component.Animations)
{
container.LightBehaviour.Initialize(uid, _random, EntityManager);
}
// we need to initialize all behaviours before starting any
foreach (var container in component.Animations)
{
if (container.LightBehaviour.Enabled)
{
component.StartLightBehaviour(container.LightBehaviour.ID);
}
}
}
}

View File

@@ -1,18 +1,10 @@
using System.Numerics; using System.Numerics;
namespace Content.Server.Movement.Components namespace Content.Server.Movement.Components;
[RegisterComponent]
public sealed class StressTestMovementComponent : Component
{ {
[RegisterComponent] public float Progress { get; set; }
public sealed class StressTestMovementComponent : Component public Vector2 Origin { get; set; }
{
public float Progress { get; set; }
public Vector2 Origin { get; set; }
protected override void Startup()
{
base.Startup();
Origin = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).WorldPosition;
}
}
} }

View File

@@ -1,32 +1,42 @@
using System.Numerics; using System.Numerics;
using Content.Server.Movement.Components; using Content.Server.Movement.Components;
using JetBrains.Annotations;
namespace Content.Server.Movement namespace Content.Server.Movement;
public sealed class StressTestMovementSystem : EntitySystem
{ {
[UsedImplicitly] [Dependency] private readonly SharedTransformSystem _transform = default!;
internal sealed class StressTestMovementSystem : EntitySystem
public override void Initialize()
{ {
public override void Update(float frameTime) base.Initialize();
SubscribeLocalEvent<StressTestMovementComponent, ComponentStartup>(OnStressStartup);
}
private void OnStressStartup(EntityUid uid, StressTestMovementComponent component, ComponentStartup args)
{
component.Origin = _transform.GetWorldPosition(uid);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<StressTestMovementComponent, TransformComponent>();
while (query.MoveNext(out var uid, out var stressTest, out var transform))
{ {
base.Update(frameTime); stressTest.Progress += frameTime;
foreach (var stressTest in EntityManager.EntityQuery<StressTestMovementComponent>(true)) if (stressTest.Progress > 1)
{ {
var transform = EntityManager.GetComponent<TransformComponent>(stressTest.Owner); stressTest.Progress -= 1;
stressTest.Progress += frameTime;
if (stressTest.Progress > 1)
{
stressTest.Progress -= 1;
}
var x = MathF.Sin(stressTest.Progress * MathHelper.TwoPi);
var y = MathF.Cos(stressTest.Progress * MathHelper.TwoPi);
transform.WorldPosition = stressTest.Origin + (new Vector2(x, y) * 5);
} }
var x = MathF.Sin(stressTest.Progress * MathHelper.TwoPi);
var y = MathF.Cos(stressTest.Progress * MathHelper.TwoPi);
_transform.SetWorldPosition(transform, stressTest.Origin + new Vector2(x, y) * 5);
} }
} }
} }

View File

@@ -50,12 +50,13 @@ namespace Content.Server.Tabletop
TabletopMap = _mapManager.CreateMap(); TabletopMap = _mapManager.CreateMap();
_tabletops = 0; _tabletops = 0;
var mapUid = _mapManager.GetMapEntityId(TabletopMap);
var mapComp = EntityManager.GetComponent<MapComponent>(_mapManager.GetMapEntityId(TabletopMap)); var mapComp = EntityManager.GetComponent<MapComponent>(mapUid);
// Lighting is always disabled in tabletop world. // Lighting is always disabled in tabletop world.
mapComp.LightingEnabled = false; mapComp.LightingEnabled = false;
mapComp.Dirty(); Dirty(mapUid, mapComp);
} }
/// <summary> /// <summary>