public void Instantiate(Part p, PrimitiveType colliderType)
        {
            Debug.Log("# Creating internal collider " + Name + " for part " + p);
            if (IvaGameObject != null)
            {
                // TODO: Prevents multiple copies of the same part from having colliders.
                Debug.LogError("[FreeIVA] InternalCollider " + Name + " has already been instantiated.");
                return;
            }

            // These values will be cleared on creating the object.
            Vector3    scale         = Scale;
            Vector3    localPosition = LocalPosition;
            Quaternion rotation      = Rotation;

            IvaGameObject = GameObject.CreatePrimitive(colliderType);
            IvaGameObject.GetComponentCached(ref IvaGameObjectCollider).enabled = true;
            //IvaGameObject.collider.isTrigger = true;
            if (p.internalModel == null)
            {
                Debug.Log($"# Creating blank InternalModel for {p.name}");
                p.internalModel = new InternalModel();
            }

            if (p.internalModel != null)
            {
                IvaGameObject.transform.parent = p.internalModel.transform;
            }
            IvaGameObject.layer = (int)Layers.InternalSpace;
            IvaGameObject.transform.localScale    = scale;
            IvaGameObject.transform.localPosition = localPosition;
            IvaGameObject.transform.localRotation = rotation;
            IvaGameObject.name = Name;
            PhysicMaterial physMat = IvaGameObjectCollider.material;

            physMat.bounciness = 0;
            //FixedJoint joint = IvaGameObject.AddComponent<FixedJoint>();
            //joint.connectedBody = p.collider.rigidbody;
            if (IvaGameObject.GetComponent <Rigidbody>() == null)
            {
                Rigidbody rb = IvaGameObject.AddComponent <Rigidbody>();
                rb.useGravity = false;

                rb.constraints = RigidbodyConstraints.FreezeAll;
            }

            if (AlwaysVisible)
            {
                // Change the colour from the default white.
                IvaGameObjectRenderer.material.color = Color.grey;
            }
            else
            {
                IvaGameObjectRenderer.enabled = false;
            }
        }
Exemple #2
0
        //public override void OnLoad(ConfigNode node)
        //{
        //    Hatch h = LoadFromCfg(node);
        //    Instantiate(this.part);
        //}
        public override void Instantiate(Part p)
        {
            Part = p;
            Debug.Log("# Creating hatch for part " + p);
            if (IvaGameObject != null)
            {
                Debug.LogError("[FreeIVA] Hatch has already been instantiated.");
                return;
            }

            // These values will be cleared on creating the object.
            Vector3    scale         = Scale;
            Vector3    localPosition = LocalPosition;
            Quaternion rotation      = Rotation;

            IvaGameObject = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
            UnityEngine.Object.Destroy(IvaGameObject.GetComponentCached(ref IvaGameObjectCollider));
            if (p.internalModel == null)
            {
                p.CreateInternalModel(); // TODO: Detect this in an event instead.
            }
            IvaGameObject.transform.parent = p.internalModel.transform;
            IvaGameObject.layer            = (int)Layers.InternalSpace;

            // Restore cleared values.
            Scale         = scale;
            LocalPosition = localPosition;
            Rotation      = rotation;
            IvaGameObject.transform.localScale    = scale;
            IvaGameObject.transform.localPosition = localPosition;
            IvaGameObject.transform.localRotation = rotation;
            IvaGameObject.name = Name;
            if (Collider != null)
            {
                Collider.Instantiate(p);
                ModuleFreeIva mfi = p.GetModule <ModuleFreeIva>();
                if (mfi != null)
                {
                    mfi.InternalColliders.Add(Collider);
                }
            }

            Shader depthMask = Utils.GetDepthMask();

            if (depthMask != null)
            {
                IvaGameObjectRenderer.material.shader = depthMask;
            }
            IvaGameObjectRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;

            ChangeMesh(IvaGameObject);
            SetupAudio();
            //IvaGameObject.renderer.enabled = false; Gets reenabled by EnableInternals.
        }
 public void Enable(bool enabled)
 {
     if (IvaGameObject != null)
     {
         IvaGameObjectCollider = IvaGameObject.GetComponent <Collider>(); // Why get this every time?
     }
     if (IvaGameObjectCollider != null)
     {
         IvaGameObjectCollider.enabled = enabled;
     }
 }
        //public override void Open(bool open)
        public void Open(bool open)
        {
            if (open && _isLocked)
            {
                ScreenMessages.PostScreenMessage("Hatch is locked", 1f, ScreenMessageStyle.LOWER_CENTER);
                return;
            }

            if (IvaGameObject != null)
            {
                Renderer r = IvaGameObject.GetComponentCached <Renderer>(ref depthMaskRenderer);
                if (r != null)
                {
                    r.enabled = open;
                }
            }

            AnimationState animationState = unlockAnimation[openAnimationName];

            if (open ^ animationState.speed > 0)
            {
                animationState.speed = -animationState.speed;
            }
            if (animationState.speed > 0)
            {
                animationState.normalizedTime = 0f;
            }
            else
            {
                animationState.normalizedTime = 1f;
            }
            unlockAnimation.clip     = animationState.clip; // This animation player could be used for multiple clips.
            unlockAnimation.enabled  = true;
            unlockAnimation.wrapMode = WrapMode.Once;
            unlockAnimation.Play();

            FreeIva.SetRenderQueues(FreeIva.CurrentPart);

            if (open)
            {
                HatchOpenSound.audio.Play();
            }
            else
            {
                HatchCloseSound.audio.Play();
            }

            _isOpen = !_isOpen;
        }
