EyeComponent improvements (#1384)

* EyeComponent improvements

* Use EqualsApprox where appropiate.

* Update Robust.Client/GameObjects/Components/Eye/EyeComponent.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Víctor Aguilera Puerto
2020-11-06 09:29:44 +01:00
committed by GitHub
parent 60400418a5
commit fd93fcb89c
3 changed files with 120 additions and 56 deletions

View File

@@ -19,15 +19,15 @@ namespace Robust.Client.GameObjects
public override string Name => "Eye";
[ViewVariables]
private Eye _eye = default!;
private Eye? _eye = default!;
// Horrible hack to get around ordering issues.
private bool setCurrentOnInitialize;
private bool setDrawFovOnInitialize;
private Vector2 setZoomOnInitialize = Vector2.One/2f;
private Vector2 offset = Vector2.Zero;
private bool _setCurrentOnInitialize;
private bool _setDrawFovOnInitialize;
private Vector2 _setZoomOnInitialize = Vector2.One/2f;
private Vector2 _offset = Vector2.Zero;
public IEye Eye => _eye;
public IEye? Eye => _eye;
[ViewVariables(VVAccess.ReadWrite)]
public bool Current
@@ -37,7 +37,7 @@ namespace Robust.Client.GameObjects
{
if (_eye == null)
{
setCurrentOnInitialize = value;
_setCurrentOnInitialize = value;
return;
}
@@ -55,15 +55,14 @@ namespace Robust.Client.GameObjects
}
}
[ViewVariables(VVAccess.ReadWrite)]
public Vector2 Zoom
public override Vector2 Zoom
{
get => _eye?.Zoom ?? setZoomOnInitialize;
get => _eye?.Zoom ?? _setZoomOnInitialize;
set
{
if (_eye == null)
{
setZoomOnInitialize = value;
_setZoomOnInitialize = value;
}
else
{
@@ -72,10 +71,9 @@ namespace Robust.Client.GameObjects
}
}
[ViewVariables(VVAccess.ReadWrite)]
public Angle Rotation
public override Angle Rotation
{
get => _eye.Rotation;
get => _eye?.Rotation ?? Angle.Zero;
set
{
if (_eye != null)
@@ -83,36 +81,32 @@ namespace Robust.Client.GameObjects
}
}
[ViewVariables(VVAccess.ReadWrite)]
public Vector2 Offset
public override Vector2 Offset
{
get => offset;
get => _offset;
set
{
if(offset == value)
if(_offset.EqualsApprox(value))
return;
offset = value;
_offset = value;
UpdateEyePosition();
}
}
[ViewVariables(VVAccess.ReadWrite)]
public override bool DrawFov
{
get => _eye?.DrawFov ?? setDrawFovOnInitialize;
get => _eye?.DrawFov ?? _setDrawFovOnInitialize;
set
{
if (_eye == null)
{
setDrawFovOnInitialize = value;
_setDrawFovOnInitialize = value;
}
else
{
_eye.DrawFov = value;
}
Dirty();
}
}
@@ -127,13 +121,13 @@ namespace Robust.Client.GameObjects
_eye = new Eye
{
Position = Owner.Transform.MapPosition,
Zoom = setZoomOnInitialize,
DrawFov = setDrawFovOnInitialize
Zoom = _setZoomOnInitialize,
DrawFov = _setDrawFovOnInitialize
};
if ((_eyeManager.CurrentEye == _eye) != setCurrentOnInitialize)
if ((_eyeManager.CurrentEye == _eye) != _setCurrentOnInitialize)
{
if (setCurrentOnInitialize)
if (_setCurrentOnInitialize)
{
_eyeManager.ClearCurrentEye();
}
@@ -144,6 +138,21 @@ namespace Robust.Client.GameObjects
}
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is EyeComponentState state))
{
return;
}
DrawFov = state.DrawFov;
Zoom = state.Zoom;
Offset = state.Offset;
Rotation = state.Rotation;
}
public override void OnRemove()
{
base.OnRemove();
@@ -156,8 +165,8 @@ namespace Robust.Client.GameObjects
{
base.ExposeData(serializer);
serializer.DataFieldCached(ref setZoomOnInitialize, "zoom", Vector2.One/2f);
serializer.DataFieldCached(ref setDrawFovOnInitialize, "drawFov", true);
serializer.DataFieldCached(ref _setZoomOnInitialize, "zoom", Vector2.One/2f);
serializer.DataFieldCached(ref _setDrawFovOnInitialize, "drawFov", true);
}
/// <summary>
@@ -166,8 +175,9 @@ namespace Robust.Client.GameObjects
/// </summary>
public void UpdateEyePosition()
{
if (_eye == null) return;
var mapPos = Owner.Transform.MapPosition;
_eye.Position = new MapCoordinates(mapPos.Position + offset, mapPos.MapId);
_eye.Position = new MapCoordinates(mapPos.Position + _offset, mapPos.MapId);
}
}
}

