Border.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using FarseerPhysics.Common;
  2. using FarseerPhysics.Dynamics;
  3. using FarseerPhysics.Factories;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. namespace FarseerPhysics.SamplesFramework
  7. {
  8. public class Border
  9. {
  10. private Body _anchor;
  11. private BasicEffect _basicEffect;
  12. private VertexPositionColorTexture[] _borderVerts;
  13. private PhysicsGameScreen _screen;
  14. private World _world;
  15. public Border(World world, PhysicsGameScreen screen, Viewport viewport)
  16. {
  17. _world = world;
  18. _screen = screen;
  19. float halfWidth = ConvertUnits.ToSimUnits(viewport.Width) / 2f - 0.75f;
  20. float halfHeight = ConvertUnits.ToSimUnits(viewport.Height) / 2f - 0.75f;
  21. Vertices borders = new Vertices(4);
  22. borders.Add(new Vector2(-halfWidth, halfHeight));
  23. borders.Add(new Vector2(halfWidth, halfHeight));
  24. borders.Add(new Vector2(halfWidth, -halfHeight));
  25. borders.Add(new Vector2(-halfWidth, -halfHeight));
  26. _anchor = BodyFactory.CreateLoopShape(_world, borders);
  27. _anchor.CollisionCategories = Category.All;
  28. _anchor.CollidesWith = Category.All;
  29. _basicEffect = new BasicEffect(_screen.ScreenManager.GraphicsDevice);
  30. _basicEffect.VertexColorEnabled = true;
  31. _basicEffect.TextureEnabled = true;
  32. _basicEffect.Texture = _screen.ScreenManager.Content.Load<Texture2D>("Materials/pavement");
  33. VertexPositionColorTexture[] vertice = new VertexPositionColorTexture[8];
  34. vertice[0] = new VertexPositionColorTexture(new Vector3(-halfWidth, -halfHeight, 0f),
  35. Color.LightGray, new Vector2(-halfWidth, -halfHeight) / 5.25f);
  36. vertice[1] = new VertexPositionColorTexture(new Vector3(halfWidth, -halfHeight, 0f),
  37. Color.LightGray, new Vector2(halfWidth, -halfHeight) / 5.25f);
  38. vertice[2] = new VertexPositionColorTexture(new Vector3(halfWidth, halfHeight, 0f),
  39. Color.LightGray, new Vector2(halfWidth, halfHeight) / 5.25f);
  40. vertice[3] = new VertexPositionColorTexture(new Vector3(-halfWidth, halfHeight, 0f),
  41. Color.LightGray, new Vector2(-halfWidth, halfHeight) / 5.25f);
  42. vertice[4] = new VertexPositionColorTexture(new Vector3(-halfWidth - 2f, -halfHeight - 2f, 0f),
  43. Color.LightGray,
  44. new Vector2(-halfWidth - 2f, -halfHeight - 2f) / 5.25f);
  45. vertice[5] = new VertexPositionColorTexture(new Vector3(halfWidth + 2f, -halfHeight - 2f, 0f),
  46. Color.LightGray,
  47. new Vector2(halfWidth + 2f, -halfHeight - 2f) / 5.25f);
  48. vertice[6] = new VertexPositionColorTexture(new Vector3(halfWidth + 2f, halfHeight + 2f, 0f),
  49. Color.LightGray,
  50. new Vector2(halfWidth + 2f, halfHeight + 2f) / 5.25f);
  51. vertice[7] = new VertexPositionColorTexture(new Vector3(-halfWidth - 2f, halfHeight + 2f, 0f),
  52. Color.LightGray,
  53. new Vector2(-halfWidth - 2f, halfHeight + 2f) / 5.25f);
  54. _borderVerts = new VertexPositionColorTexture[24];
  55. _borderVerts[0] = vertice[0];
  56. _borderVerts[1] = vertice[5];
  57. _borderVerts[2] = vertice[4];
  58. _borderVerts[3] = vertice[0];
  59. _borderVerts[4] = vertice[1];
  60. _borderVerts[5] = vertice[5];
  61. _borderVerts[6] = vertice[1];
  62. _borderVerts[7] = vertice[6];
  63. _borderVerts[8] = vertice[5];
  64. _borderVerts[9] = vertice[1];
  65. _borderVerts[10] = vertice[2];
  66. _borderVerts[11] = vertice[6];
  67. _borderVerts[12] = vertice[2];
  68. _borderVerts[13] = vertice[7];
  69. _borderVerts[14] = vertice[6];
  70. _borderVerts[15] = vertice[2];
  71. _borderVerts[16] = vertice[3];
  72. _borderVerts[17] = vertice[7];
  73. _borderVerts[18] = vertice[3];
  74. _borderVerts[19] = vertice[4];
  75. _borderVerts[20] = vertice[7];
  76. _borderVerts[21] = vertice[3];
  77. _borderVerts[22] = vertice[0];
  78. _borderVerts[23] = vertice[4];
  79. }
  80. public void Draw()
  81. {
  82. GraphicsDevice device = _screen.ScreenManager.GraphicsDevice;
  83. LineBatch batch = _screen.ScreenManager.LineBatch;
  84. device.SamplerStates[0] = SamplerState.AnisotropicWrap;
  85. device.RasterizerState = RasterizerState.CullNone;
  86. _basicEffect.Projection = _screen.Camera.SimProjection;
  87. _basicEffect.View = _screen.Camera.SimView;
  88. _basicEffect.CurrentTechnique.Passes[0].Apply();
  89. device.DrawUserPrimitives(PrimitiveType.TriangleList, _borderVerts, 0, 8);
  90. batch.Begin(_screen.Camera.SimProjection, _screen.Camera.SimView);
  91. batch.DrawLineShape(_anchor.FixtureList[0].Shape);
  92. batch.End();
  93. }
  94. }
  95. }