24_2DSprite.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. Scene scene;
  31. List<NodeInfo> spriteNodes;
  32. const uint NumSprites = 200;
  33. public SpriteSample() : base() { }
  34. public override void Start()
  35. {
  36. base.Start();
  37. CreateScene();
  38. SimpleCreateInstructionsWithWasd("\nuse PageUp PageDown keys to zoom.");
  39. SetupViewport();
  40. }
  41. protected override void Update(float timeStep)
  42. {
  43. SimpleMoveCamera2D(timeStep);
  44. var graphics = GetSubsystem<Graphics>();
  45. float halfWidth = graphics.Width * 0.5f * PixelSize;
  46. float halfHeight = graphics.Height * 0.5f * PixelSize;
  47. foreach (var nodeInfo in spriteNodes)
  48. {
  49. Vector3 position = nodeInfo.Node.Position;
  50. Vector3 moveSpeed = nodeInfo.MoveSpeed;
  51. Vector3 newPosition = position + moveSpeed * timeStep;
  52. if (newPosition.X < -halfWidth || newPosition.X > halfWidth)
  53. {
  54. newPosition.X = position.X;
  55. moveSpeed.X = -moveSpeed.X;
  56. nodeInfo.MoveSpeed = moveSpeed;
  57. }
  58. if (newPosition.Y < -halfHeight || newPosition.Y > halfHeight)
  59. {
  60. newPosition.Y = position.Y;
  61. moveSpeed.Y = -moveSpeed.Y;
  62. nodeInfo.MoveSpeed = moveSpeed;
  63. }
  64. nodeInfo.Node.Position = (newPosition);
  65. nodeInfo.Node.Roll(nodeInfo.RotateSpeed * timeStep, TransformSpace.TS_LOCAL);
  66. }
  67. }
  68. void SetupViewport()
  69. {
  70. var renderer = GetSubsystem<Renderer>();
  71. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
  72. }
  73. void CreateScene()
  74. {
  75. scene = new Scene();
  76. scene.CreateComponent<Octree>();
  77. spriteNodes = new List<NodeInfo>((int) NumSprites);
  78. // Create camera node
  79. CameraNode = scene.CreateChild("Camera");
  80. // Set camera's position
  81. CameraNode.Position = (new Vector3(0.0f, 0.0f, -10.0f));
  82. Camera camera = CameraNode.CreateComponent<Camera>();
  83. camera.Orthographic = true;
  84. var graphics = GetSubsystem<Graphics>();
  85. camera.OrthoSize=(float)graphics.Height * PixelSize;
  86. var cache = GetSubsystem<ResourceCache>();
  87. // Get sprite
  88. Sprite2D sprite = cache.Get<Sprite2D>("Urho2D/Aster.png");
  89. if (sprite == null)
  90. return;
  91. float halfWidth = graphics.Width * 0.5f * PixelSize;
  92. float halfHeight = graphics.Height * 0.5f * PixelSize;
  93. for (uint i = 0; i < NumSprites; ++i)
  94. {
  95. Node spriteNode = scene.CreateChild("StaticSprite2D");
  96. spriteNode.Position = (new Vector3(NextRandom(-halfWidth, halfWidth), NextRandom(-halfHeight, halfHeight), 0.0f));
  97. StaticSprite2D staticSprite = spriteNode.CreateComponent<StaticSprite2D>();
  98. // Set random color
  99. staticSprite.Color = (new Color(NextRandom(1.0f), NextRandom(1.0f), NextRandom(1.0f), 1.0f));
  100. // Set blend mode
  101. staticSprite.BlendMode = BlendMode.BLEND_ALPHA;
  102. // Set sprite
  103. staticSprite.Sprite=sprite;
  104. // Add to sprite node vector
  105. spriteNodes.Add(new NodeInfo(spriteNode, new Vector3(NextRandom(-2.0f, 2.0f), NextRandom(-2.0f, 2.0f), 0.0f), NextRandom(-90.0f, 90.0f)));
  106. }
  107. // Get animation set
  108. AnimationSet2D animationSet = cache.Get<AnimationSet2D>("Urho2D/GoldIcon.scml");
  109. if (animationSet == null)
  110. return;
  111. var spriteNode2 = scene.CreateChild("AnimatedSprite2D");
  112. spriteNode2.Position = (new Vector3(0.0f, 0.0f, -1.0f));
  113. AnimatedSprite2D animatedSprite = spriteNode2.CreateComponent<AnimatedSprite2D>();
  114. // Set animation
  115. animatedSprite.AnimationSet = animationSet;
  116. animatedSprite.SetAnimation("idle", LoopMode2D.LM_DEFAULT);
  117. }
  118. class NodeInfo
  119. {
  120. public Node Node { get; set; }
  121. public Vector3 MoveSpeed { get; set; }
  122. public float RotateSpeed { get; set; }
  123. public NodeInfo(Node node, Vector3 moveSpeed, float rotateSpeed)
  124. {
  125. Node = node;
  126. MoveSpeed = moveSpeed;
  127. RotateSpeed = rotateSpeed;
  128. }
  129. }
  130. }
  131. }