void ClearAboutPathTest() { { StartNodeMarker[] markers = GameObject.FindObjectsOfType <StartNodeMarker>(); foreach (StartNodeMarker marker in markers) { DestroyImmediate(marker); } } { GoalNodeMarker[] markers = GameObject.FindObjectsOfType <GoalNodeMarker>(); foreach (GoalNodeMarker marker in markers) { DestroyImmediate(marker); } } { GameObject pathMarker = GameObject.Find(NodePathMarker.GameObjName); if (pathMarker) { DestroyImmediate(pathMarker); } } pathFindTestStartNode = null; pathFindTestGoalNode = null; selectedTileMapNodes.Clear(); }
void MakeConnectionEachOtherNodes(Dictionary <TilePos, SquareTileMapNode> nodeMap) { int nodeIndex = 0; for (int y = 0; y < tileHeightCount; ++y) { for (int x = 0; x < tileWidthCount; ++x, ++nodeIndex) { TilePos nodePos = new TilePos(x, y); SquareTileMapNode node = nodeMap[nodePos]; node.ClearConnectionNode(); if (TileMapEngine.Instance.Invalid(node)) { continue; } List <TilePos> adjacentNodeIndexes = new List <TilePos>(); if (x != 0) //left node { adjacentNodeIndexes.Add(nodePos - new TilePos(1, 0)); } if (y < tileHeightCount - 1) // top node { adjacentNodeIndexes.Add(nodePos + new TilePos(0, 1)); } if (x < tileWidthCount - 1) // rightn node { adjacentNodeIndexes.Add(nodePos + new TilePos(1, 0)); } if (y != 0) // bottom node { adjacentNodeIndexes.Add(nodePos - new TilePos(0, 1)); } foreach (TilePos adjacentNodeIndex in adjacentNodeIndexes) { SquareTileMapNode adjacentNode = null; nodeMap.TryGetValue(adjacentNodeIndex, out adjacentNode); if (adjacentNode == null) { Debug.LogError(string.Format("invalid nodePos={0}_{1}", adjacentNodeIndex.x, adjacentNodeIndex.y)); continue; } if (TileMapEngine.Instance.Invalid(adjacentNode)) { continue; } node.AddConnectionNode(adjacentNode); } } } }
SquareTileMapNode MakeTileMapOneTile(int nodeIndex, int x, int y, SquareTileMapNode tilePrefab, GameObject tileMapNodeGroup) { // create tile-map-node-gameobject GameObject newTileMapNodeGameObj = PrefabUtility.InstantiatePrefab(tilePrefab.gameObject) as GameObject; newTileMapNodeGameObj.transform.parent = tileMapNodeGroup.transform; newTileMapNodeGameObj.name = string.Format("tile_{0}-{1}", x, y); SquareTileMapNode squareTileMapNode = newTileMapNodeGameObj.GetComponent <SquareTileMapNode>(); squareTileMapNode.Init(nodeIndex, x, y); Vector3 worldPos = Vector3.zero; { worldPos.x = x * squareTileMapNode.squareSize.x; if (screen2D) { worldPos.y = y * squareTileMapNode.squareSize.y; } else { worldPos.z = y * squareTileMapNode.squareSize.y; } } newTileMapNodeGameObj.transform.position = worldPos; if (screen2D && newTileMapNodeGameObj.GetComponentInChildren <MeshRenderer>() != null) { newTileMapNodeGameObj.transform.rotation = Quaternion.AngleAxis(-90.0f, Vector3.right); } return(squareTileMapNode); }
public void SetTilePos(SquareTileMapNode tileMapNode) { SubOnObjectToTileNodesByCurPos(); this.tilePos = new TilePos(tileMapNode.TilePosX, tileMapNode.TilePosY); this.transform.position = tileMapNode.WorldPosition; AddOnObjectToTileNodesByCurPos(); }
void MakeTileMap() { if (tilePrefab == null) { Debug.LogError("failed to MakeTileMap. null tilePrefab."); return; } // clear curTileMap = null; ClearAboutPathTest(); // new root gameobject const string tileMapRootGameObjectName = "_TileMapRoot"; GameObject tileMapRootGameObj = new GameObject(tileMapRootGameObjectName); tileMapRootGameObj.transform.position = Vector3.zero; TileMapRoot tileMapRootCompo = tileMapRootGameObj.AddComponent <TileMapRoot>() as TileMapRoot; Vector2 tileSize = Vector2.one; if (tilePrefab) { SquareTileMapNode nodeCompo = tilePrefab.GetComponent <SquareTileMapNode>(); tileSize = nodeCompo.squareSize; } tileMapRootCompo.InitFromEditor(screen2D, tileWidthCount, tileHeightCount, tileSize); curTileMap = tileMapRootCompo; Selection.activeObject = curTileMap.gameObject; // new tilenode group gameobject GameObject tileMapNodeGroup = tileMapRootCompo.NodeGroup; // create tile-map-nodes Dictionary <int, SquareTileMapNode> nodeMap = new Dictionary <int, SquareTileMapNode>(); int nodeIndex = 0; for (int y = 0; y < tileHeightCount; ++y) { for (int x = 0; x < tileWidthCount; ++x, ++nodeIndex) { SquareTileMapNode squareTileMapNode = MakeTileMapOneTile(nodeIndex, x, y, tilePrefab, tileMapNodeGroup); nodeMap[nodeIndex] = squareTileMapNode; } } // connect eachother nodes MakeConnectionEachOtherNodes(); }
void ExtendTileMap() { if (curTileMap == null) { return; } if (tilePrefab == null) { Debug.LogError("failed to ExtendTileMap. null tilePrefab."); return; } // clear ClearAboutPathTest(); curTileMap.InitTileMapEngine(); // new tilenode group gameobject GameObject tileMapNodeGroup = curTileMap.NodeGroup; // create tile-map-nodes int nodeIndex = 0; for (int y = 0; y < tileHeightCount; ++y) { for (int x = 0; x < tileWidthCount; ++x, ++nodeIndex) { SquareTileMapNode node = TileMapEngine.Instance.GetTileNode(new TilePos(x, y)); if (node == null) { MakeTileMapOneTile(nodeIndex, x, y, tilePrefab, tileMapNodeGroup); } else { node.Init(nodeIndex, x, y); EditorUtility.SetDirty(node); } } } curTileMap.ExtendFromEditor(tileWidthCount, tileHeightCount); EditorUtility.SetDirty(curTileMap); // connect eachother nodes MakeConnectionEachOtherNodes(); }
void RemakeTileMap() { if (curTileMap == null) { return; } if (tilePrefab == null) { Debug.LogError("failed to MakeTileMap. null tilePrefab."); return; } // clear ClearAboutPathTest(); Vector2 tileSize = Vector2.one; if (tilePrefab) { SquareTileMapNode nodeCompo = tilePrefab.GetComponent <SquareTileMapNode>(); tileSize = nodeCompo.squareSize; } curTileMap.InitFromEditor(screen2D, tileWidthCount, tileHeightCount, tileSize); // new tilenode group gameobject GameObject tileMapNodeGroup = curTileMap.NodeGroup; // create tile-map-nodes Dictionary <int, SquareTileMapNode> nodeMap = new Dictionary <int, SquareTileMapNode>(); int nodeIndex = 0; for (int y = 0; y < tileHeightCount; ++y) { for (int x = 0; x < tileWidthCount; ++x, ++nodeIndex) { SquareTileMapNode squareTileMapNode = MakeTileMapOneTile(nodeIndex, x, y, tilePrefab, tileMapNodeGroup); nodeMap[nodeIndex] = squareTileMapNode; } } // connect eachother nodes MakeConnectionEachOtherNodes(); }
public void AddOnObjectToTileNodesByCurPos() { foreach (TileMapObject obj in tileMapObjects) { TilePos objTilePos = obj.TilePos + this.TilePos; TileMapSize objUnitTileMapSize = obj.TileMapSize; for (int x = 0; x < objUnitTileMapSize.width; ++x) { for (int y = 0; y < objUnitTileMapSize.height; ++y) { TilePos tilePos = objTilePos + new TilePos(x, y); SquareTileMapNode node = TileMapEngine.Instance.GetTileNode(tilePos); if (node) { node.AddOnObject(this); } } } } }
void OnGUI() { scrollPos = EditorGUILayout.BeginScrollView(scrollPos); selectMode = (SelectMode)EditorGUILayout.EnumPopup("Select Mode: ", selectMode); // about create tile { TileMapRoot beforeTileMap = curTileMap; curTileMap = EditorGUILayout.ObjectField("curTileMap", curTileMap, typeof(TileMapRoot), true) as TileMapRoot; if (beforeTileMap != curTileMap) { UpdateTileMapInfo(); } if (curTileMap) { if (GUILayout.Button("select", GUILayout.Width(100.0f))) { Selection.activeObject = curTileMap.gameObject; } } tilePrefab = EditorGUILayout.ObjectField("Tile Prefab", tilePrefab, typeof(SquareTileMapNode), false) as SquareTileMapNode; screen2D = EditorGUILayout.Toggle("screen2D", screen2D); tileWidthCount = EditorGUILayout.IntField("Tile Width Count: ", tileWidthCount); if (tileWidthCount < 0) { tileWidthCount = 0; } if (tileWidthCount > maxTileWidthCount) { tileWidthCount = maxTileWidthCount; } tileHeightCount = EditorGUILayout.IntField("Tile Height Count: ", tileHeightCount); if (tileHeightCount < 0) { tileHeightCount = 0; } if (tileHeightCount > maxTileHeightCount) { tileHeightCount = maxTileHeightCount; } if (tilePrefab && tileWidthCount > 0 && tileHeightCount > 0) { GUILayout.BeginHorizontal(); if (GUILayout.Button("Make New TileMap")) { MakeTileMap(); } if (curTileMap) { if (GUILayout.Button("Remake TileMap")) { RemakeTileMap(); } if (curTileMap.GetMapInfo().tileWidthCount <= tileWidthCount && curTileMap.GetMapInfo().tileHeightCount <= tileHeightCount) { if (GUILayout.Button("Extend TileMap")) { ExtendTileMap(); } } } GUILayout.EndHorizontal(); } } GUIDrawLine(); // about tile selection { // collect selected tileMapNodes selectedTileMapNodes.Clear(); GameObject[] selectedObjects = Selection.gameObjects; foreach (GameObject selectedGameObj in selectedObjects) { SquareTileMapNode tileMapNode = selectedGameObj.GetComponent <SquareTileMapNode>(); if (tileMapNode && selectedGameObj.activeInHierarchy) { selectedTileMapNodes.Add(tileMapNode); } } // show selected tileMapNodes count GUILayout.Label(string.Format("selectedTileMapNodes count: {0}", selectedTileMapNodes.Count)); // about tile remake tilePrefabForChange = EditorGUILayout.ObjectField("Tile Prefab for change", tilePrefabForChange, typeof(SquareTileMapNode), true) as SquareTileMapNode; if (tilePrefabForChange && selectedTileMapNodes.Count > 0) { if (GUILayout.Button("Change selected tiles")) { foreach (SquareTileMapNode selectedMapTile in selectedTileMapNodes) { GameObject tileMapNodeGroup = selectedMapTile.transform.parent.gameObject; SquareTileMapNode squareTileMapNode = MakeTileMapOneTile( selectedMapTile.NodeID, selectedMapTile.TilePosX, selectedMapTile.TilePosY, tilePrefabForChange, tileMapNodeGroup); } foreach (SquareTileMapNode selectedMapTile in selectedTileMapNodes) { DestroyImmediate(selectedMapTile.gameObject); } MakeConnectionEachOtherNodes(); } } GUIDrawLine(); // about tile map object creation { curTileMapObjectGroup = EditorGUILayout.ObjectField("TileMapObjectGroup ", curTileMapObjectGroup, typeof(TileMapObjectGroup), true) as TileMapObjectGroup; if (curTileMapObjectGroup) { if (IsPrefabTarget(curTileMapObjectGroup.gameObject)) { if (selectedTileMapNodes.Count > 0 && curTileMap != null) { if (GUILayout.Button("Create TileMapObjectGroup")) { if (positionedObjGroupGameObj == null) { const string positionGroupOfTileMapObjectGroup = "_PositionedObjectGroups"; positionedObjGroupGameObj = GameObject.Find(positionGroupOfTileMapObjectGroup); if (positionedObjGroupGameObj == null) { positionedObjGroupGameObj = new GameObject(positionGroupOfTileMapObjectGroup); } } foreach (SquareTileMapNode tileMapNode in selectedTileMapNodes) { GameObject newTileMapObjGroup = PrefabUtility.InstantiatePrefab(curTileMapObjectGroup.gameObject) as GameObject; newTileMapObjGroup.transform.parent = positionedObjGroupGameObj.transform; TileMapObjectGroup newTileMapObjGroupCompo = newTileMapObjGroup.GetComponent <TileMapObjectGroup>(); newTileMapObjGroupCompo.SetTilePos(tileMapNode); } } } } else { groundTileForChangeAtSelectedObjectBlockPos = EditorGUILayout.ObjectField("GroundTileForChangeAtObjectBlockPositions", groundTileForChangeAtSelectedObjectBlockPos, typeof(SquareTileMapNode), true) as SquareTileMapNode; if (groundTileForChangeAtSelectedObjectBlockPos && curTileMap) { if (GUILayout.Button("Change groundTile at object block pos")) { MakeConnectionEachOtherNodes(); List <SquareTileMapNode> forChangeGroundTileList = new List <SquareTileMapNode>(); int[,] blockTileData = curTileMapObjectGroup.GetBlockTileData(); for (int x = 0; x < curTileMapObjectGroup.TileMapSize.width; ++x) { for (int y = 0; y < curTileMapObjectGroup.TileMapSize.height; ++y) { int blockFlag = blockTileData[x, y]; if (blockFlag == 1) { TilePos tilePos = curTileMapObjectGroup.TilePos + new TilePos(x, y); SquareTileMapNode node = TileMapEngine.Instance.GetTileNode(tilePos); if (node == null) { Debug.LogError(string.Format("null node : {0}_{1}", tilePos.x, tilePos.y)); continue; } forChangeGroundTileList.Add(node); } } } foreach (SquareTileMapNode node in forChangeGroundTileList) { GameObject tileMapNodeGroup = node.transform.parent.gameObject; SquareTileMapNode squareTileMapNode = MakeTileMapOneTile( node.NodeID, node.TilePosX, node.TilePosY, groundTileForChangeAtSelectedObjectBlockPos, tileMapNodeGroup); } foreach (SquareTileMapNode node in forChangeGroundTileList) { DestroyImmediate(node.gameObject); } } } } } tileMapObjPrefabForCreation = EditorGUILayout.ObjectField("TileMapObj Prefab for Creation", tileMapObjPrefabForCreation, typeof(TileMapObject), true) as TileMapObject; if (tileMapObjPrefabForCreation && selectedTileMapNodes.Count > 0 && curTileMap != null) { if (GUILayout.Button("Create TileMapObjs to selected nodes pos")) { GameObject objectGroupGameObj; if (curTileMapObjectGroup != null) { objectGroupGameObj = curTileMapObjectGroup.gameObject; } // get objectGroup gameobject { const string objectGroupGameObjectName = "_ObjectGroup"; objectGroupGameObj = GameObject.Find(objectGroupGameObjectName); if (objectGroupGameObj == null) { objectGroupGameObj = new GameObject(objectGroupGameObjectName); } objectGroupGameObj.transform.position = Vector3.zero; //BattleObject bo = objectGroupGameObj.GetComponent<BattleObject>(); //if (bo == null) { //bo = objectGroupGameObj.AddComponent<BattleObject>(); //} TileMapObjectGroup objectGroup = objectGroupGameObj.GetComponent <TileMapObjectGroup>(); if (objectGroup == null) { objectGroup = objectGroupGameObj.AddComponent <TileMapObjectGroup>(); } objectGroup.Init(new TileMapSize(curTileMap.TileWidthCount, curTileMap.TileHeightCount), new TilePos(0, 0)); curTileMapObjectGroup = objectGroup; } foreach (SquareTileMapNode tileMapNode in selectedTileMapNodes) { // create tile-map-object GameObject newTileMapObj = PrefabUtility.InstantiatePrefab(tileMapObjPrefabForCreation.gameObject) as GameObject; newTileMapObj.transform.position = tileMapNode.transform.position; TileMapObject tileMapObjCompo = newTileMapObj.GetComponent <TileMapObject>(); tileMapObjCompo.Init(Kino.TileMap.Direction.Bottom, new TilePos(tileMapNode.TilePosX, tileMapNode.TilePosY)); newTileMapObj.transform.parent = objectGroupGameObj.transform; } } } } } GUIDrawLine(); // about tile map data export { if (curTileMap) { GUILayout.Label(string.Format("tileMapID: {0}, tileMapName: {1}, width: {2}, height: {3}", curTileMap.MapID, curTileMap.MapName, curTileMap.TileWidthCount, curTileMap.TileHeightCount)); if (GUILayout.Button("select", GUILayout.Width(100.0f))) { Selection.activeObject = curTileMap.gameObject; } if (GUILayout.Button("Export TileMapData")) { ExportCurrentTileMapData(); } if (GUILayout.Button("Export TileMapData for client")) { ExportCurrentTileMapDataForClient(); } } } // about tile map object data export { if (curTileMapObjectGroup) { //BattleObject bo = curTileMapObjectGroup.GetComponent<BattleObject>(); //if (bo) { //GUILayout.Label(string.Format("objectID: {0}, objectName: {1}, width: {2}, height: {3}, hp: {4}, respawnTime: {5}", //bo.ObjectID, //bo.ObjectName, //curTileMapObjectGroup.TileMapSize.width, //curTileMapObjectGroup.TileMapSize.height, //bo.HP, //bo.RespawnTimeBySec)); //} if (GUILayout.Button("select", GUILayout.Width(100.0f))) { Selection.activeObject = curTileMapObjectGroup.gameObject; } if (GUILayout.Button("Export TileMapObjectGroupData")) { ExportCurrentTileMapObjectGroupData(); } } } // about tile map object groups pos data export { positionedObjGroupGameObj = EditorGUILayout.ObjectField("positionedObjGroupGameObj for export", positionedObjGroupGameObj, typeof(GameObject), true) as GameObject; if (curTileMap && positionedObjGroupGameObj) { if (GUILayout.Button("Export PositionInfos about positionedObjectGroups")) { ExportPositionInfosAboutPositionedObjectGroupsDatas(); } } } GUIDrawLine(); // PathFinder Test { GUILayout.Label("PathFind Test"); if (GUILayout.Button("Init for Test")) { MakeConnectionEachOtherNodes(); ClearAboutPathTest(); } if (GUILayout.Button("clear")) { ClearAboutPathTest(); } if (selectedTileMapNodes.Count > 0) { SquareTileMapNode targetNode = selectedTileMapNodes[0]; GUILayout.Label(string.Format("target nodeID:{0}", targetNode.nodeID)); if (GUILayout.Button("Set StartNode")) { if (!targetNode.Invalid) { if (pathFindTestStartNode) { DestroyImmediate(pathFindTestStartNode.gameObject.GetComponent <StartNodeMarker>()); DestroyImmediate(pathFindTestStartNode.gameObject.GetComponent <GoalNodeMarker>()); } pathFindTestStartNode = targetNode; pathFindTestStartNode.gameObject.AddComponent <StartNodeMarker>(); } } if (GUILayout.Button("Set GoalNode")) { if (!targetNode.Invalid) { if (pathFindTestGoalNode) { DestroyImmediate(pathFindTestGoalNode.gameObject.GetComponent <StartNodeMarker>()); DestroyImmediate(pathFindTestGoalNode.gameObject.GetComponent <GoalNodeMarker>()); } pathFindTestGoalNode = targetNode; pathFindTestGoalNode.gameObject.AddComponent <GoalNodeMarker>(); } } } if (pathFindTestStartNode != null && pathFindTestGoalNode != null) { if (GUILayout.Button("Find Path")) { List <SquareTileMapNode> findedPath = TileMapEngine.Instance.Calculate(pathFindTestStartNode, pathFindTestGoalNode); GameObject nodePathGameObj = GameObject.Find(NodePathMarker.GameObjName); if (nodePathGameObj) { DestroyImmediate(nodePathGameObj); } if (findedPath == null || findedPath.Count == 0) { ShowNotification(new GUIContent("cannot find path")); } else { nodePathGameObj = new GameObject(NodePathMarker.GameObjName); NodePathMarker nodePathMarker = nodePathGameObj.AddComponent <NodePathMarker>(); nodePathMarker.findedPath = findedPath; } } } } GUIDrawLine(); GUILayout.Label("tood:"); EditorGUILayout.EndScrollView(); if (Event.current.type == EventType.MouseMove) { Repaint(); } }
List <Object> GetNodeObjectList(SquareTileMapNode beginNode, SquareTileMapNode destNode) { List <Object> newSelectionList = new List <Object>(); SquareTileMapNode lastSelectedNodeCompo = beginNode; GameObject nodeGrp = lastSelectedNodeCompo.transform.parent.gameObject; const string nodeNameFormat = "tile_{0}-{1}"; int loopYGrowUpValue = 0; if (lastSelectedNodeCompo.TilePosY < destNode.TilePosY) { loopYGrowUpValue = 1; } else if (lastSelectedNodeCompo.TilePosY > destNode.TilePosY) { loopYGrowUpValue = -1; } int loopXGrowUpValue = 0; if (lastSelectedNodeCompo.TilePosX < destNode.TilePosX) { loopXGrowUpValue = 1; } else if (lastSelectedNodeCompo.TilePosX > destNode.TilePosX) { loopXGrowUpValue = -1; } for (int y = lastSelectedNodeCompo.TilePosY; loopYGrowUpValue > 0 ? y <= destNode.TilePosY : (loopYGrowUpValue < 0 ? y >= destNode.TilePosY : y == destNode.TilePosY); ) { for (int x = lastSelectedNodeCompo.TilePosX; loopXGrowUpValue > 0 ? x <= destNode.TilePosX : (loopXGrowUpValue < 0 ? x >= destNode.TilePosX : x == destNode.TilePosX); ) { string nodeName = string.Format(nodeNameFormat, x, y); Transform nodeTm = nodeGrp.transform.FindChild(nodeName); newSelectionList.Add(nodeTm.gameObject); if (loopXGrowUpValue == 0) { ++x; } else { x += loopXGrowUpValue; } } if (loopYGrowUpValue == 0) { ++y; } else { y += loopYGrowUpValue; } } return(newSelectionList); }
public void AddConnectionNode(SquareTileMapNode node) { connections.Add(node); }