Exemplo n.º 1
0
    public List <Vector2Int> FindPath(Vector2 from, Vector2 to, IShipDataContainer data)
    {
        Vector2Int _from = new Vector2Int(Mathf.RoundToInt(from.x), Mathf.RoundToInt(from.y));
        Vector2Int _to   = new Vector2Int(Mathf.RoundToInt(to.x), Mathf.RoundToInt(to.y));

        return(FindPath(_from, _to, data));
    }
Exemplo n.º 2
0
    public void Build(IShipDataContainer shipData)
    {
        for (int x = 0; x < shipTexture.width; x++)
        {
            for (int y = 0; y < shipTexture.height; y++)
            {
                Color pixel = shipTexture.GetPixel(x, y);

                int _x = x - shipTexture.width / 2;
                int _y = y - shipTexture.height / 2;

                if (pixel.a > 0)
                {
                    foreach (var keyPair in blockDictionary)
                    {
                        if (IsColor(pixel, keyPair.Key))
                        {
                            ShipBlock block = ShipBlock.Create(keyPair.Value, _x, _y);
                            shipData.AddBlock(block);

                            break;
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 3
0
    public static GameObject Build(IShipDataContainer data, IEnumerable <ShipBlock> shipBlocks)
    {
        List <int>     triangles = new List <int>();
        List <Vector2> uvs       = new List <Vector2>();
        List <Vector2> uvs2      = new List <Vector2>();
        List <Vector3> vertices  = new List <Vector3>();

        var ship = new GameObject("Ship mesh");

        var meshFilter   = ship.AddComponent <MeshFilter>();
        var meshRenderer = ship.AddComponent <MeshRenderer>();

        float delta     = 0.5F;
        float uv2Width  = 1F / data.Bounds.width;
        float uv2Height = 1F / data.Bounds.height;

        foreach (ShipBlock block in shipBlocks)
        {
            BlockResourceItem blockResource = ResourceUtility.Blocks.GetBlock(block.ID);

            if (blockResource == null)
            {
                continue;
            }

            Vector3    localPosition = new Vector3(block.X, block.Y);
            Quaternion localRotation = Quaternion.Euler(0, 0, block.Rotation);

            if (blockResource.usePrefab == true && blockResource.prefab != null)
            {
                GameObject prefab = Object.Instantiate(blockResource.prefab);

                prefab.transform.parent        = ship.transform;
                prefab.transform.localPosition = localPosition;
                prefab.transform.localRotation = localRotation;
            }

            int       verticesIndex = vertices.Count;
            Matrix4x4 rotateMatrix  = Matrix4x4.Rotate(localRotation);

            Rect uv  = Rect.zero;
            Rect uv2 = new Rect((block.X - data.Bounds.x) / (float)data.Bounds.width, (block.Y - data.Bounds.y) / (float)data.Bounds.height, uv2Width, uv2Height);

            if (blockResource.useAtlas == true)
            {
                uv = blockResource.uv;
            }

            Vector2 uv_center = new Vector2(uv.x + uv.width * 0.5F, uv.y + uv.height * 0.5F);
            Vector2 uv_up     = rotateMatrix.MultiplyPoint(new Vector2(0, uv.height * 0.5F));
            Vector2 uv_right  = rotateMatrix.MultiplyPoint(new Vector2(uv.width * 0.5F, 0));


            vertices.Add(localPosition + new Vector3(-delta, -delta));
            vertices.Add(localPosition + new Vector3(-delta, delta));
            vertices.Add(localPosition + new Vector3(delta, delta));
            vertices.Add(localPosition + new Vector3(delta, -delta));


            triangles.Add(verticesIndex);
            triangles.Add(verticesIndex + 1);
            triangles.Add(verticesIndex + 2);
            triangles.Add(verticesIndex);
            triangles.Add(verticesIndex + 2);
            triangles.Add(verticesIndex + 3);


            uvs.Add(uv_center - uv_up - uv_right);
            uvs.Add(uv_center + uv_up - uv_right);
            uvs.Add(uv_center + uv_up + uv_right);
            uvs.Add(uv_center - uv_up + uv_right);

            uvs2.Add(new Vector2(uv2.xMin, uv2.yMin));
            uvs2.Add(new Vector2(uv2.xMin, uv2.yMax));
            uvs2.Add(new Vector2(uv2.xMax, uv2.yMax));
            uvs2.Add(new Vector2(uv2.xMax, uv2.yMin));
        }

        Mesh mesh = new Mesh();

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.uv        = uvs.ToArray();
        mesh.uv2       = uvs2.ToArray();

        mesh.RecalculateNormals();
        mesh.RecalculateBounds();

        meshFilter.sharedMesh = mesh;
        meshRenderer.material = ResourceUtility.shipMaterial;

        return(ship);
    }
Exemplo n.º 4
0
    public List <Vector2Int> FindPath(Vector2Int from, Vector2Int to, IShipDataContainer data)
    {
        PathTracingItem        currentCell;
        List <Vector2Int>      path    = new List <Vector2Int>();
        List <PathTracingItem> viewed  = new List <PathTracingItem>();
        List <PathTracingItem> viewing = new List <PathTracingItem>();

        if (from == to)
        {
            return(path);
        }

        viewing.Add(PathTracingItem.Create(from, from, to));

        while (viewing.Count > 0)
        {
            currentCell = FindPathTracingItem(viewing);

            foreach (Vector2Int offset in offsets)
            {
                var isValid = false;
                var point   = offset + currentCell.point;
                var blocks  = data.GetBlocks(point.x, point.y);

                foreach (var block in blocks)
                {
                    if (block.ID == (int)Blocks.Floor)
                    {
                        isValid = true;
                    }

                    if (block.ID != (int)Blocks.Floor)
                    {
                        isValid = false;
                        break;
                    }
                }

                if (isValid)
                {
                    viewing.Add(PathTracingItem.Create(point, from, to, currentCell));
                }

                if (point == to)
                {
                    path = Trace(currentCell);

                    return(path);
                }
            }

            viewed.Add(currentCell);
            viewing.RemoveAll(x => viewed.Contains(x));

            if (viewed.Count > 1000)
            {
                return(path);
            }
        }

        return(path);
    }