Esempio n. 1
0
    private void Start()
    {
        var width  = 16;
        var height = 16;

        _cost = new int[height][];
        for (var i = 0; i < width; i++)
        {
            _cost[i] = new int[height];
            for (var j = 0; j < height; j++)
            {
                _cost[i][j] = Random.Range(1, 100);
            }
        }

        for (var i = 0; i < 80; i++)
        {
            _cost[Random.Range(0, 15)][Random.Range(0, 15)] = int.MaxValue;
        }

        _start = Pos.Create(0, 0);
        _goal  = Pos.Create(12, 14);

        _cost[_start.X][_start.Y] = 0;
        _cost[_goal.X][_goal.Y]   = 1;

        Show(_start, _goal, _cost);
    }
Esempio n. 2
0
    private void DrawGrid(Graph graph, List <Transform> units, Color normalColor)
    {
        for (var i = 0; i < graph.GetWidth(); i++)
        {
            for (var j = 0; j < graph.GetHeight(); j++)
            {
                var unit = units[i + j * graph.GetWidth()];

                unit.localScale = new Vector3(UnitSize, UnitSize, 1);
                unit.gameObject.SetActive(true);

                var t = unit.GetComponentInChildren <TextMeshProUGUI>();
                var c = graph.GetCost(Pos.Create(i, j));
                t.text = c == int.MaxValue ? "∞" : c.ToString();
                var spriteRenderer = unit.GetComponent <SpriteRenderer>();
                spriteRenderer.color = c == int.MaxValue ? Color.red : normalColor;
            }
        }
    }
Esempio n. 3
0
    private void SpawnUnit(Graph graph, Vector2 offset, ref List <Transform> units)
    {
        units.Clear();
        for (var j = 0; j < graph.GetHeight(); j++)
        {
            for (var i = 0; i < graph.GetWidth(); i++)
            {
                var unit = Instantiate(Unit,
                                       new Vector3(
                                           i * UnitSize + (i - 1) * UnitSpace + offset.x,
                                           j * UnitSize + (j - 1) * UnitSpace + offset.y,
                                           0),
                                       Quaternion.identity);
                unit.name = $"{i},{j}";
                units.Add(unit);
                var btn = unit.GetComponentInChildren <Button>();
                btn.onClick.AddListener(() =>
                {
                    var goals = unit.name.Split(',');
                    var pos   = Pos.Create(int.Parse(goals[0]), int.Parse(goals[1]));

                    if (Input.GetKey(KeyCode.W))
                    {
                        if (_cost[pos.X][pos.Y] == int.MaxValue)
                        {
                            _cost[pos.X][pos.Y] = Random.Range(1, 100);
                        }
                        else
                        {
                            _cost[pos.X][pos.Y] = int.MaxValue;
                        }
                    }
                    else
                    {
                        _goal = pos;
                    }

                    Show(_start, _goal, _cost);
                });
            }
        }
    }