Пример #1
0
        public override void OnStart()
        {
            base.OnStart();

            var mountsLookup = GetComponent <MountsManager>();

            if (mountsLookup)
            {
                if (mountsLookup.mountIdLookup.ContainsKey(defaultMounting.id))
                {
                    DefaultMount = mountsLookup.mountIdLookup[defaultMounting.id];
                }
                else
                {
                    Debug.LogWarning("Sync Vitals has a Default Mount setting of "
                                     + MountSettings.GetName(defaultMounting.id) +
                                     " but no such mount is defined yet on GameObject: '" + name + "'. Root mount will be used as a failsafe.");

                    /// Invalid default mounting (doesn't exist)... warn and set to Root
                    defaultMounting.id = 0;
                    DefaultMount       = mountsLookup.mountIdLookup[0];
                }
            }
        }
Пример #2
0
 public void OnStateChange(ObjState newState, ObjState previousState, Transform attachmentTransform, Mount attachTo = null, bool isReady = true)
 {
     if (previousState == ObjState.Despawned && (newState & ObjState.Visible) != 0)
     {
         _charges = initialCharges;
     }
 }
Пример #3
0
        protected override Consumption ProcessContactEvent(ContactEvent contactEvent)
        {
            //Debug.Log("Process " + contactEvent + " --  " + (contactEvent.contactSystem as IVitalsSystem));

            //double consumed;

            var system = (contactEvent.contactSystem as IVitalsSystem);

            if (system == null)
            {
                return(Consumption.None);
            }

            double value = GetValueForTriggerType(contactEvent.contactType);

            //if (vitalNameType.type == VitalType.None)
            //{
            //    consumed = system.Vitals.ApplyCharges(vitalNameType, value, allowOverload, propagate);
            //}
            //else
            //{
            //    Vital vital = system.Vitals.GetVital(vitalNameType);

            //    if (ReferenceEquals(vital, null))
            //    {
            //        Debug.LogWarning("No matching Vital found.");
            //        return Consumption.None;
            //    }

            //    //consumed = vital.ApplyChange(value, this);
            //}

            double consumed = system.Vitals.ApplyCharges(vitalNameType, value, allowOverload, propagate);

            Consumption consumption;

            if (useCharges)
            {
                consumption = ConsumeCharges(consumed);
            }
            else if (consumed != 0)
            {
                if (consumed == value)
                {
                    consumption = Consumption.All;
                }
                else
                {
                    consumption = Consumption.Partial;
                }

                Consume(consumption);
            }
            else
            {
                //Debug.LogWarning("Reactor not consumed.");
                return(Consumption.None);
            }


            ///// TEST - Notify other component of pickup condition
            //var onContact = transform.GetComponent<IOnPickup>();
            //Debug.Log("POST 2 " + (onContact != null));

            //if (onContact != null)
            //    syncState.HardMount(onContact.OnPickup(contactEvent));

            if (isPickup && consumption != Consumption.None)
            {
                Mount mount = system.TryPickup(this, contactEvent);
                if (mount)
                {
                    syncState.HardMount(mount);
                }
            }

            return(consumption);
        }
Пример #4
0
 public void OnStateChange(ObjState newState, ObjState previousState, Transform attachmentTransform, Mount attachTo = null, bool isReady = true)
 {
     /// Detect respawn (change from despawned to any other state currently) and reset values when that occurs.
     if (wasDespawned && newState != ObjState.Despawned)
     {
         for (int i = 0; i < vitalsCount; ++i)
         {
             vitalArray[i].ResetValues();
         }
     }
     wasDespawned = newState == ObjState.Despawned;
 }
Пример #5
0
 /// <summary>
 /// Modify StateChange call if the mount was invalid (Mount likely destroyed).
 /// </summary>
 protected virtual void InvalidMountHandler(ObjState newState, Mount newMount, bool force)
 {
     Debug.LogWarning("Invalid Mount Handled!!");
     ChangeState(new StateChangeInfo(ObjState.Visible, null, true));
 }
Пример #6
0
        public void Drop(Mount newMount, bool force = false)
        {
            const ObjState state = ObjState.Visible | ObjState.Dropped;

            stateChangeQueue.Enqueue(new StateChangeInfo(state, newMount, force));
        }
