Regen System

Note:

This feature requires the Altruist Engine!

Regeneration System

To use this feature first enable the engine!

Overview

The Regeneration System in Altruist is a powerful feature designed to handle automatic regeneration of various player or entity attributes, such as health points (HP), mana points (MP), stamina points (SP), or any other custom properties. This system is useful for games where entities need to regenerate resources or stats over time.

The regeneration logic is flexible and can be extended to accommodate various regeneration mechanics, such as regenerating health over time or replenishing resources in specific scenarios.

How to Use

To enable and use the Regeneration System, follow these steps:

1. Enable the Altruist Engine

Before using the regeneration system, you must enable the Altruist engine with a specified frame rate. The system requires a working engine for the calculation and sync process. You can enable the engine by calling:

.EnableEngine(FrameRate.Hz30)

This ensures that the engine updates 30 times per frame, suitable for handling continuous calculations like regeneration.

2. Extend the AltruistRegenPortal Class

To begin utilizing the regeneration system, you must create a portal class by extending the AltruistRegenPortal. This class will handle the regeneration calculations for your specific entity type (e.g., Spaceship, Player, etc.).

Here's an example of how to extend the AltruistRegenPortal class:

public class RegenPortal : AltruistRegenPortal<Spaceship> {
// Implement the required regeneration logic here
}

3. Implement the CalculateRegen Logic

In the custom portal class, you need to implement the regeneration logic by overriding the CalculateRegen method. This method will handle the regeneration calculations for all entities in the game. It should return a list of updated entities after the regeneration process.

Note:

This calculation must be extremely fast—avoid any database lookups during the regen calculation.


For optimal performance, the best strategy is to process regeneration in chunks and calculate those in parallel.


A solid chunking strategy could be by room or other natural groupings, which is supported by the framework out-of-the-box.


Additionally, leverage the frame-based processing approach. For example, you can process one chunk per frame to distribute the load over time and prevent any single frame from becoming too heavy.

Here's a sample implementation of CalculateRegen:

public abstract Task<List<TPlayerEntity>> CalculateRegen();

This method will perform the necessary calculations to regenerate properties such as health, mana, or stamina for all entities in the same room as the user and return the updated entities.

The rest of the synchronization and event handling is automatically managed by the Altruist framework, so you don't need to worry about syncing the updated values to the clients.

Future Plans

In the future, we are planning to introduce a basic regeneration system that works with commonly used properties in games such as:

  • HP (Health Points)
  • MP (Mana Points)
  • SP (Stamina Points)

This will allow for simple and customizable regeneration of these properties, making it easier to implement basic health/mana/stamina regeneration mechanics in your games.

Example Usage

Below is an example of how you might implement a simple CalculateRegen method for regenerating HP (Health Points) for all entities in the same room:

public class SpaceshipRegenPortal : AltruistRegenPortal<Spaceship>
{
    ...
    public override async Task<List<Spaceship>> CalculateRegen()
    {
        var updatedEntities = new List<Spaceship>();

        foreach (var entity in GetEntitiesInSameRoom()) 
        {
            if (entity.Health < entity.MaxHealth)
            {
                entity.Health += 5; // Regenerate 5 HP per frame
                updatedEntities.Add(entity);
            }
        }

        return updatedEntities;
    }
}

In this example, we check each spaceship's health and regenerate it if it's below the maximum health. The updated entities are then returned for synchronization.