public void CmdSplitPlatoon(uint platoonNetId) { NetworkIdentity identity; if (NetworkIdentity.spawned.TryGetValue(platoonNetId, out identity)) { PlatoonBehaviour platoon = identity.gameObject.GetComponent <PlatoonBehaviour>(); // We do not do something like 'while (Units.Count > 0)' // because the RPCs finish executing and hence update the unit count // way after the loop has concluded! int newPlatoonsCount = platoon.Units.Count - 1; while (newPlatoonsCount > 0) { UnitDispatcher u = platoon.Units[newPlatoonsCount]; uint unitNetId = u.GetComponent <NetworkIdentity>().netId; platoon.RpcRemoveUnit(unitNetId); PlatoonBehaviour newPlatoon = PlatoonBehaviour.CreateGhostMode( platoon.Unit, platoon.Owner); GhostPlatoonBehaviour ghostPlatoon = newPlatoon.GhostPlatoon; NetworkServer.Spawn(ghostPlatoon.gameObject); NetworkServer.Spawn(newPlatoon.gameObject); newPlatoon.RpcEstablishReferences( ghostPlatoon.netId, new[] { unitNetId }); newPlatoon.RpcActivate(u.Transform.position); newPlatoonsCount--; } } }
public void CmdSpawnPlatoon( byte playerId, byte categoryId, int unitId, int unitCount, Vector3 spawnPos, Vector3 destinationCenter, float destinationHeading) { Logger.LogNetworking( LogLevel.DEBUG, this, $"Spawning platoon at {spawnPos}"); if (MatchSession.Current.Players.Count > playerId && unitCount >= MIN_PLATOON_SIZE && unitCount <= MAX_PLATOON_SIZE) { PlayerData owner = MatchSession.Current.Players[playerId]; if (categoryId < owner.Deck.Categories.Length && unitId < MatchSession.Current.Armory.Categories[categoryId].Count) { Unit unit = MatchSession.Current.Armory.Categories[categoryId][unitId]; PlatoonBehaviour newPlatoon = PlatoonBehaviour.CreateGhostMode(unit, owner); GhostPlatoonBehaviour ghostPlatoon = newPlatoon.GhostPlatoon; NetworkServer.Spawn(ghostPlatoon.gameObject); NetworkServer.Spawn(newPlatoon.gameObject); uint[] unitIds = new uint[unitCount]; for (int i = 0; i < unitCount; i++) { GameObject unitGO = Instantiate(unit.Prefab); // Any added unit initialization must be done in an RPC, // otherwise it won't show up on the clients! NetworkServer.Spawn(unitGO); unitIds[i] = unitGO.GetComponent <NetworkIdentity>().netId; } newPlatoon.RpcEstablishReferences(ghostPlatoon.netId, unitIds); newPlatoon.RpcInitializeUnits(); ghostPlatoon.RpcSetOrientation(destinationCenter, destinationHeading); newPlatoon.RpcActivate(spawnPos); } else { Debug.LogError("Got bad unit id from a client."); } } else { // Got an invalid player id, server is trying to crash us? Debug.LogError( "Client asked to create a platoon with an invalid player id."); } }
/// <summary> /// Create the smallest platoon allowed. /// </summary> private void StartNewPlatoon() { _smallestPlatoonSize = MIN_PLATOON_SIZE; _newestPlatoon = PlatoonBehaviour.CreateGhostMode(Unit, Owner); for (int i = 0; i < MIN_PLATOON_SIZE; i++) { _newestPlatoon.AddSingleUnit(); } PreviewPlatoons.Add(_newestPlatoon); }
public void CmdSpawnPlatoon( byte playerId, byte unitCategoryId, int unitId, int unitCount, Vector3 spawnPos, Vector3 destinationCenter, float destinationHeading) { Logger.LogNetworking( LogLevel.DEBUG, this, $"Spawning platoon at {spawnPos}"); if (MatchSession.Current.Players.Count > playerId && unitCount >= MIN_PLATOON_SIZE && unitCount <= MAX_PLATOON_SIZE) { PlayerData owner = MatchSession.Current.Players[playerId]; if (unitCategoryId < owner.Deck.Categories.Length && unitId < GameSession.Singleton.Armory.Categories[unitCategoryId].Count) { Unit unit = GameSession.Singleton.Armory.Categories[unitCategoryId][unitId]; Logger.LogNetworking(LogLevel.INFO, $"Spawning a platoon with category = {unitCategoryId}, unit id = {unitId}."); PlatoonBehaviour newPlatoon = PlatoonBehaviour.CreateGhostMode(unit, owner); GhostPlatoonBehaviour ghostPlatoon = newPlatoon.GhostPlatoon; ghostPlatoon.transform.position = destinationCenter; ghostPlatoon.FinalHeading = destinationHeading; NetworkServer.Spawn(ghostPlatoon.gameObject); NetworkServer.Spawn(newPlatoon.gameObject); uint[] unitIds = new uint[unitCount]; for (int i = 0; i < unitCount; i++) { GameObject unitGO = Instantiate(unit.Prefab); // Any added unit initialization must be done in an RPC, // otherwise it won't show up on the clients! NetworkServer.Spawn(unitGO); unitIds[i] = unitGO.GetComponent <NetworkIdentity>().netId; } newPlatoon.RpcEstablishReferences(ghostPlatoon.netId, unitIds); newPlatoon.RpcInitializeUnits(); newPlatoon.RpcActivate(spawnPos); } else { if (unitCategoryId < GameSession.Singleton.Armory.Categories.Length) { Logger.LogNetworking(LogLevel.ERROR, $"Got bad unit id = {unitId} from " + $"the server. Total units = {GameSession.Singleton.Armory.Categories[unitCategoryId].Count} " + $"(category = {unitCategoryId})."); } else { Logger.LogNetworking(LogLevel.ERROR, $"Got bad category id = {unitCategoryId} from " + $"the server. Total categories = {GameSession.Singleton.Armory.Categories.Length}"); } } } else { // Got an invalid player id, client is trying to crash us? Logger.LogNetworking(LogLevel.ERROR, $"Client asked to create a platoon with an invalid player id {playerId}."); } }