Пример #7
0
        /// <summary>
        /// Call this method to change the state of this object. This state will be synced over the network,
        /// and callbacks will trigger locally and remotely. Typically it is preferred to call QueueStateChange(),
        /// which will defer the ChangeState application until the appropriate timing.
        /// </summary>
        protected virtual void ChangeState(StateChangeInfo stateChangeInfo)
        {
            if (!gameObject)
            {
                Debug.LogWarning(name + " has been destroyed. Will not try to change state.");
                return;
            }

            //if (photonView.IsMine && iSpawnController != null && iSpawnController.SupressStateMask != ~ObjState.Despawned)
            //{
            //    Debug.Log("<color=red>Suppressing State change due to spawn timer</color>");
            //    stateChangeInfo.objState &= iSpawnController.SupressStateMask;
            //}


            //if (GetComponent<SyncPickup>())
            //    Debug.LogError(Time.time + " " + name + " <b>ChangeState " + stateChangeInfo + "</b> par: " + currentMount + " tr: " + transform.position + " rb: " + Rb.position);

            var oldState = currentState.state;
            var oldMount = currentMount;
            var newState = stateChangeInfo.objState;
            var newMount = stateChangeInfo.mount;

            bool respawn;

            /// Assuming first Visible after a despawn is a Respawn - this is here to handle lost teleport packets
            if (autoReset && oldState == ObjState.Despawned && newState != ObjState.Despawned && (newState & ObjState.Anchored) == 0)
            {
                stateChangeInfo = new StateChangeInfo(respawnStateInfo)
                {
                    objState = stateChangeInfo.objState
                };
                respawn = true;
            }
            else
            {
                respawn = false;
            }

            var force = stateChangeInfo.force;

            bool stateChanged = newState != oldState;
            bool mountChanged = oldMount != newMount;

            var prevParent = transform.parent;


            /// Test nothing has changed
            if (!force && !stateChanged && !mountChanged)
            {
                //if (GetComponent<SyncPickup>())
                //    Debug.LogError("NO CHANGE");
                return;
            }


            currentState.state = newState;
            var prevMount = currentMount;

            currentMount = newMount;

            //if (GetComponent<SyncPickup>())
            //    Debug.LogError("<b>ChangeState </b>" + currentState + " par: " + currentMount);

            bool nowMounted         = (newState & ObjState.Mounted) != 0;
            bool mountInfoWasCulled = nowMounted && ReferenceEquals(newMount, null);

            // TODO: Might not be needed.
            /// Handling for attached without a valid Mount - Test for if we have a reference to a Mount, but it is null (Unity destroyed). Sets to hard null and reruns this method.
            if (nowMounted && !mountInfoWasCulled && newMount == null)
            {
                Debug.LogError("Invalid Mount!");
                InvalidMountHandler(newState, newMount, force);
                return;
            }

            if (IsMine)
            {
                if (mountChanged || respawn)
                {
                    //Debug.LogError("TELEPORT");
                    for (int i = 0; i < flagTeleportCallbacks.Count; ++i)
                    {
                        flagTeleportCallbacks[i].FlagTeleport();
                    }
                }
            }

            /// The State is mounted
            /*else*/
            if (mountChanged)
            {
                /// Attaching to a mount
                // If Attached bit is true
                if (nowMounted)
                {
#if PUN_2_OR_NEWER
                    //if (GetComponent<SyncPickup>() /*&& newState == (ObjState)(15)*/)
                    //    Debug.LogError(Time.time + " " + name + " " + newState + " ATTACH " + newMount.name + " ");

                    currentState.mountToViewID = newMount.ViewID;
                    currentState.mountTypeId   = newMount.mountType.id;
#endif
                    transform.parent = newMount.transform;

                    // If anchor bit is true
                    if ((newState & ObjState.AnchoredPosition) != 0)
                    {
                        transform.localPosition = new Vector3();
                        //Debug.LogError("<b>Anchor Pos</b> " + transform.parent.name + " " + transform.localPosition);
                    }

                    // If anchor bit is true
                    if ((newState & ObjState.AnchoredRotation) != 0)
                    {
                        transform.localRotation = new Quaternion();
                        //Debug.Log("<b>Anchor Rot</b>");
                    }
                }

                /// Detaching from a mount
                else
                {
                    //if (GetComponent<SyncPickup>() /*&& newState == (ObjState)(15)*/)
                    //    Debug.Log(Time.time + " " + name + " " + newState + " DETTACH " + (newMount ? newMount.name : " null"));

                    currentState.mountToViewID = null;
                    currentState.mountTypeId   = null;
                    transform.parent           = null;
                }

                // TODO: Add handling for dismounting returning ownership to scene?
                // Ownership changes are deferred, to let the current tick finish processing before changing Owner/Controller.
                if (autoOwnerChange && newMount && !ReferenceEquals(oldMount, newMount))
                {
                    ChangeOwnerToParentMountsOwner();
                }
                Mount.ChangeMounting(this, prevMount, newMount);
            }

            var pos = stateChangeInfo.offsetPos;
            var rot = stateChangeInfo.offsetRot;
            var vel = stateChangeInfo.velocity;

            if (rot.HasValue)
            {
                //if (GetComponent<SyncPickup>())
                //    Debug.Log(Time.time + " " + name + " STATE ROT APPLY " + rot.Value);

                transform.rotation = rot.Value;
            }

            // TODO: Move pos/rot handling to virtual method
            if (pos.HasValue)
            {
                //if (GetComponent<SyncPickup>())
                //    Debug.Log(Time.time + " " + name + " STATE POS APPLY " + pos.Value);

                transform.position = pos.Value;
            }

            if (vel.HasValue)
            {
                var rb = netObj.Rb;
                if (rb)
                {
                    rb.velocity = vel.Value;
                }
                else
                {
                    var rb2d = netObj.Rb2D;

                    if (rb2d)
                    {
                        rb2d.velocity = vel.Value;
                    }
                }
            }

            ///// Apply the vector values
            //this.ApplyVectors(stateChangeInfo, prevParent, onTeleportCallbacks);

            //Debug.Log(Time.time + " " + stateChangeInfo + " mountisactive? " + (currentMount ? currentMount.isActiveAndEnabled.ToString() : "null"));

            if (mountChanged || stateChanged || force)
            {
                //if (GetComponent<SyncPickup>())
                //    Debug.Log(Time.time + " " + name +  " " + photonView.OwnerActorNr + " <b>NEW STATE: " + newState + "</b> ready? " + netObj.AllObjsAreReady);

                /// Send out callbacks
                for (int i = 0; i < onStateChangeCallbacks.Count; ++i)
                {
                    onStateChangeCallbacks[i].OnStateChange(newState, oldState, transform, currentMount, netObjIsReady);
                }
            }
        }
