H073.StateKit 1.0.0

Prefix Reserved
dotnet add package H073.StateKit --version 1.0.0                
NuGet\Install-Package H073.StateKit -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="H073.StateKit" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add H073.StateKit --version 1.0.0                
#r "nuget: H073.StateKit, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install H073.StateKit as a Cake Addin
#addin nuget:?package=H073.StateKit&version=1.0.0

// Install H073.StateKit as a Cake Tool
#tool nuget:?package=H073.StateKit&version=1.0.0                

StateKit

StateKit is a robust state management library designed to handle state transitions efficiently in any application. It offers two distinct approaches for state management:

  1. State Management with Context (StateCTracker<T>): Designed for scenarios where states need to share and modify common data (Context).
  2. Simple State Management (StateTracker): Ideal for lightweight state machines where no shared data (Context) is required.

Features

  • Versatile State Management:
    • Use StateCTracker<T> for shared context.
    • Use StateTracker for simple state handling.
  • Modular State Logic:
    • Encapsulate logic in StateBase or StateCBase<T> classes.
  • Seamless Transitions:
    • Transition between states easily with CheckTransitions.
  • Lightweight and Flexible:
    • Handle both reference and value types (StateCTracker<T>).

Installation

You can install the package via NuGet:

dotnet add package StateKit

Why Two Types of State Management?

1. State Management with Context

  • States share a common context (T), which allows them to read and modify shared data.
  • Advantages:
    • Useful for managing complex systems where states depend on shared information.
    • Reduces external dependencies by keeping shared data within the state machine.
  • Example Use Case:
    • A game where a player’s health, position, or inventory is shared across multiple states.

2. Simple State Management

  • States are independent and do not require shared data.
  • Advantages:
    • Lightweight and straightforward.
    • Suitable for simpler applications or systems with isolated states.
  • Example Use Case:
    • A traffic light system with states like Red, Yellow, and Green.

Usage

1. State Management with Context (StateCTracker<T>)

Define Contextual States
using StateKit;

public class Player
{
    public int Health { get; set; }
    public bool IsMoving { get; set; }
}

public class IdleState : StateCBase<Player>
{
    public override void Enter()
    {
        Console.WriteLine("Player is now idle.");
    }

    public override void Update(float delta)
    {
        Console.WriteLine("Player stands does nothing");
    }

    public override StateCBase<Player>? CheckTransitions()
    {
        if (Context.IsMoving)
        {
            return new WalkingState();
        }
        return null;
    }
}

public class WalkingState : StateCBase<Player>
{
    public override void Enter()
    {
        Console.WriteLine("Player started walking.");
    }

    public override void Update(float delta)
    {
        Console.WriteLine("Player is walking...");
    }

    public override StateCBase<Player>? CheckTransitions()
    {
        if (!Context.IsMoving)
        {
            return new IdleState();
        }
        return null;
    }
}
Initialize and Use StateCTracker<T>
Player player = new Player { Health = 100, IsMoving = false };
StateCTracker<Player> tracker = new StateCTracker<Player>(player);

tracker.SetState(new IdleState());

while (true)
{
    tracker.Update(0.016f); // Simulate update loop at 60 FPS
}

2. Simple State Management (StateTracker)

Define Simple States
using StateKit;

public class RedLightState : StateBase
{
    public override void Enter()
    {
        Console.WriteLine("Red Light: Stop.");
    }

    public override StateBase? CheckTransitions()
    {
        return new GreenLightState();
    }
}

public class GreenLightState : StateBase
{
    public override void Enter()
    {
        Console.WriteLine("Green Light: Go.");
    }

    public override StateBase? CheckTransitions()
    {
        return new RedLightState();
    }
}
Initialize and Use StateTracker
StateTracker tracker = new StateTracker();

tracker.SetState(new RedLightState());

while (true)
{
    tracker.Update(1.0f); // Simulate a 1-second interval
}

Comparison Table

Feature StateCTracker<T> StateTracker
Shared Context Yes No
State Transition Handling Via StateCBase<T>.CheckTransitions Via StateBase.CheckTransitions
Suitable for Complex Systems Yes No
Lightweight Implementation No Yes

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 62 11/17/2024