Skip to content

Commit

Permalink
Improves examine code
Browse files Browse the repository at this point in the history
Examining now has larger range. Ghosts have no range limit.
Fixed some messy code and some bad netcode.
  • Loading branch information
PJB3005 committed Jul 19, 2019
1 parent 1f320ec commit 1a92d08
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Content.Client/EntryPoint.cs
Expand Up @@ -137,6 +137,8 @@ public override void Init()
factory.RegisterIgnore("AiController");
factory.RegisterIgnore("PlayerInputMover");

factory.Register<ExaminerComponent>();

IoCManager.Register<IGameHud, GameHud>();
IoCManager.Register<IClientNotifyManager, ClientNotifyManager>();
IoCManager.Register<ISharedNotifyManager, ClientNotifyManager>();
Expand Down
19 changes: 8 additions & 11 deletions Content.Client/GameObjects/EntitySystems/ExamineSystem.cs
@@ -1,6 +1,7 @@
using System.Threading;
using System.Threading.Tasks;
using Content.Shared.GameObjects.EntitySystemMessages;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Input;
using JetBrains.Annotations;
using Robust.Client.GameObjects.EntitySystems;
Expand All @@ -11,7 +12,6 @@
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
Expand All @@ -22,13 +22,10 @@
namespace Content.Client.GameObjects.EntitySystems
{
[UsedImplicitly]
internal sealed class ExamineSystem : EntitySystem
internal sealed class ExamineSystem : ExamineSystemShared
{
public const string StyleClassEntityTooltip = "entity-tooltip";

public const float ExamineRange = 1.5f;
public const float ExamineRangeSquared = ExamineRange * ExamineRange;

#pragma warning disable 649
[Dependency] private IInputManager _inputManager;
[Dependency] private IUserInterfaceManager _userInterfaceManager;
Expand Down Expand Up @@ -56,19 +53,19 @@ public override void RegisterMessageTypes()

private void HandleExamine(ICommonSession session, GridCoordinates coords, EntityUid uid)
{
if (!uid.IsValid() || !_entityManager.TryGetEntity(uid, out var entity))
if (!uid.IsValid() || !_entityManager.TryGetEntity(uid, out var examined))
{
return;
}

var playerEntity = _playerManager.LocalPlayer.ControlledEntity;
if(playerEntity == null)
return;

if((entity.Transform.WorldPosition - playerEntity.Transform.WorldPosition).LengthSquared > ExamineRangeSquared)

if (playerEntity == null || !CanExamine(playerEntity, examined))
{
return;
}

DoExamine(entity);
DoExamine(examined);
}

public async void DoExamine(IEntity entity)
Expand Down
2 changes: 2 additions & 0 deletions Content.Server/EntryPoint.cs
Expand Up @@ -195,6 +195,8 @@ public override void Init()

factory.Register<CombatModeComponent>();

factory.Register<ExaminerComponent>();

IoCManager.Register<ISharedNotifyManager, ServerNotifyManager>();
IoCManager.Register<IServerNotifyManager, ServerNotifyManager>();
IoCManager.Register<IGameTicker, GameTicker>();
Expand Down
29 changes: 8 additions & 21 deletions Content.Server/GameObjects/EntitySystems/Click/ExamineSystem.cs
@@ -1,20 +1,11 @@
using System;
using System.Text;
using Content.Shared.GameObjects.EntitySystemMessages;
using Content.Shared.Input;
using Robust.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.EntitySystemMessages;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Players;
using Robust.Shared.Utility;

namespace Content.Server.GameObjects.EntitySystems
Expand All @@ -27,11 +18,8 @@ public interface IExamine
void Examine(FormattedMessage message);
}

public class ExamineSystem : EntitySystem
public class ExamineSystem : ExamineSystemShared
{
public const float ExamineRange = 1.5f;
public const float ExamineRangeSquared = ExamineRange * ExamineRange;

#pragma warning disable 649
[Dependency] private IEntityManager _entityManager;
[Dependency] private IPlayerManager _playerManager;
Expand Down Expand Up @@ -105,18 +93,17 @@ public override void HandleNetMessage(INetChannel channel, EntitySystemMessage m
var session = _playerManager.GetSessionByChannel(channel);
var playerEnt = session.AttachedEntity;

if((playerEnt == null) ||
(!_entityManager.TryGetEntity(request.EntityUid, out var entity)) ||
(entity.Transform.MapID != playerEnt.Transform.MapID) ||
((entity.Transform.WorldPosition - playerEnt.Transform.WorldPosition).LengthSquared > ExamineRangeSquared))
if (playerEnt == null
|| !_entityManager.TryGetEntity(request.EntityUid, out var entity)
|| !CanExamine(playerEnt, entity))
{
RaiseNetworkEvent(new ExamineSystemMessages.ExamineInfoResponseMessage(
request.EntityUid, _entityNotFoundMessage));
request.EntityUid, _entityNotFoundMessage), channel);
return;
}

var text = GetExamineText(entity);
RaiseNetworkEvent(new ExamineSystemMessages.ExamineInfoResponseMessage(request.EntityUid, text));
RaiseNetworkEvent(new ExamineSystemMessages.ExamineInfoResponseMessage(request.EntityUid, text), channel);
}
}
}
Expand Up @@ -163,6 +163,9 @@ public LandEventArgs(IEntity user, GridCoordinates landingLocation)
public GridCoordinates LandingLocation { get; }
}

