//安卓下使用www的方法拷贝到永久存在的文件夹 IEnumerator CopyDataForPersist(CallBack copyFinishCallBack) { //创建文件夹目录 Directory.CreateDirectory(Application.persistentDataPath + "/Data"); Directory.CreateDirectory(Application.persistentDataPath + "/Data/MapData"); Directory.CreateDirectory(Application.persistentDataPath + "/Data/NPCs"); Directory.CreateDirectory(Application.persistentDataPath + "/Data/GameItems"); //循环拷贝文件 for (int i = 0; i < CommonData.originDataArr.Length; i++) { //获取数组中的文件字典数据 KVPair originDataKV = CommonData.originDataArr[i]; string fileName = originDataKV.key; string filePath = originDataKV.value; if (fileName.Equals("Level")) { //执行level的循环操作 for (int j = 0; j < 51; j++) { filePath = "/Data/MapData/Level_" + j + ".json"; Debug.Log(Application.streamingAssetsPath + filePath); WWW data = MyResourceManager.Instance.LoadAssetsUsingWWW(filePath); yield return(data); //判断www访问的数据是否发生了错误 if (!String.IsNullOrEmpty(data.error)) { //打印错误的信息 Debug.Log(data.error); } else { FileStream originFile = File.Create(Application.persistentDataPath + filePath); originFile.Write(data.bytes, 0, data.bytes.Length); originFile.Flush(); originFile.Close(); } Debug.Log("地图完成" + j); if (data.isDone) { data.Dispose(); } } } else if (fileName.Equals("NPC")) { //执行npc的循环操作 for (int j = 0; j < 13; j++) { filePath = "/Data/NPCs/NPC_" + j + ".json"; WWW data = MyResourceManager.Instance.LoadAssetsUsingWWW(filePath); yield return(data); //判断www访问的数据是否发生了错误 if (!String.IsNullOrEmpty(data.error)) { //打印错误的信息 Debug.Log(data.error); } else { FileStream originFile = File.Create(Application.persistentDataPath + filePath); originFile.Write(data.bytes, 0, data.bytes.Length); originFile.Flush(); originFile.Close(); } Debug.Log("NPC完成" + j); if (data.isDone) { data.Dispose(); } } } else { Debug.Log(Application.streamingAssetsPath + filePath); WWW data = MyResourceManager.Instance.LoadAssetsUsingWWW(filePath); // WWW data = new WWW(pathHead + Application.streamingAssetsPath + filePath); yield return(data); //判断www访问的数据是否发生了错误 if (!String.IsNullOrEmpty(data.error)) { //打印错误的信息 Debug.Log(data.error); } else { FileStream originFile = File.Create(Application.persistentDataPath + filePath); originFile.Write(data.bytes, 0, data.bytes.Length); originFile.Flush(); originFile.Close(); } Debug.Log(fileName + "完成了"); Debug.Log(filePath); if (data.isDone) { data.Dispose(); } } } if (copyFinishCallBack != null) { copyFinishCallBack(); } GameSettings gameSettings = GameManager.Instance.gameDataCenter.gameSettings; string dateString = DateTime.Now.ToShortDateString(); gameSettings.installDateString = dateString; GameManager.Instance.persistDataManager.SaveGameSettings(); //初始化数据 IEnumerator initDataCoroutine = InitData(); StartCoroutine(initDataCoroutine); }
/// <summary> /// 使用地图附加信息初始化怪物 /// </summary> /// <param name="mapIndex">Map index.</param> /// <param name="attachedInfo">Attached info.</param> public override void InitializeWithAttachedInfo(int mapIndex, MapAttachedInfoTile attachedInfo) { // 地图序号 this.mapIndex = mapIndex; this.oriPos = attachedInfo.position; this.moveOrigin = attachedInfo.position; this.moveDestination = attachedInfo.position; if (monsterSayContainer != null) { monsterSayContainer.enabled = false; } if (tmPro != null) { tmPro.text = string.Empty; } // 标记怪物是否可以移动 canMove = bool.Parse(KVPair.GetPropertyStringWithKey("canMove", attachedInfo.properties)); // 击败怪物对应控制的地图事件的位置信息 string pairEventPosString = KVPair.GetPropertyStringWithKey("pairEventPos", attachedInfo.properties); // 击败怪物后控制地图事件的位置信息 if (pairEventPosString != string.Empty && pairEventPosString != "-1") { string[] posXY = pairEventPosString.Split(new char[] { '_' }, System.StringSplitOptions.RemoveEmptyEntries); int posX = int.Parse(posXY [0]); int posY = mapHeight - int.Parse(posXY [1]) - 1; pairEventPos = new Vector3(posX, posY, transform.position.z); } else { pairEventPos = -Vector3.one; } this.gameObject.SetActive(true); for (int i = 0; i < alertAreas.Length; i++) { alertAreas [i].InitializeAlertArea(); } // 隐藏所有的探测区域 HideAllAlertAreas(); // 随机朝向 RandomTowards(); // 如果可以运动,则显示探测区域 if (canMove) { ShowAlertAreaTint(); } transform.position = attachedInfo.position; gameObject.SetActive(true); GetComponent <Monster> ().ResetBattleAgentProperties(true); // 激活怪物 baCtr.SetAlive(); // 重置属性 bc2d.enabled = true; isReadyToFight = false; isTriggered = false; baCtr.isIdle = false; isInMoving = false; isInAutoWalk = false; alertTint.gameObject.SetActive(false); }
/// <summary> /// 读取地图数据,并将数据转化为自定义的MapData模型 /// </summary> /// <returns>The map.</returns> /// <param name="mapDataPath">Map data path.</param> private static HLHMapData ReadMap(string mapDataPath, FileInfo fi) { // 读取原始json数据 string oriMapDataString = DataHandler.LoadDataString(mapDataPath); // MiniJson解析json数据 Dictionary <string, object> oriMapDataDic = Json.Deserialize(oriMapDataString) as Dictionary <string, object>; // 获取地图宽高 int mapHeight = int.Parse(oriMapDataDic ["height"].ToString()); int mapWidth = int.Parse(oriMapDataDic ["width"].ToString()); Debug.LogFormat("name:{0},height:{1},width:{2}", fi.Name, mapHeight, mapWidth); // 获取标准地图块宽高 int mapTileHeight = int.Parse(oriMapDataDic ["tileheight"].ToString()); int mapTileWidth = int.Parse(oriMapDataDic ["tilewidth"].ToString()); // 获取地图上所有图层的数据 List <object> layersDataArray = (List <object>)oriMapDataDic ["layers"]; List <object> tileSetsArray = (List <object>)oriMapDataDic ["tilesets"]; // 所有构建地图使用的图层 List <MapLayer> mapLayers = new List <MapLayer> (); // 所有地图附加信息图层 List <MapAttachedInfoLayer> attachedInfoLayers = new List <MapAttachedInfoLayer> (); // 地图块图集名称 string tileSetsImageName = string.Empty; // 地图块数据(内含walkable info array) MapTilesInfo tileInfo = null; Dictionary <string, object> tileSetInfo = null; int[,] mapWalkableInfoArray = new int[mapWidth, mapHeight]; for (int m = 0; m < mapWidth; m++) { for (int n = 0; n < mapHeight; n++) { mapWalkableInfoArray[m, n] = 1; } } for (int j = 0; j < layersDataArray.Count; j++) { Dictionary <string, object> layerDataDic = layersDataArray [j] as Dictionary <string, object>; List <MapTile> tileDatas = new List <MapTile> (); //获取当前层的类型名称 string layerType = layerDataDic ["type"].ToString(); // 当前层为地图层 if (layerType.Equals("tilelayer")) { List <object> tileDataArray = (List <object>)layerDataDic["data"]; string layerName = layerDataDic["name"].ToString(); if (layerName.Equals("FloorLayer")) { Dictionary <string, object> tileSetsNameDic = layerDataDic["properties"] as Dictionary <string, object>; tileSetsImageName = tileSetsNameDic["TilesetImageName"].ToString(); tileInfo = GetMapTilesInfoWithName(tileSetsImageName); } if (tileSetInfo == null) { for (int k = 0; k < tileSetsArray.Count; k++) { Dictionary <string, object> tempTileSetInfo = tileSetsArray[k] as Dictionary <string, object>; string sourceName = tempTileSetInfo["source"] as string; if (sourceName.Contains(tileSetsImageName)) { tileSetInfo = tempTileSetInfo; break; } } } //获取当前层使用的地图块的firstGid int firstGid = int.Parse(tileSetInfo["firstgid"].ToString()); for (int k = 0; k < tileDataArray.Count; k++) { int row = mapHeight - k / mapWidth - 1; int col = k % mapWidth; int tileIndex = int.Parse(tileDataArray [k].ToString()) - firstGid; if (tileIndex >= 0) { bool canWalk = false; //Debug.LogFormat("mapName:{0},layerName:{1},tileIndex:{2},firstGid:{3}",fi.Name, layerName, tileIndex,firstGid); canWalk = tileInfo.walkableInfoArray [tileIndex] == 1; int miniMapInfo = tileInfo.miniMapInfoArray[tileIndex]; MapTile tile = new MapTile(new Vector2(col, row), tileIndex, canWalk, miniMapInfo); tileDatas.Add(tile); if (layerName == "DecorationLayer") { mapWalkableInfoArray[col, row] = 0; } } } MapLayer layer = new MapLayer(layerName, tileDatas); mapLayers.Add(layer); } // 当前层为事件层 if (layerType.Equals("objectgroup")) { string layerName = layerDataDic ["name"].ToString(); List <MapAttachedInfoTile> attachedInfoTiles = new List <MapAttachedInfoTile> (); List <object> attachedInfoList = (List <object>)layerDataDic ["objects"]; //int monsterCount = 0; for (int k = 0; k < attachedInfoList.Count; k++) { Dictionary <string, object> attachedInfo = attachedInfoList [k] as Dictionary <string, object>; string type = attachedInfo ["type"].ToString(); //Debug.Log(type); //if(type=="monster"){ // monsterCount++; //} float posX = float.Parse(attachedInfo ["x"].ToString()); float posY = float.Parse(attachedInfo ["y"].ToString()); int col = Mathf.RoundToInt(posX / mapTileWidth); int row = mapHeight - Mathf.RoundToInt(posY / mapTileHeight); Vector2 pos = new Vector2(col, row); if (mapWalkableInfoArray[col, row] == 0 && type != "doorGear" && type != "keyDoorGear") { Debug.LogFormat("地图:{0},事件位置和某种装饰位置重合:[{1}/{2},事件类型:{3}](如果装饰不是墙体,则不是错误)", fi.Name, col, mapHeight - row - 1, type); } Dictionary <string, object> properties = null; MapAttachedInfoTile attachedInfoTile = null; List <KVPair> kvPairs = new List <KVPair> (); if (!attachedInfo.ContainsKey("properties")) { attachedInfoTile = new MapAttachedInfoTile(type, pos, kvPairs); attachedInfoTiles.Add(attachedInfoTile); continue; } properties = attachedInfo ["properties"] as Dictionary <string, object>; IDictionaryEnumerator dicEnumerator = properties.GetEnumerator(); while (dicEnumerator.MoveNext()) { string key = dicEnumerator.Key.ToString(); string value = dicEnumerator.Value.ToString(); // Debug.LogFormat ("key:{0},value:{1}",key,value); KVPair kv = new KVPair(key, value); kvPairs.Add(kv); } attachedInfoTile = new MapAttachedInfoTile(type, pos, kvPairs); attachedInfoTiles.Add(attachedInfoTile); } MapAttachedInfoLayer attachedInfoLayer = new MapAttachedInfoLayer(layerName, attachedInfoTiles); attachedInfoLayers.Add(attachedInfoLayer); } } HLHMapData mapData = new HLHMapData(mapHeight, mapWidth, tileSetsImageName, mapLayers, attachedInfoLayers); return(mapData); }
public override void InitializeWithAttachedInfo(int mapIndex, MapAttachedInfoTile attachedInfo) { this.mapIndex = mapIndex; transform.position = attachedInfo.position; //this.hasPuzzle = true; this.hasPuzzle = false; isWordTrigger = bool.Parse(KVPair.GetPropertyStringWithKey("isWordTrigger", attachedInfo.properties)); isOpen = bool.Parse(KVPair.GetPropertyStringWithKey("isOpen", attachedInfo.properties)); isSwitchControledDoor = !isWordTrigger && !isOpen; direction = int.Parse(KVPair.GetPropertyStringWithKey("direction", attachedInfo.properties)); if (MapEventsRecord.IsMapEventTriggered(mapIndex, attachedInfo.position)) { OpenTheDoor(false); } else if (GameManager.Instance.gameDataCenter.currentMapEventsRecord.IsMapEventTriggered(mapIndex, attachedInfo.position)) { OpenTheDoor(false); } if (!isOpen) { mapItemRenderer.enabled = true; mapItemRenderer.sprite = doorCloseSprites [direction]; } else { mapItemRenderer.sprite = null; mapItemRenderer.enabled = false; } string pairDoorPosString = KVPair.GetPropertyStringWithKey("pairDoorPos", attachedInfo.properties); string[] posXY = pairDoorPosString.Split(new char[] { '_' }, System.StringSplitOptions.RemoveEmptyEntries); int posX = int.Parse(posXY [0]); int posY = mapHeight - int.Parse(posXY [1]) - 1; pairDoorPos = new Vector3(posX, posY, transform.position.z); bc2d.enabled = true; SetSortingOrder(-(int)transform.position.y); if (isWordTrigger) { bool hasValidWord = false; for (int i = 0; i < wordsArray.Length; i++) { if (wordsArray[i].spell.Length <= 7) { hasValidWord = true; word = wordsArray[i]; break; } } if (!hasValidWord) { word = GetAValidWord(); } GameManager.Instance.pronounceManager.DownloadPronounceCache(word); } }
public override void InitializeWithAttachedInfo(int mapIndex, MapAttachedInfoTile attachedInfo) { this.mapIndex = mapIndex; transform.position = attachedInfo.position; isGoldTreasureBox = false; if (KVPair.ContainsKey("type", attachedInfo.properties)) { string type = KVPair.GetPropertyStringWithKey("type", attachedInfo.properties); isGoldTreasureBox = type == "1"; } int rewardItemId = 0; if (!isGoldTreasureBox) { mapItemRenderer.sprite = normalTbSprite; treasureType = TreasureType.NormalTreasureBox; } else { mapItemRenderer.sprite = specialTbSprite; treasureType = TreasureType.GoldTreasureBox; } if (KVPair.ContainsKey("dropID", attachedInfo.properties)) { rewardItemId = int.Parse(KVPair.GetPropertyStringWithKey("dropID", attachedInfo.properties)); } else { rewardItemId = GetRandomItemIdFromGameLevelData(); } if (isGoldTreasureBox && MapEventsRecord.IsMapEventTriggered(mapIndex, attachedInfo.position)) { AddToPool(ExploreManager.Instance.newMapGenerator.mapEventsPool); } rewardItem = Item.NewItemWith(rewardItemId, 1); if (rewardItem.itemType == ItemType.Equipment) { Equipment eqp = rewardItem as Equipment; int randomSeed = Random.Range(0, 100); int graySeed = 0; int blueSeed = 0; // 物品品质概率 switch (Player.mainPlayer.luckInOpenTreasure) { case 0: graySeed = 65 - Player.mainPlayer.extraLuckInOpenTreasure; blueSeed = 95 - Player.mainPlayer.extraLuckInOpenTreasure; break; case 1: graySeed = 60 - Player.mainPlayer.extraLuckInOpenTreasure; blueSeed = 90 - Player.mainPlayer.extraLuckInOpenTreasure; break; } EquipmentQuality quality = EquipmentQuality.Gray; // 如果不是金色宝箱,则随机一种品质 if (!isGoldTreasureBox) { if (randomSeed < graySeed) { quality = EquipmentQuality.Gray; } else if (randomSeed < blueSeed) { quality = EquipmentQuality.Blue; } else { quality = EquipmentQuality.Gold; } } else { quality = EquipmentQuality.Gold; } eqp.ResetPropertiesByQuality(quality); } CheckIsWordTriggeredAndShow(); bc2d.enabled = true; //mapItemAnimator.gameObject.SetActive (false); mapItemRenderer.enabled = true; int sortingOrder = -(int)transform.position.y; SetSortingOrder(sortingOrder); //SetAnimationSortingOrder (sortingOrder); }