public void Clone(Mode mode) { var layers = _tileMapTarget.Layers; for (int l = 0; l < layers.Length; l++) { var layer = layers[l]; for (int w = 0; w < _tileMapTarget.width; w++) { for (int h = 0; h < _tileMapTarget.height; h++) { var tileId = _tileMapTarget.GetTile(w, h, l); if (tileId != -1) { var tileInfo = _tileMapTarget.GetTileInfoForTileId(tileId); if (tileInfo.stringVal == "n") { continue; } var nameIndex = _targetNameIndexes[tileId]; var tileToChange = 0; if (tileInfo.stringVal == "c") { tileToChange = getSpriteId("tileset/" + tileInfo.intVal); } if (tileInfo.stringVal == "") { tileToChange = getSpriteId("tileset/" + (nameIndex + _offset)); } switch (mode) { case Mode.Safe: cloneSafe(tileId, tileToChange, w, h, l); break; case Mode.Override: cloneOverride(tileToChange, w, h, l); break; case Mode.SafeOverride: cloneSafeAdd(tileId, tileToChange, w, h, l); break; } } } } } _tileMap.Build(); }
void Update() { if (isGoal) { if (!isGoalWalkStarted) { isGoalWalkStarted = true; player.animator.Play("Walk"); player.animator.ClipFps = 2f; player.sprite.scale = new Vector3(player.sprite.scale.x * -1f, player.sprite.scale.y, player.sprite.scale.z); } } // Attempt to get ground position if (player.isGrounded) { tempGroundPosition = lastGroundPositionY; lastGroundPositionY = player.transform.position.y; } if (!isFairyPause && player.isAlive && isGameReady) { // Falling death if (playerTransform.position.y < cameraTransform.position.y - camera.ScreenExtents.height / 2f) { player.Die(false, true); } if (player.isAlive) { UpdateScore(); healthTimer -= Time.deltaTime; if (healthTimer < 0) { healthTimer = healthTimerAmount; Damage(1); } // FAIRY INVINCIBILITY COUNTDOWN if (isFairy) { fairyTimer += Time.deltaTime; if (fairyTimer >= fairyTime) { Fairy.instance.transform.DOLocalMove(new Vector2(8f, 18f), 2.5f).SetEase(Ease.Linear).OnComplete(() => { isFairy = false; Destroy(Fairy.instance.gameObject); }); // Fairy.instance.FlyAway (); } } } if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } if (Input.GetKeyDown(KeyCode.R)) { UnityEngine.SceneManagement.SceneManager.LoadScene(0); } RaycastHit2D hit = Physics2D.Raycast(playerTransform.position, Vector2.down, 200f, LayerMask.GetMask("Ground TM")); if (hit) { tileInfo = tilemap.GetTileInfoForTileId(tilemap.GetTileIdAtPosition(hit.point, 1)); if (tileInfo != null) { if (tileInfo.intVal == 1) { isSlope = true; tilemap.GetTileAtPosition(hit.point, out tileX, out tileY); } else { isSlope = false; } } else { isSlope = false; } int tileId = tilemap.GetTileIdAtPosition(hit.point, 1); tileInfo = tilemap.GetTileInfoForTileId(tilemap.GetTileIdAtPosition(hit.point, 1)); } } }
/// <summary> /// 获取地砖信息 /// </summary> /// <returns>The tile info.</returns> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> /// <param name="layer">Layer.</param> public tk2dRuntime.TileMap.TileInfo GetTileInfo(int x, int y, int layer) { return(Map.GetTileInfoForTileId(Map.GetTile(x, y, layer))); }
//绘制窗口时调用 void OnGUI() { if (Prefab == null) { return; } else { Selection.activeGameObject = Prefab; } data = null; GUILayout.BeginArea(new Rect(5, 5, 800, 20)); GUI.Label(new Rect(0, 0, 50, 18), "搜索名称:"); searchKeyword = GUI.TextField(new Rect(55, 0, 100, 18), searchKeyword); if (GUI.Button(new Rect(160, 0, 30, 18), "搜索")) { selGridInt = 0; fetchData(searchKeyword); } GUI.Label(new Rect(195, 0, 60, 18), "区域名:"); areaName = GUI.TextField(new Rect(260, 0, 100, 18), areaName); if (GUI.Button(new Rect(365, 0, 40, 18), "改名")) { if (areaData[sceneName] != null) { areaData[sceneName]["Name"] = areaName; writeAreaDataToJson(); this.ShowNotification(new GUIContent("改名成功")); } else { this.ShowNotification(new GUIContent("区域名称数据不存在!")); } } GUILayout.EndArea(); float listStartX = 5; float listStartY = 25; float scrollHeight = Screen.currentResolution.height - 110; if (listNames != null && listNames.Count > 0) { float contextHeight = listNames.Count * 21; //开始滚动视图 scrollPosition = GUI.BeginScrollView(new Rect(listStartX, listStartY, 200, scrollHeight), scrollPosition, new Rect(5, 5, 190, contextHeight), false, scrollHeight < contextHeight); selGridInt = GUILayout.SelectionGrid(selGridInt, listNames.ToArray(), 1, GUILayout.Width(190)); selGridInt = selGridInt >= listNames.Count ? listNames.Count - 1 : selGridInt; data = showListData[selGridInt]; if (selGridInt != oldSelGridInt) { oldSelGridInt = selGridInt; showId = data.Id; eventName = data.Name; sceneEventIndex = sceneEventIndexMapping[data.Type]; eventId = data.EventId; string[] fen = showId.Split(new char[] { '_' }); Prefab.transform.position = map.GetTilePosition(int.Parse(fen[1]), int.Parse(fen[2])); SceneView.lastActiveSceneView.pivot = Prefab.transform.position; switch (data.Type) { case SceneEventType.EnterArea: birthPointEventIdIndex = allBirthPointIdIndexs.ContainsKey(data.EventId) ? allBirthPointIdIndexs[data.EventId] : 0; break; case SceneEventType.EnterCity: citySceneIdIndex = allCitySceneIdIndexs.ContainsKey(data.EventId) ? allCitySceneIdIndexs[data.EventId] : 0; break; case SceneEventType.Battle: fightEventIdIndex = allFightIdIndexs.ContainsKey(data.EventId) ? allFightIdIndexs[data.EventId] : 0; break; default: break; } eventOpenTypeIndex = sceneEventOpenIndexMapping.ContainsKey(data.OpenType) ? sceneEventOpenIndexMapping[data.OpenType] : 0; switch (sceneEventOpenTypeEnums[eventOpenTypeIndex]) { case SceneEventOpenType.FightWined: if (allFightNames.Count <= openKeyIndex) { openKeyIndex = 0; } openKeyIndex = allFightIdIndexs.ContainsKey(data.OpenKey) ? allFightIdIndexs[data.OpenKey] : 0; break; case SceneEventOpenType.NeedItem: if (Base.ItemDataNames.Count <= openKeyIndex) { openKeyIndex = 0; } openKeyIndex = Base.ItemDataIdIndexs.ContainsKey(data.OpenKey) ? Base.ItemDataIdIndexs[data.OpenKey] : 0; break; case SceneEventOpenType.NeedTasks: stringValue = !string.IsNullOrEmpty(data.StringValue) ? data.StringValue : ""; break; default: openKey = ""; break; } eventNotice = data.Notice; intValue = data.IntValue; if (meetEnemyRatesMapping.ContainsKey(sceneName)) { currentMeetEnemyRates = meetEnemyRatesMapping[sceneName]; } else { currentMeetEnemyRates = new List <RateData>(); meetEnemyRatesMapping.Add(sceneName, currentMeetEnemyRates); } for (int i = 0; i < currentMeetEnemyRates.Count; i++) { currentMeetEnemyRates[i].IdIndex = allFightIdIndexs.ContainsKey(currentMeetEnemyRates[i].Id) ? allFightIdIndexs[currentMeetEnemyRates[i].Id] : 0; } } //结束滚动视图 GUI.EndScrollView(); if (data != null) { GUILayout.BeginArea(new Rect(listStartX + 205, listStartY, 800, 300)); GUI.Label(new Rect(0, 0, 60, 18), "Id:"); EditorGUI.TextField(new Rect(65, 0, 150, 18), showId); GUI.Label(new Rect(220, 0, 50, 18), "开启条件:"); eventOpenTypeIndex = EditorGUI.Popup(new Rect(275, 0, 80, 18), eventOpenTypeIndex, sceneEventOpenStrs.ToArray()); switch (sceneEventOpenTypeEnums[eventOpenTypeIndex]) { case SceneEventOpenType.FightWined: if (allFightNames.Count <= openKeyIndex) { openKeyIndex = 0; } openKeyIndex = EditorGUI.Popup(new Rect(360, 0, 150, 18), openKeyIndex, allFightNames.ToArray()); openKey = allFights[openKeyIndex].Id; eventNotice = EditorGUI.TextArea(new Rect(515, 0, 100, 36), eventNotice); break; case SceneEventOpenType.NeedItem: if (Base.ItemDataNames.Count <= openKeyIndex) { openKeyIndex = 0; } openKeyIndex = EditorGUI.Popup(new Rect(360, 0, 150, 18), openKeyIndex, Base.ItemDataNames.ToArray()); openKey = Base.ItemDatas[openKeyIndex].Id; eventNotice = EditorGUI.TextArea(new Rect(515, 0, 100, 36), eventNotice); break; case SceneEventOpenType.NeedTasks: stringValue = EditorGUI.TextField(new Rect(360, 0, 150, 18), stringValue); eventNotice = EditorGUI.TextArea(new Rect(515, 0, 100, 36), eventNotice); break; default: openKey = ""; break; } GUI.Label(new Rect(0, 20, 60, 18), "事件名称:"); eventName = EditorGUI.TextField(new Rect(65, 20, 150, 18), eventName); GUI.Label(new Rect(0, 40, 60, 18), "事件类型:"); sceneEventIndex = EditorGUI.Popup(new Rect(65, 40, 150, 18), sceneEventIndex, sceneEventStrs.ToArray()); switch (sceneEventTypeEnums[sceneEventIndex]) { case SceneEventType.EnterArea: birthPointEventIdIndex = EditorGUI.Popup(new Rect(220, 40, 150, 18), birthPointEventIdIndex, allBirthPointNames.ToArray()); eventId = allBirthPointEvents[birthPointEventIdIndex].Id; break; case SceneEventType.EnterCity: citySceneIdIndex = EditorGUI.Popup(new Rect(220, 40, 150, 18), citySceneIdIndex, allCitySceneNames.ToArray()); eventId = allCityScenes[citySceneIdIndex].Id; break; case SceneEventType.Battle: fightEventIdIndex = EditorGUI.Popup(new Rect(220, 40, 150, 18), fightEventIdIndex, allFightNames.ToArray()); eventId = allFights[fightEventIdIndex].Id; break; case SceneEventType.EatFood: GUI.Label(new Rect(220, 40, 65, 18), "开启干粮数:"); intValue = (int)EditorGUI.Slider(new Rect(285, 40, 180, 18), (float)intValue, 1, 99); break; default: break; } GUI.Label(new Rect(0, 60, 100, 18), "事件Id:"); eventId = EditorGUI.TextField(new Rect(65, 60, 150, 18), eventId); if (GUI.Button(new Rect(0, 80, 80, 18), "修改事件")) { if (eventName == "") { this.ShowNotification(new GUIContent("事件名不能为空!")); return; } if (eventId == "") { this.ShowNotification(new GUIContent("事件Id不能为空!")); return; } data.Name = eventName; data.Type = sceneEventTypeEnums[sceneEventIndex]; data.EventId = eventId; data.OpenType = sceneEventOpenTypeEnums[eventOpenTypeIndex]; data.OpenKey = openKey; data.Notice = eventNotice; data.IntValue = intValue; data.StringValue = stringValue; writeDataToJson(); oldSelGridInt = -1; getData(); fetchData(searchKeyword); this.ShowNotification(new GUIContent("修改成功")); } string[] fen = showId.Split(new char[] { '_' }); if (fen.Length >= 3) { int i = int.Parse(fen[1]); int j = int.Parse(fen[2]); if (map.GetTileInfoForTileId(map.GetTile(i, j, 1)) == null) { if (GUI.Button(new Rect(85, 80, 300, 18), "该事件已从场景中被删除,点击清除此残余数据")) { if (!dataMapping.ContainsKey(data.Id)) { this.ShowNotification(new GUIContent("要删除的数据不存在!")); return; } dataMapping.Remove(data.Id); writeDataToJson(); oldSelGridInt = -1; getData(); fetchData(searchKeyword); this.ShowNotification(new GUIContent("删除残余事件成功")); } } } GUILayout.EndArea(); GUILayout.BeginArea(new Rect(listStartX + 205, listStartY + 305, 800, 500)); GUI.Label(new Rect(0, 0, 60, 18), "随机遇敌:"); RateData rateData; for (int i = 0; i < currentMeetEnemyRates.Count; i++) { if (currentMeetEnemyRates.Count <= i) { continue; } rateData = currentMeetEnemyRates[i]; GUI.Label(new Rect(0, 20 + i * 20, 60, 18), "遇敌战斗:"); rateData.IdIndex = EditorGUI.Popup(new Rect(65, 20 + i * 20, 150, 18), rateData.IdIndex, allFightNames.ToArray()); rateData.Id = allFights[rateData.IdIndex].Id; GUI.Label(new Rect(220, 20 + i * 20, 60, 18), "遇敌概率:"); rateData.Rate = EditorGUI.Slider(new Rect(285, 20 + i * 20, 180, 18), rateData.Rate, 0, 100); if (GUI.Button(new Rect(470, 20 + i * 20, 40, 18), "删除")) { currentMeetEnemyRates.RemoveAt(i); } } if (GUI.Button(new Rect(0, 180, 60, 18), "修改战斗")) { // if (currentMeetEnemyRates.Count == 0) { // this.ShowNotification(new GUIContent("还没有添加任何随机战斗!")); // return; // } Base.CreateFile(Application.dataPath + "/Resources/Data/Json", "AreaMeetEnemys.json", JsonManager.GetInstance().SerializeObject(meetEnemyRatesMapping)); this.ShowNotification(new GUIContent("修改随机遇敌数据成功")); } if (GUI.Button(new Rect(65, 180, 60, 18), "添加战斗")) { if (currentMeetEnemyRates.Count >= 8) { this.ShowNotification(new GUIContent("每个区域最多出现8种战斗!")); return; } currentMeetEnemyRates.Add(new RateData(100, "0", 0)); } GUILayout.EndArea(); } } }
static void InitParams() { //读取区域大地图宽高数据表 areaSizeMapping = new Dictionary <string, SizeData>(); JObject obj = JsonManager.GetInstance().GetJson("AreaSizeDatas", false); foreach (var item in obj) { if (item.Key != "0") { areaSizeMapping.Add(item.Value["Id"].ToString(), JsonManager.GetInstance().DeserializeObject <SizeData>(item.Value.ToString())); } } int index = 0; FieldInfo fieldInfo; object[] attribArray; DescriptionAttribute attrib; //加载全部的SceneEventType枚举类型 sceneEventTypeEnums = new List <SceneEventType>(); sceneEventStrs = new List <string>(); sceneEventIndexMapping = new Dictionary <SceneEventType, int>(); index = 0; foreach (SceneEventType type in Enum.GetValues(typeof(SceneEventType))) { sceneEventTypeEnums.Add(type); fieldInfo = type.GetType().GetField(type.ToString()); attribArray = fieldInfo.GetCustomAttributes(false); attrib = (DescriptionAttribute)attribArray[0]; sceneEventStrs.Add(attrib.Description); sceneEventIndexMapping.Add(type, index); index++; } //加载全部的SceneEventOpeType枚举类型 sceneEventOpenTypeEnums = new List <SceneEventOpenType>(); sceneEventOpenStrs = new List <string>(); sceneEventOpenIndexMapping = new Dictionary <SceneEventOpenType, int>(); index = 0; foreach (SceneEventOpenType type in Enum.GetValues(typeof(SceneEventOpenType))) { sceneEventOpenTypeEnums.Add(type); fieldInfo = type.GetType().GetField(type.ToString()); attribArray = fieldInfo.GetCustomAttributes(false); attrib = (DescriptionAttribute)attribArray[0]; sceneEventOpenStrs.Add(attrib.Description); sceneEventOpenIndexMapping.Add(type, index); index++; } //初始化地图中的静态事件地砖对应的事件关联数据 map = GameObject.Find("TileMap").GetComponent <tk2dTileMap>(); string[] fen = EditorApplication.currentScene.Split(new char[] { '/' }); sceneName = fen[fen.Length - 1].Replace(".unity", ""); tk2dRuntime.TileMap.TileInfo tile; EventData exsitEventData; EventData newEventData; string id; List <EventData> eventDatas = new List <EventData>(); allBirthPointNames = new List <string>(); allBirthPointIdIndexs = new Dictionary <string, int>(); allBirthPointEvents = new List <EventData>(); //处理区域大地图中文名 areaData = JsonManager.GetInstance().GetJson("AreaNames", false); if (areaData[sceneName] == null) { areaData[sceneName] = new JObject(); areaData[sceneName]["Id"] = sceneName; areaData[sceneName]["Name"] = sceneName; areaName = sceneName; } else { areaName = areaData[sceneName]["Name"].ToString(); } writeAreaDataToJson(); //获取旧数据 dataMapping = new Dictionary <string, EventData>(); obj = JsonManager.GetInstance().GetJson("AreaEventDatas", false); EventData eventData; index = 0; foreach (var item in obj) { if (item.Key != "0") { eventData = JsonManager.GetInstance().DeserializeObject <EventData>(item.Value.ToString()); if (eventData.SceneId == sceneName) { dataMapping.Add(eventData.Id, eventData); } //查找所有的出生点事件 if (eventData.Type == SceneEventType.BirthPoint) { allBirthPointNames.Add(eventData.Name); allBirthPointIdIndexs.Add(eventData.Id, index); allBirthPointEvents.Add(eventData); index++; } } } for (int i = 0; i < map.width; i++) { for (int j = 0; j < map.height; j++) { tile = map.GetTileInfoForTileId(map.GetTile(i, j, 1)); if (tile != null) { id = sceneName + "_" + i + "_" + j; newEventData = new EventData(); newEventData.Id = id; newEventData.SceneId = sceneName; if (dataMapping.ContainsKey(newEventData.Id)) { exsitEventData = dataMapping[newEventData.Id]; newEventData.Name = exsitEventData.Name; newEventData.EventId = exsitEventData.EventId; } else { newEventData.Name = "默认事件点 - " + i + "," + j; } eventDatas.Add(newEventData); } } } //删除掉旧的数据 dataMapping.Clear(); JsonManager.GetInstance().Clear(); getData(); //添加新数据 foreach (EventData item in eventDatas) { if (!dataMapping.ContainsKey(item.Id)) { dataMapping.Add(item.Id, item); } } //生成新数据 writeDataToJson(); getData(); allCitySceneIdIndexs = new Dictionary <string, int>(); allCitySceneNames = new List <string>(); allCityScenes = new List <SceneData>(); obj = JsonManager.GetInstance().GetJson("Scenes", false); SceneData sceneData; index = 0; foreach (var item in obj) { if (item.Key != "0") { sceneData = JsonManager.GetInstance().DeserializeObject <SceneData>(item.Value.ToString()); if (sceneData.BelongToAreaName == sceneName) { allCitySceneNames.Add(sceneData.Name); allCitySceneIdIndexs.Add(sceneData.Id, index); allCityScenes.Add(sceneData); index++; } } } allFightIdIndexs = new Dictionary <string, int>(); allFightNames = new List <string>(); allFights = new List <FightData>(); obj = JsonManager.GetInstance().GetJson("Fights", false); FightData fightData; index = 0; foreach (var item in obj) { if (item.Key != "0") { fightData = JsonManager.GetInstance().DeserializeObject <FightData>(item.Value.ToString()); if (fightData.Type == FightType.Normal) { allFightNames.Add(fightData.Name); allFightIdIndexs.Add(fightData.Id, index); allFights.Add(fightData); index++; } } } }
//false:不可通过 //true:可通过 public bool checkTile(int x, int y) { if (!(x < 11 && y < 11 && x > -1 && y > -1)) { return(false); } tm = GameObject.Find("TileMap").GetComponent <tk2dTileMap>(); AM = GetComponent <ActionManager>(); int tileID = tm.GetTile(x, y, 1); if (tileID == -1) { return(true); } tk2dRuntime.TileMap.TileInfo tileinfo = tm.GetTileInfoForTileId(tileID); if (tileinfo != null) { if (tileinfo.stringVal == "Wall") { return(false); } //拥有Prefab Vector2 startPosition = new Vector2(x - 0.2f, y); Vector2 Direction = new Vector2(x, y); RaycastHit2D hit = Physics2D.Raycast(startPosition, Direction, 0.4f); //Debug.DrawLine(new Vector3(x - 0.2f, y, 0), new Vector3(x + 0.2f, y, 0), Color.red); if (hit.collider != null) { switch (hit.collider.tag) { case "Talk": AM.talk(hit.collider.gameObject.GetComponent <Talk>()); return(false); case "Key": if (AM.key(hit.collider.gameObject.GetComponent <Key>())) { tm.ClearTile(x, y, 1); } break; case "Door": if (AM.door(hit.collider.gameObject.GetComponent <Door>())) { tm.ClearTile(x, y, 1); } return(false); case "DoorWall": if (AM.doorwall(hit.collider.gameObject.GetComponent <DoorWall>())) { tm.ClearTile(x, y, 1); } return(false); case "Baoshi": if (AM.baoshi(hit.collider.gameObject.GetComponent <Baoshi>())) { tm.ClearTile(x, y, 1); } break; case "Pingzi": if (AM.pingzi(hit.collider.gameObject.GetComponent <Pingzi>())) { tm.ClearTile(x, y, 1); } break; case "Yaoshui": if (AM.yaoshui(hit.collider.gameObject.GetComponent <Yaoshui>())) { tm.ClearTile(x, y, 1); } break; case "Zhuangbei": if (AM.zhuangbei(hit.collider.gameObject.GetComponent <Zhuangbei>())) { tm.ClearTile(x, y, 1); } break; case "Enemy": if (AM.enemy(hit.collider.gameObject.GetComponent <Enemy>())) { tm.ClearTile(x, y, 1); } return(false); case "StairUp": AM.stair(hit.collider.gameObject.GetComponent <Stair>()); break; case "StairDown": AM.stair(hit.collider.gameObject.GetComponent <Stair>()); break; case "Tool": if (AM.mttool(hit.collider.gameObject.GetComponent <mtTool>())) { tm.ClearTile(x, y, 1); } break; } return(true); } else { return(true); } } else { return(false); } }