Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SubsystemPlayers.cs
浏览该文件的文档.
1using System.Globalization;
2using Engine;
5
6namespace Game {
9
10 public List<PlayerData> m_playersData = [];
11
12 public List<ComponentPlayer> m_componentPlayers = [];
13
15
16 public static int MaxPlayers = 4;
17
18 public bool PlayerStartedPlaying = false;
19
20 public ReadOnlyList<PlayerData> PlayersData => new(m_playersData);
21
22 public ReadOnlyList<ComponentPlayer> ComponentPlayers => new(m_componentPlayers);
23
24 public Vector3 GlobalSpawnPosition { get; set; }
25
26 public UpdateOrder UpdateOrder => UpdateOrder.SubsystemPlayers;
27
28 public virtual Action<PlayerData> PlayerAdded { get; set; }
29
30 public virtual Action<PlayerData> PlayerRemoved { get; set; }
31
32 public bool IsPlayer(Entity entity) {
33 foreach (ComponentPlayer componentPlayer in m_componentPlayers) {
34 if (entity == componentPlayer.Entity) {
35 return true;
36 }
37 }
38 return false;
39 }
40
42 ComponentPlayer result = null;
43 float num = float.MaxValue;
44 foreach (ComponentPlayer componentPlayer in ComponentPlayers) {
45 float num2 = Vector3.DistanceSquared(componentPlayer.ComponentBody.Position, position);
46 if (num2 < num) {
47 num = num2;
48 result = componentPlayer;
49 }
50 }
51 return result;
52 }
53
54 public void AddPlayerData(PlayerData playerData) {
55 if (m_playersData.Count >= MaxPlayers) {
56 throw new InvalidOperationException("Too many players.");
57 }
58 if (m_playersData.Contains(playerData)) {
59 throw new InvalidOperationException("Player already added.");
60 }
61 m_playersData.Add(playerData);
62 playerData.PlayerIndex = ++m_nextPlayerIndex;
63 PlayerAdded?.Invoke(playerData);
64 }
65
66 public void RemovePlayerData(PlayerData playerData) {
67 if (!m_playersData.Contains(playerData)) {
68 throw new InvalidOperationException("Player does not exist.");
69 }
70 m_playersData.Remove(playerData);
71 if (playerData.ComponentPlayer != null) {
72 Project.RemoveEntity(playerData.ComponentPlayer.Entity, true);
73 }
74 PlayerRemoved?.Invoke(playerData);
75 playerData.Dispose();
76 }
77
78 public virtual void Update(float dt) {
79 if (m_playersData.Count == 0) {
81 }
82 foreach (PlayerData playersDatum in m_playersData) {
83 playersDatum.Update();
84 }
85 }
86
87 public override void Dispose() {
88 foreach (PlayerData playersDatum in m_playersData) {
89 playersDatum.Dispose();
90 }
91 }
92
93 public override void Load(ValuesDictionary valuesDictionary) {
94 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
95 m_nextPlayerIndex = valuesDictionary.GetValue<int>("NextPlayerIndex");
96 GlobalSpawnPosition = valuesDictionary.GetValue<Vector3>("GlobalSpawnPosition");
97 foreach (KeyValuePair<string, object> item in valuesDictionary.GetValue<ValuesDictionary>("Players")) {
98 PlayerData playerData = new(Project);
99 playerData.Load((ValuesDictionary)item.Value);
100 playerData.PlayerIndex = int.Parse(item.Key, CultureInfo.InvariantCulture);
101 m_playersData.Add(playerData);
102 }
103 }
104
105 public override void Save(ValuesDictionary valuesDictionary) {
106 valuesDictionary.SetValue("NextPlayerIndex", m_nextPlayerIndex);
107 valuesDictionary.SetValue("GlobalSpawnPosition", GlobalSpawnPosition);
108 ValuesDictionary valuesDictionary2 = new();
109 valuesDictionary.SetValue("Players", valuesDictionary2);
110 foreach (PlayerData playersDatum in m_playersData) {
111 ValuesDictionary valuesDictionary3 = new();
112 valuesDictionary2.SetValue(playersDatum.PlayerIndex.ToString(CultureInfo.InvariantCulture), valuesDictionary3);
113 playersDatum.Save(valuesDictionary3);
114 }
115 }
116
117 public override void OnEntityAdded(Entity entity) {
118 foreach (PlayerData playersDatum in m_playersData) {
119 playersDatum.OnEntityAdded(entity);
120 }
122 }
123
124 public override void OnEntityRemoved(Entity entity) {
125 foreach (PlayerData playersDatum in m_playersData) {
126 playersDatum.OnEntityRemoved(entity);
127 }
129 }
130
131 public virtual void UpdateComponentPlayers() {
132 m_componentPlayers.Clear();
133 foreach (PlayerData playersDatum in m_playersData) {
134 if (playersDatum.ComponentPlayer != null) {
135 m_componentPlayers.Add(playersDatum.ComponentPlayer);
136 }
137 }
138 }
139 }
140}
void Load(ValuesDictionary valuesDictionary)
ComponentPlayer ComponentPlayer
virtual void Update()
void Save(ValuesDictionary valuesDictionary)
void OnEntityRemoved(Entity entity)
void OnEntityAdded(Entity entity)
static void SwitchScreen(string name, params object[] parameters)
List< ComponentPlayer > m_componentPlayers
override void Load(ValuesDictionary valuesDictionary)
ReadOnlyList< PlayerData > PlayersData
virtual void Update(float dt)
virtual Action< PlayerData > PlayerRemoved
void RemovePlayerData(PlayerData playerData)
override void Save(ValuesDictionary valuesDictionary)
override void OnEntityAdded(Entity entity)
ComponentPlayer FindNearestPlayer(Vector3 position)
ReadOnlyList< ComponentPlayer > ComponentPlayers
bool IsPlayer(Entity entity)
override void OnEntityRemoved(Entity entity)
virtual Action< PlayerData > PlayerAdded
void AddPlayerData(PlayerData playerData)
List< PlayerData > m_playersData
ValuesDictionary ValuesDictionary
static float DistanceSquared(Vector3 v1, Vector3 v2)