sneak/Shared/World.cs
Colin McMillen 096f577e61 change namespace to SemiColinGames
GitOrigin-RevId: 3c4e116e770edfcca7f661b3f0d74bb312aa6a04
2020-02-13 14:48:35 -05:00

180 lines
8.2 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace SemiColinGames {
enum Terrain {
Empty,
Grass,
GrassL,
GrassR,
Rock,
RockL,
RockR,
Water,
Block
}
class Tile {
Texture2D texture;
Terrain terrain;
Rectangle position;
public Tile(Texture2D texture, Terrain terrain, Rectangle position) {
this.texture = texture;
this.terrain = terrain;
this.position = position;
}
public Rectangle Position { get { return position; } }
public Terrain Terrain { get { return terrain; } }
public void Draw(SpriteBatch spriteBatch, Camera camera) {
int size = World.TileSize;
Vector2 drawPos = new Vector2(position.Left - camera.Left, position.Top);
switch (terrain) {
case Terrain.Grass: {
// TODO: hold these rectangles statically instead of making them anew constantly.
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;
int width;
int height;
Tile[,] tiles;
public int Width { get; }
public int Height { get; }
string[] worldDesc = new string[] {
" ",
" ",
" ",
" ",
" ",
" X ",
" . ",
" X <======> <==X X <=> <XX> XX . ",
" XXX . ",
" XXXX . ",
" XXXXX . ",
" X <X=X> <> <> <X> = <> X X X X <> X X XX X <=X> XXXXXX . ",
" <> [] [] XX XX XXX XX XXXXXXX . ",
" <> [] [] [] XXX XXX XXXX XXX <> <> XXXXXXXX ",
" []12345678[]123456[]123456789[]1234567890 123456 123456 12345 1234 12345 1234 123XXXX XXXX1234XXXXX XXXX1234[]123 1234567[]XXXXXXXXX12345678 ",
"===========================..========..======..=========..=========> <=============> <==============================================================> <=======..==============..==============================",
"...................................................................] [.............] [..............................................................] [......................................................." };
public World(Texture2D texture) {
// TODO: better error handling for if the string[] isn't rectangular.
width = worldDesc[0].Length;
height = worldDesc.Length;
tiles = new Tile[width, height];
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
Terrain terrain;
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;
case ' ':
default:
terrain = Terrain.Empty;
break;
}
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
tiles[i, j] = new Tile(texture, terrain, position);
}
}
}
public void Draw(SpriteBatch spriteBatch, Camera camera) {
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
tiles[i, j].Draw(spriteBatch, camera);
}
}
}
public List<Rectangle> CollisionTargets() {
var result = new List<Rectangle>();
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
var t = tiles[i, j];
if (t.Terrain != Terrain.Empty) {
result.Add(t.Position);
}
}
}
return result;
}
}
}