33_Urho2DSpriterAnimation.as 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Urho2D sprite example.
  2. // This sample demonstrates:
  3. // - Creating a 2D scene with spriter animation
  4. // - Displaying the scene using the Renderer subsystem
  5. // - Handling keyboard to move and zoom 2D camera
  6. #include "Scripts/Utilities/Sample.as"
  7. Node@ spriteNode;
  8. int animationIndex = 0;
  9. Array<String> animationNames =
  10. {
  11. "idle",
  12. "run",
  13. "attack",
  14. "hit",
  15. "dead",
  16. "dead2",
  17. "dead3",
  18. };
  19. void Start()
  20. {
  21. // Execute the common startup for samples
  22. SampleStart();
  23. // Create the scene content
  24. CreateScene();
  25. // Create the UI content
  26. CreateInstructions();
  27. // Setup the viewport for displaying the scene
  28. SetupViewport();
  29. // Hook up to the frame update events
  30. SubscribeToEvents();
  31. }
  32. void CreateScene()
  33. {
  34. scene_ = Scene();
  35. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  36. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  37. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  38. // optimizing manner
  39. scene_.CreateComponent("Octree");
  40. // Create a scene node for the camera, which we will move around
  41. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  42. cameraNode = scene_.CreateChild("Camera");
  43. // Set an initial position for the camera scene node above the plane
  44. cameraNode.position = Vector3(0.0f, 0.0f, -10.0f);
  45. Camera@ camera = cameraNode.CreateComponent("Camera");
  46. camera.orthographic = true;
  47. camera.orthoSize = graphics.height * PIXEL_SIZE;
  48. AnimationSet2D@ animationSet = cache.GetResource("AnimationSet2D", "Urho2D/imp/imp.scml");
  49. if (animationSet is null)
  50. return;
  51. spriteNode = scene_.CreateChild("SpriterAnimation");
  52. spriteNode.position = Vector3(-1.4f, 2.0f, 0.0f);
  53. AnimatedSprite2D@ animatedSprite = spriteNode.CreateComponent("AnimatedSprite2D");
  54. animatedSprite.SetAnimation(animationSet, animationNames[animationIndex]);
  55. }
  56. void CreateInstructions()
  57. {
  58. // Construct new Text object, set string to display and font to use
  59. Text@ instructionText = ui.root.CreateChild("Text");
  60. instructionText.text = "Mouse click to play next animation, \nUse WASD keys to move, use PageUp PageDown keys to zoom.";
  61. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  62. // Position the text relative to the screen center
  63. instructionText.horizontalAlignment = HA_CENTER;
  64. instructionText.verticalAlignment = VA_CENTER;
  65. instructionText.SetPosition(0, ui.root.height / 4);
  66. }
  67. void SetupViewport()
  68. {
  69. // 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
  70. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  71. // use, but now we just use full screen and default render path configured in the engine command line options
  72. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  73. renderer.viewports[0] = viewport;
  74. }
  75. void MoveCamera(float timeStep)
  76. {
  77. // Do not move if the UI has a focused element (the console)
  78. if (ui.focusElement !is null)
  79. return;
  80. // Movement speed as world units per second
  81. const float MOVE_SPEED = 4.0f;
  82. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  83. if (input.keyDown['W'])
  84. cameraNode.Translate(Vector3(0.0f, 1.0f, 0.0f) * MOVE_SPEED * timeStep);
  85. if (input.keyDown['S'])
  86. cameraNode.Translate(Vector3(0.0f, -1.0f, 0.0f) * MOVE_SPEED * timeStep);
  87. if (input.keyDown['A'])
  88. cameraNode.Translate(Vector3(-1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  89. if (input.keyDown['D'])
  90. cameraNode.Translate(Vector3(1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  91. if (input.keyDown[KEY_PAGEUP])
  92. {
  93. Camera@ camera = cameraNode.GetComponent("Camera");
  94. camera.zoom = camera.zoom * 1.01f;
  95. }
  96. if (input.keyDown[KEY_PAGEDOWN])
  97. {
  98. Camera@ camera = cameraNode.GetComponent("Camera");
  99. camera.zoom = camera.zoom * 0.99f;
  100. }
  101. }
  102. void SubscribeToEvents()
  103. {
  104. // Subscribe HandleUpdate() function for processing update events
  105. SubscribeToEvent("Update", "HandleUpdate");
  106. SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown");
  107. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  108. UnsubscribeFromEvent("SceneUpdate");
  109. }
  110. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  111. {
  112. // Take the frame time step, which is stored as a float
  113. float timeStep = eventData["TimeStep"].GetFloat();
  114. // Move the camera, scale movement with time step
  115. MoveCamera(timeStep);
  116. }
  117. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  118. {
  119. AnimatedSprite2D@ animatedSprite = spriteNode.GetComponent("AnimatedSprite2D");
  120. animationIndex = (animationIndex + 1) % 7;
  121. animatedSprite.SetAnimation(animationNames[animationIndex], LM_FORCE_LOOPED);
  122. }
  123. // Create XML patch instructions for screen joystick layout specific to this sample app
  124. String patchInstructions =
  125. "<patch>" +
  126. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" +
  127. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" +
  128. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" +
  129. " <element type=\"Text\">" +
  130. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  131. " <attribute name=\"Text\" value=\"PAGEUP\" />" +
  132. " </element>" +
  133. " </add>" +
  134. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" +
  135. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" +
  136. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" +
  137. " <element type=\"Text\">" +
  138. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  139. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" +
  140. " </element>" +
  141. " </add>" +
  142. "</patch>";