


using System;
using System.Collections;
using UnityEngine;

namespace Subworlds.Scripts
{
  class SpaceJellyfishScript : EnemyScript
  {
    private Vector3 dir;
    private bool attacking;

    private void Awake()
    {
      base.Initialize(130, 7, 80, new int[] { 25, 25, 52 }, 60);
    }

    private void Update()
    {
      if (Network.isServer)
      {
        if (!this.attacking)
        {
          this.r.velocity = new Vector3(0f, 0f, 0f);
          if (this.target)
          {
            if (Mathf.Abs(this.target.transform.position.x - this.t.position.x) < 45f)
            {
              this.attacking = true;
              base.StartCoroutine(this.Attack());
            }
            else
            {
              this.target = null;
              this.r.velocity = new Vector3(0f, 0f, 0f);
            }
          }
        }
      }
    }

    private float speedVariant = 1;

    private IEnumerator Attack()
    {
      //gameObject.transform.GetChild(0).gameObject.GetComponent<Animation>().Play();
      yield return new WaitForSeconds(0.35f);
      speedVariant = (1 + UnityEngine.Random.Range(-10, 10) / 20f);
      yield return new WaitForSeconds(0.01f * UnityEngine.Random.Range(0, 25));
      for (int i = 0; i < 20; i++)
      {
        this.dir = this.target.transform.position - this.t.position;
        this.dir.Normalize();
        var prevRotation = gameObject.transform.GetChild(0).localEulerAngles.z + 90;
        var newRotation = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        var diffAngle = (prevRotation - newRotation + 180) % 360 - 180;
        gameObject.transform.GetChild(0).localEulerAngles = new Vector3(0, 0, prevRotation - 90 - diffAngle / 6);
        yield return new WaitForSeconds(0.01f);
      }
      this.r.velocity = this.dir * (15f);
      yield return new WaitForSeconds(0.2f);
      for (int j = 0; j < 8; j++)
      {
        this.r.velocity = this.dir * (16f) * (1f / (j + 1)) * speedVariant;
        yield return new WaitForSeconds(0.5f);
      }
      this.r.velocity = new Vector3(0f, 0f, 0f);
      yield return new WaitForSeconds(0.2f);
      this.attacking = false;
      yield break;
    }
  }
}