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
/
TilemapParser.cs
executable file
·110 lines (85 loc) · 3.34 KB
/
TilemapParser.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
//
// 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;
namespace PixelVision8.Runner
{
public class TilemapParser : SpriteImageParser
{
private readonly bool autoImport;
private readonly TilemapChip tilemapChip;
private bool autoResize;
public TilemapParser(string sourceFile, IImageParser parser, ColorChip colorChip, SpriteChip spriteChip, TilemapChip tilemapChip, bool autoResize = false) :
base(sourceFile, parser, colorChip, spriteChip)
{
this.tilemapChip = tilemapChip;
autoImport = tilemapChip.autoImport;
this.autoResize = autoResize;
}
public override void CutOutSprites()
{
// TODO the image should be the right size from the beginning
if (autoResize)
tilemapChip.Resize(ImageData.Columns, ImageData.Rows);
// if(autoResize)
//
var tmpColumns = ImageData.Columns > tilemapChip.columns ? tilemapChip.columns : ImageData.Columns;
var tmpRows = ImageData.Rows > tilemapChip.rows ? tilemapChip.rows : ImageData.Rows;
// Make sure the tilemap matches the image size
// tilemapChip.Resize(image.Columns, image.Rows);
for (var i = 0; i < totalSprites; i++)
{
var pos = Utilities.CalculatePosition(i, ImageData.Columns);
if (pos.X < tmpColumns && pos.Y < tmpRows)
{
// Convert sprite to color index
ConvertColorsToIndexes(cps);
ProcessSpriteData();
}
index++;
}
if (tmpColumns < ImageData.Columns || tmpRows < ImageData.Rows)
{
ImageData.Resize(tmpColumns * tmpColumns * spriteChip.width, tmpRows * spriteChip.height);
}
StepCompleted();
}
protected override void ProcessSpriteData()
{
var id = spriteChip.FindSprite(spriteData);
if (id == -1 && autoImport)
{
id = spriteChip.NextEmptyID();
spriteChip.UpdateSpriteAt(id, spriteData);
}
x = index % ImageData.Columns;
y = index / ImageData.Columns;
var tile = tilemapChip.GetTile(x, y);
tile.SpriteId = id;
}
}
public partial class Loader
{
[FileParser("tilemap.png")]
public void ParseTilemapImage(string file, IPlayerChips engine)
{
AddParser(new TilemapParser(file, _imageParser, engine.ColorChip, engine.SpriteChip, engine.TilemapChip, true));
}
}
}