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

public class CoreObjectShared : ObjectShared
{
    private GameObject _currentPlayer;
    private PlayerObjectShared _playerObjectShared;
    private Rigidbody _objectRigidBody;

    private void Start()
    {
        _currentPlayer = GameEngine.mainPlayer;
        _playerObjectShared = _currentPlayer.GetComponent<PlayerObjectShared>();
        _objectRigidBody = gameObject.GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        if (_currentPlayer != null)
        {
            if (!GameEngine.isPaused)
            {        
                if (_playerObjectShared != null)
                {
                    Vector3 directionToPlayer = transform.position - _currentPlayer.transform.position;
                    float distanceToPlayer = directionToPlayer.magnitude;
                    if (distanceToPlayer <= _playerObjectShared.coreMagnetRange)
                    {                        
                        _objectRigidBody.AddForce(-directionToPlayer.normalized * distanceToPlayer * _playerObjectShared.coreMagnetForce * Time.fixedDeltaTime, ForceMode.VelocityChange);
                    }
                }
            }
        }
    }
}