Exemple #5
0
        private void SetupAudio()
        {
            HatchOpenSound       = new FXGroup("HatchOpen");
            HatchOpenSound.audio = IvaGameObject.AddComponent <AudioSource>();
            HatchOpenSound.audio.dopplerLevel = 0f;
            HatchOpenSound.audio.Stop();
            HatchOpenSound.audio.clip = GameDatabase.Instance.GetAudioClip(HatchOpenSoundFile);
            HatchOpenSound.audio.loop = false;

            HatchCloseSound       = new FXGroup("HatchClose");
            HatchCloseSound.audio = IvaGameObject.AddComponent <AudioSource>();
            HatchCloseSound.audio.dopplerLevel = 0f;
            HatchCloseSound.audio.Stop();
            HatchCloseSound.audio.clip = GameDatabase.Instance.GetAudioClip(HatchCloseSoundFile);
            HatchCloseSound.audio.loop = false;
        }
Exemple #6
0
        public void Instantiate(Part p, PrimitiveType colliderType)
        {
            Debug.Log("# Creating internal collider " + Name + " for part " + p);
            if (IvaGameObject != null)
            {
                Debug.LogError("[FreeIVA] InternalCollider " + Name + " has already been instantiated.");
                return;
            }

            // These values will be cleared on creating the object.
            Vector3    scale         = Scale;
            Vector3    localPosition = LocalPosition;
            Quaternion rotation      = Rotation;

            IvaGameObject                  = GameObject.CreatePrimitive(colliderType);
            IvaGameObject.layer            = (int)Layers.Kerbals;
            IvaGameObject.collider.enabled = true;
            //IvaGameObject.collider.isTrigger = true;
            IvaGameObject.transform.parent        = p.internalModel.transform;
            IvaGameObject.layer                   = (int)Layers.Kerbals;
            IvaGameObject.transform.localScale    = scale;
            IvaGameObject.transform.localPosition = localPosition;
            IvaGameObject.transform.localRotation = rotation;
            IvaGameObject.name = Name;
            //IvaGameObject.AddComponent<IvaCollisionPrinter>();
            //FixedJoint joint = IvaGameObject.AddComponent<FixedJoint>();
            //joint.connectedBody = p.collider.rigidbody;
            if (IvaGameObject.GetComponent <Rigidbody>() == null)
            {
                Rigidbody rb = IvaGameObject.AddComponent <Rigidbody>();
                rb.useGravity = false;

                rb.constraints = RigidbodyConstraints.FreezeAll;
            }

            if (AlwaysVisible)
            {
                // Change the colour from the default white.
                IvaGameObject.renderer.material.color = Color.grey;
            }
            else
            {
                IvaGameObject.renderer.enabled = false;
            }
        }
