A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Jumpy {
  6. enum Terrain {
  7. Empty,
  8. Grass,
  9. Rock
  10. }
  11. class Tile {
  12. Texture2D texture;
  13. Terrain terrain;
  14. Rectangle position;
  15. public Tile(Texture2D texture, Terrain terrain, Rectangle position) {
  16. this.texture = texture;
  17. this.terrain = terrain;
  18. this.position = position;
  19. }
  20. public Rectangle Position { get { return position; } }
  21. public Terrain Terrain { get { return terrain; } }
  22. public void Draw(SpriteBatch spriteBatch) {
  23. int size = World.TileSize;
  24. switch (terrain) {
  25. case Terrain.Grass: {
  26. // TODO: hold these rectangles statically instead of making them anew constantly.
  27. Rectangle source = new Rectangle(3 * size, 0 * size, size, size);
  28. spriteBatch.Draw(texture, position, source, Color.White);
  29. break;
  30. }
  31. case Terrain.Rock: {
  32. Rectangle source = new Rectangle(3 * size, 1 * size, size, size);
  33. spriteBatch.Draw(texture, position, source, Color.White);
  34. break;
  35. }
  36. case Terrain.Empty:
  37. default:
  38. break;
  39. }
  40. }
  41. }
  42. class World {
  43. public const int TileSize = 16;
  44. int width;
  45. int height;
  46. Tile[,] tiles;
  47. public int Width { get; }
  48. public int Height { get; }
  49. public World(Texture2D texture) {
  50. width = Camera.Width / TileSize;
  51. height = Camera.Height / TileSize + 1;
  52. tiles = new Tile[width, height];
  53. for (int j = 0; j < height; j++) {
  54. for (int i = 0; i < width; i++) {
  55. Terrain terrain;
  56. if (j < height - 2) {
  57. terrain = Terrain.Empty;
  58. } else if (j == height - 2) {
  59. terrain = Terrain.Grass;
  60. } else {
  61. terrain = Terrain.Rock;
  62. }
  63. if (j == 6 && 11 < i && i < 16) {
  64. terrain = Terrain.Grass;
  65. }
  66. if (j == 3 && 15 < i && i < 19) {
  67. terrain = Terrain.Grass;
  68. }
  69. if (j == 7 && i == 5) {
  70. terrain = Terrain.Grass;
  71. }
  72. if (7 <= j && j <= 10 && i == 12) {
  73. terrain = Terrain.Rock;
  74. }
  75. var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
  76. tiles[i, j] = new Tile(texture, terrain, position);
  77. }
  78. }
  79. }
  80. public void Draw(SpriteBatch spriteBatch) {
  81. for (int j = 0; j < height; j++) {
  82. for (int i = 0; i < width; i++) {
  83. tiles[i, j].Draw(spriteBatch);
  84. }
  85. }
  86. }
  87. public List<Rectangle> CollisionTargets() {
  88. var result = new List<Rectangle>();
  89. for (int j = 0; j < height; j++) {
  90. for (int i = 0; i < width; i++) {
  91. var t = tiles[i, j];
  92. if (t.Terrain != Terrain.Empty) {
  93. result.Add(t.Position);
  94. }
  95. }
  96. }
  97. return result;
  98. }
  99. }
  100. }