예제 #1
0
 void Start()
 {
     Assert.IsNotNull(magnetData, "ScriptableObject MagnetData not found. Was this removed from the Resources/ScriptableObjects/ folder?");
     headMagnetizable = gameObject.AddComponent <Magnetizable>();
     headMagnetizable.magnetActive = true;
     magnetData.nonMagnetizedList.Remove(headMagnetizable);
 }
예제 #2
0
 // Attach object to target object
 public void Magnetize(Magnetizable toMagnet)
 {
     AudioManager.instance.Play("Pickcargo");
     magnetActive        = true;
     magnetizedTo        = toMagnet;
     toMagnet.nextMagnet = this;
     magnetData.AddMagnetizable(this);
 }
예제 #3
0
 public void AddMagnetizable(Magnetizable mag)
 {
     if (!nonMagnetizedList.Remove(mag))
     {
         throw new System.Exception("Non Magnetized List did not contain this Magnetizable. Did you mean to use nonMagnetizedList.Add()?");
     }
     magnetizedList.Add(mag);
 }
예제 #4
0
 private void Awake()
 {
     rb      = GetComponent <Rigidbody2D>();
     magData = Resources.Load <MagnetData>("ScriptableObjects/MagnetData");
     Assert.IsNotNull(transform.Find("Magnet"), "Magnet not attached");
     mag = transform.Find("Magnet").GetComponent <Magnetizable>();
     StartCoroutine(BecomeInvincibleForSeconds(2f));
 }
예제 #5
0
    // Attach object to last object in chain
    public void Magnetize()
    {
        AudioManager.instance.Play("Pickcargo");
        magnetActive = true;
        Magnetizable _mag = magnetData.magnetizedList.GetLast();

        magnetizedTo    = _mag;
        _mag.nextMagnet = this;
        magnetData.AddMagnetizable(this);
    }
예제 #6
0
 public void DestroyMagnetizable(Magnetizable mag)
 {
     if (magnetizedList.Contains(mag))
     {
         magnetizedList.Remove(mag);
     }
     if (nonMagnetizedList.Contains(mag))
     {
         nonMagnetizedList.Remove(mag);
     }
 }
예제 #7
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.CompareTag("Cargo"))
     {
         GuideSystem.instance?.SoldCargo();
         LevelManager.instance.LevelScore += collision.transform.GetComponent <Cargo>().value;
         LevelManager.instance?.cargoSpawner.SpawnCargo();
         Magnetizable _mag = collision.GetComponentInChildren <Magnetizable>();
         if (_mag.magnetizedTo != null)
         {
             _mag.DeMagnetize();
         }
         AudioManager.instance.Play("Reward");
         PoolMe(collision.gameObject); // TODO
     }
 }
예제 #8
0
    // Gets last magnetized object in the chain and demagnetizes each object until it reaches this magnetizable
    public void DeMagnetize()
    {
        if (rigidBody == null) // Sometimes tries to demagnetize objects undergoing Destroy
        {
            return;
        }
        if (magnetData.magnetizedList.Count == 0)
        {
            return;
        }
        Magnetizable _last = magnetData.magnetizedList.GetLast();

        while (_last != this)
        {
            _last.rigidBody.velocity = Vector2.zero;
            _last.magnetActive       = false;
            magnetData.RemoveMagnetizable(_last);
            _last.magnetizedTo = null;
            _last.nextMagnet   = null;

            if (magnetData.magnetizedList.Count == 0)
            {
                break;
            }
            _last = magnetData.magnetizedList.GetLast();
        }
        rigidBody.velocity = Vector2.zero;
        magnetActive       = false;
        magnetData.RemoveMagnetizable(this);
        if (magnetizedTo != null)
        {
            magnetizedTo.nextMagnet = null;
        }
        magnetizedTo = null;
        nextMagnet   = null;
    }
