示例#1
0
    //Lukker Update

    /** Finder ud af om man må bevæge sig i den givne retning */
    Vector3 FindNextFace()
    {
        //we will be operating in grid space, so convert the position
        Vector3 newPosition = grid.WorldToGrid(transform.localPosition);

        //Add one grid unit onto position in the picked direction
        BevaegeRetning(ref newPosition);

        //return the position in world space
        return(grid.GridToWorld(newPosition));
    }
示例#2
0
    Vector3 FindNextFace()
    {
        //we will be operating in grid space, so convert the position
        Vector3 newPosition = grid.WorldToGrid(transform.position);

        //Retninger
        Vector3 opVec      = new Vector3(270, 0, 0);
        Vector3 nedVec     = new Vector3(90, 180, 0);
        Vector3 hoejreVec  = new Vector3(0, 90, 270);
        Vector3 venstreVec = new Vector3(0, 270, 90);

        //first let's pick a random number for one of the four possible directions
        int i = Random.Range(0, 4);

        //now add one grid unit onto position in the picked direction
        if (i == 0)
        {
            newPosition = newPosition + new Vector3(1, 0, 0);
            rotation    = hoejreVec;
        }
        else if (i == 1)
        {
            newPosition = newPosition + new Vector3(-1, 0, 0);
            rotation    = venstreVec;
        }
        else if (i == 2)
        {
            newPosition = newPosition + new Vector3(0, 1, 0);
            rotation    = opVec;
        }
        else if (i == 3)
        {
            newPosition = newPosition + new Vector3(0, -1, 0);
            rotation    = nedVec;
        }
        ani.SetBool("Run", true);
        transform.rotation = Quaternion.Euler(rotation);
        //if we would wander off beyond the size of the grid turn the other way around

        /*for (int j = 0; j < 2; j++) {
         *      if (Mathf.Abs (newPosition [j]) > grid.size [j])
         *              newPosition [j] -= Mathf.Sign (newPosition [j]) * 2.0f;
         * }*/

        //return the position in world space
        return(grid.GridToWorld(newPosition));
    }
示例#3
0
    Vector3 FindNextFace()
    {
        //we will be operating in grid space, so convert the position
        Vector3 newPosition = grid.WorldToGrid(cachedTransform.position);

        //first let's pick a random number for one of the four possible directions
        int i = Random.Range(0, 4);

        //now add one grid unit onto position in the picked direction
        if (i == 0)
        {
            newPosition = newPosition + new Vector3(1, 0, 0);
        }
        else if (i == 1)
        {
            newPosition = newPosition + new Vector3(-1, 0, 0);
        }
        else if (i == 2)
        {
            newPosition = newPosition + new Vector3(0, 1, 0);
        }
        else if (i == 3)
        {
            newPosition = newPosition + new Vector3(0, -1, 0);
        }
        //if we would wander off beyond the size of the grid turn the other way around
        for (int j = 0; j < 2; j++)
        {
            if (Mathf.Abs(newPosition[j]) > grid.size[j] / grid.spacing[j])
            {
                newPosition[j] -= Mathf.Sign(newPosition[j]) * 2.0f;
            }
        }

        //return the position in world space
        return(grid.GridToWorld(newPosition));
    }
    void BuildMesh()
    {
        // this will be parsing our text file
        var sr = new StringReader(_heightMap.text);
        // line stores the text of the current line that's being read, text
        // stores the concatenation of all lines without line breaks
        string line, text = "";

        // instantiate a new mesh and wipe it clean
        mesh = new Mesh();
        mesh.Clear();

        // let's first get the amount of rows and columns
        while ((line = sr.ReadLine()) != null)
        {
            rows++;                                    // count the amount of rows so far
            columns = Mathf.Max(columns, line.Length); // the longest line is the amount of columns
        }
        // build the height array from that
        heights = new int[rows * columns];

        // now concatenate all lines into one large string.
        sr = new StringReader(_heightMap.text);         // reset the reader first
        while ((line = sr.ReadLine()) != null)
        {
            // Fill short lines with extra 0 to make them all have the same length
            for (int i = line.Length; i < columns; i++)
            {
                line += "0";
            }
            text += line;             // concatenate all the lines into one large string (strips out the line breaks)
        }

        //copy the text into the heights array
        for (int i = 0; i < text.Length; i++)
        {
            heights[i] = (int)char.GetNumericValue(text[i]);
        }

        //this is where we will store the vertices
        var vertices = new Vector3[rows * columns];

        for (int i = 1; i <= rows; i++)          // traverse the row first
        {
            for (int j = 1; j <= columns; j++)   // then the column
            // pick the height from text. The row and column get translated into the index within the long text string
            {
                int h = heights[RowColumn2Index(i, j)];
                // now assign the vertex depending on its row, column and
                // height (vector in local space; row, column and height in
                // grid space)
                vertices[RowColumn2Index(i, j)] = transform.InverseTransformPoint(grid.GridToWorld(new Vector3(j - 1, h, -i + 1)));
            }
        }
        // assign the vertices to the mesh
        mesh.vertices = vertices;

        // now for the triangles; 3 vertices per triangle, two triangles per
        // square, one square between two columns/rows
        var triangles = new int[3 * 2 * (rows - 1) * (columns - 1)];
        int counter   = 0;       // this will keep track of where in the triangles array we currently are

        for (int i = 1; i < rows; i++)
        {
            for (int j = 1; j < columns; j++)
            {
                // assign the vertex indices in a clockwise direction
                triangles[counter]     = RowColumn2Index(i, j);
                triangles[counter + 1] = RowColumn2Index(i, j + 1);
                triangles[counter + 2] = RowColumn2Index(i + 1, j);
                triangles[counter + 3] = RowColumn2Index(i + 1, j);
                triangles[counter + 4] = RowColumn2Index(i, j + 1);
                triangles[counter + 5] = RowColumn2Index(i + 1, j + 1);
                counter += 6;                 // increment the counter for the next two triangles (six vertices)
            }
        }
        // assign the triangles to the mesh
        mesh.triangles = triangles;

        // add some dummy UVs to keep the shader happy or else it complains,
        // but they are not used in this example
        var uvs = new Vector2[vertices.Length];

        for (int k = 0; k < uvs.Length; k++)
        {
            uvs[k] = new Vector2(vertices[k].x, vertices[k].y);
        }
        mesh.uv = uvs;

        // the usual cleanup
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mesh.Optimize();
    }
