/**
         * Sorry about the length, but each part is pretty well seperated, so it shouldn't be too difficult to
         * follow hopefully (-nonanon).
         *
         * This code takes the information about what tiles are adjacent (each of the 8 directions either has an adjacent connectable or not),
         * and converts this into the mesh to use, along with a direction to rotate the mesh in.
         */
        private void UpdateMeshAndDirection()
        {
            // Count number of connections along cardinal, to determine which 'outer' mesh we use of O, C, I/L, T, X
            var generalCardinals = generalAdjacents.GetCardinalInfo();

            float rotation = 0.0f;
            Mesh  mesh;

            if (generalCardinals.IsO())
            {
                mesh = o;
            }
            else if (generalCardinals.IsC())
            {
                mesh     = c;
                rotation = DirectionHelper.AngleBetween(Direction.East, generalCardinals.GetOnlyPositive());
            }
            else if (generalCardinals.IsI())
            {
                var specificCardinals = specificAdjacents.GetCardinalInfo();

                if (specificCardinals.numConnections == 1)
                {
                    mesh     = iBorder;
                    rotation = DirectionHelper.AngleBetween(Direction.West, specificCardinals.GetOnlyPositive());
                }
                else
                {
                    mesh     = specificCardinals.numConnections == 2 ? i : iAlone;
                    rotation = OrientationHelper.AngleBetween(Orientation.Horizontal, generalCardinals.GetFirstOrientation());
                }
            }
            else if (generalCardinals.IsL())
            {
                // Determine lSolid or lCorner by finding whether the area between the two connections is filled
                // We check for if any of the following bitfields matches the connection bitfield
                // N+NE+E = 0/1/2, E+SE+S = 2/3/4, S+SW+W = 4/5/6, W+NW+N = 6/7/0
                bool isFilled = (generalAdjacents.Connections & 0b00000111) == 0b00000111 || (generalAdjacents.Connections & 0b00011100) == 0b00011100 || (generalAdjacents.Connections & 0b01110000) == 0b01110000 || (generalAdjacents.Connections & 0b11000001) == 0b11000001;
                mesh     = isFilled ? lSingle : lNone;
                rotation = DirectionHelper.AngleBetween(Direction.SouthEast, generalCardinals.GetCornerDirection());
            }
            else if (generalCardinals.IsT())
            {
                // We make another bitfield (noticing a pattern?). 0x0 means no fills, 0x1 means right corner filled, 0x2 means left corner filled,
                // therefore both corners filled = 0x3.
                int corners = ((1 - generalCardinals.north) * 2 | (1 - generalCardinals.east)) * generalAdjacents.Adjacent(Direction.SouthWest)
                              + ((1 - generalCardinals.east) * 2 | (1 - generalCardinals.south)) * generalAdjacents.Adjacent(Direction.NorthWest)
                              + ((1 - generalCardinals.south) * 2 | (1 - generalCardinals.west)) * generalAdjacents.Adjacent(Direction.NorthEast)
                              + ((1 - generalCardinals.west) * 2 | (1 - generalCardinals.north)) * generalAdjacents.Adjacent(Direction.SouthEast);
                mesh = corners == 0 ? tNone
                    : corners == 1 ? tSingleLeft
                    : corners == 2 ? tSingleRight
                    : tDouble;

                rotation = DirectionHelper.AngleBetween(Direction.West, generalCardinals.GetOnlyNegative());
            }
            else
            {
                // This sneaky piece of code uses the cardinal info to store diagonals by rotating everything -45 degrees
                // NE -> N, SW -> S, etc.
                var diagonals = new AdjacencyBitmap.CardinalInfo((byte)(generalAdjacents.Connections >> 1));

                switch (diagonals.numConnections)
                {
                case 0:
                    mesh = xNone;
                    break;

                case 1:
                    mesh     = xSingle;
                    rotation = DirectionHelper.AngleBetween(Direction.West, diagonals.GetOnlyPositive());
                    break;

                case 2:
                    if (diagonals.north == diagonals.south)
                    {
                        mesh     = xOpposite;
                        rotation = OrientationHelper.AngleBetween(Orientation.Vertical, diagonals.GetFirstOrientation());
                    }
                    else
                    {
                        mesh     = xSide;
                        rotation = DirectionHelper.AngleBetween(Direction.SouthWest, diagonals.GetCornerDirection());
                    }
                    break;

                case 3:
                    mesh     = xTriple;
                    rotation = DirectionHelper.AngleBetween(Direction.East, diagonals.GetOnlyNegative());
                    break;

                default:
                    mesh = xQuad;
                    break;
                }
            }

            if (filter == null)
            {
                filter = GetComponent <MeshFilter>();
            }

            filter.mesh             = mesh;
            transform.localRotation = Quaternion.Euler(transform.localRotation.eulerAngles.x, rotation, transform.localRotation.eulerAngles.z);
        }
