예제 #1
0
파일: Node.cs 프로젝트: lordbenedikt/Benko
    void OnMouseOver()
    {
        // preview before building wall
        // should be optimized
        if (!isOccupied && !isObstacle && !isWall && gameController.UI.ActivateBuildMode && GameObject.Find("Canvas").GetComponent <UI_Manager>().GoldAmount >= 10)
        {
            bool enemyTooClose = false;
            foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy"))
            {
                if (Vector3.Distance(transform.position, enemy.transform.position) < 1.5f)
                {
                    enemyTooClose = true;
                }
            }
            if (!enemyTooClose)
            {
                gameController.wallPreview.SetActive(true);
                gameController.wallPreview.transform.position = new Vector3(transform.position.x, gameController.wallPreview.transform.position.y, transform.position.z);
            }
            else
            {
                gameController.wallPreview.SetActive(false);
            }
        }
        else
        {
            gameController.wallPreview.SetActive(false);
        }


        // left button
        if (Input.GetMouseButton(0))
        {
            if (!isOccupied && !isObstacle && !isWall && gameController.UI.ActivateBuildMode && GameObject.Find("Canvas").GetComponent <UI_Manager>().GoldAmount >= 10)
            {
                bool enemyTooClose = false;
                foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy"))
                {
                    if (Vector3.Distance(transform.position, enemy.transform.position) < 1.5f)
                    {
                        enemyTooClose = true;
                    }
                }
                if (!enemyTooClose)
                {
                    // should be optimized
                    customGrid.pathMap.Clear();
                    // foreach (long key in customGrid.pathMap.Keys.ToArray())
                    // {
                    //     if (customGrid.pathMap[key].Contains(gameObject))
                    //     {
                    //         customGrid.pathMap.Remove(key);
                    //     }
                    // }

                    // print("siize "+customGrid.pathMap.Count);
                    GameObject.Find("Canvas").GetComponent <UI_Manager>().AddGold(-10);
                    isWall     = true;
                    isObstacle = true;

                    // connect walls
                    foreach (GameObject wall in customGrid.walls)
                    {
                        Destroy(wall);
                    }
                    gameController.wallPreview.SetActive(false);
                    customGrid.BuildWall();
                }
            }
        }
        // right button
        if (Input.GetMouseButton(1))
        {
            if (isWall && gameController.UI.ActivateBuildMode)
            {
                customGrid.pathMap.Clear();
                // foreach(long key in customGrid.pathMap.Keys.ToArray()) {
                //     float pathLength = 0;
                //     List<GameObject> path = customGrid.pathMap[key];
                //     for(int i = 0; i<customGrid.pathMap[key].Count-2;i++) {
                //         pathLength += Vector3.Distance(path[i].gameObject.transform.position, path[i+1].gameObject.transform.position);
                //     }
                // }
                // print("siize "+customGrid.pathMap.Count);
                GameObject.Find("Canvas").GetComponent <UI_Manager>().AddGold(5);
                isWall     = false;
                isObstacle = false;
                Destroy(cornerStone);

                // connect walls
                foreach (GameObject wall in customGrid.walls)
                {
                    Destroy(wall);
                }
                customGrid.BuildWall();
            }
            else
            {
                foreach (GameObject go in customGrid.nodes)
                {
                    go.GetComponent <Node>().inPath = false;
                }
                // print(customGrid.aStar(gameObject, customGrid.nodes[customGrid.nodes.Length - 1], shortestPath));
            }
        }
        // middle button
        if (Input.GetKeyDown(KeyCode.U))
        {
            GameObject go = Instantiate(enemy, transform.position, Quaternion.identity);
        }
    }