Ejemplo n.º 1
0
    public override void OnInspectorGUI()
    {
        TriGrid rectGrid = (TriGrid)target;

        int newWidth  = EditorGUILayout.IntField("width", rectGrid.width);
        int newHeight = EditorGUILayout.IntField("height", rectGrid.height);

        EditorGUILayout.LabelField("cell count", rectGrid.Graph.Size.ToString());

        if (newWidth != prevWidth || newHeight != prevHeight)
        {
            prevWidth  = newWidth;
            prevHeight = newHeight;
            ChangeSize(newWidth, newHeight);
        }

        int builderIndex = EditorGUILayout.Popup(prevBuilderIndex, builderNames);

        if (builderIndex != prevBuilderIndex || GUILayout.Button("rebuild"))
        {
            prevBuilderIndex = builderIndex;
            RebuildMaze(builders[builderIndex]);
        }

        DisplayImage();
    }
Ejemplo n.º 2
0
    private void DrawOutsideWalls(TriGrid grid)
    {
        float halfSideSize    = sideSize / 2;
        int   halfSideSizeInt = (int)halfSideSize;
        float height          = TriangleHeight;
        float halfHeight      = height / 2.0f;
        int   centerX         = halfSideSizeInt;
        int   west            = Mathf.Max(0, centerX - halfSideSizeInt);

        // Upright (0 col, so first even)
        int row = 0;

        while (row < grid.height)
        {
            int centerY = tex.height - ((int)(halfHeight + row * height));
            int north   = (int)(centerY + halfHeight);
            int south   = north - (int)height;

            tex.Line(new Vector2Int(west, south), new Vector2Int(centerX, north), wallColor);
            row += 2;
        }

        row = 1;

        while (row < grid.height)
        {
            int centerY = tex.height - ((int)(halfHeight + row * height));
            int north   = (int)(centerY + halfHeight);
            int south   = north - (int)height;

            tex.Line(new Vector2Int(west, north), new Vector2Int(centerX, south), wallColor);
            row += 2;
        }
    }
Ejemplo n.º 3
0
    public IEnumerator <Coroutine> FindPathAndMove(TriCell target, bool entityCheck, bool terrainCheck)
    {
        TriGrid inst = TriGrid.Instance;

        if (target && IsValidDestination(target))
        {
            inst.FindPath(Location, target, entityCheck, terrainCheck);
            if (inst.HasPath)
            {
                pathToTravel = inst.GetPath();
                CancelNowAct();
                yield return(nowRoutine = StartCoroutine(TravelPath()));

                inst.ClearPath();
                ActResult = true;
            }
            else
            {
                ActResult = false;
            }
        }
        else
        {
            inst.ClearPath();
            ActResult = false;
        }
        yield return(null);
    }
Ejemplo n.º 4
0
 void Awake()
 {
     TriMetrics.colors      = colors;
     TriMetrics.noiseSource = noiseSource;
     Entity.unitPrefab      = unitPrefab;
     CreateMap(cellCountX, cellCountZ);
     Instance = this;
 }
Ejemplo n.º 5
0
    public static new BuildCommand Load(BinaryReader reader)
    {
        TriGrid      instance = TriGrid.Instance;
        TriCell      tCell    = instance.GetCell(TriCoordinates.Load(reader));
        TriDirection tDir     = (TriDirection)reader.ReadInt32();
        Entity       prefab   = TriIsland.GetBuildingPrefabs(reader.ReadInt32(), reader.ReadInt32(), 0);

        return(new BuildCommand(tCell, tDir, prefab));
    }
Ejemplo n.º 6
0
 void Awake()
 {
     Time.timeScale         = 0;
     TriMetrics.colors      = colors;
     TriMetrics.noiseSource = noiseSource;
     Entity.unitPrefab      = unitPrefab;
     //StartCoroutine(CreateMap(cellCountX, cellCountZ));
     Instance = this;
 }
Ejemplo n.º 7
0
    private void ChangeSize(int width, int height)
    {
        TriGrid rectGrid = (TriGrid)target;

        rectGrid.SetSize(width, height);

        EditorUtility.SetDirty(target);

        image.Draw(rectGrid);
    }
