-
Notifications
You must be signed in to change notification settings - Fork 0
Core.Server
The class acts as a lifecycle manager. In a console application or a background service, the program would normally exit as soon as the method reaches the end. This utility prevents that by waiting for a shutdown signal (like Ctrl+C or a system kill command) before allowing the application to close gracefully. ServerApplication``Main
Key Features:
-
Graceful Shutdown: It listens for , , and events.
CancelKeyPress``ProcessExit``Unloading -
Global Registration: It automatically registers itself with the .
GlobalObjectManager -
Asynchronous Waiting: The method returns a Task that only completes when the application is requested to stop.
Run()
Example Usage:
using Meatcorps.Engine.Core.Server;
// In your Program.cs
var app = new ServerApplication();
Console.WriteLine("Server is starting...");
// You can initialize other services here
// var loop = new SimpleGameLoop().Start();
// This line will block the execution until you press Ctrl+C
// or the process is terminated.
await app.Run();
Console.WriteLine("Server has shut down gracefully.");The utility provides a consistent "heartbeat" or "tick" for your application. While standard web applications are request-based, games and real-time simulations need to run logic (like physics or AI updates) at a fixed interval (e.g., 60 times per second). SimpleGameLoop
Key Features:
- DeltaTime Calculation: It calculates the time passed since the last frame, ensuring logic runs consistently regardless of minor timing fluctuations.
-
Three-Stage Update: It executes logic in three phases: , , and , which is a common pattern for managing dependencies between systems.
PreUpdate``Update``LateUpdate -
Service Integration: it automatically fetches all registered objects from the and updates them.
IBackgroundService``GlobalObjectManager
Example Usage:
First, create a service that implements : IBackgroundService
using Meatcorps.Engine.Core.Interfaces.Services;
public class MovementService : IBackgroundService
{
public void PreUpdate(float deltaTime) { /* Prepare data */ }
public void Update(float deltaTime)
{
// Move units or update game logic
Console.WriteLine($"Updating logic with delta: {deltaTime}");
}
public void LateUpdate(float deltaTime) { /* Finalize state */ }
}Then, start the loop in your application:
using Meatcorps.Engine.Core.Server;
using Meatcorps.Engine.Core.ObjectManager;
// 1. Register your services so the loop can find them
GlobalObjectManager.ObjectManager.Register<IBackgroundService>(new MovementService());
// 2. Initialize and start the loop
using var gameLoop = new SimpleGameLoop();
gameLoop.Start();
// 3. Keep the application alive (usually combined with ServerApplication)
var app = new ServerApplication();
await app.Run();When building a server or a Blazor-based game engine with these tools, the typical flow is:
- your logic classes (Services) into the . Register
GlobalObjectManager - the to begin invoking calls on those services. Start
SimpleGameLoop``Update -
Await
ServerApplication.Run()to keep the process alive and ensure that when the process stops, everything is disposed of correctly.