33_Urho2DSpriterAnimation.as 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. camera.zoom = 1.5f * Min(graphics.width / 1280.0f, graphics.height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.5) is set for full visibility at 1280x800 resolution)
  49. AnimationSet2D@ animationSet = cache.GetResource("AnimationSet2D", "Urho2D/imp/imp.scml");
  50. if (animationSet is null)
  51. return;
  52. spriteNode = scene_.CreateChild("SpriterAnimation");
  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. instructionText.textAlignment = HA_CENTER; // Center rows in relation to each other
  63. // Position the text relative to the screen center
  64. instructionText.horizontalAlignment = HA_CENTER;
  65. instructionText.verticalAlignment = VA_CENTER;
  66. instructionText.SetPosition(0, ui.root.height / 4);
  67. }
  68. void SetupViewport()
  69. {
  70. // 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
  71. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  72. // use, but now we just use full screen and default render path configured in the engine command line options
  73. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  74. renderer.viewports[0] = viewport;
  75. }
  76. void MoveCamera(float timeStep)
  77. {
  78. // Do not move if the UI has a focused element (the console)
  79. if (ui.focusElement !is null)
  80. return;
  81. // Movement speed as world units per second
  82. const float MOVE_SPEED = 4.0f;
  83. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  84. if (input.keyDown['W'])
  85. cameraNode.Translate(Vector3(0.0f, 1.0f, 0.0f) * MOVE_SPEED * timeStep);
  86. if (input.keyDown['S'])
  87. cameraNode.Translate(Vector3(0.0f, -1.0f, 0.0f) * MOVE_SPEED * timeStep);
  88. if (input.keyDown['A'])
  89. cameraNode.Translate(Vector3(-1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  90. if (input.keyDown['D'])
  91. cameraNode.Translate(Vector3(1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
  92. if (input.keyDown[KEY_PAGEUP])
  93. {
  94. Camera@ camera = cameraNode.GetComponent("Camera");
  95. camera.zoom = camera.zoom * 1.01f;
  96. }
  97. if (input.keyDown[KEY_PAGEDOWN])
  98. {
  99. Camera@ camera = cameraNode.GetComponent("Camera");
  100. camera.zoom = camera.zoom * 0.99f;
  101. }
  102. }
  103. void SubscribeToEvents()
  104. {
  105. // Subscribe HandleUpdate() function for processing update events
  106. SubscribeToEvent("Update", "HandleUpdate");
  107. SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown");
  108. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  109. UnsubscribeFromEvent("SceneUpdate");
  110. }
  111. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  112. {
  113. // Take the frame time step, which is stored as a float
  114. float timeStep = eventData["TimeStep"].GetFloat();
  115. // Move the camera, scale movement with time step
  116. MoveCamera(timeStep);
  117. }
  118. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  119. {
  120. AnimatedSprite2D@ animatedSprite = spriteNode.GetComponent("AnimatedSprite2D");
  121. animationIndex = (animationIndex + 1) % 7;
  122. animatedSprite.SetAnimation(animationNames[animationIndex], LM_FORCE_LOOPED);
  123. }
  124. // Create XML patch instructions for screen joystick layout specific to this sample app
  125. String patchInstructions =
  126. "<patch>" +
  127. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" +
  128. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" +
  129. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" +
  130. " <element type=\"Text\">" +
  131. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  132. " <attribute name=\"Text\" value=\"PAGEUP\" />" +
  133. " </element>" +
  134. " </add>" +
  135. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" +
  136. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" +
  137. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" +
  138. " <element type=\"Text\">" +
  139. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  140. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" +
  141. " </element>" +
  142. " </add>" +
  143. "</patch>";