public void Entity_Factory_Create_Correct_Npc() { Npc npc = _entityFactory.CreateNpc(1, 164, string.Empty); Check.That(npc).IsNotNull(); Check.That(npc.Id).Is(1); Check.That(npc.Vnum).Is(164); Check.That(npc.Name).Is("Slade"); Check.That(npc.Level).Is(72); }
protected override void Handle(Client client, InPacket packet) { Map map = client.Character?.Map; if (map == null) { _logger.Warn("Handling InPacket but character map is null"); return; } if (map.Contains(packet.EntityType, packet.EntityId)) { _logger.Warn($"Entity {packet.EntityType} {packet.EntityId} already on map"); return; } Entity entity; switch (packet.EntityType) { case EntityType.MONSTER: entity = _entityFactory.CreateMonster(packet.EntityId, packet.Vnum); break; case EntityType.NPC: entity = _entityFactory.CreateNpc(packet.EntityId, packet.Vnum, packet.NpcSubPacket.Name); break; case EntityType.PLAYER: entity = _entityFactory.CreatePlayer(packet.EntityId, packet.Name); break; case EntityType.GROUND_ITEM: entity = _entityFactory.CreateGroundItem(packet.EntityId, packet.Vnum, packet.DropSubPacket.Amount); break; default: throw new InvalidOperationException("Undefined entity type"); } entity.Position = new Position(packet.PositionX, packet.PositionY); if (entity is Player player) { player.Level = packet.PlayerSubPacket.Level; player.Class = packet.PlayerSubPacket.Class; player.Gender = packet.PlayerSubPacket.Gender; player.Direction = packet.Direction; player.HpPercentage = packet.PlayerSubPacket.HpPercentage; player.MpPercentage = packet.PlayerSubPacket.MpPercentage; } if (entity is Monster monster) { monster.Direction = packet.Direction; monster.HpPercentage = packet.NpcSubPacket.HpPercentage; monster.MpPercentage = packet.NpcSubPacket.MpPercentage; monster.Faction = packet.NpcSubPacket.Faction; } if (entity is Npc npc) { npc.Direction = packet.Direction; npc.HpPercentage = packet.NpcSubPacket.HpPercentage; npc.MpPercentage = packet.NpcSubPacket.MpPercentage; npc.Faction = packet.NpcSubPacket.Faction; } if (entity is GroundItem drop) { drop.Owner = map.GetEntity <Player>(packet.DropSubPacket.Owner); } map.AddEntity(entity); _logger.Info($"Entity {entity.EntityType} {entity.Id} joined map"); _eventManager.Emit(new EntityJoinEvent(client) { Map = map, Entity = entity }); }
protected override void Process(IClient client, In packet) { IMap map = client.Character.Map; if (map == null) { Logger.Warn("Can't process in packet, character map is null"); return; } IEntity entity; switch (packet.EntityType) { case EntityType.Monster: entity = entityFactory.CreateMonster(packet.EntityId, packet.GameKey); break; case EntityType.Npc: entity = entityFactory.CreateNpc(packet.EntityId, packet.GameKey); break; case EntityType.Player: entity = entityFactory.CreatePlayer(packet.EntityId, packet.Name); break; case EntityType.MapObject: entity = entityFactory.CreateMapObject(packet.EntityId, packet.GameKey, packet.MapObject.Amount); break; default: Logger.Error($"Undefined switch clause for entity type {packet.EntityType}"); return; } if (entity == null) { Logger.Error($"Failed to create entity {packet.EntityType} {packet.EntityId}"); return; } entity.Position = packet.Position; if (entity is ILivingEntity livingEntity) { livingEntity.Direction = packet.Direction; if (entity is IPlayer player) { player.HpPercentage = packet.Player.HpPercentage; player.MpPercentage = packet.Player.MpPercentage; player.Gender = packet.Player.Gender; player.Class = packet.Player.Class; } else { livingEntity.HpPercentage = packet.Npc.HpPercentage > 100 ? 100 : packet.Npc.HpPercentage; livingEntity.MpPercentage = packet.Npc.MpPercentage > 100 ? 100 : packet.Npc.MpPercentage; } } map.AddEntity(entity); eventPipeline.Emit(new EntitySpawnEvent(client, map, entity)); }