Pyramid.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections.Generic;
  2. using FarseerPhysics.Collision.Shapes;
  3. using FarseerPhysics.Common;
  4. using FarseerPhysics.Dynamics;
  5. using FarseerPhysics.Factories;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. namespace FarseerPhysics.SamplesFramework
  9. {
  10. public class Pyramid
  11. {
  12. private Sprite _box;
  13. private List<Body> _boxes;
  14. private PhysicsGameScreen _screen;
  15. public Pyramid(World world, PhysicsGameScreen screen, Vector2 position, int count, float density)
  16. {
  17. Vertices rect = PolygonTools.CreateRectangle(0.5f, 0.5f);
  18. PolygonShape shape = new PolygonShape(rect, density);
  19. Vector2 rowStart = position;
  20. rowStart.Y -= 0.5f + count * 1.1f;
  21. Vector2 deltaRow = new Vector2(-0.625f, 1.1f);
  22. const float spacing = 1.25f;
  23. _boxes = new List<Body>();
  24. for (int i = 0; i < count; ++i)
  25. {
  26. Vector2 pos = rowStart;
  27. for (int j = 0; j < i + 1; ++j)
  28. {
  29. Body body = BodyFactory.CreateBody(world);
  30. body.BodyType = BodyType.Dynamic;
  31. body.Position = pos;
  32. body.CreateFixture(shape);
  33. _boxes.Add(body);
  34. pos.X += spacing;
  35. }
  36. rowStart += deltaRow;
  37. }
  38. _screen = screen;
  39. //GFX
  40. AssetCreator creator = _screen.ScreenManager.Assets;
  41. _box = new Sprite(creator.TextureFromVertices(rect, MaterialType.Dots, Color.SaddleBrown, 2f));
  42. }
  43. public void Draw()
  44. {
  45. SpriteBatch batch = _screen.ScreenManager.SpriteBatch;
  46. for (int i = 0; i < _boxes.Count; ++i)
  47. {
  48. batch.Draw(_box.Texture, ConvertUnits.ToDisplayUnits(_boxes[i].Position), null,
  49. Color.White, _boxes[i].Rotation, _box.Origin, 1f, SpriteEffects.None, 0f);
  50. }
  51. }
  52. }
  53. }