Example #1
0
    // TODO: Document (returns true if killed)
    public bool takeDamage(int amount, Object instigator = null)
    {
        if (m_health <= 0 || !m_canBeDamaged)
        {
            return(false);
        }

        if (PhotonNetwork.IsConnected && !photonView.IsMine)
        {
            return(false);
        }

        if (amount <= 0)
        {
            Debug.LogError("Cannot apply less than zero damage to a monster");
            return(false);
        }

        m_health = Mathf.Max(m_health - amount, 0);

        reaction.Ouch();

        bool bKilled = m_health <= 0;

        // Execute events before potentially destroying ourselves
        if (OnMonsterTakenDamage != null)
        {
            OnMonsterTakenDamage.Invoke(amount, bKilled);
        }

        // Nothing more needs to be done if still alive
        if (m_health > 0)
        {
            healthBar.SetHealth(m_health);
            return(false);
        }

        AnalyticsHelpers.reportMonsterDeath(this, instigator ? instigator.name : "unknown");

        // We can give gold to the local player, since we already
        // checked that this monster belongs to them
        PlayerController.localPlayer.giveGold(m_reward);

        if (!string.IsNullOrEmpty(m_deathScriptPrefab))
        {
            object[] spawnData = new object[1];
            spawnData[0] = GameManager.manager.getPlayerIdFromBoard(m_board);
            PhotonNetwork.Instantiate(m_deathScriptPrefab, transform.position, Quaternion.identity, 0, spawnData);
        }

        MonsterManager.destroyMonster(this);
        return(true);
    }