SnakeControl.cs
2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SnakeControl : MonoBehaviour
{
private Image img_Snake;
[SerializeField] private Image img_Rise;
[Tooltip("走路得播放速度")] public float runPlaySpeed = 10.0f;
[Tooltip("吃饭团得播放速度")] public float eatPlaySpeed = 10.0f;
[Tooltip("蛇得走路的图片数组 , 勿动!!!")] [SerializeField]
private Sprite[] Run_Sprites;
[Tooltip("蛇的吃东西的图片数组 , 勿动!!!")] [SerializeField]
private Sprite[] Eat_Sprites;
/// <summary>
/// 是否在吃饭团得状态下
/// </summary>
private bool IsEat;
/// <summary>
/// 当前播放得帧动画携程
/// </summary>
private Coroutine curPlayAni;
/// <summary>
/// 记录一下 肚子里得饭团得初始位置
/// </summary>
/// <returns></returns>
private Vector3 risePostion;
// Start is called before the first frame update
void Start()
{
img_Snake = transform.GetComponent<Image>();
curPlayAni = StartCoroutine(PlayRunAnimation());
if (img_Rise != null)
{
img_Rise.color = new Color(255, 255, 255, 0);
risePostion = img_Rise.transform.position;
}
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// 吃饭团帧动画播放
/// </summary>
public void EatRise()
{
if (IsEat)
{
return;
}
StopCoroutine(curPlayAni);
curPlayAni = StartCoroutine(PlayEatAnimation());
}
public IEnumerator PlayEatAnimation()
{
IsEat = true;
for (int i = 0; i < Eat_Sprites.Length; i++)
{
if (img_Snake != null)
{
img_Snake.sprite = Eat_Sprites[i];
yield return new WaitForSeconds(1f / eatPlaySpeed);
}
}
// 吃完之后在切换到走路动画
curPlayAni = StartCoroutine(PlayRunAnimation());
IsEat = false;
}
IEnumerator PlayRunAnimation()
{
while (true)
{
// 正向播放
for (int i = 0; i < Run_Sprites.Length; i++)
{
if (img_Snake != null)
{
img_Snake.sprite = Run_Sprites[i];
yield return new WaitForSeconds(1f / runPlaySpeed);
}
}
// 反向播放
for (int i = Run_Sprites.Length - 2; i >= 0; i--)
{
if (img_Snake != null)
{
img_Snake.sprite = Run_Sprites[i];
yield return new WaitForSeconds(1f / runPlaySpeed);
}
}
}
}
}