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.

92 lines
3.1 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace SemiColinGames {
  6. public sealed class TreeScene : IScene {
  7. const int MAX_SEGMENTS = 1000;
  8. const int MAX_VERTICES = MAX_SEGMENTS * 6; // 2 triangles per segment
  9. private readonly Color backgroundColor = Color.SkyBlue;
  10. private readonly GraphicsDevice graphics;
  11. private readonly BasicEffect basicEffect;
  12. private VertexPositionColor[] vertices;
  13. private VertexBuffer vertexBuffer;
  14. public TreeScene(GraphicsDevice graphics) {
  15. this.graphics = graphics;
  16. basicEffect = new BasicEffect(graphics) {
  17. World = Matrix.CreateTranslation(0, 0, 0),
  18. View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
  19. VertexColorEnabled = true,
  20. Projection = Matrix.CreateOrthographicOffCenter(-1920 / 2, 1920 / 2, -1080 / 4, 1080 * 3 / 4, -1, 1)
  21. };
  22. vertices = new VertexPositionColor[MAX_VERTICES];
  23. vertexBuffer = new VertexBuffer(
  24. graphics, typeof(VertexPositionColor), MAX_VERTICES, BufferUsage.WriteOnly);
  25. }
  26. ~TreeScene() {
  27. Dispose();
  28. }
  29. public void Dispose() {
  30. vertexBuffer.Dispose();
  31. GC.SuppressFinalize(this);
  32. }
  33. public struct Trapezoid {
  34. public Vector2 p1, p2, p3, p4;
  35. }
  36. public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
  37. var skeleton = new List<Vector2> {
  38. new Vector2(0, 0),
  39. new Vector2(0, 100),
  40. new Vector2(10, 200),
  41. new Vector2(30, 300),
  42. new Vector2(60, 400),
  43. new Vector2(100, 500),
  44. };
  45. graphics.Clear(backgroundColor);
  46. var segments = new List<Trapezoid>();
  47. for (int i = 0; i < skeleton.Count - 1; i++) {
  48. Vector2 low = skeleton[i];
  49. Vector2 high = skeleton[i + 1];
  50. Trapezoid t;
  51. int width = 10 - i;
  52. t.p1 = new Vector2(low.X - width, low.Y);
  53. t.p2 = new Vector2(low.X + width, low.Y);
  54. t.p3 = new Vector2(high.X - width + 4, high.Y);
  55. t.p4 = new Vector2(high.X + width - 4, high.Y);
  56. segments.Add(t);
  57. }
  58. Color color = Color.SaddleBrown;
  59. for (int i = 0; i < segments.Count; i++) {
  60. Trapezoid t = segments[i];
  61. vertices[i * 6] = new VertexPositionColor(new Vector3(t.p1.X, t.p1.Y, 0), color);
  62. vertices[i * 6 + 1] = new VertexPositionColor(new Vector3(t.p2.X, t.p2.Y, 0), color);
  63. vertices[i * 6 + 2] = new VertexPositionColor(new Vector3(t.p3.X, t.p3.Y, 0), color);
  64. vertices[i * 6 + 3] = new VertexPositionColor(new Vector3(t.p2.X, t.p2.Y, 0), color);
  65. vertices[i * 6 + 4] = new VertexPositionColor(new Vector3(t.p3.X, t.p3.Y, 0), color);
  66. vertices[i * 6 + 5] = new VertexPositionColor(new Vector3(t.p4.X, t.p4.Y, 0), color);
  67. }
  68. graphics.SetVertexBuffer(vertexBuffer);
  69. vertexBuffer.SetData(vertices);
  70. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) {
  71. pass.Apply();
  72. graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, segments.Count * 2);
  73. }
  74. }
  75. }
  76. }