Exemplo n.º 1
0
    private void FixedUpdate()
    {
        _time += Time.deltaTime;

        IsOn = _time >= ChargeTime;

        /*
         * Set laser start and end point
         */

        //Start is at the emitter's position
        var start = transform.position;

        //End is by default MAX_DISTANCE units away from the emitter
        var end = start + transform.up * MaxDistance;

        //If the laser is obstructed by something, that thing will be the end point
        var raycastHit = Physics2D.Raycast(transform.position, transform.up, MaxDistance, Commons.Masks.GroundOnly);

        if (raycastHit)
        {
            end = raycastHit.point;
        }

        //Update positions
        _lineRenderer.SetPositions(new[] { start, end });

        /*
         * Sets the visibility of the laser
         */
        _lineRenderer.forceRenderingOff = false;

        //Laser is firing
        if (IsOn)
        {
            /*
             * Apply hurtbox
             */
            bool lastQueriesStartInCollidersState = Physics2D.queriesStartInColliders;
            Physics2D.queriesStartInColliders = true;

            RaycastHit2D[] hits = Physics2D.RaycastAll(start, transform.up, Vector2.Distance(start, end), Commons.Masks.HitboxesHurtboxes);

            Physics2D.queriesStartInColliders = lastQueriesStartInCollidersState;

            //For each hitbox that was hit, deal damage with the hurtbox
            foreach (var i in hits)
            {
                var hitbox = i.collider.GetComponent <Hitbox>();

                if (hitbox == null)
                {
                    continue;
                }

                hitbox.TryDealDamageBy(_hurtbox);
            }

            //Set firing material
            _lineRenderer.material = FiringMaterial;
        }

        //Laser is not firing
        else
        {
            //Set charge-up material
            _lineRenderer.material = ChargingMaterial;
        }

        _isVisible = IsLaserOnScreen(start, end);

        if (IsOn && _isVisible)
        {
            _laserAudio.AddLaser(gameObject);
        }
        else
        {
            _laserAudio.RemoveLaser(gameObject);
        }
    }
Exemplo n.º 2
0
    private void FixedUpdate()
    {
        /*
         * Figure out if the laser should be on
         */
        //The progress (percentage) of the current cycle with offset taken into account
        var cycleProgress = (Commons.GetEffectValue(Time.time, EffectValueType.EnemySpeed) + (CycleOffset * CycleTime)) % CycleTime / CycleTime;

        //The laser is on if the cycle is less than the duty cycle of the laser
        IsOn = cycleProgress < DutyCycle;

        /*
         * Set laser start and end point
         */
        const float MAX_DISTANCE = 200f;

        //Start is at the emitter's position
        var start = transform.position;

        //End is by default MAX_DISTANCE units away from the emitter
        var end = start + transform.up * MAX_DISTANCE;

        //If the laser is obstructed by something, that thing will be the end point
        var raycastHit = Physics2D.Raycast(transform.position, transform.up, MAX_DISTANCE, Commons.Masks.GroundOnly);

        if (raycastHit)
        {
            end = raycastHit.point;
        }

        //Update positions
        _lineRenderer.SetPositions(new[] { start, end });

        /*
         * Sets the visibility of the laser
         */
        _lineRenderer.forceRenderingOff = !(cycleProgress <DutyCycle || cycleProgress> 1f - DutyCycle / 2f);

#if UNITY_EDITOR
        //In the editor, the laser is always on (Time is weird in edit mode) unless the laser is set to always be off
        if (!Application.isPlaying)
        {
            IsOn = DutyCycle != 0f;
            _lineRenderer.forceRenderingOff = !IsOn;
        }
#endif

        //Laser is firing
        if (IsOn)
        {
            /*
             * Apply hurtbox
             */
            bool lastQueriesStartInCollidersState = Physics2D.queriesStartInColliders;
            Physics2D.queriesStartInColliders = true;

            RaycastHit2D[] hits = Physics2D.RaycastAll(start, transform.up, Vector2.Distance(start, end), Commons.Masks.HitboxesHurtboxes);

            Physics2D.queriesStartInColliders = lastQueriesStartInCollidersState;

            //For each hitbox that was hit, deal damage with the hurtbox
            foreach (var i in hits)
            {
                var hitbox = i.collider.GetComponent <Hitbox>();

                if (hitbox == null)
                {
                    continue;
                }

                hitbox.TryDealDamageBy(_hurtbox);
            }

            //Set firing material
            _lineRenderer.material = FiringMaterial;

            _isVisible = IsLaserOnScreen(start, end);
        }

        //Laser is not firing
        else
        {
            //Set charge-up material
            _lineRenderer.material = ChargingMaterial;
        }

        //add laser to active laser list (to play audio)
        if (_isVisible && IsOn)
        {
            _laserAudio.AddLaser(gameObject);
        }
        else
        {
            _laserAudio.RemoveLaser(gameObject);
        }
    }