// Token: 0x06002B9D RID: 11165
        public static bool TryPlace()
        {
            MultiplayerBuilder.Initialize();
            if (MultiplayerBuilder.prefab == null || !MultiplayerBuilder.canPlace)
            {
                return(false);
            }
            global::Utils.PlayEnvSound(MultiplayerBuilder.placeSound, MultiplayerBuilder.ghostModel.transform.position, 10f);
            ConstructableBase componentInParent = MultiplayerBuilder.ghostModel.GetComponentInParent <ConstructableBase>();

            if (componentInParent != null)
            {
                BaseGhost component = MultiplayerBuilder.ghostModel.GetComponent <BaseGhost>();
                component.GhostBase.transform.position = overridePosition;
                component.Place();

                componentInParent.transform.position = overridePosition;

                component.transform.position = overridePosition;
                component.transform.rotation = overrideQuaternion;
                if (component.TargetBase != null)
                {
                    componentInParent.transform.SetParent(component.TargetBase.transform, true);
                }
                componentInParent.SetState(false, true);

                component.GhostBase.transform.position = overridePosition;

                if (component.TargetBase != null)
                {
                    component.TargetBase.transform.position = overridePosition;
                }
            }
            else
            {
                GameObject gameObject = UnityEngine.Object.Instantiate <GameObject>(MultiplayerBuilder.prefab);
                bool       flag       = false;
                bool       flag2      = false;
                SubRoot    currentSub = Player.main.GetCurrentSub();
                if (currentSub != null)
                {
                    flag  = currentSub.isBase;
                    flag2 = currentSub.isCyclops;
                    gameObject.transform.parent = currentSub.GetModulesRoot();
                }
                else if (MultiplayerBuilder.placementTarget != null && MultiplayerBuilder.allowedOutside)
                {
                    SubRoot componentInParent2 = MultiplayerBuilder.placementTarget.GetComponentInParent <SubRoot>();
                    if (componentInParent2 != null)
                    {
                        gameObject.transform.parent = componentInParent2.GetModulesRoot();
                    }
                }
                Transform expr_138 = gameObject.transform;
                expr_138.position = MultiplayerBuilder.placePosition;
                expr_138.rotation = MultiplayerBuilder.placeRotation;
                Constructable componentInParent3 = gameObject.GetComponentInParent <Constructable>();
                componentInParent3.SetState(false, true);
                global::Utils.SetLayerRecursively(gameObject, LayerMask.NameToLayer((!flag) ? "Interior" : "Default"), true, -1);
                componentInParent3.SetIsInside(flag | flag2);
                SkyEnvironmentChanged.Send(gameObject, currentSub);
                gameObject.transform.position = overridePosition;
                gameObject.transform.rotation = overrideQuaternion;
            }
            MultiplayerBuilder.ghostModel = null;
            MultiplayerBuilder.prefab     = null;
            MultiplayerBuilder.canPlace   = false;
            return(true);
        }