Ejemplo n.º 8
0
    private void DrawWalls(TriGrid grid)
    {
        float halfSideSize    = sideSize / 2;
        int   halfSideSizeInt = (int)halfSideSize;
        float height          = TriangleHeight;
        float halfHeight      = height / 2.0f;

        for (int i = 0; i < grid.height; ++i)
        {
            int centerY = tex.height - ((int)(halfHeight + i * height));

            int north = (int)(centerY + halfHeight);
            int south = north - (int)height;

            // upright
            for (int j = (i % 2); j < grid.width; j += 2)
            {
                int centerX = halfSideSizeInt + j * halfSideSizeInt;
                int west    = Mathf.Max(0, centerX - halfSideSizeInt);
                int east    = centerX + halfSideSizeInt;

                int currentVertex = grid.PositionToVertex(i, j);

                if (!grid.Graph.AreLinked(currentVertex, grid.EastOf(currentVertex)))
                {
                    tex.Line(new Vector2Int(east, south), new Vector2Int(centerX, north), wallColor);
                }

                if (!grid.Graph.AreLinked(currentVertex, grid.SouthOf(currentVertex)))
                {
                    tex.HorizontalLine(west, east, south, wallColor);
                }
            }

            // !upright
            for (int j = ((i + 1) % 2); j < grid.width; j += 2)
            {
                int centerX = (int)(halfSideSize + j * halfSideSize);
                int west    = centerX - halfSideSizeInt;
                int east    = centerX + halfSideSizeInt;

                int currentVertex = grid.PositionToVertex(i, j);

                if (!grid.Graph.AreLinked(currentVertex, grid.EastOf(currentVertex)))
                {
                    tex.Line(new Vector2Int(east, north), new Vector2Int(centerX, south), wallColor);
                }

                if (!grid.Graph.AreLinked(currentVertex, grid.NorthOf(currentVertex)))
                {
                    tex.HorizontalLine(west, east, north, wallColor);
                }
            }
        }
    }
Ejemplo n.º 9
0
    // index of the beams
    //      3  2
    //    4 _\/_ 1
    //       /\
    //      5  0



    public Node(TriGrid grid, Vector3 position, float length, int xIndex, int yIndex)
    {
        _grid     = grid;
        _position = position;
        _length   = length;
        _xIndex   = xIndex;
        _yIndex   = yIndex;


        CreateNode();
    }
Ejemplo n.º 10
0
    public Beam(TriGrid grid, Node startNode, Node endNode, int rotIndex, int index)
    {
        _grid      = grid;
        _rotIndex  = rotIndex;
        _startNode = startNode;
        _endNode   = endNode;
        _index     = index;

        InstantiateBeam();
        SwitchBeams();
    }
Ejemplo n.º 11
0
    public virtual List <BuildState> GetBuildStatus(TriCoordinates coord, TriDirection dir)
    {
        List <BuildState> ret  = new List <BuildState>();
        TriGrid           grid = TriGrid.Instance;
        TriCell           cell = grid.GetCell(coord);
        int elev = cell.Elevation;

        ret.Add(new BuildState(cell.coordinates, cell.IsBuildable()));
        cell = grid.GetCell(coord).GetNeighbor(dir);
        ret.Add(new BuildState(cell.coordinates, cell.IsBuildable() && Mathf.Abs(cell.Elevation - elev) < 2));
        return(ret);
    }
Ejemplo n.º 12
0
    void Start()
    {
        _iniNodes.Add(new IniNode(1, 0, true));
        _iniNodes.Add(new IniNode(2, 0, true));
        _iniNodes.Add(new IniNode(5, 0, true));
        _iniNodes.Add(new IniNode(6, 0, true));
        _iniNodes.Add(new IniNode(8, 0, true));
        _iniNodes.Add(new IniNode(4, 6, new Vector3(10000, 20, 0)));

        _level = new Level(_xAmount, _yAmount, _iniNodes, "Hold the force", 10, 100, Vector3.zero);
        Physics.autoSimulation = false;
        _triGrid = new TriGrid(_goNode, _length, _matBeam, _matLine, _breakForce, _level);
    }
Ejemplo n.º 13
0
    void Start()
    {
        triGrid = GetComponent <TriGrid>();
        var blockSize = triGrid.cellSize * 4;
        var gridSize  = triGrid.gridSize;

        this.gameObject.layer = LayerMask.NameToLayer("PuzzleBoard");
        var collider     = this.gameObject.AddComponent <BoxCollider2D>();
        var colliderSize = CoordsUtils.OrthoToSlope(new Vector2(blockSize * gridSize.x, blockSize * gridSize.y));

        collider.size   = colliderSize * 2;
        collider.offset = colliderSize / 2f;
        LoadPuzzle(puzzleId);
    }
