BackGroundSroll.cs
4.56 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
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;
}
}