예제 #1
0
파일: Tile.cs 프로젝트: koesie10/pkmn-world
    /*
     * Initialises the tile
     */
    void Start()
    {
        BoundingBox = new CoordBoundingBox((int)Position.x, (int)Position.y);

        // TODO: loading indicator

        // Start loading data
        StartCoroutine(Create(Position));
    }
예제 #2
0
    /*
     * Create a line mesh.
     *
     * It makes my eyes hurt...
     */
    public static Mesh CreateLineMesh(JSONNode coords, CoordBoundingBox bounds)
    {
        float width = 0.5f;

        List <Vector3> vertices      = new List <Vector3> ();
        List <Vector3> normals       = new List <Vector3> ();
        List <Vector2> triangulation = new List <Vector2> ();

        for (int i = 0; i < coords.Count; i++)
        {
            Vector2 currentInterpolated = bounds.Interpolate(float.Parse(coords [i] [1].Value), float.Parse(coords [i] [0].Value));
            Vector3 current             = new Vector3(currentInterpolated.x, 0, currentInterpolated.y);

            Vector2 referenceInterpolated = bounds.Interpolate(float.Parse(coords [i + (i != coords.Count - 1 ? 1 : -1)] [1].Value), float.Parse(coords [i + (i != coords.Count - 1 ? 1 : -1)] [0].Value));
            Vector3 reference             = new Vector3(referenceInterpolated.x, 0, referenceInterpolated.y);

            if (i != coords.Count - 1)
            {
                Vector3 direction = new Vector3(-(reference.z - current.z), 0, reference.x - current.x).normalized;

                vertices.Add(current + direction * width);
                normals.Add(Vector3.up);
                triangulation.Add(new Vector2((current + direction * width).x, (current + direction * width).z));

                vertices.Add(current + direction * -width);
                normals.Add(Vector3.up);
                triangulation.Add(new Vector2((current + direction * -width).x, (current + direction * -width).z));
            }
            else
            {
                Vector3 direction = new Vector3(-(current.z - reference.z), 0, current.x - reference.x).normalized;

                vertices.Add(current + direction * -width);
                normals.Add(Vector3.up);
                triangulation.Add(new Vector2((current + direction * -width).x, (current + direction * -width).z));

                vertices.Add(current + direction * width);
                normals.Add(Vector3.up);
                triangulation.Add(new Vector2((current + direction * width).x, (current + direction * width).z));
            }
        }

        Mesh m = new Mesh();

        m.vertices = vertices.ToArray();
        m.normals  = normals.ToArray();
        Triangulator t = new Triangulator(triangulation.ToArray());

        m.triangles = t.Triangulate();
        m.RecalculateNormals();
        m.RecalculateBounds();

        return(m);
    }
예제 #3
0
파일: Player.cs 프로젝트: Diik/pkmn-world
    void UpdatePosition()
    {
        // rotate player with gyroscope
        if (Input.gyro.enabled)
        {
            transform.Rotate(0, -Input.gyro.rotationRateUnbiased.z, 0);
        }

        // make sure location is initialized
        if (LocationController.GetStatus() != LocationServiceStatus.Running)
        {
            return;
        }

        var lastLoc = LocationController.GetLastData();
        // get the tile we're on
        Vector2 tileCoords = Map.WorldToTileCoords(lastLoc.latitude, lastLoc.longitude);
        Tile    tile       = GameObject.FindObjectsOfType <Tile>().Where((_tile) =>
        {
            // check if x and y are the same as the tile player is on
            return(_tile.Position.x == tileCoords.x && _tile.Position.y == tileCoords.y);
        }).First();

        // calculate our position on the map
        CoordBoundingBox bounds          = tile.BoundingBox;
        Vector2          interpolatedPos = bounds.Interpolate(lastLoc.latitude, lastLoc.longitude);
        Vector3          newPos          = new Vector3(interpolatedPos.x + tile.WorldPosition.x, transform.position.y, interpolatedPos.y + tile.WorldPosition.z);

        // TODO: hack, issue #17
        newPos.x = -newPos.x;

        // smoothly interpolate between our last position and our current position
        transform.position = Vector3.Lerp(
            prevPos,
            newPos,
            Time.fixedDeltaTime * lerpMultiply
            );

        prevPos = transform.position;
    }
예제 #4
0
    /*
     * Create a combined line mesh from a multi line one.
     * This needs the parent transform to work for now...
     */
    public static Mesh CreateMultiLineMesh(JSONNode coords, CoordBoundingBox bounds, Transform t)
    {
        List <CombineInstance> combine = new List <CombineInstance> ();

        for (int i = 0; i < coords.Count; i++)
        {
            // Create a single line mesh
            Mesh line = CreateLineMesh(coords [i], bounds);

            // Add the single line to the combine
            CombineInstance c = new CombineInstance();
            c.mesh      = line;
            c.transform = Matrix4x4.identity;
            combine.Add(c);
        }

        // Join all created single line meshes into a new mesh
        Mesh m = new Mesh();

        m.CombineMeshes(combine.ToArray(), true);

        return(m);
    }
예제 #5
0
    /*
     * Create a polygonal mesh, used for structures such as water
     */
    public static Mesh CreatePolygonMesh(JSONNode coords, CoordBoundingBox bounds)
    {
        // Keep normals and vertices in positions in 3d space
        List <Vector3> normals  = new List <Vector3> ();
        List <Vector3> vertices = new List <Vector3> ();

        // The triangulator can only handle 2d shapes so simulate one
        List <Vector2> triangulation = new List <Vector2> ();

        for (int i = 0; i < coords.Count - 1; i++)
        {
            // Store the vertex for the triangulator
            Vector2 vertex = bounds.Interpolate(float.Parse(coords [i] [1].Value), float.Parse(coords [i] [0].Value));
            triangulation.Add(vertex);

            // Add the vertex plus a y position to allow for 3d space
            vertices.Add(new Vector3(vertex.x, 0, vertex.y));

            // Normals should always point up on this flat surface
            normals.Add(Vector3.up);
        }

        // Iject triangulation points and create a new base mesh
        Triangulator t = new Triangulator(triangulation.ToArray());
        Mesh         m = new Mesh();

        // Store vertices and get triangles from the triangulator
        m.vertices  = vertices.ToArray();
        m.triangles = t.Triangulate();
        m.normals   = normals.ToArray();

        // Recalculate mesh properties
        m.RecalculateBounds();
        m.RecalculateNormals();

        return(m);
    }