Exemple #2
0
        public MeshDirectionInfo GetMeshAndDirection(AdjacencyBitmap adjacents)
        {
            var cardinalInfo = adjacents.GetCardinalInfo();

            float rotation = 0.0f;
            Mesh  mesh;

            if (cardinalInfo.IsO())
            {
                mesh = o;
            }
            else if (cardinalInfo.IsU())
            {
                mesh     = u;
                rotation = TileHelper.AngleBetween(Direction.North, cardinalInfo.GetOnlyPositive());

                if (opaque)
                {
                    viewObstacles[0].SetActive(false);
                    viewObstacles[1].SetActive(false);
                    viewObstacles[2].SetActive(true);
                    viewObstacles[3].SetActive(false);
                }
            }
            else if (cardinalInfo.IsI())
            {
                mesh     = i;
                rotation = TileHelper.AngleBetween(Orientation.Vertical, cardinalInfo.GetFirstOrientation());

                if (opaque)
                {
                    viewObstacles[0].SetActive(false);
                    viewObstacles[1].SetActive(false);
                    viewObstacles[2].SetActive(true);
                    viewObstacles[3].SetActive(true);
                }
            }
            else if (cardinalInfo.IsL())
            {
                // Determine lSolid or lCorner by finding whether the area between the two connections is filled
                // We check for if any of the following bitfields matches the connection bitfield
                // N+NE+E = 0/1/2, E+SE+S = 2/3/4, S+SW+W = 4/5/6, W+NW+N = 6/7/0
                bool isFilled = (adjacents.Connections & 0b00000111) == 0b00000111 || (adjacents.Connections & 0b00011100) == 0b00011100 || (adjacents.Connections & 0b01110000) == 0b01110000 || (adjacents.Connections & 0b11000001) == 0b11000001;
                mesh     = isFilled ? lSingle : lNone;
                rotation = TileHelper.AngleBetween(Direction.NorthEast, cardinalInfo.GetCornerDirection());

                if (opaque)
                {
                    viewObstacles[0].SetActive(false);
                    viewObstacles[1].SetActive(true);
                    viewObstacles[2].SetActive(true);
                    viewObstacles[3].SetActive(false);
                }
            }
            else if (cardinalInfo.IsT())
            {
                // We make another bitfield (noticing a pattern?). 0x0 means no fills, 0x1 means right corner filled, 0x2 means left corner filled,
                // therefore both corners filled = 0x3.
                int corners = ((1 - cardinalInfo.north) * 2 | (1 - cardinalInfo.east)) * adjacents.Adjacent(Direction.SouthWest)
                              + ((1 - cardinalInfo.east) * 2 | (1 - cardinalInfo.south)) * adjacents.Adjacent(Direction.NorthWest)
                              + ((1 - cardinalInfo.south) * 2 | (1 - cardinalInfo.west)) * adjacents.Adjacent(Direction.NorthEast)
                              + ((1 - cardinalInfo.west) * 2 | (1 - cardinalInfo.north)) * adjacents.Adjacent(Direction.SouthEast);
                mesh = corners == 0 ? tNone
                    : corners == 1 ? tSingleLeft
                    : corners == 2 ? tSingleRight
                    : tDouble;

                rotation = TileHelper.AngleBetween(Direction.South, cardinalInfo.GetOnlyNegative());

                if (opaque)
                {
                    viewObstacles[0].SetActive(true);
                    viewObstacles[1].SetActive(true);
                    viewObstacles[2].SetActive(true);
                    viewObstacles[3].SetActive(corners >= 3);
                }
            }
            else
            {
                // This sneaky piece of code uses the cardinal info to store diagonals by rotating everything -45 degrees
                // NE -> N, SW -> S, etc.
                var diagonals = new AdjacencyBitmap.CardinalInfo((byte)(adjacents.Connections >> 1));

                switch (diagonals.numConnections)
                {
                case 0:
                    mesh = xNone;
                    break;

                case 1:
                    mesh     = xSingle;
                    rotation = TileHelper.AngleBetween(Direction.South, diagonals.GetOnlyPositive());
                    break;

                case 2:
                    if (diagonals.north == diagonals.south)
                    {
                        mesh     = xOpposite;
                        rotation = TileHelper.AngleBetween(Orientation.Horizontal, diagonals.GetFirstOrientation());
                    }
                    else
                    {
                        mesh     = xSide;
                        rotation = TileHelper.AngleBetween(Direction.SouthEast, diagonals.GetCornerDirection());
                    }
                    break;

                case 3:
                    mesh     = xTriple;
                    rotation = TileHelper.AngleBetween(Direction.North, diagonals.GetOnlyNegative());
                    break;

                default:
                    mesh = xQuad;
                    break;
                }
                if (opaque)
                {
                    viewObstacles[0].SetActive(true);
                    viewObstacles[1].SetActive(true);
                    viewObstacles[2].SetActive(true);
                    viewObstacles[3].SetActive(true);
                }
            }

            return(new MeshDirectionInfo {
                mesh = mesh, rotation = rotation
            });
        }