Пример #1
0
    void RpcAddComponent(int componentSlotIndex, int componentSpawnIndex, GameObject component)
    {
        //Get component
        ShipComponentAsset componentToAdd = componentAssets[componentSpawnIndex];

        //Get componentArray type from component type
        ShipComponentAsset[] componentArray = GetComponentArrayFromComponentType(componentToAdd.componentType);

        //Assign in component array
        componentArray[componentSlotIndex] = componentAssets[componentSpawnIndex];

        //If component type is large or expansion, has a presence in the world as an object
        if (componentToAdd.componentType == ShipComponentType.Expansion || componentToAdd.componentType == ShipComponentType.Large)
        {
            //Null check for component
            if (component == null)
            {
                return;
            }

            //Get slots for this type of component
            GameObject[] slots = GetSlotArrayFromComponentType(componentToAdd.componentType);
            //Check if slots is null, if not expansion or large component basically
            if (slots == null)
            {
                return;
            }

            //Get the slot from addition index
            GameObject slot = slots[componentSlotIndex];

            //Set transform
            component.transform.parent   = slot.transform;
            component.transform.position = slot.transform.position;
            component.transform.rotation = slot.transform.rotation;
        }
    }
Пример #2
0
    void CmdAddShipComponent(int componentSpawnIndex, int componentSlotIndex)
    {
        ShipComponentAsset componentToAdd = componentAssets[componentSpawnIndex];

        //Null check
        if (componentToAdd == null)
        {
            return;
        }

        //Get relevant aray from type
        ShipComponentAsset[] componentArray = GetComponentArrayFromComponentType(componentToAdd.componentType);

        //Check if there is available space in this array at the specified index
        if (HasAvailableSpaceAtComponentIndex(componentArray, componentSlotIndex))
        {
            //Assign in component array
            componentArray[componentSlotIndex] = componentToAdd;

            GameObject component = null;

            if (componentToAdd.componentType == ShipComponentType.Expansion || componentToAdd.componentType == ShipComponentType.Large)
            {
                GameObject componentPrefab = componentToAdd.componentPrefab;

                //Null check
                if (componentPrefab == null)
                {
                    return;
                }

                //Get slots for this type of component
                GameObject[] slots = GetSlotArrayFromComponentType(componentToAdd.componentType);
                //Check if slots is null, if not expansion or large component basically
                if (slots == null)
                {
                    return;
                }

                //Get the slot from addition index
                GameObject slot = slots[componentSlotIndex];
                //Spawn game object at correct position, rotation and set the parent as relative slot.
                component = Instantiate(componentPrefab, slot.transform.position, slot.transform.rotation);
                component.transform.SetParent(slot.transform);

                //Set the components's parent object (the slot)
                ShipComponent shipComponent = component.GetComponent <ShipComponent>();
                shipComponent.parentNetID = GetComponent <NetworkIdentity>().netId;

                //Setting index for large components
                if (componentToAdd.componentType == ShipComponentType.Large)
                {
                    shipComponent.componentSlotIndex = componentStartIndex + componentSlotIndex + expansionComponentSlots.Length;
                }

                //Setting index for expansion components
                if (componentToAdd.componentType == ShipComponentType.Expansion)
                {
                    shipComponent.componentSlotIndex = componentStartIndex + componentSlotIndex;
                }

                //Spawn on server
                NetworkServer.Spawn(component, connectionToClient);
            }
            //Add component on clients
            RpcAddComponent(componentSlotIndex, componentSpawnIndex, component);
        }
    }