/// <summary>
/// This interface gives components behavior when being used to "attack".
/// </summary>
public interface IAttack
{
void Attack(AttackEventArgs eventArgs);
Expand Down
30 changes: 30 additions & 0 deletions Content.Shared/GameObjects/Components/Mobs/ExaminerComponent.cs
@@ -0,0 +1,30 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;

namespace Content.Shared.GameObjects.Components.Mobs
{
/// <summary>
/// Component required for a player to be able to examine things.
/// </summary>
public sealed class ExaminerComponent : Component
{
public override string Name => "Examiner";

[ViewVariables(VVAccess.ReadWrite)]
private bool _doRangeCheck = true;

/// <summary>
/// Whether to do a distance check on examine.
/// If false, the user can theoretically examine from infinitely far away.
/// </summary>
public bool DoRangeCheck => _doRangeCheck;

public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);

serializer.DataField(ref _doRangeCheck, "DoRangeCheck", true);
}
}
}
35 changes: 35 additions & 0 deletions Content.Shared/GameObjects/EntitySystems/ExamineSystemShared.cs
@@ -0,0 +1,35 @@
using Content.Shared.GameObjects.Components.Mobs;
using JetBrains.Annotations;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;

namespace Content.Shared.GameObjects.EntitySystems
{
public abstract class ExamineSystemShared : EntitySystem
{
public const float ExamineRange = 8f;
public const float ExamineRangeSquared = ExamineRange * ExamineRange;

[Pure]
protected static bool CanExamine(IEntity examiner, IEntity examined)
{
if (!examiner.TryGetComponent(out ExaminerComponent examinerComponent))
{
return false;
}

if (!examinerComponent.DoRangeCheck)
{
return true;
}

if (examiner.Transform.MapID != examined.Transform.MapID)
{
return false;
}

var delta = examined.Transform.WorldPosition - examiner.Transform.WorldPosition;
return delta.LengthSquared <= ExamineRangeSquared;
}
}
}
5 changes: 4 additions & 1 deletion Resources/Prototypes/Entities/Mobs.yml
Expand Up @@ -49,7 +49,7 @@

- type: Input
context: "human"

- type: Species
Template: Human
HeatResistance: 323
Expand All @@ -66,6 +66,7 @@

- type: CombatMode
- type: Teleportable
- type: Examiner

- type: entity
id: MobObserver
Expand All @@ -81,6 +82,8 @@
aabb: "-0.5,-0.25,-0.05,0.25"
- type: Input
context: "ghost"
- type: Examiner
DoRangeCheck: false


- type: entity
Expand Down

0 comments on commit 1a92d08

Please sign in to comment.