-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileset.cs
More file actions
98 lines (84 loc) · 2.93 KB
/
Tileset.cs
File metadata and controls
98 lines (84 loc) · 2.93 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace BmArrayLoader;
public class Tileset
{
private bool m_initialized;
private List<byte[]> m_palette;
private Indexmap m_master;
private List<Indexmap> m_tiles;
private ReadOnlyCollection<Indexmap> m_tilesReadOnly;
private ReadOnlyCollection<byte[]> m_paletteReadOnly;
public Indexmap Master { get => m_master; }
public ReadOnlyCollection<Indexmap> Tiles { get => m_tilesReadOnly; }
public ReadOnlyCollection<byte[]> Palette { get => m_paletteReadOnly; }
public Tileset()
{
m_initialized = false;
m_palette = new List<byte[]>(256);
for (int i = 0; i < 256; i++)
m_palette.Add(new byte[3]);
m_tiles = new List<Indexmap>();
m_tilesReadOnly = m_tiles.AsReadOnly();
m_paletteReadOnly = m_palette.AsReadOnly();
}
public Tileset(int width, int height) : this()
{
InitMaster(width, height);
}
public void InitMaster(int width, int height)
{
m_master = new Indexmap(width, height);
m_tiles.Add(m_master);
m_initialized = true;
}
public Indexmap GetTile(int offsetX, int offsetY, int width, int height)
{
int idx = GetTileId(offsetX, offsetY, width, height);
return idx != -1 ? m_tiles[idx] : null;
}
public int GetTileId(int offsetX, int offsetY, int width, int height)
{
if (!m_initialized)
return -1;
//Indexmap already in list?
int idx = m_tiles.FindIndex(x => x.Equals(offsetX, offsetY, width, height));
if (idx == -1)
{
//derive new index map from master
int sizeX = offsetX + width;
int sizeY = offsetY + height;
if (sizeX > 0 && sizeY > 0 &&
width > 0 && height > 0 &&
sizeX <= m_master.Width && sizeY <= m_master.Height)
{
Indexmap indexmap = new Indexmap(offsetX, offsetY, width, height);
int srcIdx = 0;
int tgtIdx = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
srcIdx = m_master.Width * (offsetY + y) + offsetX + x;
indexmap[tgtIdx] = m_master[srcIdx];
tgtIdx++;
}
}
m_tiles.Add(indexmap);
idx = m_tiles.Count - 1;
}
else
{
Console.WriteLine("TILESET error: Indexmap not created - invalid dimensions");
}
}
return idx;
}
public bool AddTile(int offsetX, int offsetY, int width, int height)
{
int tiles = m_tiles.Count;
int idx = GetTileId(offsetX, offsetY, width, height);
return (idx == tiles); //new tile was added
}
}