12 protected ResourceName m_DefaultPlayer;
14 protected ref map<int, SCR_EditableEntityComponent> m_MainEntities;
15 protected ref map<int, SCR_EditableEntityComponent> m_PossessedEntities;
17 protected bool m_LocalPlayerHasSpawnedOnce =
false;
19 protected ref ScriptInvoker Event_OnDeath =
new ScriptInvoker;
20 protected ref ScriptInvoker Event_OnSpawn =
new ScriptInvoker;
21 protected ref ScriptInvoker Event_OnPossessed =
new ScriptInvoker;
22 protected ref ScriptInvoker Event_OnConnected =
new ScriptInvoker;
23 protected ref ScriptInvoker Event_OnDisconnected =
new ScriptInvoker;
51 return m_PossessedEntities.GetKeyByValue(entity);
55 return m_PossessedEntities.GetKeyByValue(entity) != 0;
62 int GetPlayers(notnull map<int, SCR_EditableEntityComponent> players)
81 void TeleportPlayerToPosition(vector
position)
84 if (statesManager && statesManager.GetState() !=
EEditorState.SELECTING)
87 Rpc(TeleportPlayerToPositionServer,
position);
90 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
91 void TeleportPlayerToPositionServer(vector
position)
95 if (!editorManager || !editorManager.IsOpened())
98 IEntity player = SCR_PossessingManagerComponent.GetPlayerMainEntity(editorManager.GetPlayerID());
102 CompartmentAccessComponent compartmentAccess = CompartmentAccessComponent.Cast(player.FindComponent(CompartmentAccessComponent));
103 if (compartmentAccess && compartmentAccess.IsInCompartment())
104 TeleportPlayerToPositionOwner(
position);
106 Rpc(TeleportPlayerToPositionOwner,
position);
109 [
RplRpc(RplChannel.Reliable, RplRcver.Owner)]
110 void TeleportPlayerToPositionOwner(vector
position)
119 bool HasLocalPlayerSpawnedOnce()
121 return m_LocalPlayerHasSpawnedOnce;
128 ScriptInvoker GetOnDeath()
130 return Event_OnDeath;
136 ScriptInvoker GetOnSpawn()
138 return Event_OnSpawn;
144 ScriptInvoker GetOnPossessed()
152 ScriptInvoker GetOnConnected()
154 return Event_OnConnected;
160 ScriptInvoker GetOnDisconnected()
162 return Event_OnDisconnected;
171 Print(
"--------------------------------------------------", LogLevel.DEBUG);
172 Print(
string.Format(
"--- Players (%2)",
m_MainEntities.Count()), LogLevel.DEBUG);
175 Print(
"Player ID: " +
m_MainEntities.GetKey(i).ToString(), LogLevel.DEBUG);
180 entity.Log(
"",
true);
184 Print(
"Entity not found!", LogLevel.WARNING);
187 Print(
"--------------------------------------------------", LogLevel.DEBUG);
190 protected void OnSpawnServer(
int playerID, IEntity controlledEntity)
196 UpdatePlayerFaction(entity, controlledEntity);
197 UpdatePlayerGroup(entity, controlledEntity, playerID);
199 Rpc(OnSpawnOwner, playerID, Replication.FindId(entity));
202 protected void UpdatePlayerGroup(notnull
SCR_EditableEntityComponent editableEntity, notnull IEntity controlledEntity,
int playerID)
208 Faction entityFaction = editableEntity.GetFaction();
209 SCR_AIGroup group = groupManager.GetPlayerGroup(playerID);
210 if (group && group.GetFaction() == entityFaction)
213 SCR_AIGroup foundGroup = groupManager.GetFirstNotFullForFaction(entityFaction,
null,
true);
215 foundGroup = groupManager.CreateNewPlayableGroup(entityFaction);
217 foundGroup.AddPlayer(playerID);
222 FactionAffiliationComponent factionAffiliaton = FactionAffiliationComponent.Cast(controlledEntity.FindComponent(FactionAffiliationComponent));
223 if (!factionAffiliaton)
226 factionAffiliaton.SetAffiliatedFaction(editableEntity.GetFaction());
229 [
RplRpc(RplChannel.Reliable, RplRcver.Owner)]
230 protected void OnSpawnOwner(
int playerID,
int entityID)
240 if (entity == entityPrev)
return;
244 m_LocalPlayerHasSpawnedOnce =
true;
248 Event_OnSpawn.Invoke(playerID, entity, entityPrev);
250 protected void OnPossessedServer(
int playerID, IEntity controlledEntity, IEntity mainEntity,
bool isPossessing)
255 Rpc(OnPossessedOwner, playerID, isPossessing, Replication.FindId(entity));
257 [
RplRpc(RplChannel.Reliable, RplRcver.Owner)]
258 protected void OnPossessedOwner(
int playerID,
bool isPossessing,
int entityID)
264 m_PossessedEntities.Insert(playerID, entity);
266 m_PossessedEntities.Remove(playerID);
270 protected void OnDeathServer(
int playerId, IEntity playerEntity, IEntity killerEntity, notnull
Instigator killer)
277 Rpc(OnDeathOwner, playerId, Replication.FindId(entity), Replication.FindId(killerEditableEntity));
279 [
RplRpc(RplChannel.Reliable, RplRcver.Owner)]
280 protected void OnDeathOwner(
int playerID,
int entityID,
int killerID)
287 Event_OnDeath.Invoke(playerID, entity, killerEntity);
289 protected void OnConnectedServer(
int playerID)
291 Rpc(OnConnectedOwner, playerID);
293 [
RplRpc(RplChannel.Reliable, RplRcver.Owner)]
294 protected void OnConnectedOwner(
int playerID)
297 Event_OnConnected.Invoke(playerID);
299 protected void OnDisconnectedServer(
int playerID)
301 if (!Replication.IsRunning())
return;
307 if (!manager)
return;
310 if (playerID != manager.GetPlayerID())
311 Rpc(OnDisconnectedOwner, playerID);
313 [
RplRpc(RplChannel.Reliable, RplRcver.Owner)]
314 protected void OnDisconnectedOwner(
int playerID)
317 Event_OnDisconnected.Invoke(playerID);
319 protected void StopPosessing()
326 if (playerController)
327 playerController.SetPossessedEntity(
null);
329 protected void OnLimitedChange(
bool isLimited)
337 int playerID = entity.GetPlayerID();
341 Event_OnSpawn.Invoke(playerID, entity,
null);
344 protected void SetServerHandler(
bool toAdd)
346 if (RplSession.Mode() == RplMode.Client)
return;
352 SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
356 gameMode.GetOnPlayerSpawned().Insert(OnSpawnServer);
357 gameMode.GetOnPlayerKilled().Insert(OnDeathServer);
358 gameMode.GetOnPlayerConnected().Insert(OnConnectedServer);
359 gameMode.GetOnPlayerDisconnected().Insert(OnDisconnectedServer);
361 if (possessingManager)
362 possessingManager.GetOnPossessed().Insert(OnPossessedServer);
366 gameMode.GetOnPlayerSpawned().Remove(OnSpawnServer);
367 gameMode.GetOnPlayerKilled().Remove(OnDeathServer);
368 gameMode.GetOnPlayerConnected().Remove(OnConnectedServer);
369 gameMode.GetOnPlayerDisconnected().Remove(OnDisconnectedServer);
371 if (possessingManager)
372 possessingManager.GetOnPossessed().Remove(OnPossessedServer);
375 override void EOnEditorOpen()
378 m_PossessedEntities =
new map<int, SCR_EditableEntityComponent>;
380 SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
383 array<int> playerIDs =
new array<int>;
384 GetGame().GetPlayerManager().GetPlayers(playerIDs);
387 foreach (
auto playerID: playerIDs)
389 owner = SCR_PossessingManagerComponent.GetPlayerMainEntity(playerID);
393 if (possessingManager && possessingManager.IsPossessing(playerID))
395 owner =
GetGame().GetPlayerManager().GetPlayerControlledEntity(playerID);
398 m_PossessedEntities.Insert(playerID, entity);
403 core.Event_OnEntityRegistered.Insert(OnEntityRegistered);
405 override void EOnEditorClose()
408 m_PossessedEntities =
null;
411 core.Event_OnEntityRegistered.Remove(OnEntityRegistered);
414 override void EOnEditorOpenServerCallback()
416 SetServerHandler(
true);
421 override void EOnEditorCloseServer()
423 SetServerHandler(
false);
425 override void EOnEditorInit()
427 super.EOnEditorInit();
430 m_LocalPlayerHasSpawnedOnce =
true;
432 override void EOnEditorInitServer()
435 GetManager().GetOnModeChange().Insert(StopPosessing);
436 GetManager().GetOnLimitedChange().Insert(OnLimitedChange);
438 override void EOnEditorDeleteServer()
442 GetManager().GetOnModeChange().Remove(StopPosessing);
443 GetManager().GetOnLimitedChange().Remove(OnLimitedChange);