示例#1
0
        internal MASComponentColliderEvent(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_EVENT " + name);
            }

            string clickEvent = string.Empty, releaseEvent = string.Empty, dragEventX = string.Empty, dragEventY = string.Empty;

            config.TryGetValue("onClick", ref clickEvent);
            config.TryGetValue("onRelease", ref releaseEvent);
            config.TryGetValue("onDragX", ref dragEventX);
            config.TryGetValue("onDragY", ref dragEventY);
            if (string.IsNullOrEmpty(clickEvent) && string.IsNullOrEmpty(releaseEvent) && string.IsNullOrEmpty(dragEventX) && string.IsNullOrEmpty(dragEventY))
            {
                throw new ArgumentException("None of 'onClick', 'onRelease', 'onDragX', nor 'onDragY' found in COLLIDER_EVENT " + name);
            }

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

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

            float autoRepeat = 0.0f;

            if (!config.TryGetValue("autoRepeat", ref autoRepeat))
            {
                autoRepeat = 0.0f;
            }

            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("Only one of 'sound' or 'volume' found in COLLIDER_EVENT " + name);
            }

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

            buttonObject            = tr.gameObject.AddComponent <ButtonObject>();
            buttonObject.parent     = this;
            buttonObject.autoRepeat = (autoRepeat > 0.0f);
            buttonObject.repeatRate = autoRepeat;

            string variableName = string.Empty;

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

                buttonObject.colliderEnabled = false;
                comp.RegisterVariableChangeCallback(variableName, internalProp, VariableCallback);
            }

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

            if (!string.IsNullOrEmpty(clickEvent))
            {
                buttonObject.onClick = comp.GetAction(clickEvent, internalProp);
            }
            if (!string.IsNullOrEmpty(releaseEvent))
            {
                buttonObject.onRelease = comp.GetAction(releaseEvent, internalProp);
            }
            if (!string.IsNullOrEmpty(dragEventX))
            {
                buttonObject.onDragX = comp.GetDragAction(dragEventX, name, internalProp);
                if (buttonObject.onDragX != null)
                {
                    buttonObject.drag = true;
                    float dragSensitivity = 1.0f;
                    if (!config.TryGetValue("dragSensitivity", ref dragSensitivity))
                    {
                        dragSensitivity = 1.0f;
                    }
                    buttonObject.normalizationScalar = 0.01f * dragSensitivity;
                }
                else
                {
                    throw new ArgumentException("Unable to create 'onDragX' event for COLLIDER_EVENT " + name);
                }
            }
            if (!string.IsNullOrEmpty(dragEventY))
            {
                buttonObject.onDragY = comp.GetDragAction(dragEventY, name, internalProp);
                if (buttonObject.onDragY != null)
                {
                    buttonObject.drag = true;
                    float dragSensitivity = 1.0f;
                    if (!config.TryGetValue("dragSensitivity", ref dragSensitivity))
                    {
                        dragSensitivity = 1.0f;
                    }
                    buttonObject.normalizationScalar = 0.01f * dragSensitivity;
                }
                else
                {
                    throw new ArgumentException("Unable to create 'onDragY' event for COLLIDER_EVENT " + name);
                }
            }
            if (buttonObject.onDragX != null && buttonObject.onDragY != null)
            {
                config.TryGetValue("singleAxisDrag", ref buttonObject.singleAxisDrag);
            }
        }