예제 #9
0
    void HandleMagnets()
    {
        // If no magnet is attached to the ship, simply look for nearby magnets and magnetize those in range
        if (headMagnetizable.nextMagnet == null)
        {
            List <Magnetizable> _nonMagnetizedList = new List <Magnetizable>(magnetData.nonMagnetizedList);
            foreach (Magnetizable magnet in _nonMagnetizedList)
            {
                if (magnet == null)
                {
                    // Remove unnecessary magnet from lists
                    magnetData.DestroyMagnetizable(magnet);
                    continue;
                }

                // Snap on nearest magnet to head if in range
                float _distance = (headMagnetizable.transform.position - (magnet.transform.position)).magnitude;
                if (_distance < snapOnRange)
                {
                    if (magnet.magnetActive)
                    {
                        magnet.Magnetize(headMagnetizable);
                        break;
                    }
                }
                else if (_distance < snapOnRange + 3) // Detach magnet if not in range
                {
                    if (!magnet.magnetActive)
                    {
                        magnet.magnetActive = true;
                    }
                }
            }
        }

        // Look for nearest magnet and attach to last magnet in the chain
        if (magnetData.magnetizedList.Count > 0)
        {
            List <Magnetizable> _nonMagnetizedList = new List <Magnetizable>(magnetData.nonMagnetizedList);

            foreach (Magnetizable m in _nonMagnetizedList)
            {
                // Demagnetize magnets that are no longer chained to the ship
                Magnetizable _mag = magnetData.magnetizedList.GetLast();
                while (_mag.magnetizedTo == null && magnetData.magnetizedList.Count > 0)
                {
                    magnetData.RemoveMagnetizable(_mag);
                    _mag = magnetData.magnetizedList.GetLast();
                }
                if (m == null)
                {
                    magnetData.DestroyMagnetizable(m);
                    continue;
                }
                else if (m.magnetizedTo != null || m.nextMagnet != null)
                {
                    m.magnetizedTo = null;
                    m.nextMagnet   = null;
                }

                // Snap first nearby magnet to the end of the chain
                float _distance = (_mag.transform.position - m.transform.position).magnitude;
                if (_distance < snapOnRange)
                {
                    if (m.magnetActive)
                    {
                        m.Magnetize(_mag);
                    }
                }
                else if (_distance > snapOnRange + 3)
                {
                    if (!m.magnetActive)
                    {
                        m.magnetActive = true;
                    }
                }
            }

            // Move every magnetized magnet towards target magnet in chain
            List <Magnetizable> _magnetizedList = new List <Magnetizable>(magnetData.magnetizedList);
            foreach (Magnetizable m in _magnetizedList)
            {
                if (m.magnetizedTo == null)
                {
                    m.DeMagnetize();
                    continue;
                }
                float _distance = (m.magnetizedTo.transform.position - m.transform.position).magnitude;
                if (_distance < snapOnRange + 1)
                {
                    if (m.magnetActive)
                    {
                        // Allows for elastic movement between magnets. Increasing speed increases distance between magnets
                        #region ElasticRopeMethod
                        if (elasticMagnets)
                        {
                            Vector3 moveVector = (m.magnetizedTo.posMag.position - m.negMag.position) * magnetData.magnetForceScalar;
                            m.rigidBody.AddForce(moveVector);
                        }
                        #endregion

                        // Allows for chain-like movement between magnets (Default), such as whip-like movement and dangling down.
                        #region AbsoluteMovementMethod
                        if (chainMagnets)
                        {
                            m.transform.parent.position = m.transform.parent.position + (m.magnetizedTo.posMag.position - m.negMag.position) / 3;
                            m.rigidBody.AddForce(Vector2.down * m.rigidBody.drag * m.rigidBody.mass - m.rigidBody.gravityScale * Vector2.down);
                        }
                        #endregion

                        // Rotate towards target magnet
                        Vector3 rotateVector = (m.magnetizedTo.transform.position - m.negMag.position).normalized;
                        if (rotateVector.y >= 0)
                        {
                            m.transform.parent.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Acos(rotateVector.x) * (180 / Mathf.PI) - 90));
                        }
                        else
                        {
                            m.transform.parent.rotation = Quaternion.Euler(new Vector3(0, 0, -Mathf.Acos(rotateVector.x) * (180 / Mathf.PI) - 90));
                        }
                    }
                }
                else if (_distance > snapOnRange + 3) // Detach if too far away from target magnet
                {
                    if (!m.magnetActive)
                    {
                        m.magnetActive = true;
                    }
                }
            }
        }
    }
예제 #10
0
 public void RemoveMagnetizable(Magnetizable mag)
 {
     magnetizedList.Remove(mag);
     nonMagnetizedList.Add(mag);
 }