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