24_2DSprite.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. // Copyright (c) 2016 THUNDERBEAST GAMES LLC
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. using System.Collections.Generic;
  25. using AtomicEngine;
  26. namespace FeatureExamples
  27. {
  28. public class SpriteSample : Sample
  29. {
  30. List<NodeInfo> spriteNodes;
  31. const uint NumSprites = 200;
  32. public SpriteSample() : base() { }
  33. public override void Start()
  34. {
  35. base.Start();
  36. CreateScene();
  37. SimpleCreateInstructionsWithWasd("\nuse PageUp PageDown keys to zoom.");
  38. SetupViewport();
  39. }
  40. protected override void Update(float timeStep)
  41. {
  42. SimpleMoveCamera2D(timeStep);
  43. var graphics = GetSubsystem<Graphics>();
  44. float halfWidth = graphics.Width * 0.5f * PixelSize;
  45. float halfHeight = graphics.Height * 0.5f * PixelSize;
  46. foreach (var nodeInfo in spriteNodes)
  47. {
  48. Vector3 position = nodeInfo.Node.Position;
  49. Vector3 moveSpeed = nodeInfo.MoveSpeed;
  50. Vector3 newPosition = position + moveSpeed * timeStep;
  51. if (newPosition.X < -halfWidth || newPosition.X > halfWidth)
  52. {
  53. newPosition.X = position.X;
  54. moveSpeed.X = -moveSpeed.X;
  55. nodeInfo.MoveSpeed = moveSpeed;
  56. }
  57. if (newPosition.Y < -halfHeight || newPosition.Y > halfHeight)
  58. {
  59. newPosition.Y = position.Y;
  60. moveSpeed.Y = -moveSpeed.Y;
  61. nodeInfo.MoveSpeed = moveSpeed;
  62. }
  63. nodeInfo.Node.Position = (newPosition);
  64. nodeInfo.Node.Roll(nodeInfo.RotateSpeed * timeStep, TransformSpace.TS_LOCAL);
  65. }
  66. }
  67. void SetupViewport()
  68. {
  69. var renderer = GetSubsystem<Renderer>();
  70. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
  71. }
  72. void CreateScene()
  73. {
  74. scene = new Scene();
  75. scene.CreateComponent<Octree>();
  76. spriteNodes = new List<NodeInfo>((int) NumSprites);
  77. // Create camera node
  78. CameraNode = scene.CreateChild("Camera");
  79. // Set camera's position
  80. CameraNode.Position = (new Vector3(0.0f, 0.0f, -10.0f));
  81. Camera camera = CameraNode.CreateComponent<Camera>();
  82. camera.Orthographic = true;
  83. var graphics = GetSubsystem<Graphics>();
  84. camera.OrthoSize=(float)graphics.Height * PixelSize;
  85. var cache = GetSubsystem<ResourceCache>();
  86. // Get sprite
  87. Sprite2D sprite = cache.Get<Sprite2D>("Urho2D/Aster.png");
  88. if (sprite == null)
  89. return;
  90. float halfWidth = graphics.Width * 0.5f * PixelSize;
  91. float halfHeight = graphics.Height * 0.5f * PixelSize;
  92. for (uint i = 0; i < NumSprites; ++i)
  93. {
  94. Node spriteNode = scene.CreateChild("StaticSprite2D");
  95. spriteNode.Position = (new Vector3(NextRandom(-halfWidth, halfWidth), NextRandom(-halfHeight, halfHeight), 0.0f));
  96. StaticSprite2D staticSprite = spriteNode.CreateComponent<StaticSprite2D>();
  97. // Set random color
  98. staticSprite.Color = (new Color(NextRandom(1.0f), NextRandom(1.0f), NextRandom(1.0f), 1.0f));
  99. // Set blend mode
  100. staticSprite.BlendMode = BlendMode.BLEND_ALPHA;
  101. // Set sprite
  102. staticSprite.Sprite=sprite;
  103. // Add to sprite node vector
  104. spriteNodes.Add(new NodeInfo(spriteNode, new Vector3(NextRandom(-2.0f, 2.0f), NextRandom(-2.0f, 2.0f), 0.0f), NextRandom(-90.0f, 90.0f)));
  105. }
  106. // Get animation set
  107. AnimationSet2D animationSet = cache.Get<AnimationSet2D>("Urho2D/GoldIcon.scml");
  108. if (animationSet == null)
  109. return;
  110. var spriteNode2 = scene.CreateChild("AnimatedSprite2D");
  111. spriteNode2.Position = (new Vector3(0.0f, 0.0f, -1.0f));
  112. AnimatedSprite2D animatedSprite = spriteNode2.CreateComponent<AnimatedSprite2D>();
  113. // Set animation
  114. animatedSprite.AnimationSet = animationSet;
  115. animatedSprite.SetAnimation("idle", LoopMode2D.LM_DEFAULT);
  116. }
  117. class NodeInfo
  118. {
  119. public Node Node { get; set; }
  120. public Vector3 MoveSpeed { get; set; }
  121. public float RotateSpeed { get; set; }
  122. public NodeInfo(Node node, Vector3 moveSpeed, float rotateSpeed)
  123. {
  124. Node = node;
  125. MoveSpeed = moveSpeed;
  126. RotateSpeed = rotateSpeed;
  127. }
  128. }
  129. }
  130. }