Beispiel #1
0
    private void OnTriggerEnter(Collider other)
    {
        if (other.attachedRigidbody == null)
        {
            return;
        }

        Hittable hittable = other.attachedRigidbody.GetComponent <Hittable>();

        if (hittable != null)
        {
            if (hittable.team == team)
            {
                return;
            }

            hittable.Hit(damagePerShot);
            Destroy(gameObject);
            return;
        }

        Bullet otherBullet = other.attachedRigidbody.GetComponent <Bullet>();

        if (otherBullet != null && otherBullet.team != team)
        {
            UpdateBullet(otherBullet);
            otherBullet.UpdateBullet(this);
        }
    }
        private Vec3 RayColor(Ray r, Hittable world, int depth)
        {
            HitRecord rec = new HitRecord();

            // If we've exceeded the ray bounce limit, no more light is gathered
            if (depth <= 0)
            {
                return(new Vec3());
            }

            if (world.Hit(r, 0.001, Double.PositiveInfinity, ref rec))
            {
                Ray  scattered   = new Ray();
                Vec3 attenuation = new Vec3();
                if (rec.Material.Scatter(r, rec, ref attenuation, ref scattered))
                {
                    return(attenuation * RayColor(scattered, world, depth - 1));
                }
                return(new Vec3());
            }
            Vec3   unitDirection = Vec3.UnitVector(r.Direction);
            double t             = 0.5 * (unitDirection.Y + 1.0);

            return((1.0 - t) * new Vec3(1.0, 1.0, 1.0) + t * new Vec3(0.5, 0.7, 1.0));
        }
Beispiel #3
0
        private static Vector3 rayColor(Ray r, Hittable world, int depth)
        {
            HitRecord record = new HitRecord();

            // Stop gathering light if the ray bounce limit is exceeded
            // Prevents stack overflow due to recursive function in the next section
            if (depth <= 0)
            {
                return(new Vector3(0, 0, 0));
            }

            // 0.001 minimum prevents shadow acne
            if (world.Hit(r, 0.001, Double.MaxValue, record))
            {
                Ray     scattered   = new Ray();
                Vector3 attenuation = new Vector3();
                if (record.Material.Scatter(r, record, attenuation, scattered, rand))
                {
                    return(attenuation * rayColor(scattered, world, depth - 1));
                }
                return(new Vector3(0, 0, 0)); // Return black
            }

            // Render a blue-to-white gradient background if no hit
            Vector3 unitDirection = r.Direction.Normalize();
            double  t             = 0.5 * (unitDirection.Y + 1);
            Vector3 white         = (1.0 - t) * new Vector3(1.0, 1.0, 1.0);
            Vector3 blue          = t * new Vector3(0.5, 0.7, 1.0);

            return(white + blue);
        }
Beispiel #4
0
 public IEnumerator DamagingBehavior(Hittable h)
 {
     do
     {
         h.Hit(damage, type);
         yield return(new WaitForSeconds(rate));
     } while (h != null && toDamage.Contains(h) && h.gameObject != null);
 }
Beispiel #5
0
        override public void OnInspectorGUI()
        {
            Hittable h = (Hittable)target;

            if (GUILayout.Button("Simulate hit"))
            {
                h.Hit();
            }
            DrawDefaultInspector();
        }
Beispiel #6
0
    protected IEnumerator AttackCouroutine(Hittable hittable)
    {
        hittable.Hit();
        Stop();
        yield return(new WaitForSeconds(stats.fireRate.ValueFloat));

        attacking = false;
        if (targetReached)
        {
            Attack(hittable);
        }
    }
Beispiel #7
0
    public override void Attack(bool mouseDown)
    {
        if (isAttacking)
        {
            setTimeSincePress(getTimeSincePress() + Time.deltaTime);
            if (!hasRaycasted && getTimeSincePress() >= timeToAttack)
            {
                //print("Hitting now " + getLookObj());
                // Apply ItemStats damage
                this.DamageCondition(1);
                RaycastHit[] hits = Physics.CapsuleCastAll(getLookObj().transform.position, getLookObj().transform.position + getLookObj().transform.forward *range, width, getLookObj().transform.forward);
                foreach (RaycastHit hit in hits)
                {
                    if (hit.distance <= range &&
                        !hit.collider.isTrigger &&
                        hit.collider.gameObject.tag != "Player")
                    {
                        // Push physics, regardless of hittable
                        Rigidbody r;
                        if (r = hit.collider.GetComponent <Rigidbody>())
                        {
                            //print("Adding force");
                            // Play around with a good factor here

                            r.AddForceAtPosition(baseDamage * getLookObj().forward * 10, getLookObj().position);
                            r.AddForce(Vector3.up * r.mass * 350);
                        }
                        // Hit with hittable
                        Hittable hittable = hit.collider.GetComponentInParent <Hittable>();
                        if (hittable != null)
                        {
                            //print("hit " + hit);
                            hittable.Hit(baseDamage * (getCondition() / 100), getLookObj().transform.forward, damageType);
                        }
                    }
                }
                hasRaycasted = true;
            }
            else if (hasRaycasted && getTimeSincePress() > timeToAttack + timeToCooldown)
            {
                isAttacking  = false;
                hasRaycasted = false;
                setTimeSincePress(0);
            }
        }
        else if (mouseDown && !isAttacking)
        {
            isAttacking = true;
            getPlayerAnim().SetTrigger(getControllerSide() + "Attack");
            getPlayerAnim().SetInteger(getControllerSide() + "AttackNum", UnityEngine.Random.Range(0, 2));
        }
    }
