예제 #1
0
파일: Swing.cs 프로젝트: elMakers/Saber
    // Update is called once per frame
    void Update()
    {
        if (_lastLocation == Vector3.zero)
        {
            _lastLocation = _body.position;
            return;
        }

        Vector3 velocity = _body.position - _lastLocation;

        _lastLocation = _body.position;
        var currentTime = Time.time;
        var speed       = velocity.magnitude;

        _speedSamples[_speedIndex] = speed;
        _sampleCount++;
        _speedIndex = (_speedIndex + 1) % SpeedSamples;
        var speedTotal = 0.0f;
        var speedCount = Math.Min(_sampleCount, SpeedSamples);

        for (var i = 0; i < speedCount; i++)
        {
            speedTotal += _speedSamples[i];
        }

        _averageSpeed = speedTotal / speedCount;
        //Debug.Log("Speed: " + speed + ", avg: " + averageSpeed + " from " + _body.position);

        if (_averageSpeed >= MinimumSwingSpeed && _extend.Extended())
        {
            // Debug.Log("SWING? " + currentTime + " : " + (_lastSwingSound + SwingSoundTimeout) + ", speed: " + averageSpeed);
            if (currentTime < _lastSwingSound + SwingSoundTimeout)
            {
                return;
            }
            _lastSwingSound = currentTime;
            var clipIndex = Random.Range(0, SwingSounds.Length);
            SwingSource.PlayOneShot(SwingSounds[clipIndex]);
        }
    }
예제 #2
0
파일: Slice.cs 프로젝트: elMakers/Saber
    void Update()
    {
        if (!_extend.Extended())
        {
            return;
        }

        var transform = Source.transform;
        var position  = transform.position;
        var direction = transform.up;

        // Raycast to check for surfaces to slice

        // Debug.DrawLine(position, position + direction * Length, Color.yellow, 2.0f, false);
        var wasHit = _isHit;

        _isHit = false;
        var hitCount = Physics.RaycastNonAlloc(position, direction, _hits, Length, _layerMask);

        if (hitCount == 0)
        {
            _isFirstHit = true;
            return;
        }
        var firstHit = _hits[0];

        Vector3[] interpolateTargets = null;
        _isHit = true;

        // Play Sound
        if (wasHit)
        {
            if (Time.time > _lastCrackleSound + _crackleSoundCooldown)
            {
                ResetCrackleCooldown();
                _lastCrackleSound  = Time.time;
                CrackleSound.pitch = Random.Range(MinPitch, MaxPitch);
                CrackleSound.transform.position = firstHit.point;
                CrackleSound.PlayOneShot(CrackleSound.clip);
            }
        }
        else
        {
            if (Time.time > _lastSliceSound + _sliceSoundCooldown)
            {
                ResetSliceCooldown();
                _lastSliceSound          = Time.time;
                Sound.pitch              = Random.Range(MinPitch, MaxPitch);
                Sound.transform.position = firstHit.point;
                Sound.PlayOneShot(Sound.clip);
            }
        }

        // Add haptics
        if (Time.time > _lastBuzz + SliceBuzzCooldown)
        {
            _lastBuzz = Time.time;
            MLInputControllerFeedbackIntensity intensity = MLInputControllerFeedbackIntensity.Low;
            var speed = _swing.AverageSpeed;
            if (speed >= SliceBuzzMediumSpeed)
            {
                intensity = MLInputControllerFeedbackIntensity.Medium;
            }
            if (speed >= SliceBuzzHighSpeed)
            {
                intensity = MLInputControllerFeedbackIntensity.High;
            }

            _controller.Controller.StartFeedbackPatternVibe(MLInputControllerFeedbackPatternVibe.Buzz, intensity);
        }

        // Check for distance travelled and interpolate
        if (!_isFirstHit)
        {
            var interpolateDirection = (firstHit.point - _lastHit);
            var distance             = interpolateDirection.magnitude;
            if (distance <= DecalMinDistance)
            {
                // Not moved far enough for decals
                // Still want some sparks though
                var particleRotation = Quaternion.LookRotation(firstHit.normal);
                PlaySparks(firstHit, particleRotation);
                return;
            }

            if (distance >= DecalMinDistance * 2)
            {
                interpolateDirection = interpolateDirection.normalized * DecalMinDistance;
                interpolateTargets   = new Vector3[(int)Math.Ceiling(distance / DecalMinDistance) - 1];
                Vector3 targetLocation = _lastHit + interpolateDirection;
                for (int i = 0; i < interpolateTargets.Length; i++)
                {
                    interpolateTargets[i] = targetLocation;
                    targetLocation       += interpolateDirection;
                }
            }
        }

        _isFirstHit = false;
        _lastHit    = firstHit.point;

        // Check for particle limit
        var particleCount = hitCount;

        if (interpolateTargets != null)
        {
            particleCount *= (interpolateTargets.Length + 1);
        }
        PurgeParticles(particleCount);

        // Check current location
        CheckHits(hitCount, position, direction);

        // Check interpolated locations
        if (interpolateTargets != null)
        {
            foreach (Vector3 target in interpolateTargets)
            {
                var interpolateDirection = target - position;
                hitCount = Physics.RaycastNonAlloc(position, interpolateDirection, _hits, Length, _layerMask);
                if (hitCount != 0)
                {
                    CheckHits(hitCount, position, interpolateDirection);
                }
            }
        }
    }