Пример #8
0
        public static void DrawAllMountMappings(Mount thismount, MountsManager mountsLookup)
        {
            EnsureStylesExists();

            int usedmask = 0;

            int cnt = mountsLookup.indexedMounts.Count;

            EditorGUILayout.LabelField("All Mounts On NetObject", (GUIStyle)"BoldLabel");

            /// Massive warning if too many mounts are on the object, and the last ones will not get serialized correctly.
            if (mountsLookup.indexedMounts.Count > MountSettings.mountTypeCount)
            {
                EditorGUILayout.HelpBox("NetObject has too many mounts. Mount count is limited to the count value in MountSettings.", MessageType.Error);
            }

            if (cnt > 0)
            {
                EditorGUILayout.BeginVertical((GUIStyle)"HelpBox");


                for (int i = 0; i < mountsLookup.indexedMounts.Count; ++i)
                {
                    SerializedObject mount = new SerializedObject(mountsLookup.indexedMounts[i]);


                    var mountType = mount.FindProperty("mountType");
                    var index     = mountType.FindPropertyRelative("id");

                    /// Make sure Root is selected for the first mount, and only the first mount
                    if (i == 0)
                    {
                        if (index.intValue != 0)
                        {
                            index.intValue = 0;
                            mount.ApplyModifiedProperties();
                        }
                    }
                    /// Switch off Root for all other mounts
                    else if (index.intValue == 0)
                    {
                        index.intValue = i;
                        mount.ApplyModifiedProperties();
                        Debug.LogWarning("Only the first root Mount on a NetOjbect can be set to mountType 'Root'");
                    }

                    int idx = index.intValue;

                    /// Watch for repeats
                    int  idxmask       = 1 << idx;
                    bool isAlreadyUsed = (idxmask & usedmask) != 0;
                    usedmask |= idxmask;

                    bool      isthis    = (ReferenceEquals(mount.targetObject, thismount));
                    const int CNT_WIDTH = 30;
                    const int LABL_PAD  = 2;

                    /// Start drawing the row
                    EditorGUILayout.BeginHorizontal(isthis ? mountHiliteStyle : mountNoliteStyle);

                    /// Get the indented rect
                    Rect indexrect = EditorGUILayout.GetControlRect(GUILayout.MaxWidth(CNT_WIDTH), GUILayout.MinWidth(CNT_WIDTH));
                    indexrect.xMin += 8;

                    /// Warning Icon
                    if (isAlreadyUsed)
                    {
                        EditorGUI.LabelField(new Rect(indexrect)
                        {
                            xMin = indexrect.xMin - 3
                        }, new GUIContent(EditorGUIUtility.FindTexture("CollabError"), "More than one Mount has the same compatible mount selection. Only the first will be used."));
                    }
                    else
                    {
                        /// Index
                        EditorGUI.BeginDisabledGroup(!isthis);
                        EditorGUI.LabelField(indexrect, i.ToString(), mountlabelstyle);
                        EditorGUI.EndDisabledGroup();
                    }

                    /// Mount Selector
                    float selectorWidth = EditorGUIUtility.labelWidth - (CNT_WIDTH + LABL_PAD + 8);
                    EditorGUI.BeginDisabledGroup(i == 0);
                    EditorGUILayout.PropertyField(mountType, GUIContent.none, GUILayout.MaxWidth(selectorWidth), GUILayout.MinWidth(selectorWidth));
                    EditorGUI.EndDisabledGroup();


                    if (isthis)
                    {
                        /// Object
                        Rect thisrect = EditorGUILayout.GetControlRect(GUILayout.MinWidth(40));
                        thisrect.xMin += 16;
                        EditorGUI.LabelField(thisrect, "This", mountlabelstyle);
                    }
                    else
                    {
                        EditorGUI.BeginDisabledGroup(!isthis);
                        EditorGUILayout.ObjectField(mountsLookup.indexedMounts[i], typeof(Mount), false /*, GUILayout.MaxWidth(EditorGUIUtility.labelWidth - cntwidth), GUILayout.MinWidth(EditorGUIUtility.labelWidth - cntwidth)*/);
                        EditorGUI.EndDisabledGroup();
                    }

                    EditorGUILayout.EndHorizontal();
                }

                EditorGUILayout.EndVertical();
            }
        }
