This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
TilemapJsonParser.cs
executable file
·309 lines (221 loc) · 11.6 KB
/
TilemapJsonParser.cs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
// portions of the code. See LICENSE file in the project root for full
// license information. Third-party libraries used by Pixel Vision 8 are
// under their own licenses. Please refer to those libraries for details
// on the license they use.
//
// Contributors
// --------------------------------------------------------
// This is the official list of Pixel Vision 8 contributors:
//
// Jesse Freeman - @JesseFreeman
// Christina-Antoinette Neofotistou @CastPixel
// Christer Kaitila - @McFunkypants
// Pedro Medeiros - @saint11
// Shawn Rakowski - @shwany
//
using PixelVision8.Player;
using PixelVision8.Runner;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PixelVision8.Runner
{
public class TilemapJsonParser : JsonParser
{
protected IPlayerChips target;
public TilemapJsonParser(string filePath, IFileLoader fileLoadHelper, IPlayerChips target) : base(filePath, fileLoadHelper)
{
this.target = target;
}
public override void CalculateSteps()
{
base.CalculateSteps();
_steps.Add(ParseMap);
}
public void ParseMap()
{
var version = (int)(long)Data["version"];
if (version == 2)
{
_steps.Add(ConfigureTilemapV2);
}
else
{
_steps.Add(ConfigureTilemapV1);
}
StepCompleted();
}
public virtual void ConfigureTilemapV2()
{
var tilemapChip = target.TilemapChip;
if (Data.ContainsKey("layers"))
{
var layers = Data["layers"] as List<object>;
// var tileSets = Data["tilesets"] as List<object>;
var total = layers.Count;
for (var i = 0; i < total; i++)
try
{
var layer = layers[i] as Dictionary<string, object>;
var layerType = (string)layer["type"];
if (layerType == "objectgroup")
{
var objects = layer["objects"] as List<object>;
var totalTiles = objects.Count;
for (var j = 0; j < totalTiles; j++)
{
var tileObject = objects[j] as Dictionary<string, object>;
var column = (int)Math.Floor((float)(long)tileObject["x"] / 8);
var row = (int)Math.Floor((float)(long)tileObject["y"] / 8) - 1;
var tile = tilemapChip.GetTile(column, row);
var gid = (uint)(long)tileObject["gid"];
var idMask = (1 << 30) - 1;
tile.SpriteId = (int)(gid & idMask) - 1;
var hMask = 1 << 31;
tile.FlipH = (hMask & gid) != 0;
var vMask = 1 << 30;
tile.FlipV = (vMask & gid) != 0;
var properties = tileObject["properties"] as List<object>;
// int flagID = -1;
// int colorOffset = 0;
for (var k = 0; k < properties.Count; k++)
{
var prop = properties[k] as Dictionary<string, object>;
var propName = (string)prop["name"];
if (propName == "flagID")
tile.Flag = (int)(long)prop["value"];
else if (propName == "colorOffset")
tile.ColorOffset = (int)(long)prop["value"];
}
// tile.Invalidate();
}
}
tilemapChip.Invalidate();
// TODO need to make sure that the layer is the same size as the display chip
// TODO copy the tilemap data over to layer correctly
}
catch (Exception e)
{
Console.WriteLine(e);
// Just ignore any layers that don't exist
throw new Exception(
"Unable to parse 'tilemap.json' file. It may be corrupt. Try deleting it and creating a new one.");
}
}
StepCompleted();
}
public virtual void ConfigureTilemapV1()
{
var tilemapChip = target.TilemapChip;
if (Data.ContainsKey("layers"))
{
var layers = Data["layers"] as List<object>;
var tileSets = Data["tilesets"] as List<object>;
var total = layers.Count;
for (var i = 0; i < total; i++)
try
{
var layer = layers[i] as Dictionary<string, object>;
var layerType = (string)layer["type"];
if (layerType == "tilelayer")
{
var tileSet = tileSets[i] as Dictionary<string, object>;
var offset = (int)(long)tileSet["firstgid"];
var columns = (int)(long)layer["width"];
var rows = (int)(long)layer["height"];
var rawLayerData = layer["data"] as List<object>;
var dataValues = rawLayerData
.Select(x => (int)(long)x - offset < -1 ? -1 : (int)(long)x - offset).ToArray();
if (columns != tilemapChip.columns || rows > tilemapChip.rows)
{
// Create texture data that matches the memory of the tilemap chip
var tmpPixelData = new PixelData(tilemapChip.columns, tilemapChip.rows);
Utilities.Clear(tmpPixelData);
// tmpPixelData.Clear();
var jsonData = new PixelData(columns, rows);
Utilities.Clear(jsonData);
// jsonData.Clear();
Utilities.SetPixels(dataValues, 0, 0, columns, rows, jsonData);
// jsonData.SetPixels(0, 0, columns, rows, dataValues);
var tmpCol = columns > tilemapChip.columns ? tilemapChip.columns : columns;
var tmpRow = rows > tilemapChip.rows ? tilemapChip.rows : rows;
if (tmpCol > columns) tmpCol = columns;
if (tmpRow > rows) tmpRow = rows;
var tmpData = new int[tmpCol * tmpRow];
Utilities.CopyPixels(jsonData, 0, 0, tmpCol, tmpRow, ref tmpData);
// jsonData.CopyPixels(ref tmpData, 0, 0, tmpCol, tmpRow);
Utilities.SetPixels(tmpData, 0, 0, tmpCol, tmpRow, tmpPixelData);
// tmpPixelData.SetPixels(0, 0, tmpCol, tmpRow, tmpData);
// TODO why is this happening twice?
// PixelDataUtil.CopyPixels(ref dataValues, tmpPixelData, 0, 0, tmpPixelData.Width, tmpPixelData.Height);
// tmpPixelData.CopyPixels(ref dataValues, 0, 0, tmpPixelData.width, tmpPixelData.height);
Utilities.CopyPixels(tmpPixelData, 0, 0, tilemapChip.columns, tilemapChip.rows, ref dataValues);
// tmpPixelData.CopyPixels(ref dataValues, 0, 0, tilemapChip.columns, tilemapChip.rows);
}
for (var j = 0; j < tilemapChip.total; j++)
{
var tile = tilemapChip.tiles[j];
if ((string)layer["name"] == "Sprites")
tile.SpriteId = dataValues[j];
else if ((string)layer["name"] == "Flags") tile.Flag = dataValues[j];
// tile.Invalidate();
}
}
else if (layerType == "objectgroup")
{
var objects = layer["objects"] as List<object>;
var totalTiles = objects.Count;
for (var j = 0; j < totalTiles; j++)
{
var tileObject = objects[j] as Dictionary<string, object>;
var column = (int)Math.Floor((float)(long)tileObject["x"] / 8);
var row = (int)Math.Floor((float)(long)tileObject["y"] / 8) - 1;
var tile = tilemapChip.GetTile(column, row);
var gid = (uint)(long)tileObject["gid"];
var idMask = (1 << 30) - 1;
tile.SpriteId = (int)(gid & idMask) - 1;
var hMask = 1 << 31;
tile.FlipH = (hMask & gid) != 0;
var vMask = 1 << 30;
tile.FlipV = (vMask & gid) != 0;
var properties = tileObject["properties"] as List<object>;
// int flagID = -1;
// int colorOffset = 0;
for (var k = 0; k < properties.Count; k++)
{
var prop = properties[k] as Dictionary<string, object>;
var propName = (string)prop["name"];
if (propName == "flagID")
tile.Flag = (int)(long)prop["value"];
else if (propName == "colorOffset") tile.ColorOffset = (int)(long)prop["value"];
}
// tile.Invalidate();
}
}
tilemapChip.Invalidate();
// TODO need to make sure that the layer is the same size as the display chip
// TODO copy the tilemap data over to layer correctly
}
catch
{
// Just ignore any layers that don't exist
throw new Exception(
"Unable to parse 'tilemap.json' file. It may be corrupt. Try deleting it and creating a new one.");
}
}
StepCompleted();
}
}
public partial class Loader
{
[FileParser("tilemap.json")]
public void ParseTilemapJson(string file, IPlayerChips engine)
{
AddParser(new TilemapJsonParser(file, _fileLoadHelper, engine));
}
}
}