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.");
            }
        }
Пример #2
0
        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}.");
            }
        }