Skip to content

Register System

E Aakash edited this page Jul 18, 2019 · 1 revision

Another implementation for "pools" is to create a system, which registers all such pools and does the corresponding logic handling.

Why this way and why not?

This method helps us in code uniformity. A single system handling all "pools" keeps all code in single place, allowing easy code maintenance.

The downside of this method is it becomes difficult for expansion of features for any "pool". Any addition would require careful implementation such that it is generalized for all pools.

Implementation Details

Component

The component for this implementation would hold the details of all the pools that would be attached to an entity. The component for such pool will look like:

public class PoolComponent implements Component {
     public Map<String, int> PoolValue;
     public Map<String, int> PoolRegenRate;
     public Map<String, int> maxValue;
}

All the individual pools will be distinguished by unique strings.

Events

This implementation will have generic events for handling the pools. For example:

public class FillPoolEvent implements Event {
     private int amount; 
     private string pool;  // Used for identifying which pool is being filled
     public getAmount();
     public getPool();     // Returns the string identifier
}

These events will trigger changes in the pool by other systems. The events will have the string identifier for the pool for which it is sent.

System

This system will handle the events, using the string identifier for a specific pool:

@RegisterSystem(RegisterMode.AUTHORITY)
public class PoolAuthoritySystem extends BaseComponentSystem {
     @RecieveEvent
     public void onFillEvent (FillPoolEvent event, EntityRef entity, PoolComponent componenet){
          string Pool = event.getPool();
          // Logic to fill the specific pool
     }

     @RecieveEvent
     public void onDrainEvent (DrainPoolEvent event, EntityRef entity, PoolComponent componenet){
          string Pool = event.getPool();
          // Logic to Drain the specific pool
     }
}

This is another way of handling pools.