25_Urho2DParticle.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. camera.zoom = 1.2 * Min(graphics.width / 1280, graphics.height / 800) -- Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)
  38. local particleEffect = cache:GetResource("ParticleEffect2D", "Urho2D/sun.pex")
  39. if particleEffect == nil then
  40. return
  41. end
  42. particleNode = scene_:CreateChild("ParticleEmitter2D")
  43. local particleEmitter = particleNode:CreateComponent("ParticleEmitter2D")
  44. particleEmitter.effect = particleEffect
  45. local greenSpiralEffect = cache:GetResource("ParticleEffect2D", "Urho2D/greenspiral.pex")
  46. if greenSpiralEffect == nil then
  47. return
  48. end
  49. greenSpiralNode = scene_:CreateChild("GreenSpiral")
  50. local greenSpiralEmitter = greenSpiralNode:CreateComponent("ParticleEmitter2D")
  51. greenSpiralEmitter.effect = greenSpiralEffect
  52. end
  53. function CreateInstructions()
  54. -- Construct new Text object, set string to display and font to use
  55. local instructionText = ui.root:CreateChild("Text")
  56. instructionText:SetText("Use mouse to move the particle.")
  57. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  58. -- Position the text relative to the screen center
  59. instructionText.horizontalAlignment = HA_CENTER
  60. instructionText.verticalAlignment = VA_CENTER
  61. instructionText:SetPosition(0, ui.root.height / 4)
  62. end
  63. function SetupViewport()
  64. -- 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
  65. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  66. -- use, but now we just use full screen and default render path configured in the engine command line options
  67. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  68. renderer:SetViewport(0, viewport)
  69. end
  70. function SubscribeToEvents()
  71. -- Subscribe HandleMouseMove() function for tracking mouse/touch move events
  72. SubscribeToEvent("MouseMove", "HandleMouseMove")
  73. if touchEnabled then SubscribeToEvent("TouchMove", "HandleMouseMove") end
  74. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  75. UnsubscribeFromEvent("SceneUpdate")
  76. end
  77. function HandleMouseMove(eventType, eventData)
  78. if particleNode ~= nil then
  79. local x = eventData:GetInt("x")
  80. local y = eventData:GetInt("y")
  81. local camera = cameraNode:GetComponent("Camera")
  82. particleNode.position = camera:ScreenToWorldPoint(Vector3(x / graphics.width, y / graphics.height, 10.0))
  83. end
  84. end
  85. -- Create XML patch instructions for screen joystick layout specific to this sample app
  86. function GetScreenJoystickPatchString()
  87. return
  88. "<patch>" ..
  89. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  90. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  91. " </add>" ..
  92. "</patch>"
  93. end