* Parse tileset asset from text content * 从文本内容解析瓦片集资产
(content: IAssetContent, _context: IAssetParseContext)
| 62 | * 从文本内容解析瓦片集资产 |
| 63 | */ |
| 64 | async parse(content: IAssetContent, _context: IAssetParseContext): Promise<ITilesetAsset> { |
| 65 | if (!content.text) { |
| 66 | throw new Error('Tileset content is empty'); |
| 67 | } |
| 68 | |
| 69 | const jsonData = JSON.parse(content.text) as ITilesetAsset; |
| 70 | |
| 71 | // 验证必要字段 |
| 72 | // Validate required fields |
| 73 | if (!jsonData.tileWidth || !jsonData.tileHeight || !jsonData.image) { |
| 74 | throw new Error('Invalid tileset format: missing required fields'); |
| 75 | } |
| 76 | |
| 77 | // 计算派生字段(如果未提供) |
| 78 | // Calculate derived fields if not provided |
| 79 | if (!jsonData.columns && jsonData.imageWidth) { |
| 80 | jsonData.columns = Math.floor(jsonData.imageWidth / jsonData.tileWidth); |
| 81 | } |
| 82 | if (!jsonData.rows && jsonData.imageHeight) { |
| 83 | jsonData.rows = Math.floor(jsonData.imageHeight / jsonData.tileHeight); |
| 84 | } |
| 85 | if (!jsonData.tileCount && jsonData.columns && jsonData.rows) { |
| 86 | jsonData.tileCount = jsonData.columns * jsonData.rows; |
| 87 | } |
| 88 | |
| 89 | return jsonData; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Dispose loaded asset |