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

namespace WindowsGame1
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class fire : Microsoft.Xna.Framework.DrawableGameComponent
    {

        Texture2D my;
        public double scale = 1; // 圖形縮放倍數
        public byte alpha = 255; // 不透明度
        public Vector2 pos = new Vector2(0, 0);  // 精靈圖 的座標
        List<Point> ICON_List = new List<Point>(); // 紀錄小圖的位置
        short Current_No = -1;  // // 目前是第幾張小圖
        Point ICON_Size = new Point();   // 小圖的寬高
        double ElapsedTime = 0; // 用來更新小圖的顯示
        public double FrameSpeed = 10; // 小圖間隔時間 (預設值為 10 個千分秒)

        curvemove Path3D = new curvemove(CurveLoopType.Linear); // 建立一個Curve3D物件
        float ElapsedTime_forPos = 0; //累積經過的時間 用來更新 精靈圖 的位置

        public int aliveTime = 1000;  // 壽命
        public bool alive = true;
        
        public int Count  // 屬性 幾張小圖
        {
            get
            {
                return ICON_List.Count;
            }
        }

        public fire(Game game, Texture2D my, short col, short row, Vector2 pos1, Vector2 pos2)
            : base(game)
        {
            // TODO: Construct any child components here
            this.my = my;
            Point point = new Point();

            ICON_Size.X = my.Width / col;  // 每張小圖的寬
            ICON_Size.Y = my.Height / row;

            for (int i = 0; i < row; i++)  // i 往下走
                for (int j = 0; j < col; j++) // j 往右走
                {
                    point.X = j * ICON_Size.X;  // j 乘成上 小圖的寬
                    point.Y = i * ICON_Size.Y;  // i 乘成上 小圖的高
                    ICON_List.Add(point);
                }
            Current_No = 0;  // 目前是第幾張小圖

            Path3D.AddPoint(new Vector3(pos1.X, pos1.Y, 0), 0);    // 加入關鍵值
            Path3D.AddPoint(new Vector3(pos2.X, pos2.Y, 0), aliveTime); // 加入關鍵值

            Path3D.SetTangents(CurveTangent.Linear); // 內插值 計算的方式
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (alive == false)
            {
                Game.Components.Remove(this);  // 時間到了 自動移除
                return;
            }

            ElapsedTime += gameTime.ElapsedGameTime.TotalMilliseconds; // 兩個 Frame 間隔的千分秒
            if (ElapsedTime > FrameSpeed)
            {
                ElapsedTime = 0;
                Current_No++;
                if (Current_No >= ICON_List.Count) Current_No = 0;
            }

            ElapsedTime_forPos += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            Vector3 position = Path3D.GetPointOnCurve(ElapsedTime_forPos);
            pos = new Vector2(position.X, position.Y); // 將得到的 內插值 拿來使用 

            if (ElapsedTime_forPos > aliveTime)
            {
                alive = false;
                Game.Components.Remove(this);  // 時間到了 自動移除
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            if (spriteBatch == null) return;

            spriteBatch.Begin();

            Rectangle dest;
            dest.Width = (int)(ICON_Size.X * scale);
            dest.Height = (int)(ICON_Size.Y * scale);
            dest.X = (int)pos.X - dest.Width / 2;
            dest.Y = (int)pos.Y - dest.Height / 2;

            Rectangle src;
            src.Width = ICON_Size.X;
            src.Height = ICON_Size.Y;
            src.X = ICON_List[Current_No].X;
            src.Y = ICON_List[Current_No].Y;
            
            spriteBatch.Draw(my,   // 2D Texture
                             dest,   // 目的區的 矩形區塊
                             src,   // 來源區的 矩形區塊，null就是 全圖
                             new Color(255, 255, 255, alpha), // 顏色 濾鏡 + 不透明度
                             0,  // 旋轉徑度
                             Vector2.Zero, // 2D 紋理圖的旋轉中心點
                             SpriteEffects.None,  // 旋轉效果
                             1.0f); // 圖層深度 0.0 ~ 1.0 (後)

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}