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

public class ShuiMu : Creature
{
    [SerializeField] private float moveCDMax = 1f;
    [SerializeField] private float speedValue = 1f;

    private Vector3 direction;

    private float currentSpeedValue;
    private float moveCDTimer;

    protected override void Update()
    {
        base.Update();
        HandleSpeedChange();
        HandleMovement();
    }

    private void HandleSpeedChange()
    {
        if(moveCDTimer > 0)
        {
            currentSpeedValue = (moveCDTimer / moveCDMax) * speedValue;
        }
        else
        {
            currentSpeedValue = 0;
        }
        
    }

    private void HandleMovement()
    {
        moveCDTimer -= Time.deltaTime;
        if(moveCDTimer < 0)
        {
            moveCDTimer = moveCDMax;
            direction = getRandomDir();
        }
        
        Vector3 curPos = transform.position;
        curPos += direction * currentSpeedValue * Time.deltaTime;
        transform.position = curPos;
    }

    private Vector3 getRandomDir()
    {
        System.Random random = new System.Random();
        float randomX = (float)random.NextDouble();
        float randomY = (float)random.NextDouble();
        bool posX = random.Next(2) == 0 ? false : true;
        bool posY = random.Next(2) == 0 ? false : true;

        if (!posX)
        {
            randomX = -randomX;
        }
        if (!posY)
        {
            randomY = -randomY;
        }

        Vector3 result = new Vector3();
        result.x = randomX;
        result.y = randomY;
        return result;
    }
}
