示例#1
0
        private static void ApplyTileCollisions(DT1.Tile tile, int x, int y)
        {
            var pos = Iso.MapTileToWorld(x, y);
            var collisionMapOffset = Iso.Snap(Iso.MapToIso(pos));
            int flagIndex          = 0;

            DT1.BlockFlags mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk;
            for (int dy = 2; dy > -3; --dy)
            {
                for (int dx = -2; dx < 3; ++dx, ++flagIndex)
                {
                    Vector2i        subCellPos    = collisionMapOffset + new Vector2i(dx, dy);
                    bool            passable      = (tile.flags[flagIndex] & mask) == 0;
                    CollisionLayers blockedLayers = passable ? CollisionLayers.None : CollisionLayers.Walk;
                    if (tile.orientation == 0)
                    {
                        CollisionMap.SetBlocked(subCellPos, blockedLayers);
                    }
                    else if (CollisionMap.Passable(subCellPos, CollisionLayers.Walk) && !passable)
                    {
                        CollisionMap.SetBlocked(subCellPos, blockedLayers);
                    }
                }
            }
        }
示例#2
0
 private void PutToPopup(DT1.Tile tile, Renderer renderer, int x, int y)
 {
     foreach (Popup popup in WorldState.instance.Popups)
     {
         if (popup.revealMainIndex == tile.mainIndex && popup.revealArea.Contains(x, y))
         {
             popup.roofs.Add(renderer);
         }
     }
 }
示例#3
0
        private Mesh CreateTileMesh(DT1.Tile tile)
        {
            if (meshCache.ContainsKey(tile))
            {
                return(meshCache[tile]);
            }

            var     texture = tile.texture;
            float   x0      = tile.textureX;
            float   y0      = tile.textureY;
            float   w       = tile.width / Iso.pixelsPerUnit;
            float   h       = -tile.height / Iso.pixelsPerUnit;
            Mesh    mesh    = new Mesh();
            Vector3 topLeft;

            if (tile.orientation == 0 || tile.orientation == 15)
            {
                topLeft = new Vector3(-1f, 0.5f);
                if (tile.orientation == 15)
                {
                    topLeft.y += tile.roofHeight / Iso.pixelsPerUnit;
                }
            }
            else if (tile.orientation > 15)
            {
                int upperPart = Math.Min(96, -tile.height);
                y0     -= upperPart;
                topLeft = new Vector3(-1f, upperPart / Iso.pixelsPerUnit - 0.5f);
            }
            else
            {
                topLeft = new Vector3(-1f, h - 0.5f);
                y0     += tile.height;
            }

            mesh.vertices = new[]
            {
                topLeft,
                topLeft + new Vector3(0, -h),
                topLeft + new Vector3(w, -h),
                topLeft + new Vector3(w, 0)
            };
            mesh.uv = new[]
            {
                new Vector2(x0 / texture.width, -y0 / texture.height),
                new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height),
                new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height),
                new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height)
            };
            mesh.triangles = new[] { 2, 1, 0, 3, 2, 0 };

            meshCache.Add(tile, mesh);
            return(mesh);
        }
示例#4
0
        public void Reset()
        {
            int cellCount = width * height;

            for (int i = 0; i < floors.Length; ++i)
            {
                floors[i] = new DT1.Tile[cellCount];
            }
            for (int i = 0; i < walls.Length; ++i)
            {
                walls[i] = new DT1.Tile[cellCount];
            }
            shadows      = new DT1.Tile[cellCount];
            specialTiles = new DT1.Tile[cellCount];

            OnReset?.Invoke();
        }
示例#5
0
        private Renderer CreateTileRenderer(DT1.Tile tile, int x, int y, Transform parent = null)
        {
            var pos = Iso.MapTileToWorld(x, y);

            var gameObject = new GameObject();

            gameObject.name = tile.mainIndex + "_" + tile.subIndex + "_" + tile.orientation;
            gameObject.transform.position = pos;
            if (parent)
            {
                gameObject.transform.SetParent(parent);
            }
            var meshRenderer = gameObject.AddComponent <MeshRenderer>();
            var meshFilter   = gameObject.AddComponent <MeshFilter>();

            if (tile.orientation == 0 || tile.orientation == 15)
            {
                meshRenderer.sortingLayerID = tile.orientation == 0 ? SortingLayers.Floor : SortingLayers.Roof;

                gameObject.name += tile.orientation == 0 ? " (floor)" : " (roof)";
            }
            else if (tile.orientation > 15)
            {
                meshRenderer.sortingLayerID = SortingLayers.LowerWall;
                gameObject.name            += " (lower wall)";
            }
            else
            {
                if (tile.orientation == 13) // shadows
                {
                    meshRenderer.sortingLayerID = SortingLayers.Shadow;
                }

                meshRenderer.sortingOrder = Iso.SortingOrder(pos) - 4;
            }

            meshFilter.mesh = CreateTileMesh(tile);

            meshRenderer.material = tile.material;
            return(meshRenderer);
        }
