2
0

SimpleDemo3.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Text;
  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. internal class SimpleDemo3 : PhysicsGameScreen, IDemoScreen
  9. {
  10. private Agent _agent;
  11. private Border _border;
  12. private Sprite _obstacle;
  13. private Body[] _obstacles = new Body[5];
  14. #region IDemoScreen Members
  15. public string GetTitle()
  16. {
  17. return "Multiple fixtures and static bodies";
  18. }
  19. public string GetDetails()
  20. {
  21. StringBuilder sb = new StringBuilder();
  22. sb.AppendLine("This demo shows a single body with multiple shapes attached.");
  23. sb.AppendLine(string.Empty);
  24. sb.AppendLine("This demo also shows the use of static bodies.");
  25. sb.AppendLine(string.Empty);
  26. sb.AppendLine("GamePad:");
  27. sb.AppendLine(" - Rotate agent: left and right triggers");
  28. sb.AppendLine(" - Move agent: right thumbstick");
  29. sb.AppendLine(" - Move cursor: left thumbstick");
  30. sb.AppendLine(" - Grab object (beneath cursor): A button");
  31. sb.AppendLine(" - Drag grabbed object: left thumbstick");
  32. sb.AppendLine(" - Exit to menu: Back button");
  33. sb.AppendLine(string.Empty);
  34. sb.AppendLine("Keyboard:");
  35. sb.AppendLine(" - Rotate agent: left and right arrows");
  36. sb.AppendLine(" - Move agent: A,S,D,W");
  37. sb.AppendLine(" - Exit to menu: Escape");
  38. sb.AppendLine(string.Empty);
  39. sb.AppendLine("Mouse / Touchscreen");
  40. sb.AppendLine(" - Grab object (beneath cursor): Left click");
  41. sb.AppendLine(" - Drag grabbed object: move mouse / finger");
  42. return sb.ToString();
  43. }
  44. #endregion
  45. public override void LoadContent()
  46. {
  47. base.LoadContent();
  48. World.Gravity = new Vector2(0f, 20f);
  49. _border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
  50. _agent = new Agent(World, this, new Vector2(-6.9f, -11f));
  51. LoadObstacles();
  52. SetUserAgent(_agent.Body, 1000f, 400f);
  53. }
  54. private void LoadObstacles()
  55. {
  56. for (int i = 0; i < 5; ++i)
  57. {
  58. _obstacles[i] = BodyFactory.CreateRectangle(World, 5f, 1f, 1f);
  59. _obstacles[i].IsStatic = true;
  60. _obstacles[i].Restitution = 0.2f;
  61. _obstacles[i].Friction = 0.2f;
  62. }
  63. _obstacles[0].Position = new Vector2(-5f, 9f);
  64. _obstacles[1].Position = new Vector2(15f, 6f);
  65. _obstacles[2].Position = new Vector2(10f, -3f);
  66. _obstacles[3].Position = new Vector2(-10f, -9f);
  67. _obstacles[4].Position = new Vector2(-17f, 0f);
  68. // create sprite based on body
  69. _obstacle = new Sprite(ScreenManager.Assets.TextureFromShape(_obstacles[0].FixtureList[0].Shape,
  70. MaterialType.Dots,
  71. Color.SandyBrown, 0.8f));
  72. }
  73. public override void Draw(GameTime gameTime)
  74. {
  75. ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
  76. for (int i = 0; i < 5; ++i)
  77. {
  78. ScreenManager.SpriteBatch.Draw(_obstacle.Texture, ConvertUnits.ToDisplayUnits(_obstacles[i].Position),
  79. null,
  80. Color.White, _obstacles[i].Rotation, _obstacle.Origin, 1f,
  81. SpriteEffects.None, 0f);
  82. }
  83. _agent.Draw();
  84. ScreenManager.SpriteBatch.End();
  85. _border.Draw();
  86. base.Draw(gameTime);
  87. }
  88. }
  89. }