Gravity (#841)
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
using Content.Server.GameObjects.Components.Damage;
|
||||
using Content.Server.GameObjects.Components.Interactable.Tools;
|
||||
using Content.Server.GameObjects.Components.Power;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Server.Interfaces;
|
||||
using Content.Shared.GameObjects.Components.Gravity;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.Components.UserInterface;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
using Robust.Server.Interfaces.GameObjects;
|
||||
using Robust.Server.Interfaces.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Gravity
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class GravityGeneratorComponent: SharedGravityGeneratorComponent, IAttackBy, IBreakAct, IAttackHand
|
||||
{
|
||||
private BoundUserInterface _userInterface;
|
||||
|
||||
private PowerDeviceComponent _powerDevice;
|
||||
|
||||
private SpriteComponent _sprite;
|
||||
|
||||
private bool _switchedOn;
|
||||
|
||||
private bool _intact;
|
||||
|
||||
private GravityGeneratorStatus _status;
|
||||
|
||||
public bool Powered => _powerDevice.Powered;
|
||||
|
||||
public bool SwitchedOn => _switchedOn;
|
||||
|
||||
public bool Intact => _intact;
|
||||
|
||||
public GravityGeneratorStatus Status => _status;
|
||||
|
||||
public bool NeedsUpdate
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (_status)
|
||||
{
|
||||
case GravityGeneratorStatus.On:
|
||||
return !(Powered && SwitchedOn && Intact);
|
||||
case GravityGeneratorStatus.Off:
|
||||
return SwitchedOn || !(Powered && Intact);
|
||||
case GravityGeneratorStatus.Unpowered:
|
||||
return SwitchedOn || Powered || !Intact;
|
||||
case GravityGeneratorStatus.Broken:
|
||||
return SwitchedOn || Powered || Intact;
|
||||
default:
|
||||
return true; // This _should_ be unreachable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string Name => "GravityGenerator";
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>()
|
||||
.GetBoundUserInterface(GravityGeneratorUiKey.Key);
|
||||
_userInterface.OnReceiveMessage += HandleUIMessage;
|
||||
_powerDevice = Owner.GetComponent<PowerDeviceComponent>();
|
||||
_sprite = Owner.GetComponent<SpriteComponent>();
|
||||
_switchedOn = true;
|
||||
_intact = true;
|
||||
_status = GravityGeneratorStatus.On;
|
||||
UpdateState();
|
||||
}
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
|
||||
serializer.DataField(ref _switchedOn, "switched_on", true);
|
||||
serializer.DataField(ref _intact, "intact", true);
|
||||
}
|
||||
|
||||
bool IAttackHand.AttackHand(AttackHandEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent<IActorComponent>(out var actor))
|
||||
return false;
|
||||
if (Status != GravityGeneratorStatus.Off && Status != GravityGeneratorStatus.On)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
OpenUserInterface(actor.playerSession);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool AttackBy(AttackByEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.AttackWith.TryGetComponent<WelderComponent>(out var welder)) return false;
|
||||
if (welder.TryUse(5.0f))
|
||||
{
|
||||
// Repair generator
|
||||
var damagable = Owner.GetComponent<DamageableComponent>();
|
||||
var breakable = Owner.GetComponent<BreakableComponent>();
|
||||
damagable.HealAllDamage();
|
||||
breakable.broken = false;
|
||||
_intact = true;
|
||||
|
||||
var entitySystemManager = IoCManager.Resolve<IEntitySystemManager>();
|
||||
var notifyManager = IoCManager.Resolve<IServerNotifyManager>();
|
||||
|
||||
entitySystemManager.GetEntitySystem<AudioSystem>().Play("/Audio/items/welder2.ogg", Owner);
|
||||
notifyManager.PopupMessage(Owner, eventArgs.User, Loc.GetString("You repair the gravity generator with the welder"));
|
||||
|
||||
return true;
|
||||
} else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnBreak(BreakageEventArgs eventArgs)
|
||||
{
|
||||
_intact = false;
|
||||
_switchedOn = false;
|
||||
}
|
||||
|
||||
public void UpdateState()
|
||||
{
|
||||
if (!Intact)
|
||||
{
|
||||
MakeBroken();
|
||||
} else if (!Powered)
|
||||
{
|
||||
MakeUnpowered();
|
||||
} else if (!SwitchedOn)
|
||||
{
|
||||
MakeOff();
|
||||
} else
|
||||
{
|
||||
MakeOn();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleUIMessage(ServerBoundUserInterfaceMessage message)
|
||||
{
|
||||
switch (message.Message)
|
||||
{
|
||||
case GeneratorStatusRequestMessage _:
|
||||
_userInterface.SetState(new GeneratorState(Status == GravityGeneratorStatus.On));
|
||||
break;
|
||||
case SwitchGeneratorMessage msg:
|
||||
_switchedOn = msg.On;
|
||||
UpdateState();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OpenUserInterface(IPlayerSession playerSession)
|
||||
{
|
||||
_userInterface.Open(playerSession);
|
||||
}
|
||||
|
||||
private void MakeBroken()
|
||||
{
|
||||
_status = GravityGeneratorStatus.Broken;
|
||||
_sprite.LayerSetState(0, "broken");
|
||||
_sprite.LayerSetVisible(1, false);
|
||||
}
|
||||
|
||||
private void MakeUnpowered()
|
||||
{
|
||||
_status = GravityGeneratorStatus.Unpowered;
|
||||
_sprite.LayerSetState(0, "off");
|
||||
_sprite.LayerSetVisible(1, false);
|
||||
}
|
||||
|
||||
private void MakeOff()
|
||||
{
|
||||
_status = GravityGeneratorStatus.Off;
|
||||
_sprite.LayerSetState(0, "off");
|
||||
_sprite.LayerSetVisible(1, false);
|
||||
}
|
||||
|
||||
private void MakeOn()
|
||||
{
|
||||
_status = GravityGeneratorStatus.On;
|
||||
_sprite.LayerSetState(0, "on");
|
||||
_sprite.LayerSetVisible(1, true);
|
||||
}
|
||||
}
|
||||
|
||||
public enum GravityGeneratorStatus
|
||||
{
|
||||
Broken,
|
||||
Unpowered,
|
||||
Off,
|
||||
On
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,15 @@ namespace Content.Server.GameObjects.Components.Movement
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[ViewVariables]
|
||||
public float CurrentPushSpeed => 5.0f;
|
||||
|
||||
/// <inheritdoc />
|
||||
[ViewVariables]
|
||||
public float GrabRange => 0.2f;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Is the entity Sprinting (running)?
|
||||
/// </summary>
|
||||
|
||||
@@ -67,6 +67,14 @@ namespace Content.Server.GameObjects.Components.Movement
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[ViewVariables]
|
||||
public float CurrentPushSpeed => 5.0f;
|
||||
|
||||
/// <inheritdoc />
|
||||
[ViewVariables]
|
||||
public float GrabRange => 0.2f;
|
||||
|
||||
/// <summary>
|
||||
/// Is the entity Sprinting (running)?
|
||||
/// </summary>
|
||||
|
||||
@@ -34,6 +34,15 @@ namespace Content.Server.GameObjects.Components.Movement
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float CurrentWalkSpeed { get; set; } = 8;
|
||||
public float CurrentSprintSpeed { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
[ViewVariables]
|
||||
public float CurrentPushSpeed => 0.0f;
|
||||
|
||||
/// <inheritdoc />
|
||||
[ViewVariables]
|
||||
public float GrabRange => 0.0f;
|
||||
|
||||
public bool Sprinting { get; set; }
|
||||
public Vector2 VelocityDir { get; } = Vector2.Zero;
|
||||
public GridCoordinates LastPosition { get; set; }
|
||||
|
||||
134
Content.Server/GameObjects/EntitySystems/GravitySystem.cs
Normal file
134
Content.Server/GameObjects/EntitySystems/GravitySystem.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.GameObjects.Components.Gravity;
|
||||
using Content.Server.GameObjects.Components.Mobs;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
using Robust.Server.Interfaces.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Map;
|
||||
using Robust.Shared.Interfaces.Random;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class GravitySystem: EntitySystem
|
||||
{
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IMapManager _mapManager;
|
||||
[Dependency] private readonly IPlayerManager _playerManager;
|
||||
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
|
||||
[Dependency] private readonly IRobustRandom _random;
|
||||
#pragma warning restore 649
|
||||
|
||||
private const float GravityKick = 100.0f;
|
||||
|
||||
private const uint ShakeTimes = 10;
|
||||
|
||||
private Dictionary<GridId, uint> _gridsToShake;
|
||||
|
||||
private float internalTimer = 0.0f;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
EntityQuery = new TypeEntityQuery<GravityGeneratorComponent>();
|
||||
_gridsToShake = new Dictionary<GridId, uint>();
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
internalTimer += frameTime;
|
||||
var gridsWithGravity = new List<GridId>();
|
||||
foreach (var entity in RelevantEntities)
|
||||
{
|
||||
var generator = entity.GetComponent<GravityGeneratorComponent>();
|
||||
if (generator.NeedsUpdate)
|
||||
{
|
||||
generator.UpdateState();
|
||||
}
|
||||
if (generator.Status == GravityGeneratorStatus.On)
|
||||
{
|
||||
gridsWithGravity.Add(entity.Transform.GridID);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var grid in _mapManager.GetAllGrids())
|
||||
{
|
||||
if (grid.HasGravity && !gridsWithGravity.Contains(grid.Index))
|
||||
{
|
||||
grid.HasGravity = false;
|
||||
ScheduleGridToShake(grid.Index, ShakeTimes);
|
||||
} else if (!grid.HasGravity && gridsWithGravity.Contains(grid.Index))
|
||||
{
|
||||
grid.HasGravity = true;
|
||||
ScheduleGridToShake(grid.Index, ShakeTimes);
|
||||
}
|
||||
}
|
||||
|
||||
if (internalTimer > 0.2f)
|
||||
{
|
||||
ShakeGrids();
|
||||
internalTimer = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
_entitySystemManager.GetEntitySystem<AudioSystem>().Play("/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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using Content.Server.GameObjects.Components;
|
||||
using System;
|
||||
using System.Net;
|
||||
using Content.Server.GameObjects.Components;
|
||||
using Content.Server.GameObjects.Components.Mobs;
|
||||
using Content.Server.GameObjects.Components.Movement;
|
||||
using Content.Server.GameObjects.Components.Sound;
|
||||
@@ -15,6 +17,7 @@ using Robust.Server.Interfaces.Player;
|
||||
using Robust.Server.Interfaces.Timing;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects.Components.Transform;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.Input;
|
||||
@@ -28,6 +31,7 @@ using Robust.Shared.Log;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
@@ -44,6 +48,7 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
[Dependency] private readonly IMapManager _mapManager;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom;
|
||||
[Dependency] private readonly IConfigurationManager _configurationManager;
|
||||
[Dependency] private readonly IEntityManager _entityManager;
|
||||
#pragma warning restore 649
|
||||
|
||||
private AudioSystem _audioSystem;
|
||||
@@ -130,13 +135,43 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
}
|
||||
var mover = entity.GetComponent<IMoverComponent>();
|
||||
var physics = entity.GetComponent<PhysicsComponent>();
|
||||
|
||||
UpdateKinematics(entity.Transform, mover, physics);
|
||||
if (entity.TryGetComponent<CollidableComponent>(out var collider))
|
||||
{
|
||||
UpdateKinematics(entity.Transform, mover, physics, collider);
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateKinematics(entity.Transform, mover, physics);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateKinematics(ITransformComponent transform, IMoverComponent mover, PhysicsComponent physics)
|
||||
private void UpdateKinematics(ITransformComponent transform, IMoverComponent mover, PhysicsComponent physics, CollidableComponent collider = null)
|
||||
{
|
||||
bool weightless = false;
|
||||
|
||||
var tile = _mapManager.GetGrid(transform.GridID).GetTileRef(transform.GridPosition).Tile;
|
||||
|
||||
if ((!_mapManager.GetGrid(transform.GridID).HasGravity || tile.IsEmpty) && collider != null)
|
||||
{
|
||||
weightless = true;
|
||||
// No gravity: is our entity touching anything?
|
||||
var touching = false;
|
||||
foreach (var entity in _entityManager.GetEntitiesInRange(transform.Owner, mover.GrabRange, true))
|
||||
{
|
||||
if (entity.TryGetComponent<CollidableComponent>(out var otherCollider))
|
||||
{
|
||||
if (otherCollider.Owner == transform.Owner) continue; // Don't try to push off of yourself!
|
||||
touching |= ((collider.CollisionMask & otherCollider.CollisionLayer) != 0x0
|
||||
|| (otherCollider.CollisionMask & collider.CollisionLayer) != 0x0) // Ensure collision
|
||||
&& !entity.HasComponent<ItemComponent>(); // This can't be an item
|
||||
}
|
||||
}
|
||||
if (!touching)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (mover.VelocityDir.LengthSquared < 0.001 || !ActionBlockerSystem.CanMove(mover.Owner))
|
||||
{
|
||||
if (physics.LinearVelocity != Vector2.Zero)
|
||||
@@ -145,6 +180,13 @@ namespace Content.Server.GameObjects.EntitySystems
|
||||
}
|
||||
else
|
||||
{
|
||||
if (weightless)
|
||||
{
|
||||
physics.LinearVelocity = mover.VelocityDir * mover.CurrentPushSpeed;
|
||||
transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle();
|
||||
return;
|
||||
}
|
||||
|
||||
physics.LinearVelocity = mover.VelocityDir * (mover.Sprinting ? mover.CurrentSprintSpeed : mover.CurrentWalkSpeed);
|
||||
transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user