Exemple #2
0
        private void ConstructionCompleted(ConstructionCompletedEvent constructionCompleted)
        {
            GameObject constructing = NitroxEntity.RequireObjectFrom(constructionCompleted.PieceId);

            ConstructableBase constructableBase = constructing.GetComponent <ConstructableBase>();

            // For bases, we need to transfer the GUID off of the ghost and onto the finished piece.
            // Furniture just re-uses the same piece.
            if (constructableBase)
            {
                Int3 latestCell = default(Int3);
                Base latestBase = null;

                // must fetch BEFORE setState or else the BaseGhost gets destroyed
                BaseGhost baseGhost = constructing.GetComponentInChildren <BaseGhost>();

                if (baseGhost)
                {
                    latestCell = baseGhost.TargetOffset;
                    latestBase = baseGhost.TargetBase;
                }

                constructableBase.constructedAmount = 1f;
                constructableBase.SetState(true, true);

                if (latestBase == null)
                {
                    Optional <object> opConstructedBase = TransientLocalObjectManager.Get(TransientObjectType.BASE_GHOST_NEWLY_CONSTRUCTED_BASE_GAMEOBJECT);
                    latestBase = ((GameObject)opConstructedBase.Value).GetComponent <Base>();
                    Validate.NotNull(latestBase, "latestBase can not be null");
                }

                Transform cellTransform = latestBase.GetCellObject(latestCell);

                if (latestCell == default(Int3) || cellTransform == null)
                {
                    latestBase.GetClosestCell(constructing.gameObject.transform.position, out latestCell, out _, out _);
                    cellTransform = latestBase.GetCellObject(latestCell);

                    Validate.NotNull(cellTransform, $"Must have a cell transform, one not found near {constructing.gameObject.transform.position} for latestcell {latestCell}");
                }

                GameObject finishedPiece = null;

                // There can be multiple objects in a cell (such as a corridor with hatces built into it)
                // we look for a object that is able to be deconstucted that hasn't been tagged yet.
                foreach (Transform child in cellTransform)
                {
                    bool isNewBasePiece = (child.GetComponent <NitroxEntity>() == null &&
                                           child.GetComponent <BaseDeconstructable>() != null);

                    if (isNewBasePiece)
                    {
                        finishedPiece = child.gameObject;
                        break;
                    }
                }

                Validate.NotNull(finishedPiece, $"Could not find finished piece in cell {latestCell} when constructing {constructionCompleted.PieceId}");

                Log.Debug($"Construction completed on a base piece: {constructionCompleted.PieceId} {finishedPiece.name}");

                UnityEngine.Object.Destroy(constructableBase.gameObject);
                NitroxEntity.SetNewId(finishedPiece, constructionCompleted.PieceId);

                BasePieceSpawnProcessor customSpawnProcessor = BasePieceSpawnProcessor.From(finishedPiece.GetComponent <BaseDeconstructable>());
                customSpawnProcessor.SpawnPostProcess(latestBase, latestCell, finishedPiece);
            }
            else
            {
                Constructable constructable = constructing.GetComponent <Constructable>();
                constructable.constructedAmount = 1f;
                constructable.SetState(true, true);

                Log.Debug($"Construction completed on a piece of furniture: {constructionCompleted.PieceId} {constructable.gameObject.name}");
            }

            if (constructionCompleted.BaseId != null && !NitroxEntity.GetObjectFrom(constructionCompleted.BaseId).HasValue)
            {
                Log.Debug($"Creating base: {constructionCompleted.BaseId}");
                ConfigureNewlyConstructedBase(constructionCompleted.BaseId);
            }
        }
        // Token: 0x06002B9B RID: 11163 RVA: 0x00103EBC File Offset: 0x001020BC
        private static bool CreateGhost()
        {
            if (MultiplayerBuilder.ghostModel != null)
            {
                return(false);
            }

            Constructable component = MultiplayerBuilder.prefab.GetComponent <Constructable>();

            MultiplayerBuilder.constructableTechType   = component.techType;
            MultiplayerBuilder.placeMinDistance        = component.placeMinDistance;
            MultiplayerBuilder.placeMaxDistance        = component.placeMaxDistance;
            MultiplayerBuilder.placeDefaultDistance    = component.placeDefaultDistance;
            MultiplayerBuilder.allowedSurfaceTypes     = component.allowedSurfaceTypes;
            MultiplayerBuilder.forceUpright            = component.forceUpright;
            MultiplayerBuilder.allowedInSub            = component.allowedInSub;
            MultiplayerBuilder.allowedInBase           = component.allowedInBase;
            MultiplayerBuilder.allowedOutside          = component.allowedOutside;
            MultiplayerBuilder.allowedOnConstructables = component.allowedOnConstructables;
            MultiplayerBuilder.rotationEnabled         = component.rotationEnabled;
            ConstructableBase component2 = MultiplayerBuilder.prefab.GetComponent <ConstructableBase>();

            if (component2 != null)
            {
                GameObject gameObject = UnityEngine.Object.Instantiate <GameObject>(MultiplayerBuilder.prefab);
                component2 = gameObject.GetComponent <ConstructableBase>();
                MultiplayerBuilder.ghostModel = component2.model;
                BaseGhost component3 = MultiplayerBuilder.ghostModel.GetComponent <BaseGhost>();
                component3.SetupGhost();
                MultiplayerBuilder.ghostModelPosition = Vector3.zero;
                MultiplayerBuilder.ghostModelRotation = Quaternion.identity;
                MultiplayerBuilder.ghostModelScale    = Vector3.one;
                MultiplayerBuilder.renderers          = MaterialExtensions.AssignMaterial(MultiplayerBuilder.ghostModel, MultiplayerBuilder.ghostStructureMaterial);
                MaterialExtensions.SetLocalScale(MultiplayerBuilder.renderers);
                MultiplayerBuilder.InitBounds(MultiplayerBuilder.ghostModel);
            }
            else
            {
                MultiplayerBuilder.ghostModel = UnityEngine.Object.Instantiate <GameObject>(component.model);
                MultiplayerBuilder.ghostModel.SetActive(true);
                Transform  component4 = component.GetComponent <Transform>();
                Transform  component5 = component.model.GetComponent <Transform>();
                Quaternion quaternion = Quaternion.Inverse(component4.rotation);
                MultiplayerBuilder.ghostModelPosition = quaternion * (component5.position - component4.position);
                MultiplayerBuilder.ghostModelRotation = quaternion * component5.rotation;
                MultiplayerBuilder.ghostModelScale    = component5.lossyScale;
                Collider[] componentsInChildren = MultiplayerBuilder.ghostModel.GetComponentsInChildren <Collider>();
                for (int i = 0; i < componentsInChildren.Length; i++)
                {
                    UnityEngine.Object.Destroy(componentsInChildren[i]);
                }

                MultiplayerBuilder.renderers = MaterialExtensions.AssignMaterial(MultiplayerBuilder.ghostModel, MultiplayerBuilder.ghostStructureMaterial);
                MaterialExtensions.SetLocalScale(MultiplayerBuilder.renderers);
                MultiplayerBuilder.SetupRenderers(MultiplayerBuilder.ghostModel, Player.main.IsInSub());
                MultiplayerBuilder.CreatePowerPreview(MultiplayerBuilder.constructableTechType, MultiplayerBuilder.ghostModel);
                MultiplayerBuilder.InitBounds(MultiplayerBuilder.prefab);
            }

            return(true);
        }