Ejemplo n.º 1
0
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Enemy"))
        {
            EnemyClass enemy  = other.gameObject.GetComponent <EnemyClass>();
            float      attack = Damage + _player.Weapon.Damage -
                                (enemy.GetArmor() + enemy.GetDefence());
            if (attack <= 0)
            {
                attack = 10;
            }

            if (!enemy.DecreaseHealth(attack))
            {
                List <int> spawnTable      = enemy.GetCurrencySpawnTable();
                int        spawnedCurrency = 0;
                int        randomInt       = Random.Range(0, 100);
                if (randomInt < 50)
                {
                    spawnedCurrency = spawnTable[0];
                }
                else if (randomInt < 75)
                {
                    spawnedCurrency = spawnTable[1];
                }
                else if (randomInt < 90)
                {
                    spawnedCurrency = spawnTable[2];
                }
                else if (randomInt < 96)
                {
                    spawnedCurrency = spawnTable[3];
                }
                else
                {
                    spawnedCurrency = spawnTable[4];
                }

                Debug.Log($"SPAWNED CURRENCY: {spawnedCurrency}");


                ClampToParent go = Instantiate(enemy.GetClampToParent(), enemy.GetCanvas(), false);
                go.StartPoint = enemy.GetGameObject().transform.position;
                TextMeshProUGUI tmp = go.gameObject.GetComponent <TextMeshProUGUI>();
                tmp.text = $"+{spawnedCurrency}";
                _player.gameObject.GetComponent <PlayerWallet>().ReceiveCoinsGG(spawnedCurrency);
                // Destroy(enemy.GetGameObject());
            }

            GameObject particles = Instantiate(Particles, null, false);
            particles.transform.position = transform.position;
            _mainCamera.Shake(CameraShakeMagnitude, CameraShakeDuration);
            StartCoroutine(DestroySelf());
        }
    }
Ejemplo n.º 2
0
    public static bool PlayerAttack(PlayerAttack player)
    {
        // check for collider based on player attack point position and attacking range
        Collider2D collider =
            Physics2D.OverlapCircle(player.AttackPoint.position, player.AttackRange, player.AttackLayerMask);

        if (collider)
        {
            // if collider exists, do logic to calculate damage
            EnemyClass target = collider.gameObject.GetComponent <EnemyClass>();
            if (target != null)
            {
                float attack = player.Attack - (target.GetDefence() + target.GetArmor());
                if (player.Weapon != null)
                {
                    attack += player.Weapon.Damage;
                }

                if (!target.DecreaseHealth(attack))
                {
                    // GameObject.Destroy(collider.gameObject);
                    List <int> spawnTable      = target.GetCurrencySpawnTable();
                    int        spawnedCurrency = 0;
                    int        randomInt       = Random.Range(0, 100);
                    if (randomInt < 50)
                    {
                        spawnedCurrency = spawnTable[0];
                    }
                    else if (randomInt < 75)
                    {
                        spawnedCurrency = spawnTable[1];
                    }
                    else if (randomInt < 90)
                    {
                        spawnedCurrency = spawnTable[2];
                    }
                    else if (randomInt < 96)
                    {
                        spawnedCurrency = spawnTable[3];
                    }
                    else
                    {
                        spawnedCurrency = spawnTable[4];
                    }

                    GameObject.Instantiate(target.GetClampToParent(), target.GetCanvas(), false);
                    Debug.Log($"SPAWNED CURRENCY: {spawnedCurrency}");
                }
                else
                {
                    Vector2 currentTargetPosition = target.GetGameObject().transform.position;
                    Vector2 currentPlayerPosition = player.gameObject.transform.position;
                    float   x = currentTargetPosition.x;
                    float   y = currentTargetPosition.y;

                    if (x - currentPlayerPosition.x <= 0.1)
                    {
                        x -= player.KnockbackDistance;
                    }
                    else if (x - currentPlayerPosition.x >= 0.1)
                    {
                        x += player.KnockbackDistance;
                    }

                    // target.GetGameObject().transform.position = new Vector2(x, y + player.KnockbackDistance);
                    target.GetGameObject().transform.position = Vector2.MoveTowards(currentTargetPosition,
                                                                                    new Vector2(x, y + player.KnockbackDistance), player.KnockbackSpeed * Time.deltaTime);
                }
            }
        }


        return(false);
    }