PhysicsSpawner.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using AtomicEngine;
  3. public class PhysicsSpawner : CSComponent
  4. {
  5. public void Start()
  6. {
  7. viewport = AtomicNET.GetSubsystem<Renderer>().GetViewport(0);
  8. boxSprite = AtomicNET.Cache.GetResource<Sprite2D>("Sprites/Box.png");
  9. ballSprite = AtomicNET.Cache.GetResource<Sprite2D>("Sprites/Ball.png");
  10. var ground = Scene.CreateChild("Ground");
  11. ground.Position = new Vector3(0.0f, -5.0f, 0.0f);
  12. ground.Scale = new Vector3(200.0f, 1.0f, 0.0f);
  13. ground.CreateComponent<RigidBody2D>();
  14. ground.CreateComponent<StaticSprite2D>().Sprite = boxSprite;
  15. // Create box collider for ground
  16. var groundShape = ground.CreateComponent<CollisionBox2D>();
  17. // Set box size
  18. groundShape.Size = new Vector2(0.32f, 0.32f);
  19. // Set friction
  20. groundShape.Friction = 0.5f;
  21. }
  22. void Update(float timeStep)
  23. {
  24. if (spawnDelta > 0)
  25. {
  26. spawnDelta -= timeStep;
  27. return;
  28. }
  29. var input = AtomicNET.GetSubsystem<Input>();
  30. if (input.GetMouseButtonDown(Constants.MOUSEB_LEFT))
  31. {
  32. var mousePos = input.GetMousePosition();
  33. var screenPos = viewport.ScreenToWorldPoint(mousePos.X, mousePos.Y, 0);
  34. if ((lastSpawn - screenPos).Length < 0.35)
  35. return;
  36. lastSpawn = screenPos;
  37. spawnDelta = .025f;
  38. var node = Scene.CreateChild("RigidBody");
  39. node.SetPosition(new Vector3(screenPos.X, screenPos.Y, 0.0f));
  40. // Create rigid body
  41. var body = node.CreateComponent<RigidBody2D>();
  42. body.BodyType = BodyType2D.BT_DYNAMIC;
  43. var staticSprite = node.CreateComponent<StaticSprite2D>();
  44. if (spawnCount % 2 == 0)
  45. {
  46. staticSprite.Sprite = boxSprite;
  47. // Create box
  48. var box = node.CreateComponent<CollisionBox2D>();
  49. // Set size
  50. box.Size = new Vector2(0.32f, 0.32f);
  51. // Set density
  52. box.Density = 1.0f;
  53. // Set friction
  54. box.Friction = 0.5f;
  55. // Set restitution
  56. box.Restitution = 0.1f;
  57. }
  58. else
  59. {
  60. staticSprite.Sprite = ballSprite;
  61. // Create circle
  62. var circle = node.CreateComponent<CollisionCircle2D>();
  63. // Set radius
  64. circle.Radius = 0.16f;
  65. // Set density
  66. circle.Density = 1.0f;
  67. // Set friction.
  68. circle.Friction = 0.5f;
  69. // Set restitution
  70. circle.Restitution = 0.1f;
  71. }
  72. spawnCount++;
  73. }
  74. }
  75. float Random()
  76. {
  77. return (float)random.NextDouble();
  78. }
  79. Sprite2D boxSprite;
  80. Sprite2D ballSprite;
  81. Viewport viewport;
  82. float spawnDelta = 0;
  83. Vector3 lastSpawn = new Vector3(1000, 1000, 0);
  84. Random random = new Random();
  85. uint spawnCount = 0;
  86. }