Move ScaleVisuals to Content (and improve it) (#6096)

* împrove ScaleVisuals

* toolshedify

* fix

* rerun tests

* remove redundant code

* move to content
This commit is contained in:
slarticodefast
2025-08-06 14:33:35 +02:00
committed by GitHub
parent 9f0dad80e4
commit 4d4f353680
6 changed files with 41 additions and 157 deletions

View File

@@ -411,9 +411,6 @@ cmd-spawn-help = spawn <prototype> OR spawn <prototype> <relative entity ID> OR
cmd-cspawn-desc = Spawns a client-side entity with specific type at your feet.
cmd-cspawn-help = cspawn <entity type>
cmd-scale-desc = Increases or decreases an entity's size naively.
cmd-scale-help = scale <entityUid> <float>
cmd-dumpentities-desc = Dump entity list.
cmd-dumpentities-help = Dumps entity list of UIDs and prototype.

View File

@@ -1,25 +0,0 @@
using System.Numerics;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
namespace Robust.Client.GameObjects;
public sealed class ScaleVisualsSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ScaleVisualsComponent, AppearanceChangeEvent>(OnChangeData);
}
private void OnChangeData(EntityUid uid, ScaleVisualsComponent component, ref AppearanceChangeEvent ev)
{
if (!ev.AppearanceData.TryGetValue(ScaleVisuals.Scale, out var scale) ||
ev.Sprite == null) return;
var vecScale = (Vector2)scale;
// Set it directly because prediction may call this multiple times.
ev.Sprite.Scale = vecScale;
}
}

View File

@@ -1,112 +0,0 @@
using System;
using System.Numerics;
using Robust.Server.GameObjects;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Systems;
namespace Robust.Server.Console.Commands;
public sealed class ScaleCommand : LocalizedCommands
{
[Dependency] private readonly IEntityManager _entityManager = default!;
public override string Command => "scale";
public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
switch (args.Length)
{
case 1:
return CompletionResult.FromOptions(CompletionHelper.NetEntities(args[0], entManager: _entityManager));
case 2:
return CompletionResult.FromHint(Loc.GetString("cmd-hint-float"));
default:
return CompletionResult.Empty;
}
}
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 2)
{
shell.WriteError($"Insufficient number of args supplied: expected 2 and received {args.Length}");
return;
}
if (!NetEntity.TryParse(args[0], out var netEntity))
{
shell.WriteError($"Unable to find entity {args[0]}");
return;
}
if (!float.TryParse(args[1], out var scale))
{
shell.WriteError($"Invalid scale supplied of {args[0]}");
return;
}
if (scale < 0f)
{
shell.WriteError($"Invalid scale supplied that is negative!");
return;
}
// Event for content to use
// We'll just set engine stuff here
var physics = _entityManager.System<SharedPhysicsSystem>();
var appearance = _entityManager.System<AppearanceSystem>();
var uid = _entityManager.GetEntity(netEntity);
_entityManager.EnsureComponent<ScaleVisualsComponent>(uid);
var @event = new ScaleEntityEvent();
_entityManager.EventBus.RaiseLocalEvent(uid, ref @event);
var appearanceComponent = _entityManager.EnsureComponent<AppearanceComponent>(uid);
if (!appearance.TryGetData<Vector2>(uid, ScaleVisuals.Scale, out var oldScale, appearanceComponent))
oldScale = Vector2.One;
appearance.SetData(uid, ScaleVisuals.Scale, oldScale * scale, appearanceComponent);
if (_entityManager.TryGetComponent(uid, out FixturesComponent? manager))
{
foreach (var (id, fixture) in manager.Fixtures)
{
switch (fixture.Shape)
{
case EdgeShape edge:
physics.SetVertices(uid, id, fixture,
edge,
edge.Vertex0 * scale,
edge.Vertex1 * scale,
edge.Vertex2 * scale,
edge.Vertex3 * scale, manager);
break;
case PhysShapeCircle circle:
physics.SetPositionRadius(uid, id, fixture, circle, circle.Position * scale, circle.Radius * scale, manager);
break;
case PolygonShape poly:
var verts = poly.Vertices;
for (var i = 0; i < poly.VertexCount; i++)
{
verts[i] *= scale;
}
physics.SetVertices(uid, id, fixture, poly, verts, manager);
break;
default:
throw new NotImplementedException();
}
}
}
}
[ByRefEvent]
public readonly record struct ScaleEntityEvent(EntityUid Uid) {}
}

View File

@@ -1,6 +0,0 @@
using Robust.Shared.GameStates;
namespace Robust.Shared.GameObjects;
[RegisterComponent, NetworkedComponent]
public sealed partial class ScaleVisualsComponent : Component {}

View File

@@ -1,11 +0,0 @@
using System;
using Robust.Shared.Serialization;
namespace Robust.Shared.GameObjects;
[Serializable, NetSerializable]
public enum ScaleVisuals : byte
{
// Blep
Scale,
}

View File

@@ -1,5 +1,7 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Utility;
@@ -71,6 +73,45 @@ public abstract partial class SharedPhysicsSystem
_fixtures.FixtureUpdate(uid, manager: manager);
}
/// <summary>
/// Increases or decreases all fixtures of an entity in size by a certain factor.
/// </summary>
public void ScaleFixtures(Entity<FixturesComponent?> ent, float factor)
{
if (!Resolve(ent, ref ent.Comp))
return;
foreach (var (id, fixture) in ent.Comp.Fixtures)
{
switch (fixture.Shape)
{
case EdgeShape edge:
SetVertices(ent, id, fixture,
edge,
edge.Vertex0 * factor,
edge.Vertex1 * factor,
edge.Vertex2 * factor,
edge.Vertex3 * factor, ent.Comp);
break;
case PhysShapeCircle circle:
SetPositionRadius(ent, id, fixture, circle, circle.Position * factor, circle.Radius * factor, ent.Comp);
break;
case PolygonShape poly:
var verts = poly.Vertices;
for (var i = 0; i < poly.VertexCount; i++)
{
verts[i] *= factor;
}
SetVertices(ent, id, fixture, poly, verts, ent.Comp);
break;
default:
throw new NotImplementedException();
}
}
}
#region Collision Masks & Layers
/// <summary>