MainHud/CombatManager.cs

Game combat manager utility. Provides static methods to apply damage to a Player, heal players or AI with a Weapon, and simple random checks for critical hits and blocking. It updates player HP and defense, handles magic shield effects and writes log messages via GameManager.AddLog; it also marks local player damage on the GameManager instance.

NetworkingFile Access
using System;
using System.Collections.Generic;
using Keyboard_Warriors_.Models;
using Keyboard_Warriors_.Systems; // Added to access your EventLog system

namespace Keyboard_Warriors_.Managers;

public static class CombatManager
{
	private static Random rand = new Random();

	public static void applyDamage( ref Player target, int damage )
	{

		if ( damage  <= 0 ) return;

		var manager = Sandbox.Game.ActiveScene.Get<GameManager>();
		if ( manager != null && target == manager.LocalPlayer )
		{
			manager.HasPlayerTakenAnyDamage = true;
			
		}

		if ( target.isMagicBlocking )
		{
			if ( target.activeMagicShield == "Fortify" )
			{
				GameManager.AddLog( "  Fortify blocks incoming damage!" );
				damage = 0;
			}
			else if ( target.activeMagicShield == "Magic Wall" )
			{
				GameManager.AddLog( "  Magic Wall reduces incoming damage!" );
				damage /= 2;
			}
		}

		if ( target.defense > 0 )
		{
			int absorbed = Math.Min( target.defense, damage );
			target.defense -= absorbed;
			damage -= absorbed;
			GameManager.AddLog( $"  {target.name}'s armor absorbed {absorbed} damage!" );
		}

		if ( damage > 0 )
		{
			target.hp -= damage;
			GameManager.AddLog( $"  {target.name} takes {damage} HP damage!" );
		}

		if ( target.hp < 0 ) target.hp = 0;
	}

	public static void healPlayer( ref Player p, Weapon w )
	{
		p.hp += 10 + w.healBonus;
		if ( p.hp > 100 ) p.hp = 100;
	}

	public static void healAI( ref Player ai, Weapon w )
	{
		ai.hp += 10 + w.healBonus;
		if ( ai.hp > 100 ) ai.hp = 100;
	}

	public static bool isCriticalHit()
	{
		return rand.Next( 100 ) < 5;
	}

	public static bool attemptBlock( float modifier = 1.0f )
	{
		return rand.Next( 100 ) < (int)(50 * modifier);
	}
}