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 {
|
2020-02-20 21:36:54 +00:00
|
|
|
|
2020-02-28 00:13:34 +00:00
|
|
|
public class Terrain {
|
2020-02-20 17:25:46 +00:00
|
|
|
|
|
|
|
public static Terrain FromSymbol(char symbol) {
|
|
|
|
if (mapping.ContainsKey(symbol)) {
|
|
|
|
return mapping[symbol];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 18:36:59 +00:00
|
|
|
private readonly static Dictionary<char, Terrain> mapping = new Dictionary<char, Terrain>();
|
2020-02-20 17:25:46 +00:00
|
|
|
|
2020-03-06 17:02:41 +00:00
|
|
|
public static Terrain Empty =
|
|
|
|
new Terrain('\0', Textures.Grassland, 0, 0);
|
|
|
|
public static Terrain Spike =
|
|
|
|
new Terrain('^', Textures.Grassland, 11, 8, isObstacle: true, isHarmful: true);
|
|
|
|
public static Terrain Grass =
|
|
|
|
new Terrain('=', Textures.Grassland, 3, 0, isObstacle: true);
|
|
|
|
public static Terrain GrassL =
|
|
|
|
new Terrain('<', Textures.Grassland, 2, 0, isObstacle: true);
|
|
|
|
public static Terrain GrassR =
|
|
|
|
new Terrain('>', Textures.Grassland, 4, 0, isObstacle: true);
|
|
|
|
public static Terrain Rock =
|
|
|
|
new Terrain('.', Textures.Grassland, 3, 1, isObstacle: true);
|
|
|
|
public static Terrain RockL =
|
|
|
|
new Terrain('[', Textures.Grassland, 1, 2, isObstacle: true);
|
|
|
|
public static Terrain RockR =
|
|
|
|
new Terrain(']', Textures.Grassland, 5, 2, isObstacle: true);
|
|
|
|
public static Terrain Block =
|
|
|
|
new Terrain('X', Textures.Ruins, 2, 0, isObstacle: true);
|
|
|
|
public static Terrain Wood =
|
|
|
|
new Terrain('_', Textures.Grassland, 10, 3, isObstacle: true);
|
|
|
|
public static Terrain WoodL =
|
|
|
|
new Terrain('(', Textures.Grassland, 9, 3, isObstacle: true);
|
|
|
|
public static Terrain WoodR =
|
|
|
|
new Terrain(')', Textures.Grassland, 12, 3, isObstacle: true);
|
|
|
|
public static Terrain WoodVert =
|
|
|
|
new Terrain('|', Textures.Grassland, 9, 5);
|
|
|
|
public static Terrain WoodVertL =
|
|
|
|
new Terrain('/', Textures.Grassland, 9, 4);
|
|
|
|
public static Terrain WoodVertR =
|
|
|
|
new Terrain('\\', Textures.Grassland, 12, 4);
|
|
|
|
public static Terrain WoodBottom =
|
|
|
|
new Terrain('v', Textures.Grassland, 10, 5);
|
|
|
|
public static Terrain WaterL =
|
|
|
|
new Terrain('~', Textures.Grassland, 9, 2);
|
|
|
|
public static Terrain WaterR =
|
|
|
|
new Terrain('`', Textures.Grassland, 10, 2);
|
|
|
|
public static Terrain FenceL =
|
|
|
|
new Terrain('d', Textures.Grassland, 5, 4);
|
|
|
|
public static Terrain Fence =
|
|
|
|
new Terrain('f', Textures.Grassland, 6, 4);
|
|
|
|
public static Terrain FencePost =
|
|
|
|
new Terrain('x', Textures.Grassland, 7, 4);
|
|
|
|
public static Terrain FenceR =
|
|
|
|
new Terrain('b', Textures.Grassland, 8, 4);
|
|
|
|
public static Terrain VineTop =
|
|
|
|
new Terrain('{', Textures.Ruins, 12, 5);
|
|
|
|
public static Terrain VineMid =
|
|
|
|
new Terrain(';', Textures.Ruins, 12, 6);
|
|
|
|
public static Terrain VineBottom =
|
|
|
|
new Terrain('}', Textures.Ruins, 12, 7);
|
|
|
|
public static Terrain GrassTall =
|
|
|
|
new Terrain('q', Textures.Grassland, 13, 0);
|
|
|
|
public static Terrain GrassShort =
|
|
|
|
new Terrain('w', Textures.Grassland, 14, 0);
|
|
|
|
public static Terrain Shoots =
|
|
|
|
new Terrain('e', Textures.Grassland, 15, 0);
|
|
|
|
public static Terrain Bush =
|
|
|
|
new Terrain('r', Textures.Grassland, 13, 2);
|
|
|
|
public static Terrain Mushroom =
|
|
|
|
new Terrain('t', Textures.Grassland, 17, 2);
|
2020-02-20 17:25:46 +00:00
|
|
|
|
|
|
|
public bool IsObstacle { get; private set; }
|
2020-03-06 17:02:41 +00:00
|
|
|
public bool IsHarmful { get; private set; }
|
2020-02-20 21:36:54 +00:00
|
|
|
public TextureRef Texture { get; private set; }
|
|
|
|
public Rectangle TextureSource { get; private set; }
|
2020-02-20 17:25:46 +00:00
|
|
|
|
2020-03-06 17:02:41 +00:00
|
|
|
private Terrain(
|
|
|
|
char symbol, TextureRef texture, int x, int y,
|
|
|
|
bool isObstacle = false, bool isHarmful = false) {
|
2020-02-20 17:25:46 +00:00
|
|
|
if (mapping.ContainsKey(symbol)) {
|
|
|
|
throw new ArgumentException("already have a terrain with symbol " + symbol);
|
|
|
|
}
|
|
|
|
mapping[symbol] = this;
|
2020-02-20 21:36:54 +00:00
|
|
|
IsObstacle = isObstacle;
|
2020-03-06 16:44:43 +00:00
|
|
|
IsHarmful = isHarmful;
|
2020-02-20 21:36:54 +00:00
|
|
|
Texture = texture;
|
2020-02-18 20:24:08 +00:00
|
|
|
int size = World.TileSize;
|
2020-02-20 21:36:54 +00:00
|
|
|
TextureSource = new Rectangle(x * size, y * size, size, size);
|
2020-02-18 20:24:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 00:18:58 +00:00
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
class Tile {
|
2020-02-20 21:36:54 +00:00
|
|
|
public Tile(Terrain terrain, Rectangle position) {
|
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
|
|
|
|
2020-02-04 22:37:42 +00:00
|
|
|
public void Draw(SpriteBatch spriteBatch) {
|
2020-02-20 21:36:54 +00:00
|
|
|
spriteBatch.Draw(
|
2020-02-20 21:39:12 +00:00
|
|
|
Terrain.Texture.Get, Position.Location.ToVector2(), Terrain.TextureSource, Color.White);
|
2020-02-02 14:04:33 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 17:28:58 +00:00
|
|
|
public class World {
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
public const int TileSize = 16;
|
2020-01-28 02:58:48 +00:00
|
|
|
readonly Tile[] tiles;
|
2020-02-20 17:25:46 +00:00
|
|
|
readonly Tile[] decorations;
|
2020-03-05 21:17:57 +00:00
|
|
|
readonly NPC[] npcs = new NPC[4];
|
2020-02-28 22:08:34 +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;
|
|
|
|
|
2020-03-03 18:08:51 +00:00
|
|
|
public Player Player { get; private set; }
|
2020-03-03 18:04:29 +00:00
|
|
|
|
2020-01-29 23:01:41 +00:00
|
|
|
// 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-19 16:19:23 +00:00
|
|
|
public World(string levelSpecification) {
|
2020-03-03 18:08:51 +00:00
|
|
|
Player = new Player();
|
2020-03-05 21:17:57 +00:00
|
|
|
npcs[0] = new NPC(new Point(16 * 8, 16 * 6));
|
|
|
|
npcs[1] = new NPC(new Point(16 * 28, 16 * 6));
|
|
|
|
npcs[2] = new NPC(new Point(16 * 36, 16 * 6));
|
|
|
|
npcs[3] = new NPC(new Point(16 * 36, 16 * 12));
|
|
|
|
|
2020-01-28 02:58:48 +00:00
|
|
|
var tilesList = new List<Tile>();
|
2020-02-20 17:25:46 +00:00
|
|
|
var decorationsList = new List<Tile>();
|
2020-02-20 18:36:59 +00:00
|
|
|
string[] worldDesc = levelSpecification.Substring(1).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];
|
2020-02-20 17:25:46 +00:00
|
|
|
Terrain terrain = Terrain.FromSymbol(key);
|
|
|
|
if (terrain != null) {
|
2020-02-02 15:32:32 +00:00
|
|
|
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
|
2020-02-20 21:36:54 +00:00
|
|
|
Tile tile = new Tile(terrain, position);
|
2020-02-20 17:25:46 +00:00
|
|
|
if (tile.Terrain.IsObstacle) {
|
|
|
|
tilesList.Add(tile);
|
|
|
|
} else {
|
|
|
|
decorationsList.Add(tile);
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 02:58:48 +00:00
|
|
|
tiles = tilesList.ToArray();
|
2020-02-20 17:25:46 +00:00
|
|
|
decorations = decorationsList.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-02-28 00:13:34 +00:00
|
|
|
CollisionTargets[i + 1] = new AABB(center, halfSize, tiles[i].Terrain);
|
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-03-03 18:04:29 +00:00
|
|
|
public void Update(float modelTime, History<Input> input) {
|
2020-03-06 17:28:58 +00:00
|
|
|
Player.Update(modelTime, this, input);
|
2020-02-28 22:08:34 +00:00
|
|
|
foreach (NPC npc in npcs) {
|
2020-03-06 17:28:58 +00:00
|
|
|
npc.Update(modelTime, this);
|
2020-02-28 22:08:34 +00:00
|
|
|
}
|
2020-03-03 18:08:51 +00:00
|
|
|
if (Player.Health <= 0) {
|
2020-03-03 18:04:29 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
2020-03-03 18:08:51 +00:00
|
|
|
Player = new Player();
|
2020-02-28 22:08:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 17:25:46 +00:00
|
|
|
public void DrawBackground(SpriteBatch spriteBatch) {
|
|
|
|
foreach (Tile t in decorations) {
|
|
|
|
t.Draw(spriteBatch);
|
|
|
|
}
|
2020-02-28 22:08:34 +00:00
|
|
|
foreach (NPC npc in npcs) {
|
|
|
|
npc.Draw(spriteBatch);
|
|
|
|
}
|
2020-02-20 17:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawForeground(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
|
|
|
}
|
|
|
|
}
|