Esempio n. 1
0
    private void HandleAttack()
    {
        Vector3 attackDir = animatedWalker.GetLastMoveVector();

        if (Input.GetMouseButtonDown(0))
        {
            attackDir = (UtilsClass.GetMouseWorldPosition() - GetPosition()).normalized;
        }

        if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
        {
            // Attack
            SetStateBusy();

            EnemyHandler enemyHandler = EnemyHandler.GetClosestEnemy(GetPosition() + attackDir * 4f, 20f);
            if (enemyHandler != null)
            {
                enemyHandler.Damage(this);
                attackDir          = (enemyHandler.GetPosition() - GetPosition()).normalized;
                transform.position = enemyHandler.GetPosition() + attackDir * -12f;
            }
            else
            {
                transform.position = transform.position + attackDir * 4f;
            }

            /*
             * unitAnimation.PlayAnimForced(UnitAnimType.GetUnitAnimType("dBareHands_PunchStartup"), attackDir, 2f, (UnitAnim unitAnim) => {
             *  unitAnimation.PlayAnimForced(UnitAnimType.GetUnitAnimType("dBareHands_PunchQuickAttack"), attackDir, 1f, (UnitAnim unitAnim2) => SetStateNormal(), null, null);
             * }, null, null);
             */

            UnitAnimType activeAnimType = unitAnimation.GetActiveAnimType();
            switch (activeAnimType.GetName())
            {
            default:
                unitAnimation.PlayAnimForced(UnitAnimType.GetUnitAnimType("dBareHands_PunchQuick"), attackDir, 1f, (UnitAnim unitAnim2) => SetStateNormal(), null, null);
                break;

            case "dBareHands_PunchQuick":
                unitAnimation.PlayAnimForced(UnitAnimType.GetUnitAnimType("dBareHands_KickQuick"), attackDir, 1f, (UnitAnim unitAnim2) => SetStateNormal(), null, null);
                break;
            }

            //unitAnimation.PlayAnimForced(UnitAnimType.GetUnitAnimType("dBareHands_PunchQuickAttack"), attackDir, 1f, (UnitAnim unitAnim2) => SetStateNormal(), null, null);

            /*
             * Transform swordSlashTransform = Instantiate(GameAssets.i.pfSwordSlash, GetPosition() + attackDir * 13f, Quaternion.Euler(0, 0, UtilsClass.GetAngleFromVector(attackDir)));
             * swordSlashTransform.GetComponent<SpriteAnimator>().onLoop = () => Destroy(swordSlashTransform.gameObject);
             *
             * UnitAnimType activeAnimType = unitAnimation.GetActiveAnimType();
             * if (activeAnimType == GameAssets.UnitAnimTypeEnum.dSwordTwoHandedBack_Sword) {
             *  swordSlashTransform.localScale = new Vector3(swordSlashTransform.localScale.x, swordSlashTransform.localScale.y * -1, swordSlashTransform.localScale.z);
             *  unitAnimation.PlayAnimForced(GameAssets.UnitAnimTypeEnum.dSwordTwoHandedBack_Sword2, attackDir, 1f, (UnitAnim unitAnim) => SetStateNormal(), null, null);
             * } else {
             *  unitAnimation.PlayAnimForced(GameAssets.UnitAnimTypeEnum.dSwordTwoHandedBack_Sword, attackDir, 1f, (UnitAnim unitAnim) => SetStateNormal(), null, null);
             * }
             */
        }
    }
    private void HandleAttack()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // Attack
            SetStateAttacking();

            Vector3 attackDir = (UtilsClass.GetMouseWorldPosition() - GetPosition()).normalized;

            EnemyHandler enemyHandler = EnemyHandler.GetClosestEnemy(GetPosition() + attackDir * 4f, 20f);
            bool         hitEnemy;
            if (enemyHandler != null)
            {
                //enemyHandler.Damage(this);
                hitEnemy           = true;
                attackDir          = (enemyHandler.GetPosition() - GetPosition()).normalized;
                transform.position = enemyHandler.GetPosition() + attackDir * -12f;
            }
            else
            {
                hitEnemy           = false;
                transform.position = transform.position + attackDir * 4f;
            }

            float attackAngle = UtilsClass.GetAngleFromVectorFloat(attackDir);

            // Play attack animation
            if (playerBase.IsPlayingPunchAnimation())
            {
                // Play Kick animation since punch animation is currently active
                playerBase.PlayKickAnimation(attackDir, (Vector3 impactPosition) => {
                    if (hitEnemy)
                    {
                        impactPosition          += UtilsClass.GetVectorFromAngle((int)attackAngle) * 4f;
                        Transform impactEffect   = Instantiate(GameAssets.i.pfImpactEffect, impactPosition, Quaternion.identity);
                        impactEffect.eulerAngles = new Vector3(0, 0, attackAngle - 90);
                    }
                }, SetStateNormal);
            }
            else
            {
                // Play Punch animation
                playerBase.PlayPunchAnimation(attackDir, (Vector3 impactPosition) => {
                    if (hitEnemy)
                    {
                        impactPosition          += UtilsClass.GetVectorFromAngle((int)attackAngle) * 4f;
                        Transform impactEffect   = Instantiate(GameAssets.i.pfImpactEffect, impactPosition, Quaternion.identity);
                        impactEffect.eulerAngles = new Vector3(0, 0, attackAngle - 90);
                    }
                }, SetStateNormal);
            }
        }
    }