Пример #9
0
        public void OnStateChange(ObjState newState, ObjState previousState, Transform pickup, Mount attachedTo = null, bool isReady = true)
        {
            //Debug.Log(transform.root.name + ":" + name + " " + photonView.ControllerActorNr + " " + state + " " + attachedTo);
#if UNITY_EDITOR
            currentState = newState;
#endif
            if (!isReady)
            {
                //Debug.Log(transform.root.name + ":" + name + " not ready!");
                show = false;
            }
            else
            {
                bool match = stateLogic.Evaluate((int)newState);

                if (match)
                {
                    show = true;

                    /// If there is no object to attach to yet (due to keyframes) we need to keep this invisible.
                    if (reactToAttached)
                    {
                        if (attachedTo == null && (newState & ObjState.Mounted) != 0)
                        {
                            show = false;
                        }
                    }
                }
                else
                {
                    show = false;
                }
            }

            //if (!photonView.IsMine)
            //    Debug.Log(photonView.ViewID + " " + photonView.name + " : " + GetType().Name + " "
            //        + state +  " show: " + show + " match: " + stateLogic.Evaluate((int)state));
            DeferredEnable();
            //NetMasterCallbacks.postCallbackActions.Enqueue(DeferredEnable);
        }
 public void OnStateChange(ObjState newState, ObjState previousState, Transform attachmentTransform, Mount attachTo = null, bool isReady = true)
 {
     if (IsMine)
     {
         SetOwnedKinematics(newState);
     }
 }
Пример #11
0
        //protected ObjState prevState = ObjState.Despawned;
        /// <summary>
        /// Responds to State change from SyncState
        /// </summary>
        public void OnStateChange(ObjState newState, ObjState previousState, Transform attachmentTransform, Mount attachTo = null, bool isReady = true)
        {
            if (newState == previousState)
            {
                return;
            }

            //if (IsMine)
            {
                if (/*hadInitialSpawn && */ respawnEnable)
                {
                    if (newState == ObjState.Despawned)
                    {
                        //Debug.Log(Time.time + " " + name + " " + photonView.OwnerActorNr + " <b>Despawned State Chage</b>");

                        ticksUntilRespawn = respawnWaitAsTicks;
                    }
                    /// Check if the flag we are looking for just changed to true
                    else if ((previousState & respawnOn) == 0 && (newState & respawnOn) != 0)
                    {
                        Debug.Log(Time.time + " " + name + " " + photonView.OwnerActorNr + " <b>Reset </b> " + previousState + " <> " + newState);
                        ticksUntilRespawn = respawnWaitAsTicks;
                    }
                }

                if (despawnEnable)
                {
                    /// Check if the flag we are looking for just changed to true
                    if ((previousState & despawnOn) == 0 && (newState & despawnOn) != 0)
                    {
                        ticksUntilDespawn = despawnWaitAsTicks;
                    }
                }
            }


            //prevState = state;
        }
Пример #12
0
 public void OnStateChange(ObjState newState, ObjState previousState, Transform attachmentTransform, Mount attachTo = null, bool isReady = true)
 {
     // TODO: this is test code. Might need to be more selective about clearing this on state changes.
     if (preventRepeats)
     {
         this.triggeringEnters.Clear();
     }
 }