Blipples

DamageManager - ObserverRpc Snippet

Feb 16th, 2025
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. /// <summary>
  2. /// Manages damage calculations and application in the game.
  3. /// </summary>
  4. public class DamageManager : NetworkBehaviour
  5. {
  6.     /// <summary>
  7.     /// Singleton Instance of the DamageManager.
  8.     /// </summary>
  9.     public static DamageManager Instance { get; private set; }
  10.    
  11.     /// <summary>
  12.     /// Initializes the singleton Instance.
  13.     /// </summary>
  14.     private void Awake()
  15.     {
  16.         if (Instance == null)
  17.         {
  18.             Instance = this;
  19.         }
  20.         else
  21.         {
  22.             Destroy(gameObject);
  23.         }
  24.     }
  25.    
  26.     /// <summary>
  27.     /// Uses a consumable item on the specified target.
  28.     /// </summary>
  29.     /// <param name="target">The target player controller.</param>
  30.     /// <param name="healthRestoreAmount">The amount of health to restore.</param>
  31.     /// <param name="manaRestoreAmount">The amount of mana to restore.</param>
  32.     /// <param name="index">The index of the item in the inventory.</param>
  33.     [ServerRpc(RequireOwnership = false)]
  34.     public void RPC_UseConsumable(PlayerController target, int healthRestoreAmount, int manaRestoreAmount, int index)
  35.     {
  36.         if (!IsServerInitialized) return;
  37.  
  38.         var playerNetworkObject = target.GetComponent<NetworkObject>();
  39.         if (playerNetworkObject == null)
  40.         {
  41.             Debug.LogError("Player network object not found.");
  42.             return;
  43.         }
  44.  
  45.         var playerController = target.GetComponent<PlayerController>();
  46.         if (playerController == null)
  47.         {
  48.             Debug.LogError("PlayerController not found on target.");
  49.             return;
  50.         }
  51.  
  52.         var restoredHealth = DamageCalculator.RestoreHealth(playerController, healthRestoreAmount);
  53.         var restoredMana = DamageCalculator.RestoreMana(playerController, manaRestoreAmount);
  54.  
  55.         if (restoredHealth != 0 || restoredMana != 0)
  56.         {
  57.             playerController.InventoryManager.Inventory.RemoveItem(playerController.InventoryManager.Inventory.InventoryList[index], index);
  58.            
  59.             // Notify clients of the consumable use
  60.             if(restoredHealth != 0)
  61.             {
  62.                 NotifyClientsOfDamage(playerNetworkObject, restoredHealth);
  63.             }
  64.             if(restoredMana != 0)
  65.             {
  66.                 NotifyClientsOfDamage(playerNetworkObject, restoredMana);
  67.             }
  68.         }
  69.     }
  70.  
  71.     /// <summary>
  72.     /// Notifies clients of the damage dealt to a target.
  73.     /// </summary>
  74.     /// <param name="target">The target network object receiving damage.</param>
  75.     /// <param name="damage">The amount of damage dealt.</param>
  76.     [ObserverRpc]
  77.     private void NotifyClientsOfDamage(NetworkObject target, int damage)
  78.     {
  79.         FloatingTextManager.Instance.RPC_SpawnDamageNumber(target, damage);
  80.     }
  81. ```
Advertisement
Add Comment
Please, Sign In to add comment