Skip to content

Game.MapTileSystem

Assembly: Assembly-CSharp
Namespace: Game.Areas

Type: class

Base: GameSystemBase, IDefaultSerializable, ISerializable, IPostDeserialize

Summary:
MapTileSystem is a game system responsible for creating and managing map tile entities used by the game world grid. It handles legacy map-tile generation (used for older save/new-map creation paths), tracks "start" tiles (owners) for a newly generated city map, and implements serialization hooks so start tile lists survive save/load and new-game/new-map workflows. The system uses Burst-compiled parallel jobs to initialize the tile entities and their node buffers for bounds corner positions.


Fields

  • private const int LEGACY_GRID_WIDTH = 23
    This constant represents the legacy grid width in tiles (23). Used when generating the legacy 23x23 map layout.

  • private const int LEGACY_GRID_LENGTH = 23
    This constant represents the legacy grid height/length in tiles (23).

  • private const float LEGACY_CELL_SIZE = 623.3043f
    Legacy cell size (world units) used to compute tile bounds/positions in LegacyGenerateMapTiles.

  • private EntityQuery m_PrefabQuery
    EntityQuery used to locate MapTile prefab data (components MapTileData, AreaData, PrefabData) excluding locked prefabs. Used to identify the tile archetype/prefab to instantiate tiles from.

  • private EntityQuery m_MapTileQuery
    EntityQuery selecting existing MapTile entities (excluding Temp and Deleted) — used to detect & destroy existing tiles before legacy regeneration.

  • private EntityQuery m_DeletedMapTileQuery
    EntityQuery selecting MapTile entities that are marked Deleted (and not Temp). Used in OnUpdate to remove deleted tiles from the start list.

  • private NativeList<Entity> m_StartTiles
    Persistent NativeList containing Entities that are designated as "start tiles" (owner tiles). Serialized/deserialized on save/load and used as initial tile ownership for legacy/new games and editor modes.

  • private TypeHandle __TypeHandle
    Internal container for ComponentLookup/BufferLookup handles (PrefabRef, Area, Node) used by the Burst job and other internal operations. Populated in OnCreateForCompiler.

  • private struct GenerateMapTilesJob (nested, Burst-compiled)
    A parallel job used to initialize per-tile component data for a block of created tile entities. It sets PrefabRef, Area (Complete flag), and resizes/fills the Node buffer with four corner nodes corresponding to tile bounds. Uses NativeArray of entities and component lookups passed in.

  • private struct TypeHandle (nested)
    Stores ComponentLookup and BufferLookup instances for PrefabRef, Area and Node. Provides __AssignHandles to obtain those lookups from a SystemState.


Properties

  • None (no public properties exposed by MapTileSystem)

The class exposes GetStartTiles() which returns the internal NativeList rather than exposing it as a property.


Constructors

  • public MapTileSystem()
    Default constructor. System is marked with [Preserve] on lifecycle methods; constructor is standard and the system is initialized via OnCreate.

Methods

  • protected override void OnCreate()
    Initializes entity queries and allocates the persistent NativeList for start tiles. Queries:
  • m_PrefabQuery = MapTileData + AreaData + PrefabData, Exclude Locked
  • m_MapTileQuery = MapTile, Exclude Temp, Exclude Deleted
  • m_DeletedMapTileQuery = MapTile + Deleted, Exclude Temp

  • protected override void OnDestroy()
    Disposes the m_StartTiles NativeList and calls base.OnDestroy.

  • protected override void OnUpdate()
    Runs every frame. If there are any entities matched by m_DeletedMapTileQuery, completes dependencies and iterates deleted map tiles; any deleted entity found that exists in m_StartTiles will be removed from the start-tile list (RemoveAtSwapBack). Ensures start-list stays consistent when tiles are deleted.

  • public void PostDeserialize(Context context)
    Called after deserialization. Behavior depends on context.purpose:

  • Purpose.NewGame: If version >= Version.editorMapTiles, it prunes Entity.Null entries from m_StartTiles and removes the Native component from start-tile entities if m_StartTiles isn't empty. Otherwise (older version), calls LegacyGenerateMapTiles(editorMode: false).
  • Purpose.NewMap: Calls LegacyGenerateMapTiles(editorMode: true) to regenerate legacy grid for new map creation.

  • public NativeList<Entity> GetStartTiles()
    Returns the internal NativeList of start tiles. Useful for other systems to iterate or modify starting tile ownership.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes m_StartTiles length followed by each Entity value to the writer. Used for saving the start-tile list.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads an integer length and then reads that many Entity values into m_StartTiles. Used for loading.

  • public void SetDefaults(Context context)
    Clears m_StartTiles (used when initializing defaults for new contexts).

  • private void LegacyGenerateMapTiles(bool editorMode)
    Creates a legacy 23x23 block of map tile entities when no modern editor-generated tiles exist or when NewMap is requested. Steps:

  • Destroys existing m_MapTileQuery entities if present.
  • Clears start tiles and obtains the MapTile prefab entity via m_PrefabQuery.
  • Creates 529 entities (23*23) using the prefab archetype.
  • Adds the Native component to the entities when not in editorMode (so production maps mark them Native).
  • Calls AddOwner on a 3x3 center square (tile coords 10..12 × 10..12) to designate the initial owned/start tiles.
  • Schedules a Burst-parallel job (GenerateMapTilesJob) to initialize per-entity PrefabRef, Area flags and Node buffers with tile bounds/corners and completes the job.

  • private void AddOwner(int2 tile, NativeArray<Entity> entities)
    Given a tile coordinate (x,y), computes the linear index (y * 23 + x), removes the Native component from the corresponding entity (making it owned/available), and adds the entity to m_StartTiles. Used by legacy generator to designate start tiles.

  • private void __AssignQueries(ref SystemState state)
    Compiler-generated helper to assign queries (empty in the decompiled snippet but invoked in OnCreateForCompiler). Kept for the compiler/runtime.

  • protected override void OnCreateForCompiler()
    Compiler hook: assigns queries and component lookups by invoking __AssignQueries and __TypeHandle.__AssignHandles with the system state.

  • public void (implicit) GenerateMapTilesJob.Execute(int index) (inside nested job)
    Initializes a single tile entity: sets PrefabRef to the given prefab entity, sets Area(AreaFlags.Complete), computes tile bounds from the legacy grid math (23x23 grid, LEGACY_CELL_SIZE and offsets), resizes Node dynamic buffer to 4 and populates four Node entries for the corner positions with default heights (float.MinValue for Y-value sentinel).


Usage Example

// Example: access the MapTileSystem from a custom system or MonoBehaviour and query start tiles.
var mapTileSystem = World.DefaultGameObjectInjectionWorld
    .GetExistingSystemManaged<Game.Areas.MapTileSystem>();

if (mapTileSystem != null)
{
    var startTiles = mapTileSystem.GetStartTiles();
    Debug.Log($"Start tile count: {startTiles.Length}");
    // Iterate start tiles (NativeList<Entity>) carefully; respect jobs/allocators and lifetime.
}

Notes for modders: - MapTileSystem is a low-level ECS system manipulating Entities and Native collections; changes should respect the EntityManager and job dependencies. - When interacting with m_StartTiles, consider thread-safety and lifetime: the system owns a persistent NativeList; do not keep references across domain reloads or after system destruction. - LegacyGenerateMapTiles produces a 23×23 grid using hardcoded positions and cell size constants; modifying these directly requires caution and understanding of other systems that expect legacy layout math.