2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
|
|
|
enum Terrain {
|
|
|
|
Empty,
|
|
|
|
Grass,
|
|
|
|
GrassL,
|
|
|
|
GrassR,
|
|
|
|
Rock,
|
|
|
|
RockL,
|
|
|
|
RockR,
|
|
|
|
Water,
|
|
|
|
Block
|
|
|
|
}
|
|
|
|
|
|
|
|
class Tile {
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly Texture2D texture;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
public Tile(Texture2D texture, Terrain terrain, Rectangle position) {
|
|
|
|
this.texture = texture;
|
2020-01-25 17:03:26 +00:00
|
|
|
Terrain = terrain;
|
|
|
|
Position = position;
|
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
|
|
|
|
|
|
|
public void Draw(SpriteBatch spriteBatch, Camera camera) {
|
|
|
|
int size = World.TileSize;
|
2020-01-25 16:15:11 +00:00
|
|
|
Vector2 drawPos = new Vector2(Position.Left - camera.Left, Position.Top);
|
|
|
|
switch (Terrain) {
|
2020-01-18 03:41:45 +00:00
|
|
|
case Terrain.Grass: {
|
|
|
|
Rectangle source = new Rectangle(3 * size, 0 * size, size, size);
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Terrain.GrassL: {
|
|
|
|
Rectangle source = new Rectangle(2 * size, 0 * size, size, size);
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Terrain.GrassR: {
|
|
|
|
Rectangle source = new Rectangle(4 * size, 0 * size, size, size);
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Terrain.Rock: {
|
|
|
|
Rectangle source = new Rectangle(3 * size, 1 * size, size, size);
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Terrain.RockL: {
|
|
|
|
Rectangle source = new Rectangle(1 * size, 2 * size, size, size);
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Terrain.RockR: {
|
|
|
|
Rectangle source = new Rectangle(5 * size, 2 * size, size, size);
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Terrain.Water: {
|
|
|
|
Rectangle source = new Rectangle(9 * size, 2 * size, size, size);
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Terrain.Block: {
|
|
|
|
Rectangle source = new Rectangle(6 * size, 3 * size, size, size);
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Terrain.Empty:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-25 02:04:30 +00:00
|
|
|
readonly string worldString = @"
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
X
|
|
|
|
.
|
|
|
|
X <======> <==X X <=> <XX> XX .
|
|
|
|
XXX .
|
|
|
|
XXXX .
|
|
|
|
XXXXX .
|
|
|
|
X <X=X> <> <> <X> = <> X X X X <> X X XX X <=X> XXXXXX .
|
2020-01-31 13:36:29 +00:00
|
|
|
<> [] [] XX XX XXX XX XXXXXXX
|
2020-01-18 03:41:45 +00:00
|
|
|
<> [] [] [] XXX XXX XXXX XXX <> <> XXXXXXXX
|
|
|
|
[]12345678[]123456[]123456789[]1234567890 123456 123456 12345 1234 12345 1234 123XXXX XXXX1234XXXXX XXXX1234[]123 1234567[]XXXXXXXXX12345678
|
2020-01-23 19:10:02 +00:00
|
|
|
====================> <====..========..======..=========..=========> <=============> <==============================================================> <=======..==============..==============================
|
|
|
|
....................] [............................................] [.............] [..............................................................] [.......................................................";
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
public World(Texture2D texture) {
|
2020-01-28 02:58:48 +00:00
|
|
|
var tilesList = new List<Tile>();
|
2020-01-18 03:41:45 +00:00
|
|
|
string[] worldDesc = worldString.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
|
|
|
Terrain terrain = Terrain.Empty;
|
|
|
|
if (i < worldDesc[j].Length) {
|
|
|
|
switch (worldDesc[j][i]) {
|
|
|
|
case '=':
|
|
|
|
terrain = Terrain.Grass;
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
terrain = Terrain.GrassL;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
terrain = Terrain.GrassR;
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
terrain = Terrain.Rock;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
terrain = Terrain.RockL;
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
terrain = Terrain.RockR;
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
terrain = Terrain.Water;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
terrain = Terrain.Block;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
terrain = Terrain.Empty;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 16:23:37 +00:00
|
|
|
if (terrain != Terrain.Empty) {
|
|
|
|
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
|
2020-01-28 02:58:48 +00:00
|
|
|
tilesList.Add(new Tile(texture, terrain, position));
|
2020-01-25 16:23:37 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public void Draw(SpriteBatch spriteBatch, Camera camera) {
|
2020-01-25 16:23:37 +00:00
|
|
|
foreach (Tile t in tiles) {
|
|
|
|
t.Draw(spriteBatch, camera);
|
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
|
|
|
}
|
|
|
|
}
|