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

public class WeepingAngel : Hazard {

    public float MovementSpeed;

    private Vector2 _prevHumanPos;
    private Vector2 _prevGhostPos;

    private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.

    private void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        Vector2 humanPos = LevelController.Instance.PlayerHuman.transform.position;
        Vector2 ghostPos = LevelController.Instance.PlayerGhost.transform.position;
        Vector2 thisPos = transform.position;

        switch (Kills)
        {
            case PlayerEnum.Human:
                Vector2 rel = humanPos - _prevHumanPos;

                if (rel.magnitude > 0.01)
                {

                    rb2d.position += (humanPos - thisPos).normalized * MovementSpeed * Time.deltaTime;
                }
                break;
            case PlayerEnum.Ghost:

                rel = ghostPos - _prevGhostPos;

                if (rel.magnitude > 0)
                {
                    rb2d.position += (ghostPos - thisPos).normalized * MovementSpeed * Time.deltaTime;
                }
                break;
            case PlayerEnum.Both:
                rel = humanPos - _prevHumanPos;
                Debug.Log(rel);
                if (rel.magnitude > 0)
                {
                    rb2d.position += (humanPos - thisPos).normalized * MovementSpeed * Time.deltaTime;
                }

                rel = ghostPos - _prevGhostPos;
                if (rel.magnitude > 0)
                {
                    rb2d.position += (ghostPos - thisPos).normalized * MovementSpeed * Time.deltaTime;
                }
                break;
        }

        _prevHumanPos = humanPos;
        _prevGhostPos = ghostPos;
    }
}
