Пример #1
0
        /// <summary>
        /// 加载某地图的所有npc角色,将该地图的所有NPC配置到相应的格子坐标中,
        /// 玩家移动的时候,动态推送到客户端
        /// </summary>
        /// <param name="mapCode"></param>
        public static Boolean LoadMapNPCRoles(int mapCode, GameMap gameMap)
        {
            string   fileName = string.Format("Map/{0}/npcs.xml", mapCode);
            XElement xml      = GeneralCachingXmlMgr.GetXElement(Global.ResPath(fileName));

            if (null == xml)
            {
                return(false);
            }

            IEnumerable <XElement> items = xml.Elements("NPCs").Elements();

            foreach (var item in items)
            {
                NPC myNpc = new NPC();

                myNpc.NpcID = Convert.ToInt32((string)item.Attribute("Code"));
                //myNpc.GridPoint.X = Convert.ToInt32((string)item.Attribute("X")) / gameMap.MapGridWidth;
                //myNpc.GridPoint.Y = Convert.ToInt32((string)item.Attribute("Y")) / gameMap.MapGridHeight;
                myNpc.MapCode    = mapCode;
                myNpc.CurrentPos = new Point(Convert.ToInt32((string)item.Attribute("X")), Convert.ToInt32((string)item.Attribute("Y")));
                //myNpc.CurrentDir = (Dircetions)Global.GetSafeAttributeLong(item, "Dir");

                if (item.Attribute("Dir") != null)
                {
                    myNpc.CurrentDir = (Dircetions)Global.GetSafeAttributeLong(item, "Dir");
                }
                else
                {
                    myNpc.CurrentDir = (Dircetions)4;
                }

                //将推送给客户端的数据缓存起来,以后直接取出发送就行
                myNpc.RoleBufferData = GenerateNpcRoleBufferData(myNpc);

                if (null == myNpc.RoleBufferData)
                {
                    continue;
                    //LogManager.WriteLog(LogTypes.Error, string.Format("加载地图{0}的({1}, {2})处旧的NPC数据失败", myNpc.MapCode, myNpc.GridPoint.X, myNpc.GridPoint.Y));
                    //throw new Exception("NPC配置数据出错");
                }

                AddNpcToMap(myNpc);

                //为地图中的某点设置安全区
                int           safeGridNum = 2;
                SystemXmlItem npcXmlItem;
                if (GameManager.SystemNPCsMgr.SystemXmlItemDict.TryGetValue(myNpc.NpcID, out npcXmlItem))
                {
                    safeGridNum = npcXmlItem.GetIntValue("IsSafe");
                }

                if (safeGridNum > 0)
                {
                    gameMap.SetPartialSafeRegion(myNpc.GridPoint, safeGridNum);
                }
            }

            return(true);
        }
Пример #2
0
        public static bool LoadMapNPCRoles(int mapCode, GameMap gameMap)
        {
            string   fileName = string.Format("Map/{0}/npcs.xml", mapCode);
            XElement xml      = GeneralCachingXmlMgr.GetXElement(Global.ResPath(fileName));
            bool     result;

            if (null == xml)
            {
                result = false;
            }
            else
            {
                IEnumerable <XElement> items = xml.Elements("NPCs").Elements <XElement>();
                foreach (XElement item in items)
                {
                    NPC myNpc = new NPC();
                    myNpc.NpcID      = Convert.ToInt32((string)item.Attribute("Code"));
                    myNpc.MapCode    = mapCode;
                    myNpc.CurrentPos = new Point((double)Convert.ToInt32((string)item.Attribute("X")), (double)Convert.ToInt32((string)item.Attribute("Y")));
                    if (item.Attribute("Dir") != null)
                    {
                        myNpc.CurrentDir = (Dircetions)Global.GetSafeAttributeLong(item, "Dir");
                    }
                    else
                    {
                        myNpc.CurrentDir = Dircetions.DR_DOWN;
                    }
                    myNpc.RoleBufferData = NPCGeneralManager.GenerateNpcRoleBufferData(myNpc);
                    if (null != myNpc.RoleBufferData)
                    {
                        NPCGeneralManager.AddNpcToMap(myNpc);
                        int           safeGridNum = 2;
                        SystemXmlItem npcXmlItem;
                        if (GameManager.SystemNPCsMgr.SystemXmlItemDict.TryGetValue(myNpc.NpcID, out npcXmlItem))
                        {
                            safeGridNum = npcXmlItem.GetIntValue("IsSafe", -1);
                        }
                        if (safeGridNum > 0)
                        {
                            gameMap.SetPartialSafeRegion(myNpc.GridPoint, safeGridNum);
                        }
                    }
                }
                result = true;
            }
            return(result);
        }