強火で進め

このブログではプログラム関連の記事を中心に書いてます。

アステロイド(asteroid)タイプの自機の方向転換処理

PlayStation Suiteのフォーラムに上がっていたもの。

GameEngine's "RotateTo" action using Vector2? - PlayStation Forum
http://community.eu.playstation.com/t5/GameEngine2D/GameEngine-s-quot-RotateTo-quot-action-using-Vector2/td-p/15849249

この回答に上がっていたコードを実際に使ってサンプルを作ってみました。

自機の画像はこの様なものを使いました。左のアナログスティックで自機の方向転換を行えます。

【AppMain.cs】

using System;
using System.Collections.Generic;

using Sce.Pss.Core;
using Sce.Pss.Core.Environment;
using Sce.Pss.Core.Graphics;
using Sce.Pss.Core.Input;

using Sce.Pss.HighLevel.GameEngine2D;
using Sce.Pss.HighLevel.GameEngine2D.Base;

using Sce.Pss.Core.Imaging;

namespace HelloWorld
{
	public class AppMain
	{
		public static void Main (string[] args)
		{
			Director.Initialize();
			
			Scene scene = new Scene();
			scene.Camera.SetViewFromViewport();
			
			var width = Director.Instance.GL.Context.GetViewport().Width;
			var height = Director.Instance.GL.Context.GetViewport().Height;
			
			Texture2D texture = new Texture2D("/Application/Arrow.png", false);

			TextureInfo ti = new TextureInfo();
			ti.Texture = texture;
			
			SpriteUV sprite = new SpriteUV();
			sprite.TextureInfo = ti;
			
			sprite.Quad.S = ti.TextureSizef;
			sprite.CenterSprite();
			sprite.Position = scene.Camera.CalcBounds().Center;
			
			scene.AddChild(sprite);
			
			Director.Instance.RunWithScene(scene, true);
			
			bool gameOver = false;
			while(!gameOver)
			{
				Sce.Pss.HighLevel.GameEngine2D.Director.Instance.Update();
				
				if (Input2.GamePad0.AnalogLeft.Length() > 0.1f)
				    sprite.RotationNormalize = Input2.GamePad0.AnalogLeft.Xy;
				
				if (Input2.GamePad0.Select.Press == true)
					gameOver = true;
				
				Sce.Pss.HighLevel.GameEngine2D.Director.Instance.Render();
				Sce.Pss.HighLevel.GameEngine2D.Director.Instance.GL.Context.SwapBuffers();
				Sce.Pss.HighLevel.GameEngine2D.Director.Instance.PostSwap();
			}
			Director.Terminate();
		}
	}
}