Ejemplo n.º 1
0
 void SetTileMap()
 {
     if (transform.parent && transform.parent.transform.parent)
     {
         tileMap = transform.parent.transform.parent.GetComponent <TileMapRoot>();
     }
 }
        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 UpdateTileMapInfo()
        {
            this.tileWidthCount  = 0;
            this.tileHeightCount = 0;

            if (curTileMap == null)
            {
                this.curTileMap = GameObject.FindObjectOfType <TileMapRoot>();
            }

            if (curTileMap)
            {
                this.tileWidthCount  = curTileMap.TileWidthCount;
                this.tileHeightCount = curTileMap.TileHeightCount;
                this.screen2D        = curTileMap.GetMapInfo().screen2D;

                curTileMap.InitTileMapEngine();
                EditorUtility.SetDirty(TileMapEngine.Instance);
            }

            GUI.FocusControl("");
        }
        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();
            }
        }