Arcade polish (#2333)

Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>
This commit is contained in:
Paul Ritter
2020-10-30 11:25:26 +01:00
committed by GitHub
parent e9df8794da
commit d8f5bffaa0
7 changed files with 266 additions and 139 deletions

View File

@@ -13,6 +13,7 @@ using Robust.Server.GameObjects.Components.UserInterface;
using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
@@ -29,10 +30,11 @@ namespace Content.Server.GameObjects.Components.Arcade
public override string Name => "BlockGameArcade";
public override uint? NetID => ContentNetIDs.BLOCKGAME_ARCADE;
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
[ComponentDependency] private PowerReceiverComponent? _powerReceiverComponent = default!;
private bool Powered => _powerReceiverComponent?.Powered ?? false;
private BoundUserInterface? UserInterface => Owner.GetUIOrNull(BlockGameUiKey.Key);
private BlockGame _game = null!;
private BlockGame? _game;
private IPlayerSession? _player;
private List<IPlayerSession> _spectators = new List<IPlayerSession>();
@@ -47,7 +49,7 @@ namespace Content.Server.GameObjects.Components.Arcade
{
return;
}
if(!ActionBlockerSystem.CanInteract(Owner)) return;
if(!ActionBlockerSystem.CanInteract(actor.playerSession.AttachedEntity)) return;
UserInterface?.Toggle(actor.playerSession);
RegisterPlayerSession(actor.playerSession);
@@ -59,7 +61,7 @@ namespace Content.Server.GameObjects.Components.Arcade
else _spectators.Add(session);
UpdatePlayerStatus(session);
_game.UpdateNewPlayerUI(session);
_game?.UpdateNewPlayerUI(session);
}
private void DeactivePlayer(IPlayerSession session)
@@ -104,38 +106,56 @@ namespace Content.Server.GameObjects.Components.Arcade
{
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
if (_powerReceiverComponent != null)
{
_powerReceiverComponent.OnPowerStateChanged += OnPowerStateChanged;
}
_game = new BlockGame(this);
}
private void OnPowerStateChanged(object? sender, PowerStateEventArgs e)
{
if (e.Powered) return;
UserInterface?.CloseAll();
_player = null;
_spectators.Clear();
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage obj)
{
if (obj.Message is BlockGameMessages.BlockGameUserUnregisterMessage unregisterMessage)
switch (obj.Message)
{
UnRegisterPlayerSession(obj.Session);
return;
}
if (obj.Session != _player) return;
case BlockGameMessages.BlockGameUserUnregisterMessage unregisterMessage:
UnRegisterPlayerSession(obj.Session);
break;
case BlockGameMessages.BlockGamePlayerActionMessage playerActionMessage:
if (obj.Session != _player) break;
if (!ActionBlockerSystem.CanInteract(Owner))
{
DeactivePlayer(obj.Session);
}
if (!ActionBlockerSystem.CanInteract(Owner))
{
DeactivePlayer(obj.Session);
break;
}
if (!(obj.Message is BlockGameMessages.BlockGamePlayerActionMessage message)) return;
if (message.PlayerAction == BlockGamePlayerAction.NewGame)
{
if(_game.Started) _game = new BlockGame(this);
_game.StartGame();
}
else
{
_game.ProcessInput(message.PlayerAction);
if (playerActionMessage.PlayerAction == BlockGamePlayerAction.NewGame)
{
if(_game?.Started == true) _game = new BlockGame(this);
_game?.StartGame();
}
else
{
_game?.ProcessInput(playerActionMessage.PlayerAction);
}
break;
}
}
public void DoGameTick(float frameTime)
{
_game.GameTick(frameTime);
_game?.GameTick(frameTime);
}
private class BlockGame
@@ -196,13 +216,12 @@ namespace Content.Server.GameObjects.Components.Arcade
private Vector2i _currentPiecePosition;
private BlockGamePieceRotation _currentRotation;
private float _softDropOverride = 0.1f;
private float _softDropModifier = 0.1f;
private float Speed => !_softDropPressed
? -0.03f * Level + 1
: _softDropOverride;
private float Speed =>
-0.03f * Level + 1 * (!_softDropPressed ? 1 : _softDropModifier);
private float _pressCheckSpeed = 0.08f;
private const float _pressCheckSpeed = 0.08f;
private bool _running;
public bool Paused => !(_running && _started);
@@ -278,7 +297,8 @@ namespace Content.Server.GameObjects.Components.Arcade
public BlockGame(BlockGameArcadeComponent component)
{
_component = component;
_internalNextPiece = BlockGamePiece.GetRandom(_component._random);
_allBlockGamePieces = (BlockGamePieceType[]) Enum.GetValues(typeof(BlockGamePieceType));
_internalNextPiece = GetRandomBlockGamePiece(_component._random);
}
private void SendHighscoreUpdate()
@@ -343,7 +363,7 @@ namespace Content.Server.GameObjects.Components.Arcade
{
_accumulatedLeftPressTime += frameTime;
if (_accumulatedLeftPressTime >= _pressCheckSpeed)
while (_accumulatedLeftPressTime >= _pressCheckSpeed)
{
if (_currentPiece.Positions(_currentPiecePosition.AddToX(-1), _currentRotation)
@@ -361,7 +381,7 @@ namespace Content.Server.GameObjects.Components.Arcade
{
_accumulatedRightPressTime += frameTime;
if (_accumulatedRightPressTime >= _pressCheckSpeed)
while (_accumulatedRightPressTime >= _pressCheckSpeed)
{
if (_currentPiece.Positions(_currentPiecePosition.AddToX(1), _currentRotation)
.All(MoveCheck))
@@ -384,13 +404,14 @@ namespace Content.Server.GameObjects.Components.Arcade
var checkTime = Speed;
if (_accumulatedFieldFrameTime < checkTime) return;
while (_accumulatedFieldFrameTime >= checkTime)
{
if (_softDropPressed) AddPoints(1);
if(_softDropPressed) AddPoints(1);
InternalFieldTick();
InternalFieldTick();
_accumulatedFieldFrameTime -= checkTime;
_accumulatedFieldFrameTime -= checkTime;
}
}
private void InternalFieldTick()
@@ -490,7 +511,7 @@ namespace Content.Server.GameObjects.Components.Arcade
private void InitializeNewBlock()
{
InitializeNewBlock(_nextPiece);
_nextPiece = BlockGamePiece.GetRandom(_component._random);
_nextPiece = GetRandomBlockGamePiece(_component._random);
_holdBlock = false;
_component.UserInterface?.SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(_nextPiece.BlocksForPreview(), BlockGameMessages.BlockGameVisualType.NextBlock));
@@ -538,6 +559,7 @@ namespace Content.Server.GameObjects.Components.Arcade
break;
case BlockGamePlayerAction.SoftdropStart:
_softDropPressed = true;
if (_accumulatedFieldFrameTime > Speed) _accumulatedFieldFrameTime = Speed; //to prevent jumps
break;
case BlockGamePlayerAction.SoftdropEnd:
_softDropPressed = false;
@@ -707,6 +729,22 @@ namespace Content.Server.GameObjects.Components.Arcade
};
}
private readonly BlockGamePieceType[] _allBlockGamePieces;
private List<BlockGamePieceType> _blockGamePiecesBuffer = new List<BlockGamePieceType>();
private BlockGamePiece GetRandomBlockGamePiece(IRobustRandom random)
{
if (_blockGamePiecesBuffer.Count == 0)
{
_blockGamePiecesBuffer = _allBlockGamePieces.ToList();
}
var chosenPiece = random.Pick(_blockGamePiecesBuffer);
_blockGamePiecesBuffer.Remove(chosenPiece);
return BlockGamePiece.GetPiece(chosenPiece);
}
private struct BlockGamePiece
{
public Vector2i[] Offsets;
@@ -770,13 +808,6 @@ namespace Content.Server.GameObjects.Components.Arcade
return Blocks(new Vector2i(-xOffset, -yOffset), BlockGamePieceRotation.North);
}
public static BlockGamePiece GetRandom(IRobustRandom random)
{
var pieces = (BlockGamePieceType[])Enum.GetValues(typeof(BlockGamePieceType));
var choice = random.Pick(pieces);
return GetPiece(choice);
}
public static BlockGamePiece GetPiece(BlockGamePieceType type)
{
//switch statement, hardcoded offsets