Exemplo n.º 1
0
    public void DamageWall(int loss)
    {
        loss = loss - defence;
        if (loss < 0)
        {
            loss = 0;
        }
        hp -= loss;

        //Add a damage counter to show damage taken
        DamageCounter damageCounter = (Instantiate(canvasGroup, gameObject.transform.position, Quaternion.identity) as GameObject).GetComponent <DamageCounter>();

        damageCounter.setPosition(gameObject.transform.position);
        damageCounter.setDamage(Mathf.Clamp(loss, 0, loss));
        damageCounter.GetComponentInChildren <Image>().color = Color.red;
        if ((double)loss / (double)maxHp > 0.5)
        {
            damageCounter.GetComponentInChildren <Image>().color = Color.HSVToRGB(0.08f, 1, 1);
        }

        if (hp <= 0)
        {
            if (this.tag != "Wall")
            {
                //system.transform.position = this.transform.position;
                //system.Emit(10);
                Player.instance.AddExperience(experienceValue);

                int[] itemsDropped = new int[4];

                for (int i = 0; i < 4; i++)
                {
                    if (Random.Range(0, rarity) == 0 && itemDrop.Length > i)
                    {
                        itemsDropped[i] = itemDrop[i];
                    }
                    else
                    {
                        itemsDropped[i] = -1;
                    }
                }
                if (!(itemsDropped[0] == -1 && itemsDropped[1] == -1 && itemsDropped[2] == -1 && itemsDropped[3] == -1))
                {
                    //Handles the case when an enemy drops something on an exisiting bag
                    GameObject[] pickups   = GameObject.FindGameObjectsWithTag("Pickup");
                    bool         bagExists = false;
                    for (int i = 0; i < pickups.Length; i++)
                    {
                        if (pickups[i].transform.position == transform.position)
                        {
                            bagExists = true;
                            int    pickupsIndex   = 0;
                            Pickup matchingPickup = pickups[i].GetComponent <Pickup>();
                            for (int j = 0; j < 4; j++)
                            {
                                if (matchingPickup.itemId[i] == -1)
                                {
                                    matchingPickup.itemId[i] = itemsDropped[pickupsIndex];
                                    pickupsIndex++;
                                }
                            }
                        }
                    }

                    if (!bagExists)
                    {
                        BoardManager.instance.AddPickup((int)transform.position.x, (int)transform.position.y, itemsDropped);
                    }
                }
            }
            Delete();
            UpdateHealthBar();
        }
    }