/// <summary>
    /// This method allows to upgrade the base part.
    /// </summary>
    public void Upgrade(string tag)
    {
        Part.AppearanceIndex++;

        gameObject.ChangeAllMaterialsInChildren(Part.Renderers.ToArray(), Part.InitialsRenders);

        Elements.FirstOrDefault(x => !x.gameObject.activeSelf && x.tag == tag).gameObject.SetActive(true);

        PickableController.Instance.TempElements.Remove(tag);

        if (IsCompleted())
        {
            Part.DisableAllTriggers();

            Destroy(Preview);

            for (int i = 0; i < Elements.Length; i++)
            {
                if (Part != null)
                {
                    Elements[i].gameObject.SetActive(true);
                }
            }

            Part.ChangeState(StateType.Placed);

            if (Part.UseConditionalPhysics)
            {
                if (!Part.CheckStability())
                {
                    Part.ApplyPhysics();
                }
            }
        }
    }
        /// <summary>
        /// This method allows to update the destruction preview.
        /// </summary>
        public void UpdateRemovePreview()
        {
            foreach (SocketBehaviour Socket in BuildManager.Instance.Sockets)
            {
                if (Socket != null)
                {
                    Socket.DisableCollider();
                }
            }

            float Distance = OutOfRangeDistance == 0 ? ActionDistance : OutOfRangeDistance;

            if (CurrentRemovePreview != null)
            {
                AreaBehaviour NearestArea = BuildManager.Instance.GetNearestArea(CurrentRemovePreview.transform.position);

                if (NearestArea != null)
                {
                    AllowDestruction = NearestArea.AllowDestruction;
                }
                else
                {
                    AllowDestruction = true;
                }

                CurrentRemovePreview.ChangeState(StateType.Remove);

                AllowPlacement = false;
            }

            if (Physics.Raycast(GetRay(), out RaycastHit Hit, Distance, BuildManager.Instance.FreeLayers))
            {
                PartBehaviour Part = Hit.collider.GetComponentInParent <PartBehaviour>();

                if (Part != null)
                {
                    if (CurrentRemovePreview != null)
                    {
                        if (CurrentRemovePreview.GetInstanceID() != Part.GetInstanceID())
                        {
                            ClearRemovePreview();

                            CurrentRemovePreview = Part;
                        }
                    }
                    else
                    {
                        CurrentRemovePreview = Part;
                    }
                }
                else
                {
                    ClearRemovePreview();
                }
            }
Exemple #3
0
        /// <summary>
        /// This method allows to place a part.
        /// </summary>
        public PartBehaviour PlacePrefab(PartBehaviour part, Vector3 position, Vector3 rotation, Vector3 scale, GroupBehaviour group = null, SocketBehaviour socket = null)
        {
            GameObject PlacedTemp = Instantiate(part.gameObject, position, Quaternion.Euler(rotation));

            PlacedTemp.transform.localScale = scale;

            PartBehaviour PlacedPart = PlacedTemp.GetComponent <PartBehaviour>();

            if (group == null)
            {
                if (socket != null)
                {
                    if (socket.AttachedPart.HasGroup)
                    {
                        PlacedTemp.transform.SetParent(socket.AttachedPart.transform.parent, true);
                    }
                }
                else
                {
                    GameObject Group = new GameObject("Group (" + PlacedPart.GetInstanceID() + ")", typeof(GroupBehaviour));

                    PlacedTemp.transform.SetParent(Group.transform, true);
                }
            }
            else
            {
                PlacedTemp.transform.SetParent(group.transform, true);
            }

            PlacedPart.EntityInstanceId = PlacedPart.GetInstanceID();

            EventHandlers.PlacedPart(PlacedPart, socket);

            PlacedPart.ChangeState(DefaultState);

            return(PlacedPart);
        }
        /// <summary>
        /// This method allows to create a preview.
        /// </summary>
        public virtual PartBehaviour CreatePreview(GameObject prefab)
        {
            if (prefab == null)
            {
                return(null);
            }

            CurrentPreview = Instantiate(prefab).GetComponent <PartBehaviour>();
            CurrentPreview.transform.eulerAngles = Vector3.zero;
            CurrentRotationOffset = Vector3.zero;

            if (Physics.Raycast(GetRay(), out RaycastHit Hit, Mathf.Infinity, BuildManager.Instance.FreeLayers, QueryTriggerInteraction.Ignore))
            {
                CurrentPreview.transform.position = Hit.point;
            }

            CurrentPreview.ChangeState(StateType.Preview);

            SelectedPrefab = prefab.GetComponent <PartBehaviour>();

            if (UsePreviewCamera == true)
            {
                CurrentPreview.gameObject.SetLayerRecursively(PreviewLayer);
            }

            EventHandlers.PreviewCreated(CurrentPreview);

            CurrentSocket = null;

            LastSocket = null;

            AllowPlacement = false;

            HasSocket = false;

            return(CurrentPreview);
        }