/// <summary> Called when entity is spawned. </summary> private void OnSpawn(Guid id) { if (service.isMaster) { if (entities.ContainsKey(id)) { Log.Warning($"Map.OnSpawn: Tried to spawn entity {id} on map {identity}, but the entity is already in that map!"); return; } Entity entity = entityService[id]; entities[id] = entity; TRS trs = entity.GetComponent <TRS>(); Client client = service.server.GetClient(id); Log.Debug($"Map {identity} \\jspawn of entity {id}"); if (client != null) { foreach (var e in globalEntities) { entityService.Subscribe(client, e); } } if (trs != null) { Vector3Int cellPos = CellPositionFor(trs.position); Cell cell = RequireCell(cellPos); cell.AddEntity(id); } } }
/// <summary> Called when entity is despawned. </summary> private void OnDespawn(Guid id) { if (service.isMaster) { if (!entities.ContainsKey(id)) { Log.Warning($"Map.OnDespawn: Tried to remove entity {id} from map {identity}, but the entity is not in that map!"); return; } entities.Remove(id); TRS trs = entityService.GetComponent <TRS>(id); Client client = service.server.GetClient(id); Log.Warning($"Map.OnDespawn: Map {identity} \\odespawn of entity {id}"); if (client != null) { foreach (var e in globalEntities) { entityService.Unsubscribe(client, e); } } if (trs != null) { Vector3Int cellPos = CellPositionFor(trs.position); Cell cell = GetCell(cellPos); if (cell != null) { cell.RemoveEntity(id); } else { Log.Warning($"Map.OnDespawn: Tried to remove entity {id} from map {identity} cell {cellPos}, but the cell did not exist!"); } } else { Log.Warning($"Map.OnDespawn: Probably expected a TRS on entity {id} on map {identity}..."); } // Finally, remove entity id for client if the client is gone. if (client != null && client.closed) { entityService.Revoke(id); } } }
public void Initialize() { Log.Info($"Map is a {info.boundsShape} shape"); foreach (EntityInstanceInfo spawnInfo in info.entities) { EntityInfo entityInfo = entityService.GetEntityInfo(spawnInfo.type); if (entityInfo == null) { Log.Warning($"Map.Initialize: Initializing map {identity}, no entity of type {{{spawnInfo.type}}} found!"); continue; } Log.Verbose($"Map.Initialize: {identity} creating entity for {spawnInfo.type} at T{ spawnInfo.position } R{spawnInfo.rotation} S {spawnInfo.scale}"); Entity entity = entityService.CreateEntity(); Guid id = entity.guid; TRS trs = entity.AddComponent <TRS>(); trs.position = spawnInfo.position; trs.rotation = spawnInfo.rotation; trs.scale = spawnInfo.scale; OnMap onMap = entity.AddComponent <OnMap>(); onMap.mapId = name; onMap.mapInstanceIndex = instanceIndex; foreach (var comp in entityInfo.components) { var type = entityService.GetCompType(comp.type); if (type != null) { Comp c = entityService.AddComponent(id, type); Json.ReflectInto(comp.data, c); Log.Info($"Initialized Component: {c}"); } } if (entityInfo.global) { globalIds.Add(id); globalEntities.Add(entity); } else { RequireCell(CellPositionFor(spawnInfo.position)).AddEntity(id); } } }
/// <summary> Enqueues an EntityMoveRequest for the given entity to the new position. </summary> /// <param name="entityId"> ID of entity to move </param> /// <param name="position"> Position to move entity to (null if unchanged) </param> /// <param name="rotation"> Rotation to give entity (null if unchanged) </param> /// <param name="serverMove"> Did the server initiate the move? True for things such as warping via portal or being pushed by an attack. </param> public void Move(Guid entityId, Vector3?position, Vector4?rotation, bool serverMove = false) { EntityMoveRequest move = new EntityMoveRequest(); move.id = entityId; TRS trs = entityService.GetComponent <TRS>(entityId); if (trs == null) { entityService.AddComponent <TRS>(entityId); serverMove = true; } move.newPos = position ?? trs.position; move.newRot = rotation ?? trs.rotation; move.serverMove = serverMove; toMove.Enqueue(move); }