Adds shoddy death match system.
It barely even works but oh well.
This commit is contained in:
@@ -1,7 +1,18 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Content.Server.GameTicking
|
||||
{
|
||||
public class GameRule
|
||||
[PublicAPI]
|
||||
public abstract class GameRule
|
||||
{
|
||||
|
||||
public virtual void Added()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public virtual void Removed()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,112 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Content.Server.GameObjects;
|
||||
using Content.Server.Interfaces.Chat;
|
||||
using Content.Server.Interfaces.GameTicking;
|
||||
using Robust.Server.Interfaces.Player;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Timer = Robust.Shared.Timers.Timer;
|
||||
|
||||
namespace Content.Server.GameTicking.GameRules
|
||||
{
|
||||
public class DeathMatch
|
||||
/// <summary>
|
||||
/// Simple GameRule that will do a free-for-all death match.
|
||||
/// Kill everybody else to win.
|
||||
/// </summary>
|
||||
public sealed class RuleDeathMatch : GameRule, IEntityEventSubscriber
|
||||
{
|
||||
|
||||
private static readonly TimeSpan DeadCheckDelay = TimeSpan.FromSeconds(5);
|
||||
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IPlayerManager _playerManager;
|
||||
[Dependency] private readonly IEntityManager _entityManager;
|
||||
[Dependency] private readonly IChatManager _chatManager;
|
||||
[Dependency] private readonly IGameTicker _gameTicker;
|
||||
#pragma warning restore 649
|
||||
|
||||
private CancellationTokenSource _checkTimerCancel;
|
||||
|
||||
public override void Added()
|
||||
{
|
||||
_chatManager.DispatchServerAnnouncement("The game is now a death match. Kill everybody else to win!");
|
||||
|
||||
_entityManager.SubscribeEvent<MobDamageStateChangedMessage>(_onMobDamageStateChanged, this);
|
||||
_playerManager.PlayerStatusChanged += PlayerManagerOnPlayerStatusChanged;
|
||||
}
|
||||
|
||||
public override void Removed()
|
||||
{
|
||||
base.Removed();
|
||||
|
||||
_entityManager.UnsubscribeEvent<MobDamageStateChangedMessage>(this);
|
||||
_playerManager.PlayerStatusChanged -= PlayerManagerOnPlayerStatusChanged;
|
||||
}
|
||||
|
||||
private void _onMobDamageStateChanged(object sender, MobDamageStateChangedMessage message)
|
||||
{
|
||||
_runDelayedCheck();
|
||||
}
|
||||
|
||||
private void _checkForWinner()
|
||||
{
|
||||
_checkTimerCancel = null;
|
||||
|
||||
IPlayerSession winner = null;
|
||||
foreach (var playerSession in _playerManager.GetAllPlayers())
|
||||
{
|
||||
if (playerSession.AttachedEntity == null
|
||||
|| !playerSession.AttachedEntity.TryGetComponent(out SpeciesComponent species))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!species.CurrentDamageState.IsConscious)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (winner != null)
|
||||
{
|
||||
// Found a second person alive, nothing decided yet!
|
||||
return;
|
||||
}
|
||||
|
||||
winner = playerSession;
|
||||
}
|
||||
|
||||
if (winner == null)
|
||||
{
|
||||
_chatManager.DispatchServerAnnouncement("Everybody is dead, it's a stalemate!");
|
||||
}
|
||||
else
|
||||
{
|
||||
// We have a winner!
|
||||
_chatManager.DispatchServerAnnouncement($"{winner} wins the death match!");
|
||||
}
|
||||
|
||||
_chatManager.DispatchServerAnnouncement($"Restarting in 10 seconds.");
|
||||
|
||||
Timer.Spawn(TimeSpan.FromSeconds(10), () => _gameTicker.RestartRound());
|
||||
}
|
||||
|
||||
private void PlayerManagerOnPlayerStatusChanged(object sender, SessionStatusEventArgs e)
|
||||
{
|
||||
if (e.NewStatus == SessionStatus.Disconnected)
|
||||
{
|
||||
_runDelayedCheck();
|
||||
}
|
||||
}
|
||||
|
||||
private void _runDelayedCheck()
|
||||
{
|
||||
_checkTimerCancel?.Cancel();
|
||||
_checkTimerCancel = new CancellationTokenSource();
|
||||
|
||||
Timer.Spawn(DeadCheckDelay, _checkForWinner, _checkTimerCancel.Token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,8 @@ namespace Content.Server.GameTicking
|
||||
|
||||
private readonly Random _spawnRandom = new Random();
|
||||
|
||||
[ViewVariables] private readonly List<GameRule> _gameRules = new List<GameRule>();
|
||||
|
||||
#pragma warning disable 649
|
||||
[Dependency] private IEntityManager _entityManager;
|
||||
[Dependency] private IMapManager _mapManager;
|
||||
@@ -88,6 +90,7 @@ namespace Content.Server.GameTicking
|
||||
[Dependency] private IPlayerManager _playerManager;
|
||||
[Dependency] private IChatManager _chatManager;
|
||||
[Dependency] private IServerNetManager _netManager;
|
||||
[Dependency] private IDynamicTypeFactory _dynamicTypeFactory;
|
||||
#pragma warning restore 649
|
||||
|
||||
public void Initialize()
|
||||
@@ -150,7 +153,7 @@ namespace Content.Server.GameTicking
|
||||
RunLevel = GameRunLevel.InRound;
|
||||
|
||||
// TODO: Allow other presets to be selected.
|
||||
var preset = new PresetTraitor();
|
||||
var preset = _dynamicTypeFactory.CreateInstance<PresetTraitor>();
|
||||
preset.Start();
|
||||
|
||||
foreach (var (playerSession, ready) in _playersInLobby.ToList())
|
||||
@@ -219,6 +222,30 @@ namespace Content.Server.GameTicking
|
||||
_netManager.ServerSendMessage(_getStatusMsg(player), player.ConnectedClient);
|
||||
}
|
||||
|
||||
public T AddGameRule<T>() where T : GameRule, new()
|
||||
{
|
||||
var instance = _dynamicTypeFactory.CreateInstance<T>();
|
||||
|
||||
_gameRules.Add(instance);
|
||||
instance.Added();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void RemoveGameRule(GameRule rule)
|
||||
{
|
||||
if (_gameRules.Contains(rule))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
rule.Removed();
|
||||
|
||||
_gameRules.Remove(rule);
|
||||
}
|
||||
|
||||
public IEnumerable<GameRule> ActiveGameRules => _gameRules;
|
||||
|
||||
private IEntity _spawnPlayerMob()
|
||||
{
|
||||
var entity = _entityManager.ForceSpawnEntityAt(PlayerPrototypeName, _getLateJoinSpawnPoint());
|
||||
@@ -290,6 +317,14 @@ namespace Content.Server.GameTicking
|
||||
{
|
||||
unCastData.ContentData().WipeMind();
|
||||
}
|
||||
|
||||
// Clear up any game rules.
|
||||
foreach (var rule in _gameRules)
|
||||
{
|
||||
rule.Removed();
|
||||
}
|
||||
|
||||
_gameRules.Clear();
|
||||
}
|
||||
|
||||
private void _preRoundSetup()
|
||||
|
||||
Reference in New Issue
Block a user