示例#6
0
    static Renderer CreateTile(DT1.Tile tile, int x, int y, int orderInLayer = 0, Transform parent = null)
    {
        var texture = tile.texture;
        var pos     = Iso.MapTileToWorld(x, y);

        GameObject gameObject = new GameObject();

        gameObject.name = tile.mainIndex + "_" + tile.subIndex + "_" + tile.orientation;
        gameObject.transform.position = pos;
        if (parent)
        {
            gameObject.transform.SetParent(parent);
        }
        var   meshRenderer = gameObject.AddComponent <MeshRenderer>();
        var   meshFilter   = gameObject.AddComponent <MeshFilter>();
        Mesh  mesh         = new Mesh();
        float x0           = tile.textureX;
        float y0           = tile.textureY;
        float w            = tile.width / Iso.pixelsPerUnit;
        float h            = (-tile.height) / Iso.pixelsPerUnit;

        if (tile.orientation == 0 || tile.orientation == 15)
        {
            var topLeft = new Vector3(-1f, 0.5f);
            if (tile.orientation == 15)
            {
                topLeft.y += tile.roofHeight / Iso.pixelsPerUnit;
            }
            mesh.vertices = new Vector3[] {
                topLeft,
                topLeft + new Vector3(0, -h),
                topLeft + new Vector3(w, -h),
                topLeft + new Vector3(w, 0)
            };
            mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
            mesh.uv        = new Vector2[] {
                new Vector2(x0 / texture.width, -y0 / texture.height),
                new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height),
                new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height),
                new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height)
            };

            meshRenderer.sortingLayerName = tile.orientation == 0 ? "Floor" : "Roof";
            meshRenderer.sortingOrder     = orderInLayer;
        }
        else
        {
            var topLeft = new Vector3(-1f, h - 0.5f);
            mesh.vertices = new Vector3[] {
                topLeft,
                topLeft + new Vector3(0, -h),
                topLeft + new Vector3(w, -h),
                topLeft + new Vector3(w, 0)
            };
            mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
            mesh.uv        = new Vector2[] {
                new Vector2(x0 / texture.width, (-y0 - tile.height) / texture.height),
                new Vector2(x0 / texture.width, -y0 / texture.height),
                new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height),
                new Vector2((x0 + tile.width) / texture.width, (-y0 - tile.height) / texture.height)
            };
            meshRenderer.sortingOrder = Iso.SortingOrder(pos) - 4;
        }
        meshFilter.mesh = mesh;

        int  flagIndex          = 0;
        var  collisionMapOffset = Iso.Snap(Iso.MapToIso(pos));
        byte mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk;

        for (int dy = 2; dy > -3; --dy)
        {
            for (int dx = -2; dx < 3; ++dx, ++flagIndex)
            {
                var  subCellPos = collisionMapOffset + new Vector2i(dx, dy);
                bool passable   = (tile.flags[flagIndex] & mask) == 0;
                if (tile.orientation == 0)
                {
                    CollisionMap.SetPassable(subCellPos, passable);
                }
                else if (CollisionMap.Passable(subCellPos) && !passable)
                {
                    CollisionMap.SetPassable(subCellPos, false);
                }
            }
        }

        meshRenderer.material = tile.material;
        return(meshRenderer);
    }
