SnakeControl.cs 5.04 KB
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class SnakeControl : MonoBehaviour
{
    public BackGroundSroll bg;
    private Image img_Snake;
    [Tooltip("走路得播放速度")] public float runPlaySpeed = 10.0f;
    [Tooltip("吃饭团得播放速度")] public float eatPlaySpeed = 10.0f;

    private List<Sprite> Run_Sprites;
    private List<Sprite> EatRise_Sprites;
    private List<Sprite> EatGrape_Sprites;
    private List<Sprite> EatApple_Sprites;
    private List<Sprite> EatBanana_Sprites;

    private string RunSpriteLoader = "Run";
    private string EatRiseLoader = "EatFood/Rise";
    private string EatGrapeLoader = "EatFood/Grape";
    private string EatAppleLoader = "EatFood/Apple";
    private string EatBananaLoader = "EatFood/Banana";

    /// <summary>
    /// 是否正在吃食物
    /// </summary>
    private bool IsEat;

    /// <summary>
    /// 当前播放得帧动画携程
    /// </summary>
    private Coroutine curPlayAni;

    [Tooltip("吃到得食物ID")] public int eatFoodId = 0;

    private bool isPaused = true;

    // 动画状态控制变量
    private enum AnimationState
    {
        Running,
        Eating
    }

    private AnimationState currentState = AnimationState.Running;

    // 跑步动画相关变量
    private float runTimer = 0f;
    private int currentRunFrame = 0;
    private bool isRunReversing = false;

    // 吃食物动画相关变量
    private float eatTimer = 0f;
    private int currentEatFrame = 0;
    private List<Sprite> currentEatSprites;


    void Start()
    {
        img_Snake = transform.GetComponent<Image>();
        Run_Sprites = new List<Sprite>(Resources.LoadAll<Sprite>(RunSpriteLoader));
        EatRise_Sprites = new List<Sprite>(Resources.LoadAll<Sprite>(EatRiseLoader));
        EatGrape_Sprites = new List<Sprite>(Resources.LoadAll<Sprite>(EatGrapeLoader));
        EatApple_Sprites = new List<Sprite>(Resources.LoadAll<Sprite>(EatAppleLoader));
        EatBanana_Sprites = new List<Sprite>(Resources.LoadAll<Sprite>(EatBananaLoader));
        // 开始跑步动画
        currentState = AnimationState.Running;
    }

    private void Update()
    {
        if (isPaused || img_Snake == null) return;
        //  简易状态机切换
        switch (currentState)
        {
            case AnimationState.Running:
                UpdateRunAnimation();
                break;
            case AnimationState.Eating:
                UpdateEatAnimation();
                break;
        }
    }

    /// <summary>
    /// 控制蛇得帧动画播放与暂停
    /// </summary>
    /// <param name="isPlay"></param>
    public void SetSnakePlayState(bool pause)
    {
        isPaused = pause;
    }

    /// <summary>
    /// 根据小蛇吃到什么食物 播放什么动画
    /// </summary>
    public void EatSomething(int id)
    {
        if (IsEat)
        {
            return;
        }

        eatFoodId = id;
        currentState = AnimationState.Eating;
        IsEat = true;
        currentEatFrame = 0;
        eatTimer = 0f;

        switch (eatFoodId)
        {
            case 0: // 饭团
                currentEatSprites = EatRise_Sprites;
                break;
            case 1: // 葡萄
                currentEatSprites = EatGrape_Sprites;
                break;
            case 2: // 苹果
                currentEatSprites = EatApple_Sprites;
                break;
            case 3: // 香蕉
                currentEatSprites = EatBanana_Sprites;
                break;
        }
    }

    private void UpdateRunAnimation()
    {
        runTimer += Time.deltaTime;
        float frameDuration = 1f / runPlaySpeed;

        if (runTimer >= frameDuration)
        {
            runTimer = 0f;

            if (!isRunReversing)
            {
                // 正向播放
                currentRunFrame++;
                if (currentRunFrame >= Run_Sprites.Count)
                {
                    currentRunFrame = Run_Sprites.Count - 2;
                    isRunReversing = true;
                }
            }
            else
            {
                // 反向播放
                currentRunFrame--;
                if (currentRunFrame < 0)
                {
                    currentRunFrame = 1;
                    isRunReversing = false;
                }
            }

            img_Snake.sprite = Run_Sprites[currentRunFrame];
        }
    }

    private void UpdateEatAnimation()
    {
        eatTimer += Time.deltaTime;
        float frameDuration = 1f / eatPlaySpeed;

        if (eatTimer >= frameDuration)
        {
            eatTimer = 0f;
            currentEatFrame++;

            if (currentEatFrame < currentEatSprites.Count)
            {
                img_Snake.sprite = currentEatSprites[currentEatFrame];
            }
            else
            {
                IsEat = false;
                currentState = AnimationState.Running;
                runTimer = 0f;
                currentRunFrame = 0;
                isRunReversing = false;
                bg.EatAniPlayEnd();
            }
        }
    }
}