View File

@@ -1,11 +1,17 @@
using Robust.Shared.GameObjects.Components.Eye;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Eye;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Robust.Server.GameObjects.Components.Eye
{
public class EyeComponent : SharedEyeComponent
{
private bool _drawFov;
private Vector2 _zoom;
private Vector2 _offset;
private Angle _rotation;
public override bool DrawFov
{
@@ -13,20 +19,63 @@ namespace Robust.Server.GameObjects.Components.Eye
set
{
if (_drawFov == value)
{
return;
}
_drawFov = value;
Dirty();
}
}
public override Vector2 Zoom
{
get => _zoom;
set
{
if (_zoom.EqualsApprox(value))
return;
_zoom = value;
Dirty();
}
}
public override Vector2 Offset
{
get => _offset;
set
{
if (_offset.EqualsApprox(value))
return;
_offset = value;
Dirty();
}
}
public override Angle Rotation
{
get => _rotation;
set
{
if(_rotation.EqualsApprox(value))
return;
_rotation = value;
Dirty();
}
}
public override ComponentState GetComponentState()
{
return new EyeComponentState(DrawFov, Zoom, Offset, Rotation);
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _zoom, "zoom", Vector2.One/2f);
serializer.DataFieldCached(ref _drawFov, "drawFov", true);
}
}
}
}

View File

@@ -1,37 +1,42 @@
namespace Robust.Shared.GameObjects.Components.Eye
using System;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.GameObjects.Components.Eye
{
public class SharedEyeComponent : Component
{
public override string Name => "Eye";
public override uint? NetID => NetIDs.EYE;
[ViewVariables(VVAccess.ReadWrite)]
public virtual bool DrawFov { get; set; }
public override ComponentState GetComponentState()
{
return new EyeComponentState(DrawFov);
}
[ViewVariables(VVAccess.ReadWrite)]
public virtual Vector2 Zoom { get; set; }
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
[ViewVariables(VVAccess.ReadWrite)]
public virtual Vector2 Offset { get; set; }
if (!(curState is EyeComponentState state))
{
return;
}
DrawFov = state.DrawFov;
}
[ViewVariables(VVAccess.ReadWrite)]
public virtual Angle Rotation { get; set; }
}
[NetSerializable, Serializable]
public class EyeComponentState : ComponentState
{
public readonly bool DrawFov;
public EyeComponentState(bool drawFov) : base(NetIDs.EYE)
public bool DrawFov { get; }
public Vector2 Zoom { get; }
public Vector2 Offset { get; }
public Angle Rotation { get; }
public EyeComponentState(bool drawFov, Vector2 zoom, Vector2 offset, Angle rotation) : base(NetIDs.EYE)
{
DrawFov = drawFov;
Zoom = zoom;
Offset = offset;
Rotation = rotation;
}
}
}
}