-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayers.cs
89 lines (75 loc) · 2.42 KB
/
Players.cs
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
/*
Players.cs
==========
Description: Provides the player menu features.
*/
using GameNetcodeStuff;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace EasyCompany
{
internal class Players
{
private PlayerControllerB[] players;
private float lastUpdateTime = 0f;
private const float updateRate = 1f; // time between updates
private const String playerMenuTitle = "Players";
public Players(Menu menu)
{
players = new PlayerControllerB[0];
// Create player menu tab
menu.AddTab(
new MenuTab(
playerMenuTitle,
new List<BaseMenuTabItem>()
)
);
}
// Update player list and menu if necessary
public void Update(Menu menu)
{
if (Time.time - lastUpdateTime < updateRate)
{
// Too soon
return;
}
// Update player list
players = UnityEngine.Object.FindObjectsOfType<PlayerControllerB>().Where(
p => p.isPlayerControlled
).ToArray();
// Update player menu
UpdateMenu(menu);
lastUpdateTime = Time.time;
}
// Update the player menu
private void UpdateMenu(Menu menu)
{
menu.UpdateTab(
playerMenuTitle,
players.Select(
p => new HContainerMenuTabItem(
$"{p.playerUsername} [{p.health}%]",
new List<BaseMenuTabItem>()
{
new ButtonMenuTabItem("Kill", () => KillPlayer(p)),
new ButtonMenuTabItem(
"Drop Items",
// This doesn't require ownership, for some reason
() => p.DropAllHeldItemsServerRpc()
),
}
)
).ToList<BaseMenuTabItem>()
);
}
// Kill a player
private void KillPlayer(PlayerControllerB target)
{
// Tell server player was hit by no one for 101 damage from below
target.DamagePlayerFromOtherClientServerRpc(101, Vector3.up, -1);
}
}
}