瀏覽代碼

Adding 2D Sprite example

Josh Engebretson 9 年之前
父節點
當前提交
e5fb9badd2
共有 2 個文件被更改,包括 159 次插入0 次删除
  1. 154 0
      FeatureExamples/Resources/Scripts/24_2DSprite.cs
  2. 5 0
      FeatureExamples/Resources/Scripts/24_2DSprite.cs.asset

+ 154 - 0
FeatureExamples/Resources/Scripts/24_2DSprite.cs

@@ -0,0 +1,154 @@
+//
+// Copyright (c) 2008-2015 the Urho3D project.
+// Copyright (c) 2015 Xamarin Inc
+// Copyright (c) 2016 THUNDERBEAST GAMES LLC
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+using System.Collections.Generic;
+using AtomicEngine;
+
+namespace FeatureExamples
+{
+	public class SpriteSample : Sample
+	{
+		Scene scene;
+		List<NodeInfo> spriteNodes;
+		const uint NumSprites = 200;
+
+		public SpriteSample() : base() { }
+
+        public override void Start()
+        {
+			base.Start();
+			CreateScene();
+			SimpleCreateInstructionsWithWasd("\nuse PageUp PageDown keys to zoom.");
+			SetupViewport();
+		}
+
+        protected override void Update(float timeStep)
+        {
+			SimpleMoveCamera2D(timeStep);
+
+			var graphics = GetSubsystem<Graphics>();
+			float halfWidth = graphics.Width * 0.5f * PixelSize;
+			float halfHeight = graphics.Height * 0.5f * PixelSize;
+
+			foreach (var nodeInfo in spriteNodes)
+			{
+				Vector3 position = nodeInfo.Node.Position;
+				Vector3 moveSpeed = nodeInfo.MoveSpeed;
+				Vector3 newPosition = position + moveSpeed * timeStep;
+				if (newPosition.X < -halfWidth || newPosition.X > halfWidth)
+				{
+					newPosition.X = position.X;
+					moveSpeed.X = -moveSpeed.X;
+					nodeInfo.MoveSpeed = moveSpeed;
+				}
+				if (newPosition.Y < -halfHeight || newPosition.Y > halfHeight)
+				{
+					newPosition.Y = position.Y;
+					moveSpeed.Y = -moveSpeed.Y;
+					nodeInfo.MoveSpeed = moveSpeed;
+				}
+
+				nodeInfo.Node.Position = (newPosition);
+				nodeInfo.Node.Roll(nodeInfo.RotateSpeed * timeStep, TransformSpace.TS_LOCAL);
+			}
+		}
+
+		void SetupViewport()
+		{
+			var renderer = GetSubsystem<Renderer>();
+			renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
+		}
+
+		void CreateScene()
+		{
+			scene = new Scene();
+			scene.CreateComponent<Octree>();
+			spriteNodes = new List<NodeInfo>((int) NumSprites);
+
+			// Create camera node
+			CameraNode = scene.CreateChild("Camera");
+			// Set camera's position
+			CameraNode.Position = (new Vector3(0.0f, 0.0f, -10.0f));
+
+			Camera camera = CameraNode.CreateComponent<Camera>();
+			camera.Orthographic = true;
+
+
+			var graphics = GetSubsystem<Graphics>();
+			camera.OrthoSize=(float)graphics.Height * PixelSize;
+
+			var cache = GetSubsystem<ResourceCache>();
+			// Get sprite
+			Sprite2D sprite = cache.Get<Sprite2D>("Urho2D/Aster.png");
+			if (sprite == null)
+				return;
+
+			float halfWidth = graphics.Width * 0.5f * PixelSize;
+			float halfHeight = graphics.Height * 0.5f * PixelSize;
+
+			for (uint i = 0; i < NumSprites; ++i)
+			{
+				Node spriteNode = scene.CreateChild("StaticSprite2D");
+				spriteNode.Position = (new Vector3(NextRandom(-halfWidth, halfWidth), NextRandom(-halfHeight, halfHeight), 0.0f));
+
+				StaticSprite2D staticSprite = spriteNode.CreateComponent<StaticSprite2D>();
+				// Set random color
+				staticSprite.Color = (new Color(NextRandom(1.0f), NextRandom(1.0f), NextRandom(1.0f), 1.0f));
+				// Set blend mode
+				staticSprite.BlendMode = BlendMode.BLEND_ALPHA;
+				// Set sprite
+				staticSprite.Sprite=sprite;
+				// Add to sprite node vector
+				spriteNodes.Add(new NodeInfo(spriteNode, new Vector3(NextRandom(-2.0f, 2.0f), NextRandom(-2.0f, 2.0f), 0.0f), NextRandom(-90.0f, 90.0f)));
+			}
+
+			// Get animation set
+			AnimationSet2D animationSet = cache.Get<AnimationSet2D>("Urho2D/GoldIcon.scml");
+			if (animationSet == null)
+				return;
+
+			var spriteNode2 = scene.CreateChild("AnimatedSprite2D");
+			spriteNode2.Position = (new Vector3(0.0f, 0.0f, -1.0f));
+
+			AnimatedSprite2D animatedSprite = spriteNode2.CreateComponent<AnimatedSprite2D>();
+			// Set animation
+			animatedSprite.AnimationSet = animationSet;
+			animatedSprite.SetAnimation("idle", LoopMode2D.LM_DEFAULT);
+		}
+
+		class NodeInfo
+		{
+			public Node Node { get; set; }
+			public Vector3 MoveSpeed { get; set; }
+			public float RotateSpeed { get; set; }
+
+			public NodeInfo(Node node, Vector3 moveSpeed, float rotateSpeed)
+			{
+				Node = node;
+				MoveSpeed = moveSpeed;
+				RotateSpeed = rotateSpeed;
+			}
+		}
+	}
+}

+ 5 - 0
FeatureExamples/Resources/Scripts/24_2DSprite.cs.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "d22a1b9b65d04bf87925cc2c8d3c89d6",
+	"CSharpImporter": null
+}