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

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

    // Tracking the spawner this gameObject was created by so we can reduce the counter for it later
    private ObjectSpawning _spawner;

    public ObjectSpawning Spawner
    {
        get
        {
            return _spawner;
        }
        set
        {
            _spawner = value;
        }
    }

    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);
                    }
                }
            }
        }
    }
}
