public void Spawn <TEntity>(INetworkPeer owner, Action <TEntity> callback = null) where TEntity : class, IEntity { int idToUse = Interlocked.Increment(ref m_NextId); var @event = new EntitySpawnEvent(idToUse, typeof(TEntity)); m_EventBus.Emit(this, @event); if (@event.IsCancelled) { Interlocked.Decrement(ref m_NextId); return; } m_TaskScheduler.RunOnMainThread(this, () => { var entityInstance = (TEntity)m_Container.Activate(typeof(TEntity)); entityInstance.Id = idToUse; entityInstance.OwnerId = owner.Id; entityInstance.Init(); lock (m_SpawnedEntities) { m_SpawnedEntities.Add(entityInstance.Id, entityInstance); } callback?.Invoke(entityInstance); }, $"SpawnEntity-[{typeof(TEntity).Name}]"); }
public void SpawnTick() { foreach (var mob in SpawnerMobs) { if (mob.TrackedMonster == null) { var monsterInstance = (Monster)Activator.CreateInstance(mob.MonsterClassType); var spawnPosition = FindSpawnPosition(); if (spawnPosition == null) { return; } monsterInstance.Position = spawnPosition; monsterInstance.OriginSpawner = this; var spawnEvent = new EntitySpawnEvent() { Entity = monsterInstance, Position = spawnPosition }; Server.Events.Call(spawnEvent); // Let players know this monster spawned foreach (var player in monsterInstance.GetPlayersNear()) { var monsterPacket = monsterInstance.ToPacket(); monsterPacket.SpawnAnimation = true; player.Tcp.Send(monsterPacket); } monsterInstance.MovementTick(); } } }
private void OnEntitySpawned(IEventData eventData) { EntitySpawnEvent spawnEvent = eventData as EntitySpawnEvent; Entity entity = entityManager.Get(spawnEvent.EntityId); if (entity != null) { RegisterLightCaster(entity); } }
public void OnEntitySpawn(EntitySpawnEvent ev) { ev.Entity.Position = ev.Position; Server.Map.UpdateEntityPosition(ev.Entity, from: null, to: ev.Entity.Position); // Track in monsters list if its a monster if (ev.Entity.EntityType == EntityType.MONSTER) { Server.Map.Monsters.Add(ev.Entity.UID, (Monster)ev.Entity); } }
public void SpawnTick() { foreach (var mob in SpawnerMobs) { if (mob.TrackedMonster == null) { var monsterInstance = (Monster)Activator.CreateInstance(mob.MonsterClassType); var spawnPosition = FindSpawnPosition(); if (spawnPosition == null) { return; } monsterInstance.Position = spawnPosition; monsterInstance.OriginSpawner = this; var spawnEvent = new EntitySpawnEvent() { Entity = monsterInstance, Position = spawnPosition }; Server.Events.Call(spawnEvent); // Let players know this monster spawned foreach (var player in monsterInstance.GetNearbyPlayers()) { player.Tcp.Send(new MonsterSpawnPacket() { MonsterUid = monsterInstance.UID, MonsterName = monsterInstance.Name, Position = monsterInstance.Position, SpriteIndex = monsterInstance.GetSpriteAsset().SpriteRowIndex, MoveSpeed = monsterInstance.MoveSpeed, SpawnAnimation = true }); } monsterInstance.MovementTick(); } } }
public void OnEntitySpawn(EntitySpawnEvent ev) { var chunkX = ev.Position.X >> 4; var chunkY = ev.Position.Y >> 4; // Track the entity in the chunk var chunk = Server.Map.GetChunk(chunkX, chunkY); chunk.EntitiesInChunk[ev.Entity.EntityType].Add(ev.Entity); // Track the entity position cache Server.Map.EntityPositions.AddEntity(ev.Entity, ev.Position); // Track in monsters list if its a monster if (ev.Entity.EntityType == EntityType.MONSTER) { Server.Map.Monsters.Add(ev.Entity.UID, (Monster)ev.Entity); } ev.Entity.Position = ev.Position; }