using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Sowrd : WManager
{
    //武器を持つ位置
    public Vector3 attachPosition = new Vector3( 0f, 0.43f, 0f);
    public Vector3 attachAngles = new Vector3( 0f, 0f, 0f);
    public Vector3 attachScale = new Vector3( 1f, 1f, 1f);

    Player player;
    Animator animator;
    Time60 time60;
    Vector2 offset;
    Vector3 effect_pos;
    Vector3 player_pos;

    int effectNum = 0;
    int WaitTime = 0;
    int attackLevel = 0;

    //これらはインスペクターで指定する
    [SerializeField]
    private GameObject[] effects;

    // Start is called before the first frame update
    void Start()
    {
        //プレイヤーオブジェクト
        GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
        player = playerObj.GetComponentInParent<Player>();
        offset = new Vector2( player.offset.x, player.offset.y);  //エフェクトを出す基準位置
        animator = playerObj.GetComponent<Animator>();            //アニメーターコンポーネント

        //ゲームオブジェクト管理マネージャ
        GManager  gManager =  GameObject.FindObjectsOfType<GManager>()[0];
        time60 = gManager.GetComponent<Time60>();
    }

    protected override void Update()
    {
        //処理時間の調整
        if( time60.CheckFlame() != true ) return;

        if( WaitTime > 0){
            WaitTime--;
        }else{
            attackLevel = 0;
        }

        //攻撃フラグを取得する
        if( player.SetAttackFlag() != false){
            //入力受付時間かつ初撃~2段目であるか
            if( (WaitTime < 15) && (attackLevel < 3)){
                //攻撃段階に応じてプレイヤーへアニメーションの指示を出す。
                switch(attackLevel){
                    case 0:    
                        animator.Play( "Slash1", 0, 0f);
                        attackLevel++;
                    break;
                    case 1:
                        animator.Play( "Slash2", 0, 0f);
                        attackLevel++;
                    break;
                    case 2:
                            animator.Play( "Slash3", 0, 0f);
                        attackLevel++;
                    break;
                    default:
                    break;
                }

                //エフェクトを出す (現段階は3つ揃ってないので全て初撃のエフェクト)
                //新しく出すエフェクトをインスタンス化
                var effectObj = Instantiate<GameObject>(effects[effectNum]);
                var effect = effectObj.GetComponent<EffSlash0>();
                
                player_pos = player.transform.position;      
                effect_pos = new Vector3( player_pos.x + offset.x + 0.3f * player.flip,
                                             player_pos.y + offset.y + 0.3f, player_pos.z);
                effect.transform.position = new Vector3( effect_pos.x, effect_pos.y, player_pos.z);
                effect.transform.localScale = new Vector3( 2.0f *player.flip, 2.0f, 2.0f);

                WaitTime = 30;
            }
        }
    
    }

}