summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Tiled/TiledMapTileLayer.cs
blob: 5d07f06fb315e8ce2f48e091ed4c5bfb911aefae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace MonoGame.Extended.Tiled
{
    public class TiledMapTileLayer : TiledMapLayer
    {
        public TiledMapTileLayer(string name, string type, int width, int height, int tileWidth, int tileHeight, Vector2? offset = null,
            Vector2? parallaxFactor = null, float opacity = 1, bool isVisible = true)
            : base(name, type, offset, parallaxFactor, opacity, isVisible)
        {
            Width = width;
            Height = height;
            TileWidth = tileWidth;
            TileHeight = tileHeight;
            Tiles = new TiledMapTile[Width * Height];
        }

        public int Width { get; }
        public int Height { get; }
        public int TileWidth { get; }
        public int TileHeight { get; }
        public TiledMapTile[] Tiles { get; }

        public int GetTileIndex(ushort x, ushort y)
        {
            return x + y * Width;
        }

        public bool TryGetTile(ushort x, ushort y, out TiledMapTile? tile)
        {
            if (x >= Width)
            {
                tile = null;
                return false;
            }
            var index = GetTileIndex(x, y);

            if (index < 0 || index >= Tiles.Length)
            {
                tile = null;
                return false;
            }

            tile = Tiles[index];
            return true;
        }

        public TiledMapTile GetTile(ushort x, ushort y)
        {
            var index = GetTileIndex(x, y);
            return Tiles[index];
        }

        public void SetTile(ushort x, ushort y, uint globalIdentifier)
        {
            var index = GetTileIndex(x, y);
            Tiles[index] = new TiledMapTile(globalIdentifier, x, y);
        }

        public void RemoveTile(ushort x, ushort y)
        {
            SetTile(x, y, 0);
        }
    }
}