コード例 #1
0
        private void _switchLight(bool on)
        {
            _on = on;

            // Light
            _light.enabled   = _on;
            _light.intensity = _baseIntensity * _intensity;
            _glassMaterial.SetColor("_EmissionColor", _lightColor * (_on ? 100 : 0));
            _glassMaterial.SetFloat("_EmissionScaleUI", _on ? 100.0f : 0.0f);
            _switchMaterial.color = Color.red * (_on ? 1.0f : 0.2f);
            _switchMaterial.SetColor("_EmissionColor", Color.red);
            _switchMaterial.SetFloat("_EmissionScaleUI", _on ? 50.0f : 0.0f);

            if (_on)
            {
                _glassMaterial.EnableKeyword("_EMISSION");
                _switchMaterial.EnableKeyword("_EMISSION");
            }
            else
            {
                _glassMaterial.DisableKeyword("_EMISSION");
                _switchMaterial.DisableKeyword("_EMISSION");
            }

            // Sound
            if (_on)
            {
                FloodlightAudio.Play(_audio.HumLoop);
            }
            else
            {
                FloodlightAudio.Stop(_audio.HumLoop);
            }
        }
コード例 #2
0
        private FloodlightAudio _audio;                                 // Audio sources

        public FloodlightOld()
        {
            _savePath     = Path.Combine(Application.persistentDataPath, "floodlight.xml");
            _audio        = new FloodlightAudio();
            UseBattery    = new Settings("UseBattery", "Consume battery power", true);
            EnableFlicker = new Settings("EnableFlicker", "Periodic flickering (EPILEPSY WARNING)", true);
            Unbreakable   = new Settings("UnbreakableBulb", "Lightbulb never breaks", false);
        }
コード例 #3
0
        public override void Update()
        {
            //// Load audio
            //if (!_audio.HasLoaded)
            //{
            //	try
            //	{
            //		_initAudioAsync();
            //	}
            //	catch (Exception ex)
            //	{
            //		ModConsole.Error(ex.ToString());
            //	}
            //}

            // Get inputs
            float scroll = Input.GetAxis("Mouse ScrollWheel");
            bool  use    = cInput.GetButtonDown("Use");

            // Raycast
            RaycastHit[] hits = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition), 1.0f);
            for (int i = 0; i < hits.Length; ++i)
            {
                if (hits[i].collider == _lampCollider)
                {
                    PlayMakerGlobals.Instance.Variables.FindFsmBool("GUIuse").Value           = true;
                    PlayMakerGlobals.Instance.Variables.FindFsmString("GUIinteraction").Value = _on ? "light on" : "light off";
                    if (use)
                    {
                        // Toggle the light
                        _switchLight(!_on);
                        if (_on)
                        {
                            FloodlightAudio.Play(_audio.SwitchOn);
                        }
                        else
                        {
                            FloodlightAudio.Play(_audio.SwitchOff);
                            _battery       = null;
                            _batteryCharge = null;
                        }
                    }

                    if (scroll > 0)
                    {
                        // Pitch up
                        _pitch -= 5.0f;
                        if (_pitch < -60.0f)
                        {
                            _pitch = -60.0f;
                        }
                        _lamp.transform.localEulerAngles = new Vector3(_pitch, 0.0f, 0.0f);
                    }
                    if (scroll < 0)
                    {
                        // Pitch down
                        _pitch += 5.0f;
                        if (_pitch > 10.0f)
                        {
                            _pitch = 10.0f;
                        }
                        _lamp.transform.localEulerAngles = new Vector3(_pitch, 0.0f, 0.0f);
                    }

                    break;
                }
                else if (hits[i].collider == _boxColliderTeimo || hits[i].collider == _boxColliderFleetari)
                {
                    PlayMakerGlobals.Instance.Variables.FindFsmBool("GUIbuy").Value           = true;
                    PlayMakerGlobals.Instance.Variables.FindFsmString("GUIinteraction").Value = _bulbText;
                    if (Input.GetKeyDown(KeyCode.Mouse0))
                    {
                        if (_bulbHealth <= 0)
                        {
                            FsmFloat wealth = PlayMakerGlobals.Instance.Variables.FindFsmFloat("PlayerMoney");
                            if (wealth.Value >= _bulbCost)
                            {
                                wealth.Value -= _bulbCost;
                                hits[i].collider.transform.gameObject.GetComponent <AudioSource>().Play();
                                _bulbHealth = UnityEngine.Random.Range(60, 100);
                                ModConsole.Print($"Floodlight: lightbulb replaced, {_bulbHealth} health");
                            }
                            else
                            {
                                ModConsole.Print($"Floodlight: not enough money ({wealth.Value.ToString("0")} wealth, {_bulbCost.ToString("0")} cost)");
                            }
                        }
                        else
                        {
                            ModConsole.Print($"Floodlight: lightbulb is not broken ({_bulbHealth} health)");
                        }
                    }

                    break;
                }
            }

            // Conditions to switch off
            if (_on)
            {
                if (!_checkBattery(true))
                {
                    _switchLight(false);
                }
                if (_bulbHealth <= 0)
                {
                    if (_bulbHealth == 0)
                    {
                        FloodlightAudio.Play(_audio.Break);
                    }
                    --_bulbHealth;
                    _switchLight(false);
                }
            }
        }
