SnakeControl.cs
5.04 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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();
}
}
}
}