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 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;
        }