Beam Component and Lightning Component (#10196)
This commit is contained in:
172
Content.Server/Beam/BeamSystem.cs
Normal file
172
Content.Server/Beam/BeamSystem.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using Content.Server.Beam.Components;
|
||||
using Content.Server.Lightning;
|
||||
using Content.Shared.Beam;
|
||||
using Content.Shared.Beam.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Lightning;
|
||||
using Content.Shared.Physics;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Collision.Shapes;
|
||||
using Robust.Shared.Physics.Components;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
using Robust.Shared.Physics.Systems;
|
||||
|
||||
namespace Content.Server.Beam;
|
||||
|
||||
public sealed class BeamSystem : SharedBeamSystem
|
||||
{
|
||||
[Dependency] private readonly FixtureSystem _fixture = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<BeamComponent, CreateBeamSuccessEvent>(OnBeamCreationSuccess);
|
||||
SubscribeLocalEvent<BeamComponent, BeamControllerCreatedEvent>(OnControllerCreated);
|
||||
SubscribeLocalEvent<BeamComponent, BeamFiredEvent>(OnBeamFired);
|
||||
SubscribeLocalEvent<BeamComponent, ComponentRemove>(OnRemove);
|
||||
}
|
||||
|
||||
private void OnBeamCreationSuccess(EntityUid uid, BeamComponent component, CreateBeamSuccessEvent args)
|
||||
{
|
||||
component.BeamShooter = args.User;
|
||||
}
|
||||
|
||||
private void OnControllerCreated(EntityUid uid, BeamComponent component, BeamControllerCreatedEvent args)
|
||||
{
|
||||
component.OriginBeam = args.OriginBeam;
|
||||
}
|
||||
|
||||
private void OnBeamFired(EntityUid uid, BeamComponent component, BeamFiredEvent args)
|
||||
{
|
||||
component.CreatedBeams.Add(args.CreatedBeam);
|
||||
}
|
||||
|
||||
private void OnRemove(EntityUid uid, BeamComponent component, ComponentRemove args)
|
||||
{
|
||||
if (component.VirtualBeamController == null)
|
||||
return;
|
||||
|
||||
if (component.CreatedBeams.Count == 0 && component.VirtualBeamController.Value.Valid)
|
||||
QueueDel(component.VirtualBeamController.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If <see cref="TryCreateBeam"/> is successful, it spawns a beam from the user to the target.
|
||||
/// </summary>
|
||||
/// <param name="prototype">The prototype used to make the beam</param>
|
||||
/// <param name="userAngle">Angle of the user firing the beam</param>
|
||||
/// <param name="calculatedDistance">The calculated distance from the user to the target.</param>
|
||||
/// <param name="beamStartPos">Where the beam will spawn in</param>
|
||||
/// <param name="distanceCorrection">Calculated correction so the <see cref="EdgeShape"/> can be properly dynamically created</param>
|
||||
/// <param name="controller"> The virtual beam controller that this beam will use. If one doesn't exist it will be created here.</param>
|
||||
/// <param name="bodyState">Optional sprite state for the <see cref="prototype"/> if it needs a dynamic one</param>
|
||||
/// <param name="shader">Optional shader for the <see cref="prototype"/> and <see cref="bodyState"/> if it needs something other than default</param>
|
||||
private void CreateBeam(string prototype,
|
||||
Angle userAngle,
|
||||
Vector2 calculatedDistance,
|
||||
MapCoordinates beamStartPos,
|
||||
Vector2 distanceCorrection,
|
||||
EntityUid? controller,
|
||||
string? bodyState = null,
|
||||
string shader = "unshaded")
|
||||
{
|
||||
var beamSpawnPos = beamStartPos;
|
||||
var ent = Spawn(prototype, beamSpawnPos);
|
||||
var shape = new EdgeShape(distanceCorrection, new Vector2(0,0));
|
||||
var distanceLength = distanceCorrection.Length;
|
||||
|
||||
if (TryComp<PhysicsComponent>(ent, out var physics) && TryComp<BeamComponent>(ent, out var beam))
|
||||
{
|
||||
var fixture = new Fixture(physics, shape)
|
||||
{
|
||||
ID = "BeamBody",
|
||||
Hard = false,
|
||||
CollisionMask = (int)CollisionGroup.ItemMask, //Change to MobMask
|
||||
CollisionLayer = (int)CollisionGroup.MobLayer //Change to WallLayer
|
||||
};
|
||||
|
||||
_fixture.TryCreateFixture(physics, fixture);
|
||||
physics.BodyType = BodyType.Dynamic;
|
||||
|
||||
var beamVisualizerEvent = new BeamVisualizerEvent(ent, distanceLength, userAngle, bodyState, shader);
|
||||
RaiseNetworkEvent(beamVisualizerEvent);
|
||||
|
||||
if (controller != null)
|
||||
beam.VirtualBeamController = controller;
|
||||
|
||||
else
|
||||
{
|
||||
var controllerEnt = Spawn("VirtualBeamEntityController", beamSpawnPos);
|
||||
beam.VirtualBeamController = controllerEnt;
|
||||
|
||||
_audio.PlayPvs(beam.Sound, beam.Owner);
|
||||
|
||||
var beamControllerCreatedEvent = new BeamControllerCreatedEvent(ent, controllerEnt);
|
||||
RaiseLocalEvent(controllerEnt, beamControllerCreatedEvent);
|
||||
}
|
||||
|
||||
//Create the rest of the beam, sprites handled through the BeamVisualizerEvent
|
||||
for (int i = 0; i < distanceLength-1; i++)
|
||||
{
|
||||
beamSpawnPos = beamSpawnPos.Offset(calculatedDistance.Normalized);
|
||||
var newEnt = Spawn(prototype, beamSpawnPos);
|
||||
|
||||
var ev = new BeamVisualizerEvent(newEnt, distanceLength, userAngle, bodyState, shader);
|
||||
RaiseNetworkEvent(ev);
|
||||
}
|
||||
|
||||
var beamFiredEvent = new BeamFiredEvent(ent);
|
||||
RaiseLocalEvent(beam.VirtualBeamController.Value, beamFiredEvent);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called where you want an entity to create a beam from one target to another.
|
||||
/// Tries to create the beam and does calculations like the distance, angle, and offset.
|
||||
/// </summary>
|
||||
/// <param name="user">The entity that's firing off the beam</param>
|
||||
/// <param name="target">The entity that's being targeted by the user</param>
|
||||
/// <param name="bodyPrototype">The prototype spawned when this beam is created</param>
|
||||
/// <param name="bodyState">Optional sprite state for the <see cref="bodyPrototype"/> if a default one is not given</param>
|
||||
/// <param name="shader">Optional shader for the <see cref="bodyPrototype"/> if a default one is not given</param>
|
||||
/// <param name="controller"></param>
|
||||
public void TryCreateBeam(EntityUid user, EntityUid target, string bodyPrototype, string? bodyState = null, string shader = "unshaded", EntityUid? controller = null)
|
||||
{
|
||||
if (!user.IsValid() || !target.IsValid())
|
||||
return;
|
||||
|
||||
var userMapPos = Transform(user).MapPosition;
|
||||
var targetMapPos = Transform(target).MapPosition;
|
||||
|
||||
//The distance between the target and the user.
|
||||
var calculatedDistance = targetMapPos.Position - userMapPos.Position;
|
||||
var userAngle = calculatedDistance.ToWorldAngle();
|
||||
|
||||
if (userMapPos.MapId != targetMapPos.MapId)
|
||||
return;
|
||||
|
||||
//Where the start of the beam will spawn
|
||||
var beamStartPos = userMapPos.Offset(calculatedDistance.Normalized);
|
||||
|
||||
//Don't divide by zero
|
||||
if (calculatedDistance.Length == 0)
|
||||
return;
|
||||
|
||||
if (controller != null && TryComp<BeamComponent>(controller, out var controllerBeamComp))
|
||||
{
|
||||
controllerBeamComp.HitTargets.Add(user);
|
||||
controllerBeamComp.HitTargets.Add(target);
|
||||
}
|
||||
|
||||
var distanceCorrection = calculatedDistance - calculatedDistance.Normalized;
|
||||
|
||||
CreateBeam(bodyPrototype, userAngle, calculatedDistance, beamStartPos, distanceCorrection, controller, bodyState, shader);
|
||||
|
||||
var ev = new CreateBeamSuccessEvent(user, target);
|
||||
RaiseLocalEvent(user, ev);
|
||||
}
|
||||
}
|
||||
8
Content.Server/Beam/Components/BeamComponent.cs
Normal file
8
Content.Server/Beam/Components/BeamComponent.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Content.Shared.Beam.Components;
|
||||
|
||||
namespace Content.Server.Beam.Components;
|
||||
[RegisterComponent]
|
||||
public sealed class BeamComponent : SharedBeamComponent
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Content.Shared.Lightning.Components;
|
||||
|
||||
namespace Content.Server.Lightning.Components;
|
||||
[RegisterComponent]
|
||||
public sealed class LightningComponent : SharedLightningComponent
|
||||
{
|
||||
|
||||
}
|
||||
138
Content.Server/Lightning/LightningSystem.cs
Normal file
138
Content.Server/Lightning/LightningSystem.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Beam;
|
||||
using Content.Server.Beam.Components;
|
||||
using Content.Server.Lightning.Components;
|
||||
using Content.Shared.Lightning;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
using Robust.Shared.Physics.Events;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Lightning;
|
||||
|
||||
public sealed class LightningSystem : SharedLightningSystem
|
||||
{
|
||||
[Dependency] private readonly PhysicsSystem _physics = default!;
|
||||
[Dependency] private readonly BeamSystem _beam = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<LightningComponent, StartCollideEvent>(OnCollide);
|
||||
SubscribeLocalEvent<LightningComponent, ComponentRemove>(OnRemove);
|
||||
}
|
||||
|
||||
private void OnRemove(EntityUid uid, LightningComponent component, ComponentRemove args)
|
||||
{
|
||||
if (!TryComp<BeamComponent>(uid, out var lightningBeam) || !TryComp<BeamComponent>(lightningBeam.VirtualBeamController, out var beamController))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
beamController.CreatedBeams.Remove(uid);
|
||||
}
|
||||
|
||||
private void OnCollide(EntityUid uid, LightningComponent component, ref StartCollideEvent args)
|
||||
{
|
||||
if (!TryComp<BeamComponent>(uid, out var lightningBeam) || !TryComp<BeamComponent>(lightningBeam.VirtualBeamController, out var beamController))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (component.CanArc)
|
||||
{
|
||||
while (beamController.CreatedBeams.Count < component.MaxTotalArcs)
|
||||
{
|
||||
Arc(component, args.OtherFixture.Body.Owner, lightningBeam.VirtualBeamController.Value);
|
||||
|
||||
var spriteState = LightningRandomizer();
|
||||
|
||||
component.ArcTargets.Add(args.OtherFixture.Body.Owner);
|
||||
component.ArcTargets.Add(component.ArcTarget);
|
||||
|
||||
_beam.TryCreateBeam(args.OtherFixture.Body.Owner, component.ArcTarget, component.LightningPrototype, spriteState, controller: lightningBeam.VirtualBeamController.Value);
|
||||
|
||||
//Break from this loop so other created bolts can collide and arc
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires lightning from user to target
|
||||
/// </summary>
|
||||
/// <param name="user">Where the lightning fires from</param>
|
||||
/// <param name="target">Where the lightning fires to</param>
|
||||
/// <param name="lightningPrototype">The prototype for the lightning to be created</param>
|
||||
public void ShootLightning(EntityUid user, EntityUid target, string lightningPrototype = "Lightning")
|
||||
{
|
||||
var spriteState = LightningRandomizer();
|
||||
_beam.TryCreateBeam(user, target, lightningPrototype, spriteState);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks for a target to arc to in all 8 directions, adds the closest to a local dictionary and picks at random
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="controllerEntity"></param>
|
||||
private void Arc(LightningComponent component, EntityUid target, EntityUid controllerEntity)
|
||||
{
|
||||
if (!TryComp<BeamComponent>(controllerEntity, out var controller))
|
||||
return;
|
||||
|
||||
var targetXForm = Transform(target);
|
||||
var directions = Enum.GetValues<Direction>().Length;
|
||||
|
||||
var lightningQuery = GetEntityQuery<LightningComponent>();
|
||||
var beamQuery = GetEntityQuery<BeamComponent>();
|
||||
var xformQuery = GetEntityQuery<TransformComponent>();
|
||||
|
||||
Dictionary<Direction, EntityUid> arcDirections = new();
|
||||
|
||||
//TODO: Add scoring system for the Tesla PR which will have grounding rods
|
||||
for (int i = 0; i < directions; i++)
|
||||
{
|
||||
var direction = (Direction) i;
|
||||
var (targetPos, targetRot) = targetXForm.GetWorldPositionRotation(xformQuery);
|
||||
var dirRad = direction.ToAngle() + targetRot;
|
||||
var ray = new CollisionRay(targetPos, dirRad.ToVec(), component.CollisionMask);
|
||||
var rayCastResults = _physics.IntersectRay(targetXForm.MapID, ray, component.MaxLength, target, false).ToList();
|
||||
|
||||
RayCastResults? closestResult = null;
|
||||
|
||||
foreach (var result in rayCastResults)
|
||||
{
|
||||
if (lightningQuery.HasComponent(result.HitEntity)
|
||||
|| beamQuery.HasComponent(result.HitEntity)
|
||||
|| component.ArcTargets.Contains(result.HitEntity)
|
||||
|| controller.HitTargets.Contains(result.HitEntity)
|
||||
|| controller.BeamShooter == result.HitEntity)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
closestResult = result;
|
||||
break;
|
||||
}
|
||||
|
||||
if (closestResult == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
arcDirections.Add(direction, closestResult.Value.HitEntity);
|
||||
}
|
||||
|
||||
var randomDirection = (Direction) _random.Next(0, 7);
|
||||
|
||||
if (arcDirections.ContainsKey(randomDirection))
|
||||
{
|
||||
component.ArcTarget = arcDirections.GetValueOrDefault(randomDirection);
|
||||
arcDirections.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user