Exemple #1
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)
            {
                constructableBase.constructedAmount = 1f;
                constructableBase.SetState(true, true);

                Optional <object> latestBaseOp = TransientLocalObjectManager.Get(TransientObjectType.LATEST_BASE_WITH_NEW_CONSTRUCTION);

                Int3 latestCell;
                Base latestBase;

                if (latestBaseOp.IsPresent())
                {
                    latestCell = TransientLocalObjectManager.Require <Int3>(TransientObjectType.LATEST_BASE_CELL_WITH_NEW_CONSTRUCTION);
                    latestBase = (Base)latestBaseOp.Get();
                }
                else
                {
                    Optional <object> opConstructedBase = TransientLocalObjectManager.Get(TransientObjectType.BASE_GHOST_NEWLY_CONSTRUCTED_BASE_GAMEOBJECT);
                    latestBase = ((GameObject)opConstructedBase.Get()).GetComponent <Base>();
                    Validate.NotNull(latestBase, "latestBase can not be null");

                    Vector3 worldPosition;
                    float   distance;

                    latestBase.GetClosestCell(constructing.gameObject.transform.position, out latestCell, out worldPosition, out distance);
                }

                Transform  cellTransform = latestBase.GetCellObject(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);

                Log.Info("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.Info("Construction completed on a piece of furniture: " + constructionCompleted.PieceId + " " + constructable.gameObject.name);
            }

            if (constructionCompleted.BaseId != null && NitroxEntity.GetObjectFrom(constructionCompleted.BaseId).IsEmpty())
            {
                Log.Info("Creating base: " + constructionCompleted.BaseId);
                ConfigureNewlyConstructedBase(constructionCompleted.BaseId);
            }
        }
Exemple #2
0
        public void ConstructionComplete(GameObject ghost)
        {
            NitroxId          baseId            = null;
            Optional <object> opConstructedBase = TransientLocalObjectManager.Get(TransientObjectType.BASE_GHOST_NEWLY_CONSTRUCTED_BASE_GAMEOBJECT);

            NitroxId id = NitroxEntity.GetId(ghost);

            if (opConstructedBase.IsPresent())
            {
                GameObject constructedBase = (GameObject)opConstructedBase.Get();
                baseId = NitroxEntity.GetId(constructedBase);
            }

            // For base pieces, we must switch the id from the ghost to the newly constructed piece.
            // Furniture just uses the same game object as the ghost for the final product.
            if (ghost.GetComponent <ConstructableBase>() != null)
            {
                Optional <object> latestBaseOp = TransientLocalObjectManager.Get(TransientObjectType.LATEST_BASE_WITH_NEW_CONSTRUCTION);

                Int3 latestCell;
                Base latestBase;

                if (latestBaseOp.IsPresent())
                {
                    latestCell = TransientLocalObjectManager.Require <Int3>(TransientObjectType.LATEST_BASE_CELL_WITH_NEW_CONSTRUCTION);
                    latestBase = (Base)latestBaseOp.Get();
                }
                else
                {
                    latestBase = ((GameObject)opConstructedBase.Get()).GetComponent <Base>();
                    Vector3 worldPosition;
                    float   distance;

                    latestBase.GetClosestCell(ghost.transform.position, out latestCell, out worldPosition, out distance);
                }

                Transform  cellTransform = latestBase.GetCellObject(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);

                Log.Info("Setting id to finished piece: " + finishedPiece.name + " " + id);

                UnityEngine.Object.Destroy(ghost);
                NitroxEntity.SetNewId(finishedPiece, id);

                BasePieceSpawnProcessor customSpawnProcessor = BasePieceSpawnProcessor.From(finishedPiece.GetComponent <BaseDeconstructable>());
                customSpawnProcessor.SpawnPostProcess(latestBase, latestCell, finishedPiece);
            }

            Log.Info("Construction Completed " + id);

            ConstructionCompleted constructionCompleted = new ConstructionCompleted(id, baseId);

            packetSender.Send(constructionCompleted);
        }