Ejemplo n.º 1
0
 // Called when a damage receiver exits the damage receiver scanner's range
 protected virtual void OnDamageReceiverUndetected(DamageReceiver damageReceiver)
 {
     if (target != null)
     {
         if (damageReceiver.RootTransform == target.RootTransform)
         {
             targetInsideTrigger = false;
         }
     }
 }
Ejemplo n.º 2
0
        // Do a hit scan
        protected virtual bool DoHitScan()
        {
            // Raycast
            RaycastHit[] hits;
            hits = Physics.RaycastAll(beamSpawn.position, beamSpawn.forward, range);
            System.Array.Sort(hits, (a, b) => a.distance.CompareTo(b.distance));    // Sort by distance

            for (int i = 0; i < hits.Length; ++i)
            {
                bool           isSelf         = false;
                DamageReceiver damageReceiver = hits[i].collider.GetComponent <DamageReceiver>();
                if (rootTransform != null && damageReceiver != null && damageReceiver.RootTransform == rootTransform)
                {
                    isSelf = true;
                }

                if (!isSelf && !hits[i].collider.isTrigger)
                {
                    // Set beam to stop at hit point
                    beamLineRenderer.SetPosition(0, beamSpawn.position);
                    beamLineRenderer.SetPosition(1, hits[i].point);

                    // Update hit effect
                    if (beamHitEffectController != null)
                    {
                        beamHitEffectController.SetActivation(true);
                        beamHitEffectController.OnHit(hits[i]);
                    }

                    onHitDetected.Invoke(hits[i]);
                    return(true);
                }
            }

            // Set beam to stop at full range
            beamLineRenderer.SetPosition(0, beamSpawn.position);
            beamLineRenderer.SetPosition(1, beamSpawn.position + beamSpawn.forward * range);

            // Disable hit effect
            if (beamHitEffectController != null)
            {
                beamHitEffectController.SetActivation(false);
            }

            onHitNotDetected.Invoke();

            return(false);
        }
Ejemplo n.º 3
0
        private void Update()
        {
            // Raycast
            RaycastHit[] hits;
            hits = Physics.RaycastAll(aimRaycastOrigin.position, aimRaycastOrigin.forward, aimRaycastDistance);
            System.Array.Sort(hits, (a, b) => a.distance.CompareTo(b.distance));    // Sort by distance

            bool hitFound = false;

            for (int i = 0; i < hits.Length; ++i)
            {
                if (!hits[i].collider.isTrigger)
                {
                    // Prevent aiming at point on self
                    if (preventSelfCollision)
                    {
                        DamageReceiver damageReceiver = hits[i].collider.GetComponent <DamageReceiver>();
                        if (damageReceiver != null)
                        {
                            if (damageReceiver.RootTransform == selfRootTransform)
                            {
                                continue;
                            }
                        }
                    }

                    aimTarget.position      = hits[i].point;
                    aimTarget.localPosition = new Vector3(0, 0, aimTarget.transform.localPosition.z);
                    hitFound = true;
                    break;
                }
            }

            if (!hitFound)
            {
                aimTarget.localPosition = new Vector3(0, 0, aimRaycastDistance);
            }
        }
        // Called when a collider enters a trigger collider.
        protected virtual void OnTriggerEnter(Collider other)
        {
            DamageReceiver damageReceiver = other.GetComponent <DamageReceiver>();

            if (damageReceiver != null)
            {
                // Ignore if outside the designated trigger collider's radius
                if (scannerTriggerCollider != null)
                {
                    Vector3 colliderWorldPos = scannerTriggerCollider.transform.TransformPoint(scannerTriggerCollider.center);
                    float   distanceToTarget = Vector3.Distance(damageReceiver.GetClosestPoint(colliderWorldPos), colliderWorldPos);
                    if (distanceToTarget < scannerTriggerCollider.radius)
                    {
                        if (!damageReceiversInRange.Contains(damageReceiver))
                        {
                            // Add the damageable to the list and invoke the event
                            damageReceiversInRange.Add(damageReceiver);
                            onDamageReceiverDetected.Invoke(damageReceiver);
                        }
                    }
                }
            }
        }
        // Called when a module is unmounted on the module mount
        protected virtual void OnModuleUnmounted(Module module)
        {
            Damageable damageable = module.GetComponent <Damageable>();

            if (damageable != null)
            {
                // Unlink this
                damageable.onDamaged.RemoveListener(OnDamageableModuleDamaged);
                damageable.onHealed.RemoveListener(OnDamageableModuleHealed);
                damageable.onDestroyed.RemoveListener(OnDamageableModuleDestroyed);
                damageable.onRestored.RemoveListener(OnDamageableModuleRestored);

                for (int i = 0; i < damageReceivers.Count; ++i)
                {
                    damageReceivers[i].onDamaged.RemoveListener(damageable.Damage);
                    damageReceivers[i].onHealed.RemoveListener(damageable.Heal);
                    damageReceivers[i].onCollision.RemoveListener(damageable.OnCollision);

                    DamageReceiver damageReceiver = damageReceivers[i];     // Can't create lambda expressions from a list element without errors
                    damageable.onDestroyed.RemoveListener(delegate { damageReceiver.SetActivation(false); });
                    damageable.onRestored.RemoveListener(delegate { damageReceiver.SetActivation(true); });
                }
            }
        }