コード例 #4
0
        private bool _checkBattery(bool connect)
        {
            // Skip the whole shebang if battery usage is disabled
            if (!(bool)UseBattery.GetValue())
            {
                return(true);
            }

            // Disconnect the battery if needed
            if (!connect)
            {
                _battery       = null;
                _batteryCharge = null;
                return(false);
            }

            // Try to find a battery
            if (_battery == null)
            {
                foreach (GameObject o in GameObject.FindObjectsOfType <GameObject>().Where(e => e.name == "battery(Clone)"))
                {
                    if ((o.transform.position - _base.transform.position).sqrMagnitude < 1.0f)
                    {
                        if (_batteryIsInstalled(o))
                        {
                            continue;
                        }
                        _battery = o;
                        ModConsole.Print($"Floodlight: battery found, {o.GetComponents<PlayMakerFSM>().Single(c => c.FsmName == "Use").FsmVariables.FindFsmFloat("Charge").Value.ToString("0.00")} charge");
                    }
                }
                if (_battery == null)
                {
                    ModConsole.Print("Floodlight: battery not found");
                }
            }

            // Check if the battery is still connected
            if (_battery != null)
            {
                // Check if the battery is in range and not installed in the car
                if ((_battery.transform.position - _base.transform.position).sqrMagnitude < 1.0f && !_batteryIsInstalled(_battery))
                {
                    // Check the charge level
                    if (_batteryCharge == null)
                    {
                        _batteryCharge = _battery.GetComponents <PlayMakerFSM>().Single(c => c.FsmName == "Use").FsmVariables.FindFsmFloat("Charge");
                    }
                    if (_batteryCharge != null && _batteryCharge.Value >= _turnOffCharge)
                    {
                        // If the charge level is above minimum, drain energy
                        if (_on)
                        {
                            _batteryCharge.Value -= _dischargeRate * Time.deltaTime;
                            if (_batteryCharge.Value > _turnOffCharge)
                            {
                                //_intensity = (_batteryCharge.Value < _dimStartCharge ? (_batteryCharge.Value - _turnOffCharge) / (_dimStartCharge - _turnOffCharge) : 1.0f);
                                _intensity = _batteryCharge.Value < _dimStartCharge
                                                                        ? (_batteryCharge.Value - _turnOffCharge) / (_dimStartCharge - _turnOffCharge) / 2.0f + 0.5f
                                                                        : 1.0f;
                                if ((bool)EnableFlicker.GetValue())
                                {
                                    _flicker(_flickerTimer);
                                }
                            }
                            _light.intensity = _baseIntensity * _intensity * _flickerMultiplier;
                        }

                        return(true);
                    }
                    else
                    {
                        // If not, disconnect
                        ModConsole.Print("Floodlight: battery depleted");
                        _battery       = null;
                        _batteryCharge = null;
                        return(false);
                    }
                }
                else
                {
                    // If not, disconnect and play the disconnect sound clip
                    ModConsole.Print("Floodlight: battery disconnected");
                    _batteryCharge = null;
                    _battery       = null;
                    FloodlightAudio.Play(_audio.Disconnect);
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }