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
|
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended.Tiled
{
public struct TiledMapTilesetTileAnimationFrame
{
public readonly int LocalTileIdentifier;
public readonly TimeSpan Duration;
public readonly Vector2[] TextureCoordinates;
private readonly Dictionary<TiledMapTileFlipFlags, Vector2[]> _flipDictionary = new Dictionary<TiledMapTileFlipFlags, Vector2[]>();
internal TiledMapTilesetTileAnimationFrame(TiledMapTileset tileset, int localTileIdentifier, int durationInMilliseconds)
{
LocalTileIdentifier = localTileIdentifier;
Duration = new TimeSpan(0, 0, 0, 0, durationInMilliseconds);
TextureCoordinates = new Vector2[4];
CreateTextureCoordinates(tileset);
}
public Vector2[] GetTextureCoordinates(TiledMapTileFlipFlags flipFlags)
{
if (!_flipDictionary.TryGetValue(flipFlags, out Vector2[] flippedTextureCoordiantes))
{
return TextureCoordinates;
}
else
{
return flippedTextureCoordiantes;
}
}
public void CreateTextureRotations(TiledMapTileset tileset, TiledMapTileFlipFlags flipFlags)
{
if (!_flipDictionary.ContainsKey(flipFlags))
{
if (flipFlags == TiledMapTileFlipFlags.None)
{
_flipDictionary.Add(flipFlags, TextureCoordinates);
}
else
{
_flipDictionary.Add(flipFlags, TransformTextureCoordinates(tileset, flipFlags));
}
}
}
public Vector2[] TransformTextureCoordinates(TiledMapTileset tileset, TiledMapTileFlipFlags flipFlags)
{
var sourceRectangle = tileset.GetTileRegion(LocalTileIdentifier);
var texture = tileset.Texture;
var texelLeft = (float)sourceRectangle.X / texture.Width;
var texelTop = (float)sourceRectangle.Y / texture.Height;
var texelRight = (sourceRectangle.X + sourceRectangle.Width) / (float)texture.Width;
var texelBottom = (sourceRectangle.Y + sourceRectangle.Height) / (float)texture.Height;
var flipDiagonally = (flipFlags & TiledMapTileFlipFlags.FlipDiagonally) != 0;
var flipHorizontally = (flipFlags & TiledMapTileFlipFlags.FlipHorizontally) != 0;
var flipVertically = (flipFlags & TiledMapTileFlipFlags.FlipVertically) != 0;
var transform = new Vector2[4];
transform[0].X = texelLeft;
transform[0].Y = texelTop;
transform[1].X = texelRight;
transform[1].Y = texelTop;
transform[2].X = texelLeft;
transform[2].Y = texelBottom;
transform[3].X = texelRight;
transform[3].Y = texelBottom;
if (flipDiagonally)
{
FloatHelper.Swap(ref transform[1].X, ref transform[2].X);
FloatHelper.Swap(ref transform[1].Y, ref transform[2].Y);
}
if (flipHorizontally)
{
if (flipDiagonally)
{
FloatHelper.Swap(ref transform[0].Y, ref transform[1].Y);
FloatHelper.Swap(ref transform[2].Y, ref transform[3].Y);
}
else
{
FloatHelper.Swap(ref transform[0].X, ref transform[1].X);
FloatHelper.Swap(ref transform[2].X, ref transform[3].X);
}
}
if (flipVertically)
{
if (flipDiagonally)
{
FloatHelper.Swap(ref transform[0].X, ref transform[2].X);
FloatHelper.Swap(ref transform[1].X, ref transform[3].X);
}
else
{
FloatHelper.Swap(ref transform[0].Y, ref transform[2].Y);
FloatHelper.Swap(ref transform[1].Y, ref transform[3].Y);
}
}
transform[0] = transform[0];
transform[1] = transform[1];
transform[2] = transform[2];
transform[3] = transform[3];
return transform;
}
private void CreateTextureCoordinates(TiledMapTileset tileset)
{
var sourceRectangle = tileset.GetTileRegion(LocalTileIdentifier);
var texture = tileset.Texture;
var texelLeft = (float)sourceRectangle.X / texture.Width;
var texelTop = (float)sourceRectangle.Y / texture.Height;
var texelRight = (sourceRectangle.X + sourceRectangle.Width) / (float)texture.Width;
var texelBottom = (sourceRectangle.Y + sourceRectangle.Height) / (float)texture.Height;
TextureCoordinates[0].X = texelLeft;
TextureCoordinates[0].Y = texelTop;
TextureCoordinates[1].X = texelRight;
TextureCoordinates[1].Y = texelTop;
TextureCoordinates[2].X = texelLeft;
TextureCoordinates[2].Y = texelBottom;
TextureCoordinates[3].X = texelRight;
TextureCoordinates[3].Y = texelBottom;
}
public override string ToString()
{
return $"{LocalTileIdentifier}:{Duration}";
}
}
}
|