Ejemplo n.º 14
0
    public void Draw(TriGrid maze, Color[] cellColors = null)
    {
        int imageWidth  = CalcWidth(maze.width);
        int imageHeight = CalcHeight(maze.height);

        tex.Resize(imageWidth, imageHeight);

        if (cellColors == null)
        {
            tex.Fill(backgroundColor);
        }
        else
        {
            PaintDistances(maze, cellColors);
        }

        DrawOutsideWalls(maze);
        DrawWalls(maze);
        tex.Apply();
    }
Ejemplo n.º 15
0
    public void CalculateTerrain()
    {
        Clear();
        TriGrid grid = TriGrid.Instance;

        result = selector.Prefab.GetBuildStatus(selector.nowCell.coordinates, selector.dir);
        if (selector.nowCell)
        {
            foreach (BuildState i in result)
            {
                RecalculateTerrain(grid.GetCell(i.coord), i.value);
                if (!i.value)
                {
                    Buildable = false;
                }
            }
        }

        Apply();
    }
Ejemplo n.º 16
0
    // Start is called before the first frame update
    void Start()
    {
        GameObject grid = new GameObject();

        grid.name          = "grid";
        triGrid            = grid.AddComponent <TriGrid>();
        triGrid.gridSize   = new Vector2Int(1, 1);
        triGrid.blocks     = new TriBlock[] { shape };
        triGrid.cellPrefab = cellPrefab;
        triGrid.cellSize   = cellSize;
        grid.transform.SetParent(transform, false);
        grid.transform.localPosition = Vector3.zero;
        this.gameObject.layer        = LayerMask.NameToLayer("PuzzlePiece");

        var   collider     = gameObject.AddComponent <BoxCollider2D>();
        float blockSize    = cellSize * 4f;
        var   colliderSize = CoordsUtils.OrthoToSlope(new Vector2(blockSize, blockSize));

        collider.size   = colliderSize;
        collider.offset = colliderSize / 2f;
    }
Ejemplo n.º 17
0
    public static Command Load(BinaryReader reader)
    {
        TriGrid     grid = TriGrid.Instance;
        CommandType type = (CommandType)reader.ReadInt32();
        Command     t    = new Command();

        switch (type)
        {
        case CommandType.MOVE:
            t = MoveCommand.Load(reader);
            break;

        case CommandType.GETIN:
            t = GetInCommand.Load(reader);
            break;

        case CommandType.GETOUT:
            t = GetOutCommand.Load(reader);
            break;

        case CommandType.CHANGEHOME:
            t = ChangeHomeCommand.Load(reader);
            break;

        case CommandType.CHANGEJOB:
            t = ChangeJobCommand.Load(reader);
            break;

        case CommandType.CHANGEWORK:
            t = ChangeWorkCommand.Load(reader);
            break;

        case CommandType.BUILD:
            t = BuildCommand.Load(reader);
            break;
        }
        t.type = type;
        return(t);
    }
Ejemplo n.º 18
0
        public void TriGridTest()
        {
            for (int size = 1; size <= 16; size++)
            {
                Console.WriteLine($"size = {size}");

                TriGrid grid = new TriGrid(size);

                Console.WriteLine($"mapsize = {grid.MapWidth}, {grid.MapHeight}");

                Console.WriteLine("map :");

                Console.WriteLine(grid.ToString());

                Console.WriteLine("link :");

                foreach ((int index, var link) in grid.Link)
                {
                    Console.WriteLine($"{index} : {string.Join(',', link)}");
                }

                Assert.IsTrue(GridValidationUtil.IsValid(grid), $"{size}");
                Assert.AreEqual(size * (size + 1) / 2, grid.Count, $"{size}");

                if (size >= 2)
                {
                    Assert.AreEqual(2, grid[0].Links);
                    Assert.AreEqual(2, grid[grid.Count - size].Links);
                    Assert.AreEqual(2, grid[grid.Count - 1].Links);

                    Assert.AreEqual(0, grid.Cells.Where((cell) => cell.Links < 2 || cell.Links == 3).Count());
                    Assert.AreEqual(3, grid.Cells.Where((cell) => cell.Links == 2).Count());
                    Assert.AreEqual((size - 2) * 3, grid.Cells.Where((cell) => cell.Links == 4).Count());
                }

                Console.WriteLine("---------------------------");
            }
        }
