Exemple #1
0
    private void DamageRadius()
    {
        Vector2           target = GetComponent <PointFollower>().target;
        List <GameObject> units  = ObjectContainer.GetAllUnits();

        foreach (GameObject elt in units)
        {
            // elt == user for don't hurt self, CompareTag("Enemy") for any ally
            if (userTag == "Enemy" && elt.CompareTag("Enemy"))
            {
                continue;
            }

            RaycastHit2D hit = Physics2D.Raycast(target, (Vector2)elt.transform.position - target, Globals.GRENADE_YELLOW_RANGE);
            if (hit.collider != null && hit.collider.gameObject == elt)
            {
                UnitStatus status = elt.GetComponent <UnitStatus>();
                int        damage = ActionManager.DistanceToLevel(Vector2.Distance(elt.transform.position, target));
                if (userTag == "Enemy" && elt.CompareTag("Player"))
                {
                    damage = 1;
                }
                if (status)
                {
                    status.DamageHealth(damage);
                }
            }
        }
    }
Exemple #2
0
    public IEnumerator Knife(GameObject unit, Vector2 target)
    {
        Vector2      origin    = unit.transform.position;
        Vector2      direction = target - (Vector2)unit.transform.position;
        RaycastHit2D hit       = Physics2D.Raycast(origin, direction, Globals.KNIFE_RANGE, ~LayerMask.GetMask("Player"));

        if (hit.collider != null)
        {
            UnitStatus targetHit = hit.collider.GetComponent <UnitStatus>();
            //AutoMover targetMover = hit.collider.GetComponent<AutoMover>();
            //UnitStatus attacker = unit.GetComponent<UnitStatus>();
            if (targetHit != null /*&& targetMover.GetAwareness() != AutoMover.State.Alert*/)
            {
                SoundManager.instance.Play(SoundManager.Sound.Knife);
                targetHit.DamageHealth(3);
                // There is a risk of being hurt attempting to knife an enemy
                //if(attacker && Random.Range(0, 11) == 0)
                //    attacker.DamageHealth(1);
            }

            // May want to make amount of noise random
            GameObject tempNoise = Instantiate(Globals.NOISE, unit.transform.position, Quaternion.identity);
            tempNoise.GetComponent <Noise>().Initialize(unit.CompareTag("Player"), Globals.KNIFE_VOLUME, Noise.Source.Knife);

            // Create and format line
            GameObject shotLine = ShapeManager.DrawLine(origin, hit.point, Globals.BRIGHT_RED, unit.transform);
            shotLine.transform.parent = unit.transform;
            LineRenderer shotRenderer = shotLine.GetComponent <LineRenderer>();
            shotRenderer.startWidth = shotRenderer.endWidth = 0.07f;

            shotLine.AddComponent <AutoVanish>().timeToLive = 0.1f;
        }

        yield return(new WaitForSeconds(0.25f));

        Sidebar.instance.FinishAction();
    }
Exemple #3
0
    // Single gunshot, usable by both player and enemy
    public static void Gun(GameObject unit, Vector2 target)
    {
        bool attackerIsPlayer = unit.GetComponent <PlayerMover>();

        // Prevent attacker from hitting themselves
        BoxCollider2D unitCollider = unit.GetComponent <BoxCollider2D>();

        unitCollider.enabled = false;
        int instanceId = unitCollider.GetInstanceID();

        Vector2 shotOrigin = unit.transform.position;
        //(Vector2)unit.transform.position + unit.GetComponent<GridMover>().GetRotator().FrontOffset();

        // 3 Focus -> 10 deg Error, 2 Focus -> 20 deg Error
        // 1 Focus -> 30 deg Error, 0 Focus -> Can't attack
        float marginOfError = attackerIsPlayer ? 13 : 15; //+ (10 * (3 - unit.GetComponent<FieldUnit>().party[0].focus)); // min 10 err, max 30 / formerly memberIndex

        // Calculate if there's a clear line of sight
        Vector2 direction = target - (Vector2)unit.transform.position;
        // RaycastHit2D hit = Physics2D.Raycast(shotOrigin, direction/*, 9, mask*/);

        float angle = Vector2.Angle(unit.GetComponent <Rotator>().FrontOffset(), direction);

        // arg1 of Angle used to be unit.GetComponent<GridMover>().GetRotator().FrontOffset()

        // Generate an actual shot
        angle    += Random.Range(0, marginOfError) * (Random.Range(0, 2) == 0 ? 1 : -1); // add margin of error
        direction = Quaternion.AngleAxis(angle, Vector3.forward) * direction;            // This flattens the shot somehow
        RaycastHit2D hit = Physics2D.Raycast(shotOrigin, direction, 255, ~LayerMask.GetMask(LayerMask.LayerToName(unit.layer)));

        SoundManager.instance.Play(SoundManager.Sound.Gun, instanceId);
        // Debug.Log("layer: " + LayerMask.LayerToName(unit.layer));

        if (hit.collider != null)
        {
            // Make sure to check WHAT is being hit... Player? Wall? Friendly fire?

            // source.PlayOneShot(gunshot); // play sound
            // Camera.main.GetComponent<Jerk>().Shake(1); // Replace this with something better

            UnitStatus targetHit = hit.collider.GetComponent <UnitStatus>();
            if (targetHit)
            {
                targetHit.DamageHealth(); // formerly memberIndex
                AutoMover autoMover = hit.collider.GetComponent <AutoMover>();
                if (autoMover)
                {
                    autoMover.VisualToPosition(Grapher.RoundedVector(unit.transform.position));
                }
            }

            GameObject tempNoise = Instantiate(Globals.NOISE, unit.transform.position, Quaternion.identity);
            tempNoise.GetComponent <Noise>().Initialize(attackerIsPlayer, Globals.GUN_VOLUME, Noise.Source.Gun);

            // Create and format line
            GameObject shotLine = ShapeManager.DrawLine(shotOrigin, hit.point, Globals.BRIGHT_WHITE, unit.transform);
            shotLine.transform.parent = unit.transform;
            LineRenderer shotRenderer = shotLine.GetComponent <LineRenderer>();
            shotRenderer.startWidth = shotRenderer.endWidth = 0.07f;

            shotLine.AddComponent <AutoVanish>().timeToLive = 0.1f;
        }

        unit.GetComponent <BoxCollider2D>().enabled = true;
    }