25_Urho2DParticle.as 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. Node@ particleNode;
  8. void Start()
  9. {
  10. // Execute the common startup for samples
  11. SampleStart();
  12. // Set mouse visible
  13. input.mouseVisible = true;
  14. // Create the scene content
  15. CreateScene();
  16. // Create the UI content
  17. CreateInstructions();
  18. // Setup the viewport for displaying the scene
  19. SetupViewport();
  20. // Set the mouse mode to use in the sample
  21. SampleInitMouseMode(MM_FREE);
  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.orthoSize = graphics.height * PIXEL_SIZE;
  41. camera.zoom = 1.2f * Min(graphics.width / 1280.0f, graphics.height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)
  42. ParticleEffect2D@ particleEffect = cache.GetResource("ParticleEffect2D", "Urho2D/sun.pex");
  43. if (particleEffect is null)
  44. return;
  45. particleNode = scene_.CreateChild("ParticleEmitter2D");
  46. ParticleEmitter2D@ particleEmitter = particleNode.CreateComponent("ParticleEmitter2D");
  47. particleEmitter.effect = particleEffect;
  48. ParticleEffect2D@ greenSpiralEffect = cache.GetResource("ParticleEffect2D", "Urho2D/greenspiral.pex");
  49. if (greenSpiralEffect is null)
  50. return;
  51. Node@ greenSpiralNode = scene_.CreateChild("GreenSpiral");
  52. ParticleEmitter2D@ greenSpiralEmitter = greenSpiralNode.CreateComponent("ParticleEmitter2D");
  53. greenSpiralEmitter.effect = greenSpiralEffect;
  54. }
  55. void CreateInstructions()
  56. {
  57. // Construct new Text object, set string to display and font to use
  58. Text@ instructionText = ui.root.CreateChild("Text");
  59. instructionText.text = "Use mouse to move the particle.";
  60. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  61. // Position the text relative to the screen center
  62. instructionText.horizontalAlignment = HA_CENTER;
  63. instructionText.verticalAlignment = VA_CENTER;
  64. instructionText.SetPosition(0, ui.root.height / 4);
  65. }
  66. void SetupViewport()
  67. {
  68. // 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
  69. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  70. // use, but now we just use full screen and default render path configured in the engine command line options
  71. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  72. renderer.viewports[0] = viewport;
  73. }
  74. void SubscribeToEvents()
  75. {
  76. // Subscribe HandleMouseMove() function for tracking mouse/touch move events
  77. SubscribeToEvent("MouseMove", "HandleMouseMove");
  78. if (touchEnabled)
  79. SubscribeToEvent("TouchMove", "HandleMouseMove");
  80. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  81. UnsubscribeFromEvent("SceneUpdate");
  82. }
  83. void HandleMouseMove(StringHash eventType, VariantMap& eventData)
  84. {
  85. if (particleNode !is null)
  86. {
  87. float x = eventData["X"].GetInt();
  88. float y = eventData["Y"].GetInt();
  89. Camera@ camera = cameraNode.GetComponent("Camera");
  90. particleNode.position = camera.ScreenToWorldPoint(Vector3(x / graphics.width, y / graphics.height, 10.0f));
  91. }
  92. }
  93. // Create XML patch instructions for screen joystick layout specific to this sample app
  94. String patchInstructions =
  95. "<patch>" +
  96. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  97. " <attribute name=\"Is Visible\" value=\"false\" />" +
  98. " </add>" +
  99. "</patch>";