Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.3 kB
5
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;

public class EnemyMovement : MonoBehaviour
{
    [SerializeField] public float Speed = 5f;
    public Transform E_transform;

    PickupManager pManager;
    LaneManager Lm;
    List<GameObject> items = new List<GameObject>();

    float mlp1 = 1.79f, mlp2 = -1.802f, mlp3 = -2.82f, mlp4 = 2.71f;
    string[] tags = { "Cars", "Coins", "Pickups" };

    void Start()
    {
        E_transform =  GetComponent<Transform>();
        pManager = FindObjectOfType<PickupManager>();
       
        Lm = FindObjectOfType<LaneManager>();
       
        Lm.onEnemyRemoval.AddListener(ManageLanelessCars);
        Debug.Log("OnEnemyRemoval:" + Lm.onEnemyRemoval);
    }

 
    void Update()
    {

        foreach (string tag in tags)
        {
            items.AddRange(FindObjectsOfType<GameObject>().Where(go => go.tag == tag).ToList());
        }


        if (pManager != null)
      
           {
            if (pManager.LaneSpeedIncrease == true)
            {
                Speed = 10f;
                MoveCars(10f);
                
            }

         if ((pManager.LaneSpeedReset == true) || (!pManager.LaneSpeedReset && !pManager.LaneSpeedIncrease))
            {
             Speed = 5f;
              MoveCars(5f);
              
           }

            else
            {
                Speed = 5f;
                MoveCars(5f);
            }
        }
 
    }

    public void MoveCars(float speed)
    {
        transform.position -= new Vector3(0, Speed * Time.deltaTime, 0);
        if (transform.position.y <= -12)
        {
            
                Destroy(gameObject);
                items.Remove(gameObject);
           
        }
    }

    public void ManageLanelessCars()
    {


        GameObject removedLaneLeft = Lm.instantiatedLanesLeft[Lm.instantiatedLanesLeft.Count - 1];
        GameObject removedLaneRight = Lm.instantiatedLanesRight[Lm.instantiatedLanesRight.Count - 1];


        Collider2D leftLaneCollider = removedLaneLeft.GetComponent<Collider2D>();
        Collider2D rightLaneCollider = removedLaneRight.GetComponent<Collider2D>();

        //mlp1 = ReturnMiddleLeftLanePosition(removedLaneLeft);
        //mlp2 = ReturnMiddleRightLanePosition(removedLaneRight);

        
       

        foreach (GameObject i in items)
        {

            bool isWithinBoundsLeftLane = leftLaneCollider.bounds.Contains(i.transform.position);
            bool isWithinBoundsRightLane = rightLaneCollider.bounds.Contains(i.transform.position);

            if (isWithinBoundsLeftLane || isWithinBoundsRightLane)
            {
                Debug.Log("Speed changed");
                i.transform.position += new Vector3(0, ((Speed + 5f) * Time.deltaTime), 0);
            }
            else if (i.transform.position.x == mlp1 || i.transform.position.x == mlp2 || i.transform.position.x == mlp3 || i.transform.position.x == mlp4)
            {
                Debug.Log("Cars Destroyed");
                Destroy(i);
                items.Remove(i);
            }


        }

    }

 

}
Leave a Comment