SHOW:
|
|
- or go back to the newest paste.
1 | public class TileMapView : GameState | |
2 | { | |
3 | static readonly float[] ROTATIONS = { | |
4 | 0f, | |
5 | MathHelper.PiOver2, | |
6 | MathHelper.Pi, | |
7 | MathHelper.PiOver2 * 3 | |
8 | }; | |
9 | ||
10 | public static ushort MakeTileID(int x, int y, SpriteEffects m = SpriteEffects.None, int r = 0) | |
11 | { | |
12 | return (ushort)( | |
13 | (x & 63) | ((y & 63) << 6) | ((r & 3) << 12) | ((int)m << 14) | |
14 | ); | |
15 | } | |
16 | ||
17 | public TileMapView(GameStateMachine gsm, GraphicsDevice gd, ContentManager cm) : base(gsm) | |
18 | { | |
19 | spriteBatch = new SpriteBatch(gd); | |
20 | ||
21 | tileset = cm.Load<Texture2D>("proto-tileset"); | |
22 | ||
23 | width = 24; | |
24 | height = 16; | |
25 | layers = new ushort[3, width, height]; | |
26 | ||
27 | for (int x = 0; x < width; x++) | |
28 | for (int y = 0; y < height; y++) | |
29 | { | |
30 | layers[0, x, y] = MakeTileID(5, 18); | |
31 | layers[1, x, y] = layers[2, x, y] = 0xFFF; | |
32 | } | |
33 | ||
34 | for (int i = 3; i < 10; i++) | |
35 | layers[0, i + 4, i] = MakeTileID(3, 25); | |
36 | for (int i = 5; i < 11; i++) | |
37 | layers[0, i + 6, i] = MakeTileID(3, 25); | |
38 | for (int i = 4; i < 9; i++) | |
39 | layers[0, i + 8, i] = MakeTileID(3, 25); | |
40 | ||
41 | layers[1, 7, 9] = MakeTileID(1, 0, SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically, 2); | |
42 | } | |
43 | ||
44 | SpriteBatch spriteBatch; | |
45 | ||
46 | int width, height; | |
47 | Texture2D tileset; | |
48 | ushort[,,] layers; | |
49 | ||
50 | public override void Draw() | |
51 | { | |
52 | DrawLayer(0); | |
53 | DrawLayer(1); | |
54 | DrawLayer(2); | |
55 | } | |
56 | ||
57 | void DrawLayer(int layerIndex) | |
58 | { | |
59 | spriteBatch.Begin(); | |
60 | ||
61 | Rectangle source = new Rectangle(0, 0, 16, 16); | |
62 | Rectangle destin = new Rectangle(0, 0, 16, 16); | |
63 | ||
64 | int rot = 0; | |
65 | ||
66 | Vector2 origin = new Vector2(8, 8); | |
67 | ||
68 | SpriteEffects mirroring = SpriteEffects.None; | |
69 | ||
70 | for (int x = 0; x < width; x++) | |
71 | for (int y = 0; y < height; y++) | |
72 | { | |
73 | destin.X = x * 16; | |
74 | destin.Y = y * 16; | |
75 | ||
76 | source.X = (layers[layerIndex, x, y] & 63) * 16; | |
77 | source.Y = ((layers[layerIndex, x, y] >> 6) & 63) * 16; | |
78 | ||
79 | mirroring = (SpriteEffects)((layers[layerIndex, x, y] >> 14) & 3); | |
80 | - | spriteBatch.Draw(tileset, destin, source, Color.White, ROTATIONS[rot], Vector2.Zero, mirroring, 0); |
80 | + | |
81 | ||
82 | spriteBatch.Draw(tileset, destin, source, Color.White, ROTATIONS[rot], origin, mirroring, 0); | |
83 | } | |
84 | ||
85 | spriteBatch.End(); | |
86 | } | |
87 | } |