示例#5
0
    // When we want to slide a block we need to know how far we can go before we "collide" (note that there is no actual collision detection involved anywhere).
    // We can only look up to on square ahead in each direction, so the bounds need to be recalculated from time to time; this allows us to have obstacles in
    // all sorts of directions, like a maze that can change all the time.
    public static Vector3[] CalculateSlidingBounds(Vector3 pos, Vector3 scl)
    {
        //break up the block and find the lwer left and upper right square in the matrix
        Vector3[,] squares = BreakUpObstacle(pos, scl);
        int[] lowerLeft  = GetSquare(squares[0, 0]);       // we store the position inside the matrix here
        int[] upperRight = GetSquare(squares[squares.GetLength(0) - 1, squares.GetLength(2) - 1]);
        //for each adjacent left square check if all left fields are free (a bitmask would have been the way to go instead of four bools, but let's keep it simple)
        bool freeLeft = true;

        //iterate over all the squares one square left of the left edge
        for (int i = lowerLeft[1]; i < upperRight[1] + 1; i++)
        {
            //	freeLeft = freeLeft && levelMatrix[Mathf.Max(0, lowerLeft[0]-1),i]; // use the Max so we don't get negative values (the matrix starts at 0)
        }

        bool freeRight = true;

        //iterate
        for (int i = lowerLeft[1]; i < upperRight[1] + 1; i++)
        {
            //		freeRight = freeRight && levelMatrix[Mathf.Min(levelMatrix.GetLength(0)-1, upperRight[0]+1),i]; // use Min so we don't go off the matrix size
        }

        bool freeBottom = true;

        for (int i = lowerLeft[0]; i < upperRight[0] + 1; i++)
        {
            //		freeBottom = freeBottom && levelMatrix[i, Mathf.Max(0, lowerLeft[1]-1)];
        }

        bool freeTop = true;

        for (int i = lowerLeft[0]; i < upperRight[0] + 1; i++)
        {
            //		freeTop = freeTop && levelMatrix[i, Mathf.Min(levelMatrix.GetLength(1)-1, upperRight[1]+1)];
        }

        //now assume the block canot move anywhere; for each free direction loosen the constraints by one grid unit (world unit * spacing)
        Vector3[] bounds = new Vector3[2] {
            pos, pos
        };
        if (freeLeft)
        {
            bounds[0] -= _grid.spacing.x * Vector3.right;
        }
        if (freeRight)
        {
            bounds[1] += _grid.spacing.x * Vector3.right;
        }
        if (freeBottom)
        {
            bounds[0] -= _grid.spacing.z * Vector3.up;
        }
        if (freeTop)
        {
            bounds[1] += _grid.spacing.z * Vector3.up;
        }

        // the bounds can still be outside of the grid, so we need to clamp that as well
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                bounds[i][j] = Mathf.Clamp(bounds[i][j], _grid.GridToWorld(Vector3.zero)[j] + 0.5f * scl[j], _grid.GridToWorld(_grid.renderTo)[j] - 0.5f * scl[j]);
            }
        }

        return(bounds);
    }