Example #1
0
        /// <summary>
        /// Increases the player's health by the health restore property of a health potion, then removes it.
        /// This only happens if there is at least one health potion in the inventory, and if the player's health is not maxed out.
        /// </summary>
        public void Heal()
        {
            // Get the quantity of health potions available by checking the quantity property attached to the item.
            var quantity = 0;

            if (m_HealthPotion.TryGetProperty("quantity", out var quantityProperty))
            {
                quantity = quantityProperty;
            }

            if (quantity <= 0 || m_PlayerHealth >= 100)
            {
                return;
            }

            // We'll confirm that Health Potion has the healthRestore property on it before accessing to prevent exceptions
            // and only reduce the quantity of the health potion if it can in fact restore health to the player.
            if (m_HealthPotion.TryGetProperty("healthRestore", out var healthRestoreProperty))
            {
                // Mathf.Min expects a float value but "healthRestore" is an integer property.
                // This isn't a problem, we can cast it to a floating value by either doing
                // an explicit cast or using the AsFloat method.
                var health = Mathf.Min(healthRestoreProperty.AsFloat() + m_PlayerHealth, 100f);

                m_PlayerHealth = health;

                // If there is only one quantity of health potion left,
                // remove the health potion item from the Inventory;
                // otherwise, reduce the quantity in the property by one.
                if (quantity == 1)
                {
                    InventoryManager.RemoveItem(m_HealthPotion);

                    // Once we remove the m_HealthPotion item from the InventoryManager, the reference is no longer useful.
                    m_HealthPotion = null;
                }
                else
                {
                    m_HealthPotion.AdjustProperty("quantity", -1);
                }

                // Remember, we don't need to set the refresh UI flag here since it will
                // be set by OnInventoryItemChanged as soon as the changes are applied.
            }
        }
Example #2
0
        /// <summary>
        /// Apply the sword's damage value to the player's health.
        /// </summary>
        public void TakeDamage()
        {
            // Get the damage value of the sword by checking the damage property attached to the item.
            // Even though we're pretty sure our sword has a damage property attached,
            // we use TryGetProperty to prevent crashes in case we made a typo or
            // if the catalogs are not set up as expected.
            float damage = 0;

            if (m_Sword.TryGetProperty("damage", out var damageProperty))
            {
                // We explicitly use AsFloat here to get the float stored in the property
                // but we would have the same result if we directly assigned the property to the float variable.
                damage = damageProperty.AsFloat();
            }

            // Get the quantity of swords available by checking the quantity property attached to the item.
            // We could use HasProperty and GetProperty to check the property's existence then retrieve its value
            // but it is faster to use TryGetProperty since it does both in a single call.
            var quantity = 0;

            if (m_Sword.TryGetProperty("quantity", out var quantityProperty))
            {
                quantity = quantityProperty;
            }

            if (quantity <= 0 || damage <= 0 || m_PlayerHealth < damage)
            {
                return;
            }

            // Apply the damage to playerHealth
            m_PlayerHealth -= damage;

            // If the sword doesn't have a durability property, no action needs to be taken.
            // If it does have a durability property, we will lower the sword's durability,
            // if it drops to 0, a single sword has been used.
            if (m_Sword.TryGetProperty("durability", out var durabilityProperty))
            {
                var durability = durabilityProperty.AsInt();
                if (durability == 1)
                {
                    // If there is only one quantity of sword left, remove the sword item from the Inventory,
                    // otherwise, reduce the quantity in the property by one and reset the durability property.
                    if (quantity == 1)
                    {
                        InventoryManager.RemoveItem(m_Sword);

                        // Once we remove the m_Sword item from the InventoryManager, the reference is no longer useful.
                        m_Sword = null;
                    }
                    else
                    {
                        // We use AdjustProperty to apply the change to the current property's value.
                        m_Sword.AdjustProperty("quantity", -1);

                        m_Sword.SetProperty("durability", 3);
                    }
                }
                else
                {
                    m_Sword.AdjustProperty("durability", -1);
                }
            }

            RefreshUI();
        }