using System.Collections.Generic; using JetBrains.Annotations; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; namespace Robust.Shared.GameObjects { [UsedImplicitly] internal abstract class SharedMapSystem : EntitySystem { [Dependency] protected readonly IMapManagerInternal MapManager = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnMapAdded); SubscribeLocalEvent(OnMapRemoved); SubscribeLocalEvent(OnGridAdd); SubscribeLocalEvent(OnGridInit); SubscribeLocalEvent(OnGridStartup); SubscribeLocalEvent(OnGridRemove); } private void OnMapAdded(EntityUid uid, MapComponent component, ComponentInit args) { var msg = new MapChangedEvent(component.WorldMap, true); EntityManager.EventBus.RaiseLocalEvent(uid, msg); } private void OnMapRemoved(EntityUid uid, MapComponent component, ComponentShutdown args) { var msg = new MapChangedEvent(component.WorldMap, false); EntityManager.EventBus.RaiseLocalEvent(uid, msg); } private void OnGridAdd(EntityUid uid, MapGridComponent component, ComponentAdd args) { // GridID is not set yet so we don't include it. var msg = new GridAddEvent(uid); EntityManager.EventBus.RaiseLocalEvent(uid, msg); } private void OnGridInit(EntityUid uid, MapGridComponent component, ComponentInit args) { var msg = new GridInitializeEvent(uid, component.GridIndex); EntityManager.EventBus.RaiseLocalEvent(uid, msg); } private void OnGridStartup(EntityUid uid, MapGridComponent component, ComponentStartup args) { var msg = new GridStartupEvent(uid, component.GridIndex); EntityManager.EventBus.RaiseLocalEvent(uid, msg); } private void OnGridRemove(EntityUid uid, MapGridComponent component, ComponentShutdown args) { EntityManager.EventBus.RaiseLocalEvent(uid, new GridRemovalEvent(uid, component.GridIndex)); MapManager.OnComponentRemoved(component); } } /// /// Arguments for when a map is created or deleted. /// public sealed class MapChangedEvent : EntityEventArgs { /// /// Creates a new instance of this class. /// public MapChangedEvent(MapId map, bool created) { Map = map; Created = created; } /// /// Map that is being modified. /// public MapId Map { get; } /// /// The map is being created. /// public bool Created { get; } /// /// The map is being destroyed (not ). /// public bool Destroyed => !Created; } public sealed class GridStartupEvent : EntityEventArgs { public EntityUid EntityUid { get; } public GridId GridId { get; } public GridStartupEvent(EntityUid uid, GridId gridId) { EntityUid = uid; GridId = gridId; } } public sealed class GridRemovalEvent : EntityEventArgs { public EntityUid EntityUid { get; } public GridId GridId { get; } public GridRemovalEvent(EntityUid uid, GridId gridId) { EntityUid = uid; GridId = gridId; } } /// /// Raised whenever a grid is being initialized. /// public sealed class GridInitializeEvent : EntityEventArgs { public EntityUid EntityUid { get; } public GridId GridId { get; } public GridInitializeEvent(EntityUid uid, GridId gridId) { EntityUid = uid; GridId = gridId; } } /// /// Raised whenever a grid is Added /// public sealed class GridAddEvent : EntityEventArgs { public EntityUid EntityUid { get; } public GridAddEvent(EntityUid uid) { EntityUid = uid; } } /// /// Arguments for when a single tile on a grid is changed locally or remotely. /// public sealed class TileChangedEvent : EntityEventArgs { /// /// Creates a new instance of this class. /// public TileChangedEvent(EntityUid uid, TileRef newTile, Tile oldTile) { Entity = uid; NewTile = newTile; OldTile = oldTile; } /// /// EntityUid of the grid with the tile-change. TileRef stores the GridId. /// public EntityUid Entity { get; } /// /// New tile that replaced the old one. /// public TileRef NewTile { get; } /// /// Old tile that was replaced. /// public Tile OldTile { get; } } /// /// Arguments for when a one or more tiles on a grid are modified at once. /// public sealed class GridModifiedEvent : EntityEventArgs { /// /// Grid being changed. /// public IMapGrid Grid { get; } /// /// Set of tiles that were modified. /// public IReadOnlyCollection<(Vector2i position, Tile tile)> Modified { get; } /// /// Creates a new instance of this class. /// public GridModifiedEvent(IMapGrid grid, IReadOnlyCollection<(Vector2i position, Tile tile)> modified) { Grid = grid; Modified = modified; } } }