Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Manages damage calculations and application in the game.
- /// </summary>
- public class DamageManager : NetworkBehaviour
- {
- /// <summary>
- /// Singleton Instance of the DamageManager.
- /// </summary>
- public static DamageManager Instance { get; private set; }
- /// <summary>
- /// Initializes the singleton Instance.
- /// </summary>
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- }
- else
- {
- Destroy(gameObject);
- }
- }
- /// <summary>
- /// Uses a consumable item on the specified target.
- /// </summary>
- /// <param name="target">The target player controller.</param>
- /// <param name="healthRestoreAmount">The amount of health to restore.</param>
- /// <param name="manaRestoreAmount">The amount of mana to restore.</param>
- /// <param name="index">The index of the item in the inventory.</param>
- [ServerRpc(RequireOwnership = false)]
- public void RPC_UseConsumable(PlayerController target, int healthRestoreAmount, int manaRestoreAmount, int index)
- {
- if (!IsServerInitialized) return;
- var playerNetworkObject = target.GetComponent<NetworkObject>();
- if (playerNetworkObject == null)
- {
- Debug.LogError("Player network object not found.");
- return;
- }
- var playerController = target.GetComponent<PlayerController>();
- if (playerController == null)
- {
- Debug.LogError("PlayerController not found on target.");
- return;
- }
- var restoredHealth = DamageCalculator.RestoreHealth(playerController, healthRestoreAmount);
- var restoredMana = DamageCalculator.RestoreMana(playerController, manaRestoreAmount);
- if (restoredHealth != 0 || restoredMana != 0)
- {
- playerController.InventoryManager.Inventory.RemoveItem(playerController.InventoryManager.Inventory.InventoryList[index], index);
- // Notify clients of the consumable use
- if(restoredHealth != 0)
- {
- NotifyClientsOfDamage(playerNetworkObject, restoredHealth);
- }
- if(restoredMana != 0)
- {
- NotifyClientsOfDamage(playerNetworkObject, restoredMana);
- }
- }
- }
- /// <summary>
- /// Notifies clients of the damage dealt to a target.
- /// </summary>
- /// <param name="target">The target network object receiving damage.</param>
- /// <param name="damage">The amount of damage dealt.</param>
- [ObserverRpc]
- private void NotifyClientsOfDamage(NetworkObject target, int damage)
- {
- FloatingTextManager.Instance.RPC_SpawnDamageNumber(target, damage);
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment