using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace KaitenTest
{
    /// <summary>
    /// 基底 Game クラスから派生した、ゲームのメイン クラスです。
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// ゲームの開始前に実行する必要がある初期化を実行できるようにします。
        /// ここで、要求されたサービスを問い合わせて、非グラフィック関連のコンテンツを読み込むことができます。
        /// base.Initialize を呼び出すと、任意のコンポーネントが列挙され、
        /// 初期化もされます。
        /// </summary>

        Vector2 origin;
        Vector2[] position, drawPosition;   //初期配置を憶えているpositionと描画の際、回転変換を適用したdrawPosition
        float rotation, rotationSpeed;
        Matrix rotationMatrix;
        Texture2D texture;
        
        protected override void Initialize()
        {
            // TODO: ここに初期化ロジックを追加します。
            position = new Vector2[3];
            drawPosition = new Vector2[3];
            rotation = 0;
            rotationSpeed = 0.2f;

            origin = new Vector2(150, 150);

            position[0] = new Vector2(0, 0);
            position[1] = new Vector2(-150, 0);
            position[2] = new Vector2(150, 0);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent はゲームごとに 1 回呼び出され、ここですべてのコンテンツを
        /// 読み込みます。
        /// </summary>
        protected override void LoadContent()
        {
            // 新規の SpriteBatch を作成します。これはテクスチャーの描画に使用できます。
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: this.Content クラスを使用して、ゲームのコンテンツを読み込みます。
            texture = Content.Load<Texture2D>("girl");
        }

        /// <summary>
        /// UnloadContent はゲームごとに 1 回呼び出され、ここですべてのコンテンツを
        /// アンロードします。
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: ここで ContentManager 以外のすべてのコンテンツをアンロードします。
        }

        /// <summary>
        /// ワールドの更新、衝突判定、入力値の取得、オーディオの再生などの
        /// ゲーム ロジックを、実行します。
        /// </summary>
        /// <param name="gameTime">ゲームの瞬間的なタイミング情報</param>
        GamePadState state;
        protected override void Update(GameTime gameTime)
        {
            state = GamePad.GetState(PlayerIndex.One);
            rotation += (state.ThumbSticks.Left.X * rotationSpeed) % MathHelper.TwoPi;  //回転値入力

            // ゲームの終了条件をチェックします。
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: ここにゲームのアップデート ロジックを追加します。
            drawPosition = (Vector2[])position.Clone();             //回転座標を初期値にリセット
            rotationMatrix = Matrix.CreateRotationZ(rotation);      //ここがこのコードのキモ

            for (int i = 0; i < drawPosition.Length; i++)
            {
                Vector2 rotatedVec = Vector2.Transform(drawPosition[i], rotationMatrix);    //トランスフォーム変換で行列座標の回転が出来る
                drawPosition[i] = rotatedVec + origin;  //位置補正
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// ゲームが自身を描画するためのメソッドです。
        /// </summary>
        /// <param name="gameTime">ゲームの瞬間的なタイミング情報</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: ここに描画コードを追加します。
            spriteBatch.Begin();
            for (int i = 0; i < drawPosition.Length; i++)
{
                spriteBatch.Draw(texture, drawPosition[i], Color.White);
                spriteBatch.Draw(texture, drawPosition[i] + new Vector2(400, 0), null, Color.White, rotation, new Vector2(texture.Width / 2, texture.Height / 2), 1, SpriteEffects.None, 0);
}
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
最終更新:2012年08月08日 12:48