示例#1
0
        private void ConstructionCompleted(ConstructionCompletedEvent constructionCompleted)
        {
            Log.Info("Constructed completed " + constructionCompleted.PieceId);
            GameObject constructing = NitroxIdentifier.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> opBasePiece   = TransientLocalObjectManager.Get(TransientObjectType.LATEST_CONSTRUCTED_BASE_PIECE);
                GameObject        finishedPiece = (GameObject)opBasePiece.Get();
                UnityEngine.Object.Destroy(constructableBase.gameObject);
                NitroxIdentifier.SetNewId(finishedPiece, constructionCompleted.PieceId);
            }
            else
            {
                Constructable constructable = constructing.GetComponent <Constructable>();
                constructable.constructedAmount = 1f;
                constructable.SetState(true, true);
            }

            if (constructionCompleted.BaseId != null && NitroxIdentifier.GetObjectFrom(constructionCompleted.BaseId).IsEmpty())
            {
                Log.Info("Creating base: " + constructionCompleted.BaseId);
                ConfigureNewlyConstructedBase(constructionCompleted.BaseId);
            }
        }
示例#2
0
        private void DeconstructionBegin(DeconstructionBeginEvent begin)
        {
            GameObject    deconstructing = NitroxIdentifier.RequireObjectFrom(begin.PieceId);
            Constructable constructable  = deconstructing.RequireComponent <Constructable>();

            constructable.SetState(false, false);
        }
示例#3
0
        private void ConfigureNewlyConstructedBase(NitroxId newBaseId)
        {
            Optional <object> opNewlyCreatedBase = TransientLocalObjectManager.Get(TransientLocalObjectManager.TransientObjectType.BASE_GHOST_NEWLY_CONSTRUCTED_BASE_GAMEOBJECT);

            if (opNewlyCreatedBase.IsPresent())
            {
                GameObject newlyCreatedBase = (GameObject)opNewlyCreatedBase.Get();
                NitroxIdentifier.SetNewId(newlyCreatedBase, newBaseId);
            }
            else
            {
                Log.Error("Could not assign new base id as no newly constructed base was found");
            }
        }
示例#4
0
        private void BuildBasePiece(BasePiecePlacedEvent basePiecePlacedBuildEvent)
        {
            Log.Info("BuildBasePiece " + basePiecePlacedBuildEvent.BasePiece.Id + " " + basePiecePlacedBuildEvent.BasePiece.TechType);
            BasePiece  basePiece   = basePiecePlacedBuildEvent.BasePiece;
            GameObject buildPrefab = CraftData.GetBuildPrefab(basePiece.TechType.Enum());

            MultiplayerBuilder.overridePosition           = basePiece.ItemPosition;
            MultiplayerBuilder.overrideQuaternion         = basePiece.Rotation;
            MultiplayerBuilder.overrideTransform          = new GameObject().transform;
            MultiplayerBuilder.overrideTransform.position = basePiece.CameraPosition;
            MultiplayerBuilder.overrideTransform.rotation = basePiece.CameraRotation;
            MultiplayerBuilder.placePosition    = basePiece.ItemPosition;
            MultiplayerBuilder.placeRotation    = basePiece.Rotation;
            MultiplayerBuilder.rotationMetadata = basePiece.RotationMetadata;
            MultiplayerBuilder.Begin(buildPrefab);

            GameObject parentBase = null;

            if (basePiece.ParentId.IsPresent())
            {
                parentBase = NitroxIdentifier.GetObjectFrom(basePiece.ParentId.Get()).OrElse(null);
            }

            Constructable constructable;
            GameObject    gameObject;

            if (basePiece.IsFurniture)
            {
                SubRoot subRoot = (parentBase != null) ? parentBase.RequireComponent <SubRoot>() : null;

                gameObject    = MultiplayerBuilder.TryPlaceFurniture(subRoot);
                constructable = gameObject.RequireComponentInParent <Constructable>();
            }
            else
            {
                constructable = MultiplayerBuilder.TryPlaceBase(parentBase);
                gameObject    = constructable.gameObject;
            }

            NitroxIdentifier.SetNewId(gameObject, basePiece.Id);

            /**
             * Manually call start to initialize the object as we may need to interact with it within the same frame.
             */
            MethodInfo startCrafting = typeof(Constructable).GetMethod("Start", BindingFlags.NonPublic | BindingFlags.Instance);

            Validate.NotNull(startCrafting);
            startCrafting.Invoke(constructable, new object[] { });
        }
