2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-02-02 14:33:33 +00:00
|
|
|
using System;
|
2020-01-18 03:41:45 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
|
|
|
enum Terrain {
|
|
|
|
Grass,
|
|
|
|
GrassL,
|
|
|
|
GrassR,
|
|
|
|
Rock,
|
|
|
|
RockL,
|
|
|
|
RockR,
|
|
|
|
Water,
|
|
|
|
Block
|
|
|
|
}
|
|
|
|
|
2020-02-18 20:24:08 +00:00
|
|
|
class TileFactory {
|
|
|
|
|
2020-02-18 21:50:26 +00:00
|
|
|
struct TextureSource {
|
|
|
|
public Texture2D texture;
|
|
|
|
public Rectangle source;
|
|
|
|
|
|
|
|
public TextureSource(Texture2D texture, Rectangle source) {
|
|
|
|
this.texture = texture;
|
|
|
|
this.source = source;
|
2020-02-18 20:24:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 16:19:23 +00:00
|
|
|
readonly Dictionary<Terrain, TextureSource> terrainToTexture;
|
|
|
|
|
|
|
|
public TileFactory() {
|
|
|
|
terrainToTexture = new Dictionary<Terrain, TextureSource>() {
|
|
|
|
{ Terrain.GrassL, GetTextureSource(Textures.Grassland, 2, 0) },
|
|
|
|
{ Terrain.Grass, GetTextureSource(Textures.Grassland, 3, 0) },
|
|
|
|
{ Terrain.GrassR, GetTextureSource(Textures.Grassland, 4, 0) },
|
|
|
|
{ Terrain.Rock, GetTextureSource(Textures.Grassland, 3, 1) },
|
|
|
|
{ Terrain.RockL, GetTextureSource(Textures.Grassland, 1, 2) },
|
|
|
|
{ Terrain.RockR, GetTextureSource(Textures.Grassland, 5, 2) },
|
|
|
|
{ Terrain.Water, GetTextureSource(Textures.Grassland, 9, 2) },
|
|
|
|
{ Terrain.Block, GetTextureSource(Textures.Grassland, 6, 3) },
|
2020-02-18 20:24:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tile MakeTile(Terrain terrain, Rectangle position) {
|
2020-02-18 21:50:26 +00:00
|
|
|
TextureSource textureSource = terrainToTexture[terrain];
|
|
|
|
return new Tile(terrain, position, textureSource.texture, textureSource.source);
|
2020-02-18 20:24:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 16:19:23 +00:00
|
|
|
private TextureSource GetTextureSource(Texture2D texture, int x, int y) {
|
2020-02-18 20:24:08 +00:00
|
|
|
int size = World.TileSize;
|
2020-02-18 21:50:26 +00:00
|
|
|
Rectangle position = new Rectangle(x * size, y * size, size, size);
|
|
|
|
return new TextureSource(texture, position);
|
2020-02-18 20:24:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
class Tile {
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly Texture2D texture;
|
2020-02-02 14:04:33 +00:00
|
|
|
readonly Rectangle textureSource;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-02-18 20:24:08 +00:00
|
|
|
public Tile(Terrain terrain, Rectangle position, Texture2D texture, Rectangle textureSource) {
|
2020-01-25 17:03:26 +00:00
|
|
|
Terrain = terrain;
|
|
|
|
Position = position;
|
2020-02-18 20:24:08 +00:00
|
|
|
this.texture = texture;
|
|
|
|
this.textureSource = textureSource;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 16:15:11 +00:00
|
|
|
public Rectangle Position { get; private set; }
|
|
|
|
public Terrain Terrain { get; private set; }
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-02-04 22:37:42 +00:00
|
|
|
public void Draw(SpriteBatch spriteBatch) {
|
|
|
|
spriteBatch.Draw(texture, Position.Location.ToVector2(), textureSource, Color.White);
|
2020-02-02 14:04:33 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class World {
|
|
|
|
|
|
|
|
public const int TileSize = 16;
|
2020-01-28 02:58:48 +00:00
|
|
|
readonly Tile[] tiles;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 23:01:41 +00:00
|
|
|
// 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; }
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 23:27:20 +00:00
|
|
|
public int Height {
|
|
|
|
get { return tileHeight * TileSize; }
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:32:32 +00:00
|
|
|
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 }
|
|
|
|
};
|
|
|
|
|
2020-02-19 16:19:23 +00:00
|
|
|
public World(string levelSpecification) {
|
|
|
|
TileFactory factory = new TileFactory();
|
2020-01-28 02:58:48 +00:00
|
|
|
var tilesList = new List<Tile>();
|
2020-02-04 14:55:07 +00:00
|
|
|
string[] worldDesc = levelSpecification.Split('\n');
|
2020-01-29 23:01:41 +00:00
|
|
|
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++) {
|
2020-01-18 03:41:45 +00:00
|
|
|
if (i < worldDesc[j].Length) {
|
2020-02-02 15:32:32 +00:00
|
|
|
char key = worldDesc[j][i];
|
|
|
|
if (charToTerrain.ContainsKey(key)) {
|
|
|
|
Terrain terrain = charToTerrain[key];
|
|
|
|
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
|
2020-02-18 20:24:08 +00:00
|
|
|
tilesList.Add(factory.MakeTile(terrain, position));
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 02:58:48 +00:00
|
|
|
tiles = tilesList.ToArray();
|
2020-01-29 21:11:28 +00:00
|
|
|
|
2020-01-29 23:04:56 +00:00
|
|
|
// 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
|
2020-01-29 22:43:32 +00:00
|
|
|
// are nearby a given x-position.
|
2020-01-30 21:58:42 +00:00
|
|
|
CollisionTargets = new AABB[tiles.Length + 2];
|
2020-01-29 22:43:32 +00:00
|
|
|
|
|
|
|
// Add a synthetic collisionTarget on the left side of the world.
|
2020-01-30 21:58:42 +00:00
|
|
|
CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
|
2020-01-29 22:43:32 +00:00
|
|
|
|
|
|
|
// Now add all the normal collisionTargets for every static terrain tile.
|
2020-01-29 21:11:28 +00:00
|
|
|
Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
|
2020-01-28 02:58:48 +00:00
|
|
|
for (int i = 0; i < tiles.Length; i++) {
|
2020-01-29 21:11:28 +00:00
|
|
|
Vector2 center = new Vector2(
|
|
|
|
tiles[i].Position.Left + halfSize.X, tiles[i].Position.Top + halfSize.Y);
|
2020-01-30 21:58:42 +00:00
|
|
|
CollisionTargets[i + 1] = new AABB(center, halfSize);
|
2020-01-28 02:58:48 +00:00
|
|
|
}
|
2020-01-29 22:43:32 +00:00
|
|
|
|
|
|
|
// Add a final synthetic collisionTarget on the right side of the world.
|
2020-01-30 21:58:42 +00:00
|
|
|
CollisionTargets[tiles.Length + 1] = new AABB(
|
2020-01-29 23:01:41 +00:00
|
|
|
new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 22:37:42 +00:00
|
|
|
public void Draw(SpriteBatch spriteBatch) {
|
2020-01-25 16:23:37 +00:00
|
|
|
foreach (Tile t in tiles) {
|
2020-02-04 22:37:42 +00:00
|
|
|
t.Draw(spriteBatch);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:58:42 +00:00
|
|
|
public AABB[] CollisionTargets { get; }
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|