2019-08-14 14:15:07 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2021-11-29 21:24:08 +01:00
|
|
|
using MLEM.Graphics;
|
2019-08-14 14:15:07 +02:00
|
|
|
using MLEM.Startup;
|
|
|
|
|
|
|
|
namespace Demos {
|
|
|
|
/// <summary>
|
|
|
|
/// This is a demo for <see cref="AutoTiling"/>.
|
|
|
|
/// </summary>
|
2019-09-01 11:55:41 +02:00
|
|
|
public class AutoTilingDemo : Demo {
|
2019-08-14 14:15:07 +02:00
|
|
|
|
2021-11-27 15:03:30 +01:00
|
|
|
private const int TileSize = 8;
|
2019-08-14 14:15:07 +02:00
|
|
|
private Texture2D texture;
|
|
|
|
private string[] layout;
|
|
|
|
|
2019-09-01 11:55:41 +02:00
|
|
|
public AutoTilingDemo(MlemGame game) : base(game) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void LoadContent() {
|
2019-08-14 14:15:07 +02:00
|
|
|
base.LoadContent();
|
2021-11-27 15:03:30 +01:00
|
|
|
// The layout of the texture is important for auto tiling to work correctly, and is explained in the XML docs for the methods used
|
2019-08-14 14:15:07 +02:00
|
|
|
this.texture = LoadContent<Texture2D>("Textures/AutoTiling");
|
|
|
|
|
|
|
|
// in this example, a simple string array is used for layout purposes. As the AutoTiling method allows any kind of
|
|
|
|
// comparison, the actual implementation can vary (for instance, based on a more in-depth tile map)
|
|
|
|
this.layout = new[] {
|
2021-11-27 15:03:30 +01:00
|
|
|
"XXX X ",
|
|
|
|
"XXXXXX",
|
|
|
|
"XXX X",
|
|
|
|
"XXXXX ",
|
|
|
|
" X "
|
2019-08-14 14:15:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-01 11:55:41 +02:00
|
|
|
public override void DoDraw(GameTime gameTime) {
|
2019-08-14 14:15:07 +02:00
|
|
|
this.GraphicsDevice.Clear(Color.Black);
|
|
|
|
|
|
|
|
// drawing the auto tiles
|
|
|
|
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: Matrix.CreateScale(10));
|
2021-11-27 15:03:30 +01:00
|
|
|
for (var x = 0; x < 6; x++) {
|
2019-08-14 14:15:07 +02:00
|
|
|
for (var y = 0; y < 5; y++) {
|
|
|
|
// don't draw non-grass tiles ( )
|
|
|
|
if (this.layout[y][x] != 'X')
|
|
|
|
continue;
|
2019-08-15 14:59:15 +02:00
|
|
|
|
2019-08-14 14:15:07 +02:00
|
|
|
var x1 = x;
|
|
|
|
var y1 = y;
|
2020-06-18 17:24:35 +02:00
|
|
|
|
2019-08-14 14:15:07 +02:00
|
|
|
// the connectsTo function determines for any given tile if it should connect to, that is, auto-tile with the
|
|
|
|
// neighbor in the supplied offset direction. In this example, the layout determines where grass tiles are (X)
|
|
|
|
// and where there are none ( ).
|
2020-06-18 17:24:35 +02:00
|
|
|
bool ConnectsTo(int xOff, int yOff) {
|
2019-08-14 14:15:07 +02:00
|
|
|
// don't auto-tile out of bounds
|
2021-11-27 15:03:30 +01:00
|
|
|
if (x1 + xOff < 0 || y1 + yOff < 0 || x1 + xOff >= 6 || y1 + yOff >= 5)
|
2019-08-14 14:15:07 +02:00
|
|
|
return false;
|
|
|
|
// check if the neighboring tile is also grass (X)
|
|
|
|
return this.layout[y1 + yOff][x1 + xOff] == 'X';
|
2020-06-18 17:24:35 +02:00
|
|
|
}
|
|
|
|
|
2021-11-27 15:03:30 +01:00
|
|
|
// the texture region supplied to the AutoTiling method should only encompass the first filler tile's location and size
|
|
|
|
AutoTiling.DrawAutoTile(this.SpriteBatch, new Vector2(x + 1, y + 1) * TileSize, this.texture, new Rectangle(0, 0, TileSize, TileSize), ConnectsTo, Color.White);
|
|
|
|
|
|
|
|
// when drawing extended auto-tiles, the same rules apply, but the source texture layout is different
|
|
|
|
AutoTiling.DrawExtendedAutoTile(this.SpriteBatch, new Vector2(x + 8, y + 1) * TileSize, this.texture, new Rectangle(0, TileSize * 2, TileSize, TileSize), ConnectsTo, Color.White);
|
2019-08-14 14:15:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.SpriteBatch.End();
|
|
|
|
|
2019-08-15 14:59:15 +02:00
|
|
|
base.DoDraw(gameTime);
|
2019-08-14 14:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|