コード例 #1
0
ファイル: GunShooting.cs プロジェクト: Epicguru/NotQuiteDead
    private void Hit_Hitscan(Vector2 end, int bullets)
    {
        objects.Clear();
        startPos.Set(transform.position.x, transform.position.y);
        RaycastHit2D[] hits = Physics2D.LinecastAll(startPos, end);

        int penetrationCount = 0;

        trailEnd.Set(end.x, end.y);

        foreach (RaycastHit2D hit in hits)
        {
            if (penetrationCount >= Damage.Penetration)
            {
                break;
            }

            // A hit to a collider...
            Health h = hit.collider.gameObject.GetComponentInParent <Health>();

            if (h == null)
            {
                //Debug.Log("Object hit has no health, cannot be destroyed!");
                // DEBATE - Do we allow to shoot through non destructible objects, that may have multiple colliders?
                // Hell no!
                // UNLESS - the collider is a trigger!

                if (hit.collider.isTrigger)
                {
                    //Debug.Log("Collider we hit is a trigger, continue...");
                    continue;
                }

                trailEnd.Set(hit.point.x, hit.point.y);
                float angle = Mathf.Atan2(hit.normal.y, hit.normal.x) * Mathf.Rad2Deg + 180;
                HitEffect.Spawn(hit.point, angle, hit.collider, true);
                break;
            }

            // Do not allow to hit local player : Maybe some sort of team system??? EDIT - Done.
            Player p = h.GetComponent <Player>();
            if (p != null)
            {
                if (p == Player.Local._Player)
                {
                    continue;
                }
                if (Teams.I.PlayersInSameTeam(Player.Local.Name, p.Name))
                {
                    continue; // Do not allow team damage from guns. TODO IMPLEMENT GAMES RULES.
                }
            }
            if (!h.CanHit)
            {
                continue;
            }

            if (h.CannotHit.Contains(hit.collider))
            {
                continue;
            }

            if (hit.collider.gameObject.GetComponent <NeverHitMe>() != null)
            {
                continue;
            }

            if (objects.Contains(h)) // Do not allow multiple hits to one object.
            {
                continue;
            }

            if (hit.collider.gameObject.GetComponentInParent <Item>() != null)
            {
                // Is item, may be held in hands. Ignore.
                continue;
            }

            objects.Add(h);

            float damage = GetDamage(startPos, hit.point, penetrationCount, bullets);
            if (damage != 0)
            {
                CmdHitObject(h.gameObject, Player.Local.Name + ":" + gun.Item.Prefab, damage);
                float angle = Mathf.Atan2(hit.normal.y, hit.normal.x) * Mathf.Rad2Deg + 180;
                HitEffect.Spawn(hit.point, angle, hit.collider, true);
            }

            penetrationCount++;
            if (penetrationCount >= Damage.Penetration)
            {
                trailEnd.Set(hit.point.x, hit.point.y);
            }

            //Debug.Log("Hit object, can penetrate :" + h.CanPenetrate);

            if (!h.CanPenetrate)
            {
                trailEnd.Set(hit.point.x, hit.point.y);
                break;
            }
        }

        // Spawn visual line...
        GameObject GO = ObjectPool.Instantiate(GunEffects.Instance.BulletTrail.gameObject, PoolType.BULLET_PATH);

        GO.GetComponent <BulletPath>().Setup(GetBulletSpawn().position, trailEnd);

        CmdSpawnBulletTrail(GetBulletSpawn().position, trailEnd);

        objects.Clear();
    }
コード例 #2
0
    public void Update()
    {
        Vector2 a        = transform.position;
        Vector2 movement = transform.right * Time.deltaTime * Speed;
        Vector2 b        = a + movement;

        UpdateAlpha();

        RaycastHit2D hit;
        RaycastHit2D hit2;
        bool         collides         = DetectCollision(Tip.transform.position, (Vector2)Tip.transform.position + movement, out hit);
        bool         collidesInternal = DetectCollision(transform.position, Tip.transform.position, out hit2);

        if (collidesInternal)
        {
            // Check range...
            float dst = Vector2.Distance(hit2.point, startPos);
            if (dst > MaxRange)
            {
                Recycle();
                return;
            }

            // Spawn hit effect.
            float angle = Mathf.Atan2(hit2.normal.y, hit2.normal.x) * Mathf.Rad2Deg + 180;
            HitEffect.Spawn(hit2.point, angle, hit2.collider, true);

            // Deal damage to the object if we are on the server.
            HitObject(hit2);

            // Pool
            Recycle();

            // Exit early.
            return;
        }
        else if (collides)
        {
            // Check range...
            float dst = Vector2.Distance(hit.point, startPos);
            if (dst > MaxRange)
            {
                Recycle();
                return;
            }

            // Spawn hit effect.
            float angle = Mathf.Atan2(hit.normal.y, hit.normal.x) * Mathf.Rad2Deg + 180;
            HitEffect.Spawn(hit.point, angle, hit.collider, true);

            // Deal damage to the object if we are on the server.
            HitObject(hit);

            // Pool
            Recycle();

            // Exit early.
            return;
        }
        else
        {
            transform.position = b;
        }

        CheckRange();
    }