Project · Unity companions

Unity health rules companion

A complete console project with seven independent health and lease-reset checks; no UnityEngine dependency.

All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.

Program.cs

using System;
Console.WriteLine(HealthChecks.Run() + " health checks passed");

Domain/HealthState.cs

using System;
namespace SharpRune.UnityHealth
{
    public sealed class HealthState
    {
        public int Maximum { get; private set; }
        public int Current { get; private set; }
        public bool Alive { get { return Current > 0; } }
        public HealthState(int maximum)
        {
            if (maximum < 1) throw new ArgumentOutOfRangeException(nameof(maximum));
            Maximum = maximum; Current = maximum;
        }
        public void Damage(int amount)
        {
            if (amount < 0) throw new ArgumentOutOfRangeException(nameof(amount));
            Current = Math.Max(0, Current - amount);
        }
        public void Reset() { Current = Maximum; }
    }
}

Checks/HealthChecks.cs

using System;
using SharpRune.UnityHealth;
public static class HealthChecks
{
    public static int Run()
    {
        int passed = 0;
        void Check(bool condition, string name) { if (!condition) throw new InvalidOperationException(name); Console.WriteLine("PASS " + name); passed++; }
        var fresh = new HealthState(10);
        Check(fresh.Current == 10 && fresh.Alive, "fresh health");
        fresh.Damage(3); Check(fresh.Current == 7, "damage subtracts");
        fresh.Damage(100); Check(fresh.Current == 0 && !fresh.Alive, "overkill clamps");
        var unchanged = new HealthState(10); bool rejected = false;
        try { unchanged.Damage(-1); } catch (ArgumentOutOfRangeException) { rejected = true; }
        Check(rejected && unchanged.Current == 10, "negative damage rejected");
        var first = new HealthState(10); var second = new HealthState(10); first.Damage(3);
        Check(first.Current == 7 && second.Current == 10, "states independent");
        rejected = false; try { new HealthState(0); } catch (ArgumentOutOfRangeException) { rejected = true; }
        Check(rejected, "invalid capacity rejected");
        fresh.Reset(); Check(fresh.Current == 10 && fresh.Alive, "lease reset");
        return passed;
    }
}

Companion.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable></PropertyGroup>
</Project>