示例#7
0
        static Renderer CreateTile(DT1.Tile tile, int x, int y, int orderInLayer = 0, Transform parent = null)
        {
            var texture = tile.texture;
            var pos     = Iso.MapTileToWorld(x, y);

            GameObject gameObject = new GameObject();

            gameObject.name = tile.mainIndex + "_" + tile.subIndex + "_" + tile.orientation;
            gameObject.transform.position = pos;
            if (parent)
            {
                gameObject.transform.SetParent(parent);
            }
            var   meshRenderer = gameObject.AddComponent <MeshRenderer>();
            var   meshFilter   = gameObject.AddComponent <MeshFilter>();
            Mesh  mesh         = new Mesh();
            float x0           = tile.textureX;
            float y0           = tile.textureY;
            float w            = tile.width / Iso.pixelsPerUnit;
            float h            = (-tile.height) / Iso.pixelsPerUnit;

            if (tile.orientation == 0 || tile.orientation == 15)
            {
                var topLeft = new Vector3(-1f, 0.5f);
                if (tile.orientation == 15)
                {
                    topLeft.y += tile.roofHeight / Iso.pixelsPerUnit;
                }
                mesh.vertices = new Vector3[]
                {
                    topLeft,
                    topLeft + new Vector3(0, -h),
                    topLeft + new Vector3(w, -h),
                    topLeft + new Vector3(w, 0)
                };
                mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
                mesh.uv        = new Vector2[]
                {
                    new Vector2(x0 / texture.width, -y0 / texture.height),
                    new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height)
                };

                meshRenderer.sortingLayerID = tile.orientation == 0 ? SortingLayers.Floor : SortingLayers.Roof;
                meshRenderer.sortingOrder   = orderInLayer;

                gameObject.name += tile.orientation == 0 ? " (floor)" : " (roof)";
            }
            else if (tile.orientation > 15)
            {
                int upperPart = Math.Min(96, -tile.height);
                y0 -= upperPart;
                var topLeft = new Vector3(-1f, upperPart / Iso.pixelsPerUnit - 0.5f);
                mesh.vertices = new Vector3[] {
                    topLeft,
                    topLeft + new Vector3(0, -h),
                    topLeft + new Vector3(w, -h),
                    topLeft + new Vector3(w, 0)
                };
                mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
                mesh.uv        = new Vector2[] {
                    new Vector2(x0 / texture.width, -y0 / texture.height),
                    new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height)
                };
                meshRenderer.sortingLayerID = SortingLayers.LowerWall;
                meshRenderer.sortingOrder   = orderInLayer;

                gameObject.name += " (lower wall)";
            }
            else
            {
                var topLeft = new Vector3(-1f, h - 0.5f);
                mesh.vertices = new Vector3[] {
                    topLeft,
                    topLeft + new Vector3(0, -h),
                    topLeft + new Vector3(w, -h),
                    topLeft + new Vector3(w, 0)
                };
                mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 };
                mesh.uv        = new Vector2[] {
                    new Vector2(x0 / texture.width, (-y0 - tile.height) / texture.height),
                    new Vector2(x0 / texture.width, -y0 / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height),
                    new Vector2((x0 + tile.width) / texture.width, (-y0 - tile.height) / texture.height)
                };
                if (tile.orientation == 13) // shadows
                {
                    meshRenderer.sortingLayerID = SortingLayers.Shadow;
                }
                meshRenderer.sortingOrder = Iso.SortingOrder(pos) - 4;
            }
            meshFilter.mesh = mesh;

            int flagIndex          = 0;
            var collisionMapOffset = Iso.Snap(Iso.MapToIso(pos));

            DT1.BlockFlags mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk;
            for (int dy = 2; dy > -3; --dy)
            {
                for (int dx = -2; dx < 3; ++dx, ++flagIndex)
                {
                    Vector2i        subCellPos    = collisionMapOffset + new Vector2i(dx, dy);
                    bool            passable      = (tile.flags[flagIndex] & mask) == 0;
                    CollisionLayers blockedLayers = passable ? CollisionLayers.None : CollisionLayers.Walk;
                    if (tile.orientation == 0)
                    {
                        CollisionMap.SetBlocked(subCellPos, blockedLayers);
                    }
                    else if (CollisionMap.Passable(subCellPos, CollisionLayers.Walk) && !passable)
                    {
                        CollisionMap.SetBlocked(subCellPos, blockedLayers);
                    }
                }
            }

            meshRenderer.material = tile.material;
            return(meshRenderer);
        }
示例#8
0
 public void PutSpecialTile(DT1.Tile tile, int x, int y)
 {
     specialTiles[y * width + x] = tile;
 }
示例#9
0
 public void PutShadow(DT1.Tile tile, int x, int y)
 {
     shadows[y * width + x] = tile;
     ApplyTileCollisions(tile, x, y);
 }
示例#10
0
 public void PutWall(DT1.Tile tile, int x, int y, int layerIndex)
 {
     walls[layerIndex][y * width + x] = tile;
     ApplyTileCollisions(tile, x, y);
 }
示例#11
0
 public void PutFloor(DT1.Tile tile, int x, int y, int layerIndex)
 {
     floors[layerIndex][y * width + x] = tile;
     ApplyTileCollisions(tile, x, y);
 }