Clean up client game ticker status code.

Fixes #495
This commit is contained in:
Pieter-Jan Briers
2020-02-26 16:42:12 +01:00
parent 0adc134cd4
commit 0e6b55de66
14 changed files with 484 additions and 468 deletions

View File

@@ -1,171 +1,65 @@
using System.Collections.Generic;
using System.Linq;
using Content.Client.GameObjects.Components;
using Content.Shared.GameObjects;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Client.Interfaces.GameObjects;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.Interfaces.Graphics.ClientEye;
using Content.Client.Chat;
using Content.Client.Interfaces.Chat;
using Content.Client.UserInterface;
using Content.Shared.Input;
using Robust.Client.Interfaces.Input;
using Robust.Client.Interfaces.Placement;
using Robust.Client.Player;
using Robust.Shared.GameObjects;
using Robust.Client.Interfaces.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Timing;
using Robust.Shared.Localization;
using Robust.Shared.ViewVariables;
namespace Content.Client.State
{
// OH GOD.
// Ok actually it's fine.
// Instantiated dynamically through the StateManager, Dependencies will be resolved.
public sealed partial class GameScreen : Robust.Client.State.State
public class GameScreen : GameScreenBase
{
#pragma warning disable 649
[Dependency] private readonly IClientEntityManager _entityManager;
[Dependency] private readonly IComponentManager _componentManager;
[Dependency] private readonly IInputManager inputManager;
[Dependency] private readonly IPlayerManager playerManager;
[Dependency] private readonly IPlacementManager placementManager;
[Dependency] private readonly IEyeManager eyeManager;
[Dependency] private readonly IEntitySystemManager entitySystemManager;
[Dependency] private readonly IGameTiming timing;
[Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager;
[Dependency] private readonly IGameHud _gameHud;
[Dependency] private readonly IInputManager _inputManager;
[Dependency] private readonly IChatManager _chatManager;
#pragma warning restore 649
private IEntity lastHoveredEntity;
[ViewVariables] private ChatBox _gameChat;
public override void Startup()
{
inputManager.KeyBindStateChanged += OnKeyBindStateChanged;
base.Startup();
_gameChat = new ChatBox();
_userInterfaceManager.StateRoot.AddChild(_gameChat);
LayoutContainer.SetAnchorAndMarginPreset(_gameChat, LayoutContainer.LayoutPreset.TopRight, margin: 10);
LayoutContainer.SetAnchorAndMarginPreset(_gameChat, LayoutContainer.LayoutPreset.TopRight, margin: 10);
LayoutContainer.SetMarginLeft(_gameChat, -475);
LayoutContainer.SetMarginBottom(_gameChat, 235);
_userInterfaceManager.StateRoot.AddChild(_gameHud.RootControl);
_chatManager.SetChatBox(_gameChat);
_gameChat.DefaultChatFormat = "say \"{0}\"";
_gameChat.Input.PlaceHolder = Loc.GetString("Say something! [ for OOC");
_inputManager.SetInputCommand(ContentKeyFunctions.FocusChat,
InputCmdHandler.FromDelegate(s => FocusChat(_gameChat)));
}
public override void Shutdown()
{
playerManager.LocalPlayer.DetachEntity();
base.Shutdown();
inputManager.KeyBindStateChanged -= OnKeyBindStateChanged;
_gameChat.Dispose();
_gameHud.RootControl.Orphan();
}
public override void Update(FrameEventArgs e)
internal static void FocusChat(ChatBox chat)
{
_componentManager.CullRemovedComponents();
_entityManager.Update(e.DeltaSeconds);
playerManager.Update(e.DeltaSeconds);
}
public override void FrameUpdate(FrameEventArgs e)
{
placementManager.FrameUpdate(e);
_entityManager.FrameUpdate(e.DeltaSeconds);
var mousePosWorld = eyeManager.ScreenToWorld(new ScreenCoordinates(inputManager.MouseScreenPosition));
var entityToClick = GetEntityUnderPosition(mousePosWorld);
var inRange = false;
if (playerManager.LocalPlayer.ControlledEntity != null && entityToClick != null)
if (chat == null || chat.UserInterfaceManager.KeyboardFocused != null)
{
var playerPos = playerManager.LocalPlayer.ControlledEntity.Transform.WorldPosition;
var entityPos = entityToClick.Transform.WorldPosition;
inRange = (entityPos - playerPos).Length <= VerbUtility.InteractionRange;
}
InteractionOutlineComponent outline;
if (entityToClick == lastHoveredEntity)
{
if (entityToClick != null && entityToClick.TryGetComponent(out outline))
{
outline.UpdateInRange(inRange);
}
return;
}
if (lastHoveredEntity != null && !lastHoveredEntity.Deleted &&
lastHoveredEntity.TryGetComponent(out outline))
{
outline.OnMouseLeave();
}
lastHoveredEntity = entityToClick;
if (lastHoveredEntity != null && lastHoveredEntity.TryGetComponent(out outline))
{
outline.OnMouseEnter(inRange);
}
}
public IEntity GetEntityUnderPosition(GridCoordinates coordinates)
{
var entitiesUnderPosition = GetEntitiesUnderPosition(coordinates);
return entitiesUnderPosition.Count > 0 ? entitiesUnderPosition[0] : null;
}
public IList<IEntity> GetEntitiesUnderPosition(GridCoordinates coordinates)
{
// Find all the entities intersecting our click
var mapCoords = coordinates.ToMap(_mapManager);
var entities = _entityManager.GetEntitiesIntersecting(mapCoords.MapId, mapCoords.Position);
// Check the entities against whether or not we can click them
var foundEntities = new List<(IEntity clicked, int drawDepth)>();
foreach (var entity in entities)
{
if (entity.TryGetComponent<IClientClickableComponent>(out var component)
&& entity.Transform.IsMapTransform
&& component.CheckClick(coordinates.Position, out var drawDepthClicked))
{
foundEntities.Add((entity, drawDepthClicked));
}
}
if (foundEntities.Count == 0)
return new List<IEntity>();
foundEntities.Sort(new ClickableEntityComparer());
// 0 is the top element.
foundEntities.Reverse();
return foundEntities.Select(a => a.clicked).ToList();
}
internal class ClickableEntityComparer : IComparer<(IEntity clicked, int depth)>
{
public int Compare((IEntity clicked, int depth) x, (IEntity clicked, int depth) y)
{
var val = x.depth.CompareTo(y.depth);
if (val != 0)
{
return val;
}
var transx = x.clicked.Transform;
var transy = y.clicked.Transform;
return transx.GridPosition.Y.CompareTo(transy.GridPosition.Y);
}
}
/// <summary>
/// Converts a state change event from outside the simulation to inside the simulation.
/// </summary>
/// <param name="args">Event data values for a bound key state change.</param>
private void OnKeyBindStateChanged(BoundKeyEventArgs args)
{
var inputSys = entitySystemManager.GetEntitySystem<InputSystem>();
var func = args.Function;
var funcId = inputManager.NetworkBindMap.KeyFunctionID(func);
var mousePosWorld = eyeManager.ScreenToWorld(args.PointerLocation);
var entityToClick = GetEntityUnderPosition(mousePosWorld);
var message = new FullInputCmdMessage(timing.CurTick, funcId, args.State, mousePosWorld,
args.PointerLocation, entityToClick?.Uid ?? EntityUid.Invalid);
// client side command handlers will always be sent the local player session.
var session = playerManager.LocalPlayer.Session;
inputSys.HandleInputCommand(session, func, message);
chat.Input.IgnoreNext = true;
chat.Input.GrabKeyboardFocus();
}
}
}

View File

@@ -0,0 +1,160 @@
using System.Collections.Generic;
using System.Linq;
using Content.Client.GameObjects.Components;
using Content.Shared.GameObjects;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Client.Interfaces.GameObjects;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.Interfaces.Graphics.ClientEye;
using Robust.Client.Interfaces.Input;
using Robust.Client.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Timing;
namespace Content.Client.State
{
// OH GOD.
// Ok actually it's fine.
// Instantiated dynamically through the StateManager, Dependencies will be resolved.
public partial class GameScreenBase : StateBase
{
#pragma warning disable 649
[Dependency] private readonly IClientEntityManager _entityManager;
[Dependency] private readonly IInputManager _inputManager;
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IEyeManager _eyeManager;
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
[Dependency] private readonly IGameTiming _timing;
[Dependency] private readonly IMapManager _mapManager;
#pragma warning restore 649
private IEntity _lastHoveredEntity;
public override void Startup()
{
_inputManager.KeyBindStateChanged += OnKeyBindStateChanged;
}
public override void Shutdown()
{
_playerManager.LocalPlayer.DetachEntity();
_inputManager.KeyBindStateChanged -= OnKeyBindStateChanged;
}
public override void FrameUpdate(FrameEventArgs e)
{
base.FrameUpdate(e);
var mousePosWorld = _eyeManager.ScreenToWorld(new ScreenCoordinates(_inputManager.MouseScreenPosition));
var entityToClick = GetEntityUnderPosition(mousePosWorld);
var inRange = false;
if (_playerManager.LocalPlayer.ControlledEntity != null && entityToClick != null)
{
var playerPos = _playerManager.LocalPlayer.ControlledEntity.Transform.WorldPosition;
var entityPos = entityToClick.Transform.WorldPosition;
inRange = (entityPos - playerPos).Length <= VerbUtility.InteractionRange;
}
InteractionOutlineComponent outline;
if (entityToClick == _lastHoveredEntity)
{
if (entityToClick != null && entityToClick.TryGetComponent(out outline))
{
outline.UpdateInRange(inRange);
}
return;
}
if (_lastHoveredEntity != null && !_lastHoveredEntity.Deleted &&
_lastHoveredEntity.TryGetComponent(out outline))
{
outline.OnMouseLeave();
}
_lastHoveredEntity = entityToClick;
if (_lastHoveredEntity != null && _lastHoveredEntity.TryGetComponent(out outline))
{
outline.OnMouseEnter(inRange);
}
}
public IEntity GetEntityUnderPosition(GridCoordinates coordinates)
{
var entitiesUnderPosition = GetEntitiesUnderPosition(coordinates);
return entitiesUnderPosition.Count > 0 ? entitiesUnderPosition[0] : null;
}
public IList<IEntity> GetEntitiesUnderPosition(GridCoordinates coordinates)
{
// Find all the entities intersecting our click
var mapCoords = coordinates.ToMap(_mapManager);
var entities = _entityManager.GetEntitiesIntersecting(mapCoords.MapId, mapCoords.Position);
// Check the entities against whether or not we can click them
var foundEntities = new List<(IEntity clicked, int drawDepth)>();
foreach (var entity in entities)
{
if (entity.TryGetComponent<IClientClickableComponent>(out var component)
&& entity.Transform.IsMapTransform
&& component.CheckClick(coordinates.Position, out var drawDepthClicked))
{
foundEntities.Add((entity, drawDepthClicked));
}
}
if (foundEntities.Count == 0)
return new List<IEntity>();
foundEntities.Sort(new ClickableEntityComparer());
// 0 is the top element.
foundEntities.Reverse();
return foundEntities.Select(a => a.clicked).ToList();
}
internal class ClickableEntityComparer : IComparer<(IEntity clicked, int depth)>
{
public int Compare((IEntity clicked, int depth) x, (IEntity clicked, int depth) y)
{
var val = x.depth.CompareTo(y.depth);
if (val != 0)
{
return val;
}
var transx = x.clicked.Transform;
var transy = y.clicked.Transform;
return transx.GridPosition.Y.CompareTo(transy.GridPosition.Y);
}
}
/// <summary>
/// Converts a state change event from outside the simulation to inside the simulation.
/// </summary>
/// <param name="args">Event data values for a bound key state change.</param>
private void OnKeyBindStateChanged(BoundKeyEventArgs args)
{
var inputSys = _entitySystemManager.GetEntitySystem<InputSystem>();
var func = args.Function;
var funcId = _inputManager.NetworkBindMap.KeyFunctionID(func);
var mousePosWorld = _eyeManager.ScreenToWorld(args.PointerLocation);
var entityToClick = GetEntityUnderPosition(mousePosWorld);
var message = new FullInputCmdMessage(_timing.CurTick, funcId, args.State, mousePosWorld,
args.PointerLocation, entityToClick?.Uid ?? EntityUid.Invalid);
// client side command handlers will always be sent the local player session.
var session = _playerManager.LocalPlayer.Session;
inputSys.HandleInputCommand(session, func, message);
}
}
}

View File

@@ -0,0 +1,188 @@
using System;
using System.Linq;
using Content.Client.Interfaces;
using Content.Client.Interfaces.Chat;
using Content.Client.UserInterface;
using Content.Shared.Input;
using Robust.Client.Console;
using Robust.Client.Interfaces;
using Robust.Client.Interfaces.Input;
using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.Interfaces.UserInterface;
using Robust.Client.Player;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Content.Client.State
{
public class LobbyState : StateBase
{
#pragma warning disable 649
[Dependency] private readonly IBaseClient _baseClient;
[Dependency] private readonly IClientConsole _console;
[Dependency] private readonly IChatManager _chatManager;
[Dependency] private readonly IInputManager _inputManager;
[Dependency] private readonly IEntityManager _entityManager;
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IResourceCache _resourceCache;
[Dependency] private readonly IClientGameTicker _clientGameTicker;
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager;
[Dependency] private readonly IClientPreferencesManager _preferencesManager;
#pragma warning restore 649
[ViewVariables] private CharacterSetupGui _characterSetup;
[ViewVariables] private LobbyGui _lobby;
public override void Startup()
{
_characterSetup = new CharacterSetupGui(_entityManager, _resourceCache, _preferencesManager,
_prototypeManager);
LayoutContainer.SetAnchorPreset(_characterSetup, LayoutContainer.LayoutPreset.Wide);
_characterSetup.CloseButton.OnPressed += args =>
{
_characterSetup.Save();
_lobby.CharacterPreview.UpdateUI();
_userInterfaceManager.StateRoot.AddChild(_lobby);
_userInterfaceManager.StateRoot.RemoveChild(_characterSetup);
};
_lobby = new LobbyGui(_entityManager, _resourceCache, _preferencesManager);
_userInterfaceManager.StateRoot.AddChild(_lobby);
LayoutContainer.SetAnchorPreset(_lobby, LayoutContainer.LayoutPreset.Wide);
_chatManager.SetChatBox(_lobby.Chat);
_lobby.Chat.DefaultChatFormat = "ooc \"{0}\"";
_lobby.ServerName.Text = _baseClient.GameInfo.ServerName;
_inputManager.SetInputCommand(ContentKeyFunctions.FocusChat,
InputCmdHandler.FromDelegate(s => GameScreen.FocusChat(_lobby.Chat)));
UpdateLobbyUi();
_lobby.CharacterPreview.CharacterSetupButton.OnPressed += args =>
{
SetReady(false);
_userInterfaceManager.StateRoot.RemoveChild(_lobby);
_userInterfaceManager.StateRoot.AddChild(_characterSetup);
};
_lobby.ObserveButton.OnPressed += args => _console.ProcessCommand("observe");
_lobby.ReadyButton.OnPressed += args =>
{
if (!_clientGameTicker.IsGameStarted)
{
return;
}
_console.ProcessCommand("joingame");
};
_lobby.ReadyButton.OnToggled += args =>
{
SetReady(args.Pressed);
};
_lobby.LeaveButton.OnPressed += args => _console.ProcessCommand("disconnect");
UpdatePlayerList();
_playerManager.PlayerListUpdated += PlayerManagerOnPlayerListUpdated;
_clientGameTicker.InfoBlobUpdated += UpdateLobbyUi;
_clientGameTicker.LobbyStatusUpdated += UpdateLobbyUi;
}
public override void Shutdown()
{
_playerManager.PlayerListUpdated -= PlayerManagerOnPlayerListUpdated;
_clientGameTicker.InfoBlobUpdated -= UpdateLobbyUi;
_clientGameTicker.LobbyStatusUpdated -= UpdateLobbyUi;
_lobby.Dispose();
_characterSetup.Dispose();
}
public override void FrameUpdate(FrameEventArgs e)
{
if (_clientGameTicker.IsGameStarted)
{
_lobby.StartTime.Text = "";
return;
}
string text;
var difference = _clientGameTicker.StartTime - DateTime.UtcNow;
if (difference.Ticks < 0)
{
if (difference.TotalSeconds < -5)
{
text = Loc.GetString("Right Now?");
}
else
{
text = Loc.GetString("Right Now");
}
}
else
{
text = $"{(int) Math.Floor(difference.TotalMinutes)}:{difference.Seconds:D2}";
}
_lobby.StartTime.Text = Loc.GetString("Round Starts In: {0}", text);
}
private void PlayerManagerOnPlayerListUpdated(object sender, EventArgs e) => UpdatePlayerList();
private void UpdateLobbyUi()
{
if (_lobby == null)
{
return;
}
if (_clientGameTicker.IsGameStarted)
{
_lobby.ReadyButton.Text = Loc.GetString("Join");
_lobby.ReadyButton.ToggleMode = false;
_lobby.ReadyButton.Pressed = false;
}
else
{
_lobby.StartTime.Text = "";
_lobby.ReadyButton.Text = Loc.GetString("Ready Up");
_lobby.ReadyButton.ToggleMode = true;
_lobby.ReadyButton.Pressed = _clientGameTicker.AreWeReady;
}
_lobby.ServerInfo.SetInfoBlob(_clientGameTicker.ServerInfoBlob);
}
private void UpdatePlayerList()
{
_lobby.OnlinePlayerItemList.Clear();
foreach (var session in _playerManager.Sessions.OrderBy(s => s.Name))
{
_lobby.OnlinePlayerItemList.AddItem(session.Name);
}
}
private void SetReady(bool newReady)
{
if (_clientGameTicker.IsGameStarted)
{
return;
}
_console.ProcessCommand($"toggleready {newReady}");
}
}
}

View File

@@ -0,0 +1,32 @@
using Robust.Client.Interfaces.GameObjects;
using Robust.Client.Interfaces.Placement;
using Robust.Client.Player;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Timing;
namespace Content.Client.State
{
public abstract class StateBase : Robust.Client.State.State
{
#pragma warning disable 649
[Dependency] private readonly IClientEntityManager _entityManager;
[Dependency] private readonly IComponentManager _componentManager;
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IPlacementManager _placementManager;
#pragma warning restore 649
public override void Update(FrameEventArgs e)
{
_componentManager.CullRemovedComponents();
_entityManager.Update(e.DeltaSeconds);
_playerManager.Update(e.DeltaSeconds);
}
public override void FrameUpdate(FrameEventArgs e)
{
_placementManager.FrameUpdate(e);
_entityManager.FrameUpdate(e.DeltaSeconds);
}
}
}