Ejemplo n.º 19
0
    private void RebuildMaze(VertexLinker builder)
    {
        Debug.Log("rebuilding " + builder.ToString());

        TriGrid maze = (TriGrid)target;

        maze.Graph.ClearLinks();
        builder.Build(new VertexLinkerHelper(maze.Graph, maze.AdjacentGraph));
        EditorUtility.SetDirty(maze);

        BreadthFirst bf = new BreadthFirst(maze.Graph, maze.BottomLeftVertex);

        bf.Run();
        int[] distances   = bf.Distances;
        float maxDistance = (float)bf.MaxDistance;
        Color nearColor   = Color.red;
        Color farColor    = Color.black;

        Color[] distanceColors = System.Array.ConvertAll <int, Color>(
            distances, distance => Color.Lerp(nearColor, farColor, (distance / maxDistance))
            );
        image.Draw(maze, distanceColors);
    }
Ejemplo n.º 20
0
 public override void Tick()
 {
     base.Tick();
     if (Clock.IsDay())
     {
         if (Work)
         {
             RoutineTarget = Work;
         }
         else if (Company)
         {
             RoutineTarget = Company;
         }
         else if (Home)
         {
             RoutineTarget = Home;
         }
         else
         {
             RoutineTarget = TriIsland.GetCamp();
         }
     }
     else
     {
         if (Home)
         {
             RoutineTarget = Home;
         }
         else
         {
             RoutineTarget = TriIsland.GetCamp();
         }
     }
     if (nowWork == null && !acting && commandQueue.Count == 0 && RoutineTarget && RoutineTarget != Building)
     {
         CancelAllAct();
         Debug.LogWarning("<color=#ff0000>act canceled</color> and moved to : " + routineTarget);
         TriGrid inst = TriGrid.Instance;
         inst.FindPath(Location, routineTarget.EntranceLocation, true, true);
         if (inst.HasPath)
         {
             inst.ClearPath();
             AddCommand(new MoveCommand(RoutineTarget.EntranceLocation, true, true));
             AddCommand(new GetInCommand(RoutineTarget));
         }
         else
         {
             inst.ClearPath();
             if (RoutineTarget == Work)
             {
                 AddCommand(new ChangeWorkCommand(null));
             }
             else if (RoutineTarget == Company)
             {
                 AddCommand(new ChangeJobCommand(null));
             }
             else if (RoutineTarget == Home)
             {
                 AddCommand(new ChangeHomeCommand(null));
             }
         }
     }
 }
Ejemplo n.º 21
0
    private void PaintDistances(TriGrid triGrid, Color[] vertexDistanceColors)
    {
        float   halfSideSize    = sideSize / 2;
        int     halfSideSizeInt = (int)halfSideSize;
        float   height          = TriangleHeight;
        float   halfHeight      = height / 2.0f;
        Vector2 texPoint        = new Vector2();


        for (int y = 0; y != tex.height; ++y)
        {
            int row = (int)(((float)(tex.height - y)) / height);  // y texture to grid row
            texPoint.y = y + 0.5f;

            int centerY = tex.height - ((int)(halfHeight + row * height));

            float north = centerY + halfHeight + 0.5f;
            float south = north - height - 0.5f;


            for (int x = 0; x != tex.width; ++x)
            {
                Color color = backgroundColor;
                texPoint.x = x + 0.5f;

                int halfSizeCount = (int)(x / halfSideSize);
                int fromCol       = Mathf.Max(0, halfSizeCount - 1);
                int maxCol        = Mathf.Min(halfSizeCount + 2, triGrid.width);

                for (int col = fromCol; col != maxCol; ++col)
                {
                    int vertex = triGrid.PositionToVertex(row, col);

                    int     centerX = halfSideSizeInt + col * halfSideSizeInt;
                    int     west    = Mathf.Max(0, centerX - halfSideSizeInt);
                    int     east    = centerX + halfSideSizeInt;
                    Vector2 a       = new Vector2();
                    Vector2 b       = new Vector2();
                    Vector2 c       = new Vector2();

                    if (triGrid.IsUpright(vertex))
                    {
                        a.Set(centerX + 0.5f, north);
                        b.Set(east + 0.5f, south);
                        c.Set(west + 0.5f, south);
                    }
                    else
                    {
                        a.Set(centerX + 0.5f, south);
                        b.Set(west + 0.5f, north);
                        c.Set(east + 0.5f, north);
                    }

                    if (PointTest2D.IsInsideTri(texPoint, a, b, c))
                    {
                        color = vertexDistanceColors[vertex];
                        break;
                    }
                }

                tex.SetPixel(x, y, color);
            }
        }
    }