using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AX.MessageSystem;
using System;

public enum WindDirection
{
    East = 4,
    East_South = -3,
    South = -2,
    West_South = -1,
    West = 0,
    West_North = 1,
    North = 2,
    East_North = 3
}

public class WindDirectionSet : MonoBehaviour
{
    public bool IsFire = true;
    public ParticleSystem smoke;
    private Transform wind;
    private WindZone windZone;
    private float rotation_Y;
    public Vector3 dirVec;//风向
    // Use this for initialization
    void Awake()
    {
        if (smoke == null)
            smoke = transform.Find("Fire_Smoke_LG").GetComponent<ParticleSystem>();
        wind = GameObject.Find("WindZone").transform;
        windZone = wind.GetComponent<WindZone>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnEnable()
    {
        MessageDispatcher.AddListener("SET_SMOKE_DIRECTION", SmokeDirectionSet);
        MessageDispatcher.SendMessage("SET_SMOKE_DIRECTION");
    }

    private void OnDisable()
    {
        MessageDispatcher.RemoveListener("SET_SMOKE_DIRECTION", SmokeDirectionSet);
    }

    private void SmokeDirectionSet(IMessage obj)
    {
        rotation_Y = ClampAngle(Mathf.Ceil(wind.rotation.eulerAngles.y));
        int dir = (int)(rotation_Y / 45);
        dirVec = WindDir(dir);

        float windForce = windZone.windMain;

        var emission = smoke.emission;
        var rate = emission.rateOverTime;
        if (windForce == 0)
        {
            rate.constant = 1;
        }
        else
        {
            rate.constant = windForce;
        }
        if (IsFire)
            emission.rateOverTime = rate;
        else
            emission.rateOverTime = 50 - rate.constant;
        var force = smoke.forceOverLifetime;
        force.x = dirVec.x * windForce;
        force.y = dirVec.y * windForce;
        force.z = dirVec.z * windForce;
    }

    private Vector3 WindDir(int dir)
    {
        Vector3 dirVec = new Vector3();
        if (dir == (int)WindDirection.East)
        {
            dirVec = new Vector3(0, 0, 1);
        }
        else if (dir == (int)WindDirection.West)
        {
            dirVec = new Vector3(0, 0, -1);
        }
        else if (dir == (int)WindDirection.South)
        {
            dirVec = new Vector3(1, 0, 0);
        }
        else if (dir == (int)WindDirection.North)
        {
            dirVec = new Vector3(-1, 0, 0);
        }
        else if (dir == (int)WindDirection.East_South)
        {
            dirVec = new Vector3(1, 0, 1);
        }
        else if (dir == (int)WindDirection.East_North)
        {
            dirVec = new Vector3(-1, 0, 1);
        }
        else if (dir == (int)WindDirection.West_South)
        {
            dirVec = new Vector3(1, 0, -1);
        }
        else if (dir == (int)WindDirection.West_North)
        {
            dirVec = new Vector3(-1, 0, -1);
        }

        return dirVec;
    }

    private float ClampAngle(float angle)
    {
        if (angle < -180)
            angle += 360;
        if (angle > 180)
            angle -= 360;
        return angle;
    }
}