Esempio n. 3
0
    private void CharacterAimHandler_OnShoot(object sender, CharacterAimHandler.OnShootEventArgs e)
    {
        // Player shot weapon
        Shoot_Flash.AddFlash(e.gunEndPointPosition);
        WeaponTracer.Create(e.gunEndPointPosition, e.shootPosition);
        UtilsClass.ShakeCamera(.6f, .05f);

        // Any enemy hit?
        RaycastHit2D raycastHit = Physics2D.Raycast(e.gunEndPointPosition, (e.shootPosition - e.gunEndPointPosition).normalized, Vector3.Distance(e.gunEndPointPosition, e.shootPosition));

        if (raycastHit.collider != null)
        {
            EnemyHandler enemyHandler = raycastHit.collider.gameObject.GetComponent <EnemyHandler>();
            if (enemyHandler != null)
            {
                // Hit enemy
                int  damageAmount = UnityEngine.Random.Range(100, 200);
                bool isCritical   = UnityEngine.Random.Range(0, 100) < 30;
                if (isCritical)
                {
                    damageAmount *= 2;
                }
                // Deal damage
                enemyHandler.Damage(this, damageAmount);
                DamagePopup.Create(enemyHandler.GetPosition(), damageAmount, isCritical);
            }
        }
    }
Esempio n. 4
0
    private void HandleAttack()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // Attack
            SetStateAttacking();

            Vector3 attackDir = (GetMouseWorldPosition() - GetPosition()).normalized;

            EnemyHandler enemyHandler = EnemyHandler.GetClosestEnemy(GetPosition() + attackDir * 4f, 20f);
            if (enemyHandler != null)
            {
                //enemyHandler.Damage(this);
                if (enemyHandler.IsDead())
                {
                    OnEnemyKilled?.Invoke(this, EventArgs.Empty);
                }
                attackDir          = (enemyHandler.GetPosition() - GetPosition()).normalized;
                transform.position = enemyHandler.GetPosition() + attackDir * -12f;
            }
            else
            {
                transform.position = transform.position + attackDir * 4f;
            }

            Transform swordSlashTransform = Instantiate(GameAssets.i.pfSwordSlash, GetPosition() + attackDir * 13f, Quaternion.Euler(0, 0, GetAngleFromVector(attackDir)));
            swordSlashTransform.GetComponent <SpriteAnimator>().onLoop = () => Destroy(swordSlashTransform.gameObject);

            UnitAnimType activeAnimType = characterBase.GetUnitAnimation().GetActiveAnimType();
            if (activeAnimType == GameAssets.UnitAnimTypeEnum.dSwordTwoHandedBack_Sword)
            {
                swordSlashTransform.localScale = new Vector3(swordSlashTransform.localScale.x, swordSlashTransform.localScale.y * -1, swordSlashTransform.localScale.z);
                characterBase.GetUnitAnimation().PlayAnimForced(GameAssets.UnitAnimTypeEnum.dSwordTwoHandedBack_Sword2, attackDir, 1f, (UnitAnim unitAnim) => SetStateNormal(), null, null);
            }
            else
            {
                characterBase.GetUnitAnimation().PlayAnimForced(GameAssets.UnitAnimTypeEnum.dSwordTwoHandedBack_Sword, attackDir, 1f, (UnitAnim unitAnim) => SetStateNormal(), null, null);
            }
        }
    }
Esempio n. 5
0
    public static EnemyHandler GetClosestEnemy(Vector3 position, float maxRange)
    {
        EnemyHandler closest = null;

        foreach (EnemyHandler enemyHandler in enemyHandlerList)
        {
            if (enemyHandler.IsDead())
            {
                continue;
            }
            if (Vector3.Distance(position, enemyHandler.GetPosition()) <= maxRange)
            {
                if (closest == null)
                {
                    closest = enemyHandler;
                }
                else
                {
                    if (Vector3.Distance(position, enemyHandler.GetPosition()) < Vector3.Distance(position, closest.GetPosition()))
                    {
                        closest = enemyHandler;
                    }
                }
            }
        }
        return(closest);
    }
Esempio n. 6
0
 public void Damage(EnemyHandler enemyHandler)
 {
     Damage(enemyHandler.GetPosition());
 }
Esempio n. 7
0
    private EnemyHandler GetClosestEnemyHandler(Vector3 playerPosition)
    {
        const float  maxDistance = 50f;
        EnemyHandler closest     = null;

        foreach (EnemyHandler enemyMeleeHandler in enemyMeleeHandlerList)
        {
            if (Vector3.Distance(playerPosition, enemyMeleeHandler.GetPosition()) > maxDistance)
            {
                continue;
            }

            if (closest == null)
            {
                closest = enemyMeleeHandler;
            }
            else
            {
                if (Vector3.Distance(playerPosition, enemyMeleeHandler.GetPosition()) < Vector3.Distance(playerPosition, closest.GetPosition()))
                {
                    closest = enemyMeleeHandler;
                }
            }
        }
        return(closest);
    }