Re-organize all projects (#4166)
This commit is contained in:
140
Content.Server/Gravity/EntitySystems/GravitySystem.cs
Normal file
140
Content.Server/Gravity/EntitySystems/GravitySystem.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.Camera;
|
||||
using Content.Shared.Gravity;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Gravity.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class GravitySystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
private const float GravityKick = 100.0f;
|
||||
|
||||
private const uint ShakeTimes = 10;
|
||||
|
||||
private Dictionary<GridId, uint> _gridsToShake = new();
|
||||
|
||||
private float _internalTimer = 0.0f;
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
_internalTimer += frameTime;
|
||||
var gridsWithGravity = new List<GridId>();
|
||||
foreach (var generator in ComponentManager.EntityQuery<GravityGeneratorComponent>(true))
|
||||
{
|
||||
if (generator.NeedsUpdate)
|
||||
{
|
||||
generator.UpdateState();
|
||||
}
|
||||
|
||||
if (generator.Status == GravityGeneratorStatus.On)
|
||||
{
|
||||
gridsWithGravity.Add(generator.Owner.Transform.GridID);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var grid in _mapManager.GetAllGrids())
|
||||
{
|
||||
if (grid.HasGravity && !gridsWithGravity.Contains(grid.Index))
|
||||
{
|
||||
DisableGravity(grid);
|
||||
}
|
||||
else if (!grid.HasGravity && gridsWithGravity.Contains(grid.Index))
|
||||
{
|
||||
EnableGravity(grid);
|
||||
}
|
||||
}
|
||||
|
||||
if (_internalTimer > 0.2f)
|
||||
{
|
||||
ShakeGrids();
|
||||
_internalTimer = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
private void EnableGravity(IMapGrid grid)
|
||||
{
|
||||
grid.HasGravity = true;
|
||||
ScheduleGridToShake(grid.Index, ShakeTimes);
|
||||
|
||||
var message = new GravityChangedMessage(grid);
|
||||
|
||||
RaiseLocalEvent(message);
|
||||
}
|
||||
|
||||
private void DisableGravity(IMapGrid grid)
|
||||
{
|
||||
grid.HasGravity = false;
|
||||
ScheduleGridToShake(grid.Index, ShakeTimes);
|
||||
|
||||
var message = new GravityChangedMessage(grid);
|
||||
|
||||
RaiseLocalEvent(message);
|
||||
}
|
||||
|
||||
private void ScheduleGridToShake(GridId gridId, uint shakeTimes)
|
||||
{
|
||||
if (!_gridsToShake.Keys.Contains(gridId))
|
||||
{
|
||||
_gridsToShake.Add(gridId, shakeTimes);
|
||||
}
|
||||
else
|
||||
{
|
||||
_gridsToShake[gridId] = shakeTimes;
|
||||
}
|
||||
// Play the gravity sound
|
||||
foreach (var player in _playerManager.GetAllPlayers())
|
||||
{
|
||||
if (player.AttachedEntity == null
|
||||
|| player.AttachedEntity.Transform.GridID != gridId) continue;
|
||||
SoundSystem.Play(Filter.Pvs(player.AttachedEntity), "/Audio/Effects/alert.ogg", player.AttachedEntity);
|
||||
}
|
||||
}
|
||||
|
||||
private void ShakeGrids()
|
||||
{
|
||||
// I have to copy this because C# doesn't allow changing collections while they're
|
||||
// getting enumerated.
|
||||
var gridsToShake = new Dictionary<GridId, uint>(_gridsToShake);
|
||||
foreach (var gridId in _gridsToShake.Keys)
|
||||
{
|
||||
if (_gridsToShake[gridId] == 0)
|
||||
{
|
||||
gridsToShake.Remove(gridId);
|
||||
continue;
|
||||
}
|
||||
ShakeGrid(gridId);
|
||||
gridsToShake[gridId] -= 1;
|
||||
}
|
||||
_gridsToShake = gridsToShake;
|
||||
}
|
||||
|
||||
private void ShakeGrid(GridId gridId)
|
||||
{
|
||||
foreach (var player in _playerManager.GetAllPlayers())
|
||||
{
|
||||
if (player.AttachedEntity == null
|
||||
|| player.AttachedEntity.Transform.GridID != gridId
|
||||
|| !player.AttachedEntity.TryGetComponent(out CameraRecoilComponent? recoil))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
recoil.Kick(new Vector2(_random.NextFloat(), _random.NextFloat()) * GravityKick);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
130
Content.Server/Gravity/EntitySystems/WeightlessSystem.cs
Normal file
130
Content.Server/Gravity/EntitySystems/WeightlessSystem.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Alert;
|
||||
using Content.Shared.Alert;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.Gravity;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Gravity.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class WeightlessSystem : EntitySystem, IResettingEntitySystem
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
|
||||
private readonly Dictionary<GridId, List<ServerAlertsComponent>> _alerts = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<GravityChangedMessage>(GravityChanged);
|
||||
SubscribeLocalEvent<EntParentChangedMessage>(EntParentChanged);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
|
||||
UnsubscribeLocalEvent<GravityChangedMessage>();
|
||||
UnsubscribeLocalEvent<EntParentChangedMessage>();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_alerts.Clear();
|
||||
}
|
||||
|
||||
public void AddAlert(ServerAlertsComponent status)
|
||||
{
|
||||
var gridId = status.Owner.Transform.GridID;
|
||||
var alerts = _alerts.GetOrNew(gridId);
|
||||
|
||||
alerts.Add(status);
|
||||
|
||||
if (_mapManager.TryGetGrid(status.Owner.Transform.GridID, out var grid))
|
||||
{
|
||||
if (grid.HasGravity)
|
||||
{
|
||||
RemoveWeightless(status);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddWeightless(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveAlert(ServerAlertsComponent status)
|
||||
{
|
||||
var grid = status.Owner.Transform.GridID;
|
||||
if (!_alerts.TryGetValue(grid, out var statuses))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
statuses.Remove(status);
|
||||
}
|
||||
|
||||
private void GravityChanged(GravityChangedMessage ev)
|
||||
{
|
||||
if (!_alerts.TryGetValue(ev.Grid.Index, out var statuses))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (ev.HasGravity)
|
||||
{
|
||||
foreach (var status in statuses)
|
||||
{
|
||||
RemoveWeightless(status);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var status in statuses)
|
||||
{
|
||||
AddWeightless(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddWeightless(ServerAlertsComponent status)
|
||||
{
|
||||
status.ShowAlert(AlertType.Weightless);
|
||||
}
|
||||
|
||||
private void RemoveWeightless(ServerAlertsComponent status)
|
||||
{
|
||||
status.ClearAlert(AlertType.Weightless);
|
||||
}
|
||||
|
||||
private void EntParentChanged(EntParentChangedMessage ev)
|
||||
{
|
||||
if (!ev.Entity.TryGetComponent(out ServerAlertsComponent? status))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (ev.OldParent != null &&
|
||||
ev.OldParent.TryGetComponent(out IMapGridComponent? mapGrid))
|
||||
{
|
||||
var oldGrid = mapGrid.GridIndex;
|
||||
|
||||
if (_alerts.TryGetValue(oldGrid, out var oldStatuses))
|
||||
{
|
||||
oldStatuses.Remove(status);
|
||||
}
|
||||
}
|
||||
|
||||
var newGrid = ev.Entity.Transform.GridID;
|
||||
var newStatuses = _alerts.GetOrNew(newGrid);
|
||||
|
||||
newStatuses.Add(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user