25_Urho2DParticle.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. require "LuaScripts/Utilities/Sample"
  7. local particleNode = nil
  8. function Start()
  9. -- Execute the common startup for samples
  10. SampleStart()
  11. -- Set mouse visible
  12. input.mouseVisible = true
  13. -- Create the scene content
  14. CreateScene()
  15. -- Create the UI content
  16. CreateInstructions()
  17. -- Setup the viewport for displaying the scene
  18. SetupViewport()
  19. -- Hook up to the frame update events
  20. SubscribeToEvents()
  21. end
  22. function CreateScene()
  23. scene_ = Scene()
  24. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  25. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  26. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  27. -- optimizing manner
  28. scene_:CreateComponent("Octree")
  29. -- Create a scene node for the camera, which we will move around
  30. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  31. cameraNode = scene_:CreateChild("Camera")
  32. -- Set an initial position for the camera scene node above the plane
  33. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  34. local camera = cameraNode:CreateComponent("Camera")
  35. camera.orthographic = true
  36. camera.orthoSize = graphics.height * PIXEL_SIZE
  37. local particleEffect = cache:GetResource("ParticleEffect2D", "Urho2D/sun.pex")
  38. if particleEffect == nil then
  39. return
  40. end
  41. particleNode = scene_:CreateChild("ParticleEmitter2D")
  42. local particleEmitter = particleNode:CreateComponent("ParticleEmitter2D")
  43. particleEmitter.effect = particleEffect
  44. local greenSpiralEffect = cache:GetResource("ParticleEffect2D", "Urho2D/greenspiral.pex")
  45. if greenSpiralEffect == nil then
  46. return
  47. end
  48. greenSpiralNode = scene_:CreateChild("GreenSpiral")
  49. local greenSpiralEmitter = greenSpiralNode:CreateComponent("ParticleEmitter2D")
  50. greenSpiralEmitter.effect = greenSpiralEffect
  51. end
  52. function CreateInstructions()
  53. -- Construct new Text object, set string to display and font to use
  54. local instructionText = ui.root:CreateChild("Text")
  55. instructionText:SetText("Use mouse to move the particle.")
  56. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  57. -- Position the text relative to the screen center
  58. instructionText.horizontalAlignment = HA_CENTER
  59. instructionText.verticalAlignment = VA_CENTER
  60. instructionText:SetPosition(0, ui.root.height / 4)
  61. end
  62. function SetupViewport()
  63. -- 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
  64. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  65. -- use, but now we just use full screen and default render path configured in the engine command line options
  66. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  67. renderer:SetViewport(0, viewport)
  68. end
  69. function SubscribeToEvents()
  70. -- Subscribe HandleMouseMove() function for tracking mouse/touch move events
  71. SubscribeToEvent("MouseMove", "HandleMouseMove")
  72. if touchEnabled then SubscribeToEvent("TouchMove", "HandleMouseMove") end
  73. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  74. UnsubscribeFromEvent("SceneUpdate")
  75. end
  76. function HandleMouseMove(eventType, eventData)
  77. if particleNode ~= nil then
  78. local x = eventData:GetInt("x")
  79. local y = eventData:GetInt("y")
  80. local camera = cameraNode:GetComponent("Camera")
  81. particleNode.position = camera:ScreenToWorldPoint(Vector3(x / graphics.width, y / graphics.height, 10.0))
  82. end
  83. end
  84. -- Create XML patch instructions for screen joystick layout specific to this sample app
  85. function GetScreenJoystickPatchString()
  86. return
  87. "<patch>" ..
  88. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  89. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  90. " </add>" ..
  91. "</patch>"
  92. end