示例#1
0
        internal MASComponentColliderAdvanced(ConfigNode config, InternalProp internalProp, MASFlightComputer comp)
            : base(config, internalProp, comp)
        {
            string collider = string.Empty;

            if (!config.TryGetValue("collider", ref collider))
            {
                throw new ArgumentException("Missing 'collider' in COLLIDER_ADVANCED " + name);
            }

            string clickAction   = string.Empty;
            string dragAction    = string.Empty;
            string releaseAction = string.Empty;
            string monitorID     = string.Empty;

            if (!config.TryGetValue("monitorID", ref monitorID))
            {
                config.TryGetValue("onClick", ref clickAction);
                config.TryGetValue("onDrag", ref dragAction);
                config.TryGetValue("onRelease", ref releaseAction);

                if (string.IsNullOrEmpty(clickAction) && string.IsNullOrEmpty(dragAction) && string.IsNullOrEmpty(releaseAction))
                {
                    throw new ArgumentException("Missing 'monitorID', 'onClick', 'onDrag', or 'onRelease' in COLLIDER_ADVANCED " + name);
                }
            }

            string clickX = string.Empty;

            if (!config.TryGetValue("clickX", ref clickX))
            {
                throw new ArgumentException("Missing 'clickX' in COLLIDER_ADVANCED " + name);
            }
            string clickY = string.Empty;

            if (!config.TryGetValue("clickY", ref clickY))
            {
                throw new ArgumentException("Missing 'clickY' in COLLIDER_ADVANCED " + name);
            }

            Transform tr = internalProp.FindModelTransform(collider.Trim());

            if (tr == null)
            {
                throw new ArgumentException("Unable to find transform '" + collider + "' in prop for COLLIDER_ADVANCED " + name);
            }

            float volume = -1.0f;

            if (config.TryGetValue("volume", ref volume))
            {
                volume = Mathf.Clamp01(volume);
            }
            else
            {
                volume = -1.0f;
            }

            string sound = string.Empty;

            if (!config.TryGetValue("sound", ref sound) || string.IsNullOrEmpty(sound))
            {
                sound = string.Empty;
            }

            AudioClip clip = null;

            if (string.IsNullOrEmpty(sound) && (volume >= 0.0f))
            {
                throw new ArgumentException("'volume', but no 'sound', found in COLLIDER_ADVANCED " + name);
            }

            if (!string.IsNullOrEmpty(sound))
            {
                //Try Load audio
                clip = GameDatabase.Instance.GetAudioClip(sound);
                if (clip == null)
                {
                    throw new ArgumentException("Unable to load 'sound' " + sound + " in COLLIDER_ADVANCED " + name);
                }
                if (volume < 0.0f)
                {
                    volume = 1.0f;
                }
            }

            buttonObject        = tr.gameObject.AddComponent <AdvancedButtonObject>();
            buttonObject.parent = this;
            if (string.IsNullOrEmpty(monitorID))
            {
                if (!string.IsNullOrEmpty(clickAction))
                {
                    onClick = comp.GetColliderAction(clickAction, 0, "click", internalProp);
                }
                if (!string.IsNullOrEmpty(dragAction))
                {
                    onDrag = comp.GetColliderAction(dragAction, 0, "drag", internalProp);
                }
                if (!string.IsNullOrEmpty(releaseAction))
                {
                    onRelease = comp.GetColliderAction(releaseAction, 0, "release", internalProp);
                }

                buttonObject.onTouch = (Vector2 hitCoordinate, EventType eventType) =>
                {
                    if (eventType == EventType.MouseDown)
                    {
                        if (onClick != null)
                        {
                            onClick(hitCoordinate);
                        }
                    }
                    else if (eventType == EventType.MouseDrag)
                    {
                        if (onDrag != null)
                        {
                            onDrag(hitCoordinate);
                        }
                    }
                    else if (eventType == EventType.MouseUp)
                    {
                        if (onRelease != null)
                        {
                            onRelease(hitCoordinate);
                        }
                    }
                };
            }
            else
            {
                buttonObject.onTouch = comp.GetHitAction(monitorID, internalProp, comp.HandleTouchEvent);
            }
            buttonObject.hitTransformation = comp.GetColliderTransformation(clickX, clickY, name, internalProp);
            Collider btnCollider = tr.gameObject.GetComponent <Collider>();

            if (btnCollider == null)
            {
                throw new ArgumentException("Unable to retrieve Collider from GameObject in COLLIDER_ADVANCED " + name);
            }
            BoxCollider boxCollider = btnCollider as BoxCollider;

            if (boxCollider == null)
            {
                throw new ArgumentException("Collider for COLLIDER_ADVANCED " + name + " is not a BoxCollider");
            }

            buttonObject.InitBoxCollider(boxCollider);
            if (!config.TryGetValue("logHits", ref buttonObject.debugEnabled))
            {
                buttonObject.debugEnabled = false;
            }

            string variableName = string.Empty;

            if (config.TryGetValue("variable", ref variableName))
            {
                variableName = variableName.Trim();

                buttonObject.colliderEnabled = false;
                comp.RegisterVariableChangeCallback(variableName, internalProp, VariableCallback);
            }
            else
            {
                variableEnabled = true;
            }
            comp.RegisterVariableChangeCallback("fc.CrewConscious(-1)", internalProp, KerbalCallback);

            if (clip != null)
            {
                AudioSource audioSource = tr.gameObject.AddComponent <AudioSource>();
                audioSource.clip = clip;
                audioSource.Stop();
                audioSource.volume       = GameSettings.SHIP_VOLUME * volume;
                audioSource.rolloffMode  = AudioRolloffMode.Logarithmic;
                audioSource.maxDistance  = 8.0f;
                audioSource.minDistance  = 2.0f;
                audioSource.dopplerLevel = 0.0f;
                audioSource.panStereo    = 0.0f;
                audioSource.playOnAwake  = false;
                audioSource.loop         = false;
                audioSource.pitch        = 1.0f;
                buttonObject.audioSource = audioSource;
            }
        }