2019-12-11 21:57:30 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2019-12-11 22:23:30 +00:00
|
|
|
|
using System;
|
2019-12-11 21:57:30 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Jumpy {
|
|
|
|
|
enum Terrain {
|
|
|
|
|
Empty,
|
|
|
|
|
Grass,
|
2019-12-17 13:27:59 +00:00
|
|
|
|
GrassL,
|
|
|
|
|
GrassR,
|
|
|
|
|
Rock,
|
2019-12-17 22:21:14 +00:00
|
|
|
|
RockL,
|
|
|
|
|
RockR,
|
|
|
|
|
Water,
|
|
|
|
|
Block
|
2019-12-11 21:57:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 22:23:30 +00:00
|
|
|
|
public Rectangle Position { get { return position; } }
|
|
|
|
|
public Terrain Terrain { get { return terrain; } }
|
2019-12-11 21:57:30 +00:00
|
|
|
|
|
2019-12-17 20:37:32 +00:00
|
|
|
|
public void Draw(SpriteBatch spriteBatch, Camera camera) {
|
2019-12-11 21:57:30 +00:00
|
|
|
|
int size = World.TileSize;
|
2019-12-17 20:37:32 +00:00
|
|
|
|
Vector2 drawPos = new Vector2(position.Left - camera.Left, position.Top);
|
2019-12-11 21:57:30 +00:00
|
|
|
|
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);
|
2019-12-17 20:37:32 +00:00
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
2019-12-11 21:57:30 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-17 13:27:59 +00:00
|
|
|
|
case Terrain.GrassL: {
|
2019-12-17 22:21:14 +00:00
|
|
|
|
Rectangle source = new Rectangle(2 * size, 0 * size, size, size);
|
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-17 13:27:59 +00:00
|
|
|
|
case Terrain.GrassR: {
|
2019-12-17 22:21:14 +00:00
|
|
|
|
Rectangle source = new Rectangle(4 * size, 0 * size, size, size);
|
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-11 21:57:30 +00:00
|
|
|
|
case Terrain.Rock: {
|
|
|
|
|
Rectangle source = new Rectangle(3 * size, 1 * size, size, size);
|
2019-12-17 20:37:32 +00:00
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
2019-12-11 21:57:30 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-17 22:21:14 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-12-17 13:27:59 +00:00
|
|
|
|
case Terrain.Water: {
|
|
|
|
|
Rectangle source = new Rectangle(9 * size, 2 * size, size, size);
|
2019-12-17 20:37:32 +00:00
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
2019-12-17 13:27:59 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-17 22:21:14 +00:00
|
|
|
|
case Terrain.Block: {
|
|
|
|
|
Rectangle source = new Rectangle(6 * size, 3 * size, size, size);
|
|
|
|
|
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-11 21:57:30 +00:00
|
|
|
|
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; }
|
|
|
|
|
|
2019-12-17 13:27:59 +00:00
|
|
|
|
string[] worldDesc = new string[] {
|
2019-12-17 22:53:38 +00:00
|
|
|
|
" ",
|
|
|
|
|
" ",
|
|
|
|
|
" ",
|
|
|
|
|
" ",
|
|
|
|
|
" ",
|
2019-12-17 22:21:14 +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 . ",
|
|
|
|
|
" <> [] [] 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 ",
|
|
|
|
|
"===========================..========..======..=========..=========> <=============> <==============================================================> <=======..==============..==============================",
|
|
|
|
|
"...................................................................] [.............] [..............................................................] [......................................................." };
|
2019-12-17 13:27:59 +00:00
|
|
|
|
|
2019-12-11 21:57:30 +00:00
|
|
|
|
public World(Texture2D texture) {
|
2019-12-17 13:27:59 +00:00
|
|
|
|
// TODO: better error handling for if the string[] isn't rectangular.
|
|
|
|
|
width = worldDesc[0].Length;
|
|
|
|
|
height = worldDesc.Length;
|
2019-12-11 21:57:30 +00:00
|
|
|
|
tiles = new Tile[width, height];
|
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
|
Terrain terrain;
|
2019-12-17 13:27:59 +00:00
|
|
|
|
switch (worldDesc[j][i]) {
|
|
|
|
|
case '=':
|
|
|
|
|
terrain = Terrain.Grass;
|
|
|
|
|
break;
|
|
|
|
|
case '<':
|
|
|
|
|
terrain = Terrain.GrassL;
|
|
|
|
|
break;
|
|
|
|
|
case '>':
|
|
|
|
|
terrain = Terrain.GrassR;
|
|
|
|
|
break;
|
|
|
|
|
case '.':
|
|
|
|
|
terrain = Terrain.Rock;
|
|
|
|
|
break;
|
2019-12-17 22:21:14 +00:00
|
|
|
|
case '[':
|
|
|
|
|
terrain = Terrain.RockL;
|
|
|
|
|
break;
|
|
|
|
|
case ']':
|
|
|
|
|
terrain = Terrain.RockR;
|
|
|
|
|
break;
|
2019-12-17 13:27:59 +00:00
|
|
|
|
case '~':
|
|
|
|
|
terrain = Terrain.Water;
|
|
|
|
|
break;
|
2019-12-17 22:21:14 +00:00
|
|
|
|
case 'X':
|
|
|
|
|
terrain = Terrain.Block;
|
|
|
|
|
break;
|
2019-12-17 13:27:59 +00:00
|
|
|
|
case ' ':
|
|
|
|
|
default:
|
|
|
|
|
terrain = Terrain.Empty;
|
|
|
|
|
break;
|
2019-12-16 02:19:39 +00:00
|
|
|
|
}
|
2019-12-11 21:57:30 +00:00
|
|
|
|
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
|
|
|
|
|
tiles[i, j] = new Tile(texture, terrain, position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 20:37:32 +00:00
|
|
|
|
public void Draw(SpriteBatch spriteBatch, Camera camera) {
|
2019-12-11 21:57:30 +00:00
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
2019-12-17 20:37:32 +00:00
|
|
|
|
tiles[i, j].Draw(spriteBatch, camera);
|
2019-12-11 21:57:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-11 22:23:30 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2019-12-11 21:57:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|