Class ResourceSystem
ENGINE LAYER: Generic resource storage and management system.
Stores and manages multiple resource types (gold, manpower, prestige, etc.) for all countries. Uses deterministic fixed-point math for multiplayer compatibility.
Architecture:
- Dictionary storage: resourceId → array of country values
- Fixed-size arrays: countryId → resource amount
- Deterministic: FixedPoint64 only, no float operations
- Event-driven: Emits ResourceChangedEvent via EventBus
Usage:
- Initialize with country capacity and EventBus
- Register resource types with RegisterResource(resourceId, definition)
- Use AddResource/RemoveResource/GetResource for all operations
- Use CanAfford/TrySpend for cost validation
Performance:
- GetResource: O(1) dictionary lookup + O(1) array access
- Memory: sizeof(FixedPoint64) × resourceCount × countryCount
- Example: 10 resources × 200 countries × 8 bytes = 16 KB
public class ResourceSystem : IResourceProvider
- Inheritance
-
objectResourceSystem
- Implements
Properties
IsBatchMode
Whether currently in batch mode.
public bool IsBatchMode { get; }
Property Value
- bool
IsInitialized
Check if initialized.
public bool IsInitialized { get; }
Property Value
- bool
MaxCountries
Get country capacity.
public int MaxCountries { get; }
Property Value
- int
ResourceCount
Get number of registered resources.
public int ResourceCount { get; }
Property Value
- int
Methods
AddResource(ushort, ushort, FixedPoint64)
Add resource to a country (clamped to max value).
public void AddResource(ushort countryId, ushort resourceId, FixedPoint64 amount)
Parameters
countryIdushortresourceIdushortamountFixedPoint64
AddResourceToAll(ushort, FixedPoint64)
Add resource to all countries.
public void AddResourceToAll(ushort resourceId, FixedPoint64 amount)
Parameters
resourceIdushortamountFixedPoint64
BeginBatch()
Begin batch mode - suppresses events until EndBatch is called. Use during loading/setup to avoid thousands of individual events.
public void BeginBatch()
CanAfford(ushort, ResourceCost[])
Check if country can afford multiple costs.
public bool CanAfford(ushort countryId, ResourceCost[] costs)
Parameters
countryIdushortcostsResourceCost[]
Returns
- bool
CanAfford(ushort, ushort, FixedPoint64)
Check if country can afford a single cost.
public bool CanAfford(ushort countryId, ushort resourceId, FixedPoint64 amount)
Parameters
countryIdushortresourceIdushortamountFixedPoint64
Returns
- bool
EndBatch()
End batch mode - emits a single batch completed event.
public void EndBatch()
GetAllResourceIds()
Get all registered resource IDs.
public IEnumerable<ushort> GetAllResourceIds()
Returns
- IEnumerable<ushort>
GetAllResourcesForCountry(ushort)
Get all resources for a country as dictionary (for UI/debugging).
public Dictionary<ushort, FixedPoint64> GetAllResourcesForCountry(ushort countryId)
Parameters
countryIdushort
Returns
- Dictionary<ushort, FixedPoint64>
GetResource(ushort, ushort)
Get current resource amount for a country.
public FixedPoint64 GetResource(ushort countryId, ushort resourceId)
Parameters
countryIdushortresourceIdushort
Returns
GetResourceDefinition(ushort)
Get resource definition by ID.
public ResourceDefinition GetResourceDefinition(ushort resourceId)
Parameters
resourceIdushort
Returns
GetTotalResourceInWorld(ushort)
Get total amount of a resource across all countries.
public FixedPoint64 GetTotalResourceInWorld(ushort resourceId)
Parameters
resourceIdushort
Returns
Initialize(int, EventBus)
Initialize ResourceSystem with country capacity and EventBus. Must be called before any resource operations.
public void Initialize(int countryCapacity, EventBus eventBus = null)
Parameters
countryCapacityinteventBusEventBus
IsResourceRegistered(ushort)
Check if a resource type is registered.
public bool IsResourceRegistered(ushort resourceId)
Parameters
resourceIdushort
Returns
- bool
RegisterResource(ushort, ResourceDefinition)
Register a resource type with its definition. Creates storage array for all countries and initializes with starting amount.
public void RegisterResource(ushort resourceId, ResourceDefinition definition)
Parameters
resourceIdushortdefinitionResourceDefinition
RemoveResource(ushort, ushort, FixedPoint64)
Remove resource from a country (returns true if successful, false if insufficient).
public bool RemoveResource(ushort countryId, ushort resourceId, FixedPoint64 amount)
Parameters
countryIdushortresourceIdushortamountFixedPoint64
Returns
- bool
SetResource(ushort, ushort, FixedPoint64)
Set resource to exact amount (for dev commands/testing). Clamped to min/max values.
public void SetResource(ushort countryId, ushort resourceId, FixedPoint64 amount)
Parameters
countryIdushortresourceIdushortamountFixedPoint64
SetResourceForAll(ushort, FixedPoint64)
Set resource for all countries.
public void SetResourceForAll(ushort resourceId, FixedPoint64 amount)
Parameters
resourceIdushortamountFixedPoint64
Shutdown()
Shutdown resource system (cleanup).
public void Shutdown()
TransferResource(ushort, ushort, ushort, FixedPoint64)
Transfer resource between countries (returns false if insufficient).
public bool TransferResource(ushort fromCountryId, ushort toCountryId, ushort resourceId, FixedPoint64 amount)
Parameters
fromCountryIdushorttoCountryIdushortresourceIdushortamountFixedPoint64
Returns
- bool
TrySpend(ushort, ResourceCost[])
Try to spend multiple costs atomically (all or nothing). Returns false if insufficient resources (no changes made).
public bool TrySpend(ushort countryId, ResourceCost[] costs)
Parameters
countryIdushortcostsResourceCost[]
Returns
- bool