25_Urho2DParticle.lua 4.6 KB

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