PhysicsSpawner.cs 3.1 KB

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