Exemple #7
0
 public void SetupAudio()
 {
     if (!string.IsNullOrEmpty(HatchOpenSoundFile))
     {
         HatchOpenSound       = new FXGroup("HatchOpen");
         HatchOpenSound.audio = IvaGameObject.AddComponent <AudioSource>();
         HatchOpenSound.audio.dopplerLevel = 0f;
         HatchOpenSound.audio.Stop();
         HatchOpenSound.audio.clip = GameDatabase.Instance.GetAudioClip(HatchOpenSoundFile);
         HatchOpenSound.audio.loop = false;
     }
     if (!string.IsNullOrEmpty(HatchCloseSoundFile))
     {
         HatchCloseSound       = new FXGroup("HatchClose");
         HatchCloseSound.audio = IvaGameObject.AddComponent <AudioSource>();
         HatchCloseSound.audio.dopplerLevel = 0f;
         HatchCloseSound.audio.Stop();
         HatchCloseSound.audio.clip = GameDatabase.Instance.GetAudioClip(HatchCloseSoundFile);
         HatchCloseSound.audio.loop = false;
     }
 }
        public void Start()
        {
            if (HighLogic.LoadedSceneIsFlight == false)
            {
                return;
            }

            IvaGameObject = internalProp.gameObject;

            // Events
            if (!string.IsNullOrEmpty(unlockTriggerTransform))
            {
                Transform unlockTransform = internalProp.FindModelTransform(unlockTriggerTransform);
                if (unlockTransform != null)
                {
                    GameObject unlockTriggerObject = unlockTransform.gameObject;
                    if (unlockTriggerObject != null)
                    {
                        ClickWatcher clickWatcher = unlockTriggerObject.GetComponent <ClickWatcher>();
                        if (clickWatcher == null)
                        {
                            clickWatcher = unlockTriggerObject.AddComponent <ClickWatcher>();
                            clickWatcher.AddMouseDownAction(() => ToggleLock());
                        }
                    }
                }
                else
                {
                    Debug.LogError("[FreeIVA] PropHatchAnimated: Unable to find unlockTriggerTransform \"" + unlockTriggerTransform + "\".");
                }
            }
            if (!string.IsNullOrEmpty(openTriggerTransform))
            {
                Transform openTransform = internalProp.FindModelTransform(openTriggerTransform);
                if (openTransform != null)
                {
                    GameObject openTriggerObject = openTransform.gameObject;
                    if (openTriggerObject != null)
                    {
                        ClickWatcher clickWatcher = openTriggerObject.GetComponent <ClickWatcher>();
                        if (clickWatcher == null)
                        {
                            clickWatcher = openTriggerObject.AddComponent <ClickWatcher>();
                            clickWatcher.AddMouseDownAction(() => ToggleHatch());
                        }
                    }
                }
                else
                {
                    Debug.LogError("[FreeIVA] PropHatchAnimated: Unable to find unlockTriggerTransform \"" + openTriggerTransform + "\".");
                }
            }

            Collider[] colliders = IvaGameObject.GetComponentsInChildren <Collider>();
            foreach (Collider collider in colliders)
            {
                collider.material.bounciness = 0;
            }

            // Animations
            Animation[] unlockAnimations = internalProp.FindModelAnimators(unlockAnimationName);
            if (unlockAnimations == null || unlockAnimations.Length == 0)
            {
                Debug.LogError("[FreeIVA] PropHatchAnimated: unlockAnimationName " + unlockAnimationName + " was not found.");
                return;
            }
            unlockAnimation = unlockAnimations[0];
            AnimationState unlockAnimationState = unlockAnimation[unlockAnimationName];

            if (invertLockedState)
            {
                _isLocked = !_isLocked;
            }
            if (animationStartsUnlocked)
            {
                unlockAnimationState.speed = -unlockAnimationState.speed;
            }


            Animation[] openAnimations = internalProp.FindModelAnimators(openAnimationName);
            if (openAnimations == null || openAnimations.Length == 0)
            {
                Debug.LogError("[FreeIVA] PropHatchAnimated: openAnimationName " + openAnimationName + " was not found.");
                return;
            }
            openAnimation = openAnimations[0];
            AnimationState openAnimationState = openAnimation[openAnimationName];

            if (invertLockedState)
            {
                _isOpen = !_isOpen;
            }
            if (animationStartsOpened)
            {
                openAnimationState.speed = -openAnimationState.speed;
            }

            /*/ Depth mask disc
             *
             * TODO: Needs to be loaded from config properly.
             *
             * depthMaskObject = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
             * MonoBehaviour.Destroy(depthMaskObject.GetComponentCached<Collider>(ref depthMaskCollider));
             * if (part.internalModel == null)
             *  part.CreateInternalModel(); // TODO: Detect this in an event instead.
             * depthMaskObject.transform.parent = part.internalModel.transform;
             * depthMaskObject.layer = (int)Layers.InternalSpace;
             * depthMaskObject.transform.localScale = new Vector3(0.512f, 1, 0.512f); //depthMaskScale; TODO
             * depthMaskObject.transform.localPosition = new Vector3(0, 0, 0.643f); // depthMaskPosition; TODO
             * depthMaskObject.transform.localRotation = Quaternion.Euler(270, 0, 0); //depthMaskRotation; TODO
             * depthMaskObject.name = "Hatch Depth Mask";
             *
             * Shader depthMaskShader = Utils.GetDepthMask();
             * if (depthMaskShader != null)
             *  depthMaskObject.GetComponentCached<Renderer>(ref depthMaskRenderer).material.shader = depthMaskShader;
             * depthMaskObject.GetComponentCached<Renderer>(ref depthMaskRenderer).shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
             * ChangeMesh(depthMaskObject);*/

            //CreateDepthMask();
            SetupAudio();
        }