BackGroundSroll.cs 4.56 KB
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

public class BackGroundSroll : MonoBehaviour
{
    [SerializeField] public float bgScrollSpeed = 500f;
    [Tooltip("速度的缩放倍率")] public float speedScale = 0.0f;
    [Tooltip("食物得列表")] [SerializeField] private Sprite[] FoodList;
    private float backgroundWidth;
    private Transform[] backgrounds;
    private Image curFood;
    private bool isHaveFood;
    private int FoodId;

    [SerializeField] private Transform eatPoint;
    [SerializeField] private SnakeControl snake;

    private int zeroCount = 0; // 连续出现0的次数
    private float IncrementalProbability = 0.015f; // 递增概率
    public bool isCanMoveBg = false;
    private bool isTouchFood = false;


    void Start()
    {
        backgroundWidth = transform.GetChild(0).GetComponent<RectTransform>().sizeDelta.x;

        backgrounds = new Transform[3];
        for (int i = 0; i < 3; i++)
        {
            backgrounds[i] = transform.GetChild(i);
        }
    }

    void Update()
    {
        if (!isCanMoveBg)
        {
            return;
        }

        moveBg();
    }

    public void moveBg()
    {
        // 移动所有背景
        foreach (Transform bg in backgrounds)
        {
            bg.Translate(Vector3.right * bgScrollSpeed * speedScale * Time.deltaTime);
            //bg.Translate(Vector3.right * bgScrollSpeed * Time.deltaTime);
            Debug.Log("速度倍率:   " + speedScale);
        }

        snake.SetSnakePlayState(speedScale <= 0);
        // 检查是否需要重置位置
        for (int i = 0; i < backgrounds.Length; i++)
        {
            if (backgrounds[i].position.x >= backgroundWidth * 2)
            {
                // 将最左边的背景移动到最右边
                Vector3 newPos = backgrounds[i].position;
                newPos.x -= backgroundWidth * 3;
                backgrounds[i].position = newPos;
                ShowFood(backgrounds[i]);
            }
        }

        // 检查食物是否被吃到
        if (isHaveFood)
        {
            if (eatPoint != null)
            {
                if (curFood.transform.position.x < eatPoint.transform.position.x)
                {
                    float dis = Vector3.Distance(eatPoint.transform.position, curFood.transform.position);
                    if (dis <= eatPoint.transform.GetComponent<RectTransform>().sizeDelta.x / 2)
                    {
                        FoodScaleTween();
                    }
                }
            }
        }
    }


    private void FoodScaleTween()
    {
        if (isTouchFood)
        {
            return;
        }

        isTouchFood = true;
        snake.EatSomething(FoodId);
        FoodTween();
    }

    private void FoodTween()
    {
        curFood.transform.DOScale(new Vector3(0.1f, 0.1f, 0.1f), 0.2f)
            .OnComplete(() =>
            {
                curFood.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
                curFood.color = new Color(255, 255, 255, 0);
                curFood.sprite = null;
                curFood = null;
            });
    }

    public void EatAniPlayEnd()
    {
        isHaveFood = false;
        FoodId = -1;
        isTouchFood = false;
    }

    /// <summary>
    /// 随机生成食物
    /// </summary>
    private void ShowFood(Transform obj)
    {
        if (isHaveFood)
        {
            return;
        }

        // 获取当前背景得图片组件
        curFood = obj.GetChild(0).GetChild(0).GetComponent<Image>();
        if (curFood != null)
        {
            if (GetRandomNumber() == 1)
            {
                if (FoodList.Length > 0)
                {
                    isHaveFood = true;
                    FoodId = Random.Range(0, FoodList.Length);
                    curFood.sprite = FoodList[FoodId];
                    curFood.color = new Color(255, 255, 255, 255);
                    curFood.SetNativeSize();
                }
            }
        }
    }

    /// <summary>
    /// 简单得伪随机  
    /// </summary>
    /// <returns></returns>
    int GetRandomNumber()
    {
        int randomNumber = Random.Range(0, 2);

        if (randomNumber == 0)
        {
            zeroCount++;
            float adjustedProb = Mathf.Min(0.5f + zeroCount * IncrementalProbability, 0.9f);
            randomNumber = Random.Range(0f, 1f) < adjustedProb ? 1 : 0;
        }
        else
        {
            zeroCount = 0; // 重置计数器
        }

        return randomNumber;
    }
}