Beispiel #8
0
    /// <summary>
    /// Sent when an incoming collider makes contact with this object's
    /// collider (2D physics only).
    /// </summary>
    /// <param name="other">The Collision2D data associated with this collision.</param>
    void OnCollisionEnter2D(Collision2D other)
    {
        Hittable _hittable = other.gameObject.GetComponent <Hittable>();

        if (_hittable != null)
        {
            while (damage-- > 0)
            {
                _hittable.Hit();
            }
        }
        Destroy(gameObject);
    }
Beispiel #9
0
    private void OnTriggerEnter(Collider other)
    {
        if (other.attachedRigidbody == null || !other.attachedRigidbody.CompareTag("Enemy"))
        {
            return;
        }

        Enemy enemy = other.attachedRigidbody?.GetComponent <Enemy>();

        if (enemy != null)
        {
            hittable.Hit(Mathf.Infinity);
        }
    }
Beispiel #10
0
 void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.CompareTag("Enemy"))
     {
         Hittable hittable = other.gameObject.GetComponent <Hittable>();
         if (hittable != null && rigidbody != null && rigidbody.velocity.magnitude > 10.0f)
         {
             //Damage the enemy
             hittable.Hit(rigidbody.velocity, rigidbody.mass);
             // Make a hit spark
             Quaternion hitsparkRotation = Quaternion.identity;
             hitsparkRotation.SetLookRotation(rigidbody.velocity);
             Global.InstantiateHitspark(transform.position, hitsparkRotation);
             // Play sound
             float pitch = (30 + rigidbody.velocity.magnitude * 0.3f) * pitchMult;
             SoundManager.Instance.PlayClip(punchClip, 1.0f, pitch);
             Debug.Log("[HitCollider] Hit for " + rigidbody.velocity.magnitude);
         }
     }
 }
Beispiel #11
0
    public override void Attack(bool mouseDown)
    {
        // Particle controls
        if (attacking && mouseDown && holdTime > timeToAttack)
        {
            if (!shootParticles.isPlaying)
            {
                shootParticles.Play();
            }
            if (idleParticles.isPlaying)
            {
                idleParticles.Stop();
            }
        }
        else if (!mouseDown || playerStats.GetMagic() <= 0)
        {
            if (shootParticles.isPlaying)
            {
                shootParticles.Stop();
            }
            if (!idleParticles.isPlaying)
            {
                idleParticles.Play();
            }
        }

        if (getPlayerAnim() && getPlayerAnim().GetCurrentAnimatorStateInfo(2).IsTag("Idle") && !getPlayerAnim().IsInTransition(2))
        {
            // Overrides
            releaseTime = 0;
            onCooldown  = false;
            attacking   = false;
        }
        if (attacking)
        {
            if (!mouseDown || playerStats.GetMagic() <= 0)
            {
                attacking   = false;
                releaseTime = 0;
                onCooldown  = true;
                getPlayerAnim().SetBool(getControllerSide() + "MagicAttack", false); //TODO: Check for accuracy
            }
            else
            {
                holdTime += Time.deltaTime;
                if (holdTime > timeToAttack)
                {
                    if (!shootParticles.isPlaying)
                    {
                        shootParticles.Play();
                    }
                    if (idleParticles.isPlaying)
                    {
                        idleParticles.Stop();
                    }
                    playerStats.UpdateMagic(-1 * magicDraw * Time.deltaTime);
                    //print("Shoooooot");
                    RaycastHit[] hits = Physics.CapsuleCastAll(getLookObj().transform.position, getLookObj().transform.position + getLookObj().transform.forward *range, width, getLookObj().transform.forward);
                    foreach (RaycastHit hit in hits)
                    {
                        if (hit.distance <= range &&
                            hit.collider.gameObject.tag != "Player" &&
                            !hit.collider.isTrigger)
                        {
                            // Push physics, regardless of hittable
                            Rigidbody r;
                            if (r = hit.collider.GetComponent <Rigidbody>())
                            {
                                // Play around with a good factor here
                                r.AddForceAtPosition(getLookObj().forward * 300 * Time.deltaTime, getLookObj().position);
                            }
                            // Hit with hittable
                            Hittable hittable = hit.collider.GetComponentInParent <Hittable>();
                            if (hittable != null && hit.collider.gameObject.tag != "Item")   //Sometimes may hit our item that we are holding
                            {
                                print(hit.collider);
                                hittable.Hit(baseDamage, getLookObj().transform.forward, damageType);
                            }
                        }
                    }
                }
            }
        }
        else if (mouseDown && playerStats.GetMagic() > 0)
        {
            if (onCooldown)
            {
                if (releaseTime + Time.deltaTime >= timeToCooldown)
                {
                    onCooldown = false;
                }
                else
                {
                    releaseTime += Time.deltaTime;
                }
            }
            else
            {
                getPlayerAnim().SetBool(getControllerSide() + "MagicAttack", true);
                attacking = true;
                holdTime  = 0;
            }
        }
        else
        {
            releaseTime += Time.deltaTime;
        }
    }