forked from RevenantX/NetGameExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerPlayerManager.cs
More file actions
101 lines (88 loc) · 2.88 KB
/
Copy pathServerPlayerManager.cs
File metadata and controls
101 lines (88 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Collections.Generic;
using Code.Shared;
using LiteNetLib;
using UnityEngine;
namespace Code.Server
{
public class ServerPlayerManager : BasePlayerManager
{
private readonly ServerLogic _serverLogic;
private readonly ServerPlayer[] _players;
private readonly AntilagSystem _antilagSystem;
public readonly PlayerState[] PlayerStates;
private int _playersCount;
public override int Count => _playersCount;
public ServerPlayerManager(ServerLogic serverLogic)
{
_serverLogic = serverLogic;
_antilagSystem = new AntilagSystem(60, ServerLogic.MaxPlayers);
_players = new ServerPlayer[ServerLogic.MaxPlayers];
PlayerStates = new PlayerState[ServerLogic.MaxPlayers];
}
public bool EnableAntilag(ServerPlayer forPlayer)
{
return _antilagSystem.TryApplyAntilag(_players, _serverLogic.Tick, forPlayer.AssociatedPeer.Id);
}
public void DisableAntilag()
{
_antilagSystem.RevertAntilag(_players);
}
public override IEnumerator<BasePlayer> GetEnumerator()
{
int i = 0;
while (i < _playersCount)
{
yield return _players[i];
i++;
}
}
public override void OnShoot(BasePlayer from, Vector2 to, BasePlayer hit)
{
var serverPlayer = (ServerPlayer) from;
ShootPacket sp = new ShootPacket
{
FromPlayer = serverPlayer.Id,
CommandId = serverPlayer.LastProcessedCommandId,
ServerTick = _serverLogic.Tick,
Hit = to
};
_serverLogic.SendShoot(ref sp);
}
public void AddPlayer(ServerPlayer player)
{
for (int i = 0; i < _playersCount; i++)
{
if (_players[i].Id == player.Id)
{
_players[i] = player;
return;
}
}
_players[_playersCount] = player;
_playersCount++;
}
public override void LogicUpdate()
{
for (int i = 0; i < _playersCount; i++)
{
var p = _players[i];
p.Update(LogicTimer.FixedDelta);
PlayerStates[i] = p.NetworkState;
}
}
public bool RemovePlayer(byte playerId)
{
for (int i = 0; i < _playersCount; i++)
{
if (_players[i].Id == playerId)
{
_playersCount--;
_players[i] = _players[_playersCount];
_players[_playersCount] = null;
return true;
}
}
return false;
}
}
}