sneak/Shared/World.cs
Colin McMillen ff0c9ddc26 Add a transformation matrix to spriteBatch.Draw().
Instead of having every drawable object know how to transform itself based on
the camera position, we pass in a transformation matrix to spriteBatch.Draw().
Unfortunately MonoGame only lets us specify a translation that works over an
entire SpriteBatch.Begin() call, so we need to begin & end separately for
objects that *aren't* supposed to translate at the same rate as the camera.

Fixes #39.

GitOrigin-RevId: afab72c39236b1b46fe1597412209981ddae9c7c
2020-02-13 14:54:46 -05:00

136 lines
4.2 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SemiColinGames {
enum Terrain {
Grass,
GrassL,
GrassR,
Rock,
RockL,
RockR,
Water,
Block
}
class Tile {
readonly Texture2D texture;
readonly Rectangle textureSource;
static readonly Dictionary<Terrain, Point> terrainToTilePosition =
new Dictionary<Terrain, Point>() {
{ Terrain.Grass, new Point(3, 0) },
{ Terrain.GrassL, new Point(2, 0) },
{ Terrain.GrassR, new Point(4, 0) },
{ Terrain.Rock, new Point(3, 1) },
{ Terrain.RockL, new Point(1, 2) },
{ Terrain.RockR, new Point(5, 2) },
{ Terrain.Water, new Point(9, 2) },
{ Terrain.Block, new Point(6, 3) },
};
public Tile(Texture2D texture, Terrain terrain, Rectangle position) {
this.texture = texture;
Terrain = terrain;
Position = position;
this.textureSource = TextureSource();
}
public Rectangle Position { get; private set; }
public Terrain Terrain { get; private set; }
public void Draw(SpriteBatch spriteBatch) {
spriteBatch.Draw(texture, Position.Location.ToVector2(), textureSource, Color.White);
}
private Rectangle TextureSource() {
int size = World.TileSize;
Point pos = terrainToTilePosition[Terrain];
return new Rectangle(pos.X * size, pos.Y * size, size, size);
}
}
class World {
public const int TileSize = 16;
readonly Tile[] tiles;
// Size of World in terms of tile grid.
private readonly int tileWidth;
private readonly int tileHeight;
// Size of World in pixels.
public int Width {
get { return tileWidth * TileSize; }
}
public int Height {
get { return tileHeight * TileSize; }
}
private static readonly Dictionary<char, Terrain> charToTerrain =
new Dictionary<char, Terrain>() {
{ '=', Terrain.Grass },
{ '<', Terrain.GrassL },
{ '>', Terrain.GrassR },
{ '.', Terrain.Rock },
{ '[', Terrain.RockL },
{ ']', Terrain.RockR },
{ '~', Terrain.Water },
{ 'X', Terrain.Block }
};
public World(Texture2D texture, string levelSpecification) {
var tilesList = new List<Tile>();
string[] worldDesc = levelSpecification.Split('\n');
tileWidth = worldDesc.AsQueryable().Max(a => a.Length);
tileHeight = worldDesc.Length;
Debug.WriteLine("world size: {0}x{1}", tileWidth, tileHeight);
for (int i = 0; i < tileWidth; i++) {
for (int j = 0; j < tileHeight; j++) {
if (i < worldDesc[j].Length) {
char key = worldDesc[j][i];
if (charToTerrain.ContainsKey(key)) {
Terrain terrain = charToTerrain[key];
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
tilesList.Add(new Tile(texture, terrain, position));
}
}
}
}
tiles = tilesList.ToArray();
// Because we added tiles from left to right, the CollisionTargets are sorted by x-position.
// We maintain this invariant so that it's possible to efficiently find CollisionTargets that
// are nearby a given x-position.
CollisionTargets = new AABB[tiles.Length + 2];
// Add a synthetic collisionTarget on the left side of the world.
CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
// Now add all the normal collisionTargets for every static terrain tile.
Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
for (int i = 0; i < tiles.Length; i++) {
Vector2 center = new Vector2(
tiles[i].Position.Left + halfSize.X, tiles[i].Position.Top + halfSize.Y);
CollisionTargets[i + 1] = new AABB(center, halfSize);
}
// Add a final synthetic collisionTarget on the right side of the world.
CollisionTargets[tiles.Length + 1] = new AABB(
new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
}
public void Draw(SpriteBatch spriteBatch) {
foreach (Tile t in tiles) {
t.Draw(spriteBatch);
}
}
public AABB[] CollisionTargets { get; }
}
}