/// <summary> /// Creates a NPC. /// </summary> /// <param name="element"></param> private void CreateNpc(NpcDyoElement element) { var behaviors = DependencyContainer.Instance.Resolve <BehaviorLoader>(); var npcs = DependencyContainer.Instance.Resolve <NpcLoader>(); var npc = this.CreateEntity <NpcEntity>(); npc.Object = new ObjectComponent { MapId = this.Id, ModelId = element.Index, Name = element.Name, Angle = element.Angle, Position = element.Position.Clone(), Size = (short)(ObjectComponent.DefaultObjectSize * element.Scale.X), Spawned = true, Type = WorldObjectType.Mover, Level = 1 }; npc.Behavior = behaviors.NpcBehaviors.GetBehavior(npc.Object.ModelId); npc.Timers.LastSpeakTime = RandomHelper.Random(10, 15); npc.Data = npcs.GetNpcData(npc.Object.Name); if (npc.Data != null && npc.Data.HasShop) { ShopData npcShopData = npc.Data.Shop; npc.Shop = new ItemContainerComponent[npcShopData.Items.Length]; for (var i = 0; i < npcShopData.Items.Length; i++) { npc.Shop[i] = new ItemContainerComponent(100); for (var j = 0; j < npcShopData.Items[i].Count && j < npc.Shop[i].MaxCapacity; j++) { ItemBase item = npcShopData.Items[i][j]; ItemData itemData = GameResources.Instance.Items[item.Id]; npc.Shop[i].Items[j] = new Item(item.Id, itemData.PackMax, -1, j, j, item.Refine, item.Element, item.ElementRefine); } } } }
/// <summary> /// Creates a new NPC from a dyo element. /// </summary> /// <param name="dyoElement">Map Dyo element</param> /// <param name="mapId">Npc map Id</param> /// <returns></returns> public static Npc CreateFromDyo(NpcDyoElement dyoElement, int mapId) { var npc = new Npc(dyoElement.Index); npc.MapId = mapId; npc.Name = dyoElement.Name; npc.Angle = dyoElement.Angle; npc.Position = dyoElement.Position.Clone(); npc.DestinationPosition = dyoElement.Position.Clone(); npc.Size = (short)(npc.Size * dyoElement.Scale.X); if (WorldServer.NPCData.ContainsKey(npc.Name)) { npc.Data = WorldServer.NPCData[npc.Name]; } if (WorldServer.DialogsData.ContainsKey(npc.Name)) { npc.Dialog = WorldServer.DialogsData[npc.Name]; } return(npc); }
/// <inheritdoc /> public INpcEntity CreateNpc(IMapContext mapContext, NpcDyoElement element) { int npcModelId = element.Index; if (!_gameResources.Movers.TryGetValue(npcModelId, out MoverData moverData)) { throw new ArgumentException($"Cannot find mover with id '{npcModelId}' in game resources.", nameof(npcModelId)); } var npc = new NpcEntity { Object = new ObjectComponent { MapId = mapContext.Id, CurrentMap = mapContext as IMapInstance, ModelId = npcModelId, Name = element.CharacterKey, Angle = element.Angle, Position = element.Position.Clone(), Size = (short)(ObjectComponent.DefaultObjectSize * element.Scale.X), Spawned = true, Type = WorldObjectType.Mover, Level = 1 }, Data = moverData }; npc.Behavior = _behaviorManager.GetBehavior(BehaviorType.Npc, npc, npc.Object.ModelId); npc.Timers.LastSpeakTime = RandomHelper.Random(10, 15); npc.Quests = _gameResources.Quests.Values.Where(x => !string.IsNullOrEmpty(x.StartCharacter) && x.StartCharacter.Equals(npc.Object.Name, StringComparison.OrdinalIgnoreCase)).ToList(); npc.Hand = _itemFactory.CreateItem(11, 0, ElementType.None, 0); if (_gameResources.Npcs.TryGetValue(npc.Object.Name, out NpcData npcData)) { npc.NpcData = npcData; } if (npc.NpcData != null && npc.NpcData.HasShop) { ShopData npcShopData = npc.NpcData.Shop; npc.Shop = new ItemContainerComponent[npcShopData.Items.Length]; for (var i = 0; i < npcShopData.Items.Length; i++) { npc.Shop[i] = new ItemContainerComponent(100); for (var j = 0; j < npcShopData.Items[i].Count && j < npc.Shop[i].MaxCapacity; j++) { ItemDescriptor item = npcShopData.Items[i][j]; Item shopItem = _itemFactory.CreateItem(item.Id, item.Refine, item.Element, item.ElementRefine); shopItem.Slot = j; shopItem.Quantity = shopItem.Data.PackMax; npc.Shop[i].SetItemAtIndex(shopItem, j); } } } return(npc); }