25_Urho2DParticle.as 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Urho2D particle example.
  2. // This sample demonstrates:
  3. // - Creating a 2D scene with particle
  4. // - Displaying the scene using the Renderer subsystem
  5. // - Handling mouse move to move particle
  6. #include "Scripts/Utilities/Sample.as"
  7. Scene@ scene_;
  8. Node@ cameraNode;
  9. Node@ particleNode;
  10. void Start()
  11. {
  12. // Execute the common startup for samples
  13. SampleStart();
  14. // Set mouse visible
  15. input.mouseVisible = true;
  16. // Create the scene content
  17. CreateScene();
  18. // Create the UI content
  19. CreateInstructions();
  20. // Setup the viewport for displaying the scene
  21. SetupViewport();
  22. // Hook up to the frame update events
  23. SubscribeToEvents();
  24. }
  25. void CreateScene()
  26. {
  27. scene_ = Scene();
  28. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  29. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  30. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  31. // optimizing manner
  32. scene_.CreateComponent("Octree");
  33. // Create a scene node for the camera, which we will move around
  34. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  35. cameraNode = scene_.CreateChild("Camera");
  36. // Set an initial position for the camera scene node above the plane
  37. cameraNode.position = Vector3(0.0f, 0.0f, -10.0f);
  38. Camera@ camera = cameraNode.CreateComponent("Camera");
  39. camera.orthographic = true;
  40. camera.SetOrthoSize(Vector2(graphics.width, graphics.height));
  41. ParticleModel2D@ particleModel = cache.GetResource("ParticleModel2D", "Urho2D/LavaFlow.plist");
  42. if (particleModel is null)
  43. return;
  44. particleNode = scene_.CreateChild("ParticleEmitter2D");
  45. ParticleEmitter2D@ particleEmitter = particleNode.CreateComponent("ParticleEmitter2D");
  46. particleEmitter.model = particleModel;
  47. }
  48. void CreateInstructions()
  49. {
  50. // Construct new Text object, set string to display and font to use
  51. Text@ instructionText = ui.root.CreateChild("Text");
  52. instructionText.text = "Use mouse to move the particle.";
  53. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  54. // Position the text relative to the screen center
  55. instructionText.horizontalAlignment = HA_CENTER;
  56. instructionText.verticalAlignment = VA_CENTER;
  57. instructionText.SetPosition(0, ui.root.height / 4);
  58. }
  59. void SetupViewport()
  60. {
  61. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  62. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  63. // use, but now we just use full screen and default render path configured in the engine command line options
  64. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  65. renderer.viewports[0] = viewport;
  66. }
  67. void SubscribeToEvents()
  68. {
  69. SubscribeToEvent("MouseMove", "HandleMouseMove");
  70. }
  71. void HandleMouseMove(StringHash eventType, VariantMap& eventData)
  72. {
  73. if (particleNode !is null)
  74. {
  75. int x = eventData["x"].GetInt();
  76. int y = eventData["y"].GetInt();
  77. particleNode.position = Vector3(x - graphics.width * 0.5f, -y + graphics.height * 0.5f, 0.0);
  78. }
  79. }