* Draft PR, do not merge * Lathe now uses animations. Timing is still wrong, code is very wet, repetitions and unused layers are there. * Removed (unneeded?) PowerDeviceVisualizer from the lathe Refactored & Renamed code and variables Renamed animation names * WIP protolathe animation * Working protolathe animation. I still don't like the solution though.
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using Content.Shared.GameObjects.Components.Power;
|
||||
using Robust.Client.Animations;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.GameObjects.Components.Animations;
|
||||
using Robust.Client.Interfaces.GameObjects.Components;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using YamlDotNet.RepresentationModel;
|
||||
|
||||
namespace Content.Client.GameObjects.Components.Power
|
||||
{
|
||||
public class AutolatheVisualizer2D : AppearanceVisualizer
|
||||
{
|
||||
private const string AnimationKey = "autolathe_animation";
|
||||
|
||||
private Animation _buildingAnimation;
|
||||
private Animation _insertingMetalAnimation;
|
||||
private Animation _insertingGlassAnimation;
|
||||
|
||||
public override void LoadData(YamlMappingNode node)
|
||||
{
|
||||
base.LoadData(node);
|
||||
|
||||
_buildingAnimation = PopulateAnimation("autolathe_building", "autolathe_building_unlit", 0.5f);
|
||||
_insertingMetalAnimation = PopulateAnimation("autolathe_inserting_metal_plate", "autolathe_inserting_unlit", 0.9f);
|
||||
_insertingGlassAnimation = PopulateAnimation("autolathe_inserting_glass_plate", "autolathe_inserting_unlit", 0.9f);
|
||||
}
|
||||
|
||||
private Animation PopulateAnimation(string sprite, string spriteUnlit, float length)
|
||||
{
|
||||
var animation = new Animation {Length = TimeSpan.FromSeconds(length)};
|
||||
|
||||
var flick = new AnimationTrackSpriteFlick();
|
||||
animation.AnimationTracks.Add(flick);
|
||||
flick.LayerKey = AutolatheVisualLayers.Base;
|
||||
flick.KeyFrames.Add(new AnimationTrackSpriteFlick.KeyFrame(sprite, 0f));
|
||||
|
||||
var flickUnlit = new AnimationTrackSpriteFlick();
|
||||
animation.AnimationTracks.Add(flickUnlit);
|
||||
flickUnlit.LayerKey = AutolatheVisualLayers.BaseUnlit;
|
||||
flickUnlit.KeyFrames.Add(new AnimationTrackSpriteFlick.KeyFrame(spriteUnlit, 0f));
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
public override void InitializeEntity(IEntity entity)
|
||||
{
|
||||
if (!entity.HasComponent<AnimationPlayerComponent>())
|
||||
{
|
||||
entity.AddComponent<AnimationPlayerComponent>();
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnChangeData(AppearanceComponent component)
|
||||
{
|
||||
base.OnChangeData(component);
|
||||
|
||||
var sprite = component.Owner.GetComponent<ISpriteComponent>();
|
||||
var animPlayer = component.Owner.GetComponent<AnimationPlayerComponent>();
|
||||
if (!component.TryGetData(PowerDeviceVisuals.VisualState, out LatheVisualState state))
|
||||
{
|
||||
state = LatheVisualState.Idle;
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case LatheVisualState.Idle:
|
||||
if (animPlayer.HasRunningAnimation(AnimationKey))
|
||||
{
|
||||
animPlayer.Stop(AnimationKey);
|
||||
}
|
||||
|
||||
sprite.LayerSetState(AutolatheVisualLayers.Base, "autolathe");
|
||||
sprite.LayerSetState(AutolatheVisualLayers.BaseUnlit, "autolathe_unlit");
|
||||
break;
|
||||
case LatheVisualState.Producing:
|
||||
if (!animPlayer.HasRunningAnimation(AnimationKey))
|
||||
{
|
||||
animPlayer.Play(_buildingAnimation, AnimationKey);
|
||||
}
|
||||
break;
|
||||
case LatheVisualState.InsertingMetal:
|
||||
if (!animPlayer.HasRunningAnimation(AnimationKey))
|
||||
{
|
||||
animPlayer.Play(_insertingMetalAnimation, AnimationKey);
|
||||
}
|
||||
break;
|
||||
case LatheVisualState.InsertingGlass:
|
||||
if (!animPlayer.HasRunningAnimation(AnimationKey))
|
||||
{
|
||||
animPlayer.Play(_insertingGlassAnimation, AnimationKey);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
var glowingPartsVisible = !(component.TryGetData(PowerDeviceVisuals.Powered, out bool powered) && !powered);
|
||||
sprite.LayerSetVisible(AutolatheVisualLayers.BaseUnlit, glowingPartsVisible);
|
||||
}
|
||||
public enum AutolatheVisualLayers
|
||||
{
|
||||
Base,
|
||||
BaseUnlit
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user