Example #1
0
 public static UiSoundFx GetOrCreate()
 {
     if (instance == null)
     {
         UiSoundFx prefab = Resources.Load <UiSoundFx>("UiSoundFx");
         instance = Instantiate(prefab);
     }
     return(instance);
 }
Example #2
0
    private void Update()
    {
        const float distanceThreshold = 5;
        const float fuseIncrement     = 1f / Fuse.FuseTicksPerSecond;
        const float fuseTimeMax       = 10f;

        if (currentTarget == null)
        {
            return;             // not active
        }
        if (!Input.GetMouseButton(0))
        {
            currentTarget = null;
            return;             // released LMB
        }

        // get mouse movement
        Vector3 newMousePos = Input.mousePosition;
        Vector3 mouseDelta  = newMousePos - prevMousePos;
        float   delta       = mouseDelta.x + mouseDelta.y + mouseDelta.z;
        float   distance    = Mathf.Abs(delta);

        if (distance > distanceThreshold)
        {
            // calculate fuse time change
            int   increments = Mathf.FloorToInt(distance / distanceThreshold);
            float change     = increments * fuseIncrement * Mathf.Sign(delta);

            // update fuse
            float prevFuseTime = currentTarget.timeToDetonate;
            float newFuseTime  = Mathf.Clamp(prevFuseTime + change, 0, fuseTimeMax);
            if (newFuseTime != prevFuseTime)
            {
                if (prevFuseTime <= 0)
                {
                    currentTarget.GetComponent <Bomb>()?.fuseSparkleEffect.SetActive(true);
                    SimulationState.Instance.OnBombFused();
                }
                else if (newFuseTime <= 0)
                {
                    currentTarget.GetComponent <Bomb>()?.fuseSparkleEffect.SetActive(false);
                    SimulationState.Instance.OnBombDefused();
                }

                UiSoundFx.GetOrCreate().PlayAdjustFuse();
                currentTarget.SetTimeToDetonate(newFuseTime);
            }

            // update recorded mouse position, carrying over any remainder
            prevMousePos    = newMousePos;
            prevMousePos.x -= (distance % distanceThreshold) * Mathf.Sign(delta);
        }
    }