示例#5
0
        public static NitroxId GetId(GameObject gameObject)
        {
            NitroxIdentifier identifier = gameObject.GetComponent <NitroxIdentifier>();

            if (identifier != null)
            {
                return(identifier.Id);
            }

            NitroxId newId = new NitroxId();

            SetNewId(gameObject, newId);

            return(newId);
        }
示例#6
0
        /**
         * We allow small bursts of power to Build up so we don't flood with packets.
         * The buffer will be flushed upon reaching the timeout period or the threshold.
         */
        public void Update()
        {
            elapsedTime += Time.deltaTime;

            if (elapsedTime >= interpolationPeriod ||
                runningDelta > POWER_POSITIVE_THRESHOLD_TO_TRIGGER_IMMEDIATE_PACKET ||
                runningDelta < POWER_NEGATIVE_THRESHOLD_TO_TRIGGER_IMMEDIATE_PACKET)
            {
                elapsedTime = 0;

                if (runningDelta != 0)
                {
                    NitroxId id = NitroxIdentifier.GetId(gameObject);
                    powerBoardcaster.ChargeChanged(id, runningDelta, PowerType.ENERGY_INTERFACE);
                    runningDelta = 0;
                }
            }
        }
示例#7
0
        public static void SetNewId(GameObject gameObject, NitroxId id)
        {
            Validate.NotNull(gameObject);
            Validate.NotNull(id);

            NitroxIdentifier identifier = gameObject.GetComponent <NitroxIdentifier>();

            if (identifier != null)
            {
                gameObjectsById.Remove(identifier.Id);
            }
            else
            {
                identifier = gameObject.AddComponent <NitroxIdentifier>();
            }

            identifier.Id       = id;
            gameObjectsById[id] = gameObject;
        }
示例#8
0
        private void ConstructionAmountChanged(ConstructionAmountChangedEvent amountChanged)
        {
            Log.Info("Processing ConstructionAmountChanged " + amountChanged.Id + " " + amountChanged.Amount);

            GameObject          constructing        = NitroxIdentifier.RequireObjectFrom(amountChanged.Id);
            BaseDeconstructable baseDeconstructable = constructing.GetComponent <BaseDeconstructable>();

            // Bases don't  send a deconstruct being packet.  Instead, we just make sure
            // that if we are changing the amount that we set it into deconstruction mode
            // if it still has a BaseDeconstructable object on it.
            if (baseDeconstructable != null)
            {
                baseDeconstructable.Deconstruct();

                // After we have begun the deconstructing for a base piece, we need to transfer the id
                Optional <object> opGhost = TransientLocalObjectManager.Get(TransientObjectType.LATEST_DECONSTRUCTED_BASE_PIECE);

                if (opGhost.IsPresent())
                {
                    GameObject ghost = (GameObject)opGhost.Get();
                    UnityEngine.Object.Destroy(constructing);
                    NitroxIdentifier.SetNewId(ghost, amountChanged.Id);
                }
                else
                {
                    Log.Info("Could not find newly created ghost to set deconstructed id ");
                }
            }
            else
            {
                Constructable constructable = constructing.GetComponentInChildren <Constructable>();
                constructable.constructedAmount = amountChanged.Amount;

                using (packetSender.Suppress <ConstructionAmountChanged>())
                {
                    constructable.Construct();
                }
            }
        }
示例#9
0
        private void DeconstructionCompleted(DeconstructionCompletedEvent completed)
        {
            GameObject deconstructing = NitroxIdentifier.RequireObjectFrom(completed.PieceId);

            UnityEngine.Object.Destroy(deconstructing);
        }
示例#10
0
        private Optional <VehicleMovementData> GetVehicleMovement()
        {
            Vehicle vehicle = Player.main.GetVehicle();
            SubRoot sub     = Player.main.GetCurrentSub();

            NitroxId   id;
            Vector3    position;
            Quaternion rotation;
            Vector3    velocity;
            Vector3    angularVelocity;
            TechType   techType;
            float      steeringWheelYaw = 0f, steeringWheelPitch = 0f;
            bool       appliedThrottle  = false;
            Vector3    leftArmPosition  = new Vector3(0, 0, 0);
            Vector3    rightArmPosition = new Vector3(0, 0, 0);

            if (vehicle != null)
            {
                id       = NitroxIdentifier.GetId(vehicle.gameObject);
                position = vehicle.gameObject.transform.position;
                rotation = vehicle.gameObject.transform.rotation;
                techType = CraftData.GetTechType(vehicle.gameObject);

                Rigidbody rigidbody = vehicle.gameObject.GetComponent <Rigidbody>();

                velocity        = rigidbody.velocity;
                angularVelocity = rigidbody.angularVelocity;

                // Required because vehicle is either a SeaMoth or an Exosuit, both types which can't see the fields either.
                steeringWheelYaw   = (float)vehicle.ReflectionGet <Vehicle, Vehicle>("steeringWheelYaw");
                steeringWheelPitch = (float)vehicle.ReflectionGet <Vehicle, Vehicle>("steeringWheelPitch");

                // Vehicles (or the SeaMoth at least) do not have special throttle animations. Instead, these animations are always playing because the player can't even see them (unlike the cyclops which has cameras).
                // So, we need to hack in and try to figure out when thrust needs to be applied.
                if (vehicle && AvatarInputHandler.main.IsEnabled())
                {
                    if (techType == TechType.Seamoth)
                    {
                        bool flag = vehicle.transform.position.y < Ocean.main.GetOceanLevel() && vehicle.transform.position.y < vehicle.worldForces.waterDepth && !vehicle.precursorOutOfWater;
                        appliedThrottle = flag && GameInput.GetMoveDirection().sqrMagnitude > .1f;
                    }
                    else if (techType == TechType.Exosuit)
                    {
                        Exosuit exosuit = vehicle as Exosuit;
                        if (exosuit)
                        {
                            appliedThrottle = (bool)exosuit.ReflectionGet("_jetsActive") && (float)exosuit.ReflectionGet("thrustPower") > 0f;

                            Transform leftAim  = (Transform)exosuit.ReflectionGet("aimTargetLeft", true);
                            Transform rightAim = (Transform)exosuit.ReflectionGet("aimTargetRight", true);

                            Vector3 eulerAngles = exosuit.transform.eulerAngles;
                            eulerAngles.x = MainCamera.camera.transform.eulerAngles.x;
                            Quaternion quaternion = Quaternion.Euler(eulerAngles.x, eulerAngles.y, eulerAngles.z);

                            leftArmPosition  = leftAim.transform.position = MainCamera.camera.transform.position + quaternion * Vector3.forward * 100f;
                            rightArmPosition = rightAim.transform.position = MainCamera.camera.transform.position + quaternion * Vector3.forward * 100f;
                        }
                    }
                }
            }
            else if (sub != null && Player.main.isPiloting)
            {
                id       = NitroxIdentifier.GetId(sub.gameObject);
                position = sub.gameObject.transform.position;
                rotation = sub.gameObject.transform.rotation;
                Rigidbody rigidbody = sub.GetComponent <Rigidbody>();
                velocity        = rigidbody.velocity;
                angularVelocity = rigidbody.angularVelocity;
                techType        = TechType.Cyclops;

                SubControl subControl = sub.GetComponent <SubControl>();
                steeringWheelYaw   = (float)subControl.ReflectionGet("steeringWheelYaw");
                steeringWheelPitch = (float)subControl.ReflectionGet("steeringWheelPitch");
                appliedThrottle    = subControl.appliedThrottle && (bool)subControl.ReflectionGet("canAccel");
            }
            else
            {
                return(Optional <VehicleMovementData> .Empty());
            }

            VehicleMovementData model = VehicleMovementFactory.GetVehicleMovementData(techType,
                                                                                      id,
                                                                                      position,
                                                                                      rotation,
                                                                                      velocity,
                                                                                      angularVelocity,
                                                                                      steeringWheelYaw,
                                                                                      steeringWheelPitch,
                                                                                      appliedThrottle,
                                                                                      leftArmPosition,
                                                                                      rightArmPosition);

            return(Optional <VehicleMovementData> .Of(model));
        }