1 using UnityEngine; 2 using System.Collections; 3 4 public class EndLevelScript : MonoBehaviour { 5 6 void Start () { 7 8 } 9 10 void Update () { 11 12 } 13 }
EndLevelScript
1 using UnityEngine; 2 using System.Collections; 3 4 public class FruitDispenser : MonoBehaviour { 5 6 public GameObject[] fruits; 7 public GameObject bomb; 8 9 public float z; 10 11 public float powerScale; 12 13 public bool pause = false; 14 bool started = false; 15 16 //每个水果发射的计时 17 public float timer = 1.75f; 18 19 void Start () { 20 21 } 22 23 void Update () { 24 25 if (pause) return; 26 27 timer -= Time.deltaTime; 28 29 if (timer <= 0 && !started) 30 { 31 timer = 0f; 32 started = true; 33 } 34 35 if (started) 36 { 37 if (SharedSettings.LoadLevel == 0) 38 { 39 if (timer <= 0) 40 { 41 FireUp(); 42 timer = 2.5f; 43 } 44 } 45 else 46 if (SharedSettings.LoadLevel == 1) 47 { 48 if (timer <= 0) 49 { 50 FireUp(); 51 timer = 2.0f; 52 } 53 } 54 else 55 if (SharedSettings.LoadLevel == 2) 56 { 57 if (timer <= 0) 58 { 59 FireUp(); 60 timer = 1.75f; 61 } 62 } 63 else 64 if (SharedSettings.LoadLevel == 3) 65 { 66 if (timer <= 0) 67 { 68 FireUp(); 69 timer = 1.5f; 70 } 71 } 72 } 73 } 74 75 void FireUp() 76 { 77 if (pause) return; 78 79 //每次必有的水果 80 Spawn(false); 81 82 if (SharedSettings.LoadLevel == 2 && Random.Range(0, 10) < 2) 83 { 84 Spawn(true); 85 } 86 if(SharedSettings.LoadLevel == 3 && Random.Range(0, 10) < 4) 87 { 88 Spawn(true); 89 } 90 91 //炸弹 92 if (SharedSettings.LoadLevel == 1 && Random.Range(0, 100) < 10) 93 { 94 Spawn(true); 95 } 96 if (SharedSettings.LoadLevel == 2 && Random.Range(0, 100) < 20) 97 { 98 Spawn(true); 99 } 100 if (SharedSettings.LoadLevel == 3 && Random.Range(0 ,100) < 30) 101 { 102 Spawn(true); 103 } 104 } 105 106 void Spawn(bool isBomb) 107 { 108 float x = Random.Range(-3.1f, 3.1f); 109 110 z = Random.Range(14f, 19.8f); 111 112 GameObject ins; 113 114 if (!isBomb) 115 ins = Instantiate(fruits[Random.Range(0, fruits.Length)], transform.position + new Vector3(x, 0, z), Random.rotation) as GameObject; 116 else 117 ins = Instantiate(bomb, transform.position + new Vector3(x, 0, z), Random.rotation) as GameObject; 118 119 float power = Random.Range(1.5f, 1.8f) * -Physics.gravity.y * 1.5f * powerScale; 120 Vector3 direction = new Vector3(-x * 0.05f * Random.Range(0.3f, 0.8f), 1, 0); 121 direction.z = 0f; 122 123 ins.GetComponent<Rigidbody>().velocity = direction * power; 124 ins.GetComponent<Rigidbody>().AddTorque(Random.onUnitSphere * 0.1f, ForceMode.Impulse); 125 } 126 127 void OnTriggerEnter(Collider other) 128 { 129 Destroy(other.gameObject); 130 } 131 }
FruitDispenser
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 5 public class GUIManager : MonoBehaviour { 6 7 public Text guiPoints; 8 9 MouseControl mouseControl; 10 11 bool gamePause = false; 12 13 public Text pauseButtonText; 14 public FruitDispenser fd; 15 public Timer timer; 16 17 void Start () { 18 19 mouseControl = GameObject.Find("Game").GetComponent<MouseControl>(); 20 21 } 22 23 void Update () { 24 25 guiPoints.text = "Points: " + mouseControl.points; 26 27 } 28 29 public void Pause() 30 { 31 Rigidbody[] rs = GameObject.FindObjectsOfType<Rigidbody>(); 32 33 gamePause = !gamePause; 34 35 if (gamePause) 36 { 37 foreach(Rigidbody r in rs) 38 { 39 r.Sleep(); 40 pauseButtonText.text = "Resume"; 41 fd.pause = true; 42 timer.PauseTimer(gamePause); 43 } 44 } 45 else 46 { 47 foreach(Rigidbody r in rs) 48 { 49 r.WakeUp(); 50 pauseButtonText.text = "Pause"; 51 fd.pause = false; 52 timer.PauseTimer(gamePause); 53 } 54 } 55 } 56 }
GUIManager
1 using UnityEngine; 2 using System.Collections; 3 4 public class MouseControl : MonoBehaviour { 5 6 Vector2 screenInp; 7 8 bool fire = false; 9 bool fire_prev = false; 10 bool fire_down = false; 11 bool fire_up = false; 12 13 public LineRenderer trail; 14 15 Vector2 start, end; 16 17 Vector3[] trailPositions = new Vector3[10]; 18 19 int index; 20 int linePart = 0; 21 float lineTimer = 1.0f; 22 23 float trail_alpha = 0f; 24 int raycastCount = 10; 25 26 //积分 27 public int points; 28 29 bool started = false; 30 31 //果汁效果预制品 32 public GameObject[] splashPrefab; 33 public GameObject[] splashFlatPrefab; 34 35 void Start () { 36 37 } 38 39 void BlowObject(RaycastHit hit) 40 { 41 if (hit.collider.gameObject.tag != "destroyed") 42 { 43 //生成切开的水果的部分 44 hit.collider.gameObject.GetComponent<ObjectKill>().OnKill(); 45 46 //删除切到的水果 47 Destroy(hit.collider.gameObject); 48 49 if (hit.collider.tag == "red") index = 0; 50 if (hit.collider.tag == "yellow") index = 1; 51 if (hit.collider.tag == "green") index = 2; 52 53 //水果泼溅效果 54 if (hit.collider.gameObject.tag != "bomb") 55 { 56 Vector3 splashPoint = hit.point; 57 splashPoint.z = 4; 58 Instantiate(splashPrefab[index], splashPoint, Quaternion.identity); 59 splashPoint.z += 4; 60 Instantiate(splashFlatPrefab[index], splashPoint, Quaternion.identity); 61 } 62 63 //切到炸弹 64 if (hit.collider.gameObject.tag != "bomb") points++; else points -= 5; 65 points = points < 0 ? 0 : points; 66 67 hit.collider.gameObject.tag = "destroyed"; 68 } 69 } 70 71 void Update () { 72 73 Vector2 Mouse; 74 75 screenInp.x = Input.mousePosition.x; 76 screenInp.y = Input.mousePosition.y; 77 78 fire_down = false; 79 fire_up = false; 80 81 fire = Input.GetMouseButton(0); 82 if (fire && !fire_prev) fire_down = true; 83 if (!fire && fire_prev) fire_up = true; 84 fire_prev = fire; 85 86 //控制画线 87 Control(); 88 89 //设置线段的相应颜色 90 Color c1 = new Color(1, 1, 0, trail_alpha); 91 Color c2 = new Color(1, 0, 0, trail_alpha); 92 trail.SetColors(c1, c2); 93 94 if (trail_alpha > 0) trail_alpha -= Time.deltaTime; 95 } 96 97 void Control() 98 { 99 //线段开始 100 if (fire_down) 101 { 102 trail_alpha = 1.0f; 103 104 start = screenInp; 105 end = screenInp; 106 107 started = true; 108 109 linePart = 0; 110 lineTimer = 0.25f; 111 AddTrailPosition(); 112 } 113 114 //鼠标拖动中 115 if (fire && started) 116 { 117 start = screenInp; 118 119 var a = Camera.main.ScreenToWorldPoint(new Vector3(start.x, start.y, 10)); 120 var b = Camera.main.ScreenToWorldPoint(new Vector3(end.x, end.y, 10)); 121 122 //判断用户的鼠标(触屏)移动大于0.1后,我们认为这是一个有效的移动,就可以进行一次“采样”(sample) 123 if (Vector3.Distance(a, b) > 0.1f) 124 { 125 linePart++; 126 lineTimer = 0.25f; 127 AddTrailPosition(); 128 } 129 130 trail_alpha = 0.75f; 131 132 end = screenInp; 133 } 134 135 //线的alpha值大于0.5的时候,可以做射线检测 136 if (trail_alpha > 0.5f) 137 { 138 for (var p = 0; p < 8; p++) 139 { 140 for (var i = 0; i < raycastCount; i++) 141 { 142 Vector3 s = Camera.main.WorldToScreenPoint(trailPositions[p]); 143 Vector3 e = Camera.main.WorldToScreenPoint(trailPositions[p+1]); 144 Ray ray = Camera.main.ScreenPointToRay(Vector3.Lerp(s, e, i / raycastCount)); 145 146 RaycastHit hit; 147 if (Physics.Raycast(ray, out hit, 100, 1 << LayerMask.NameToLayer("fruit"))) 148 { 149 BlowObject(hit); 150 } 151 } 152 153 } 154 } 155 156 if (trail_alpha <= 0) linePart = 0; 157 158 //根据时间加入一个点 159 lineTimer -= Time.deltaTime; 160 if (lineTimer <= 0f) 161 { 162 linePart++; 163 AddTrailPosition(); 164 lineTimer = 0.01f; 165 } 166 167 if (fire_up && started) started = false; 168 169 //拷贝线段的数据到linerenderer 170 SendTrailPosition(); 171 } 172 173 void AddTrailPosition() 174 { 175 if (linePart <= 9) 176 { 177 for (int i = linePart; i <= 9; i++) 178 { 179 trailPositions[i] = Camera.main.ScreenToWorldPoint(new Vector3(start.x, start.y, 10)); 180 } 181 } 182 else 183 { 184 for (int ii = 0; ii <= 8; ii++) 185 { 186 trailPositions[ii] = trailPositions[ii + 1]; 187 } 188 189 trailPositions[9] = Camera.main.ScreenToWorldPoint(new Vector3(start.x, start.y, 10)); 190 } 191 } 192 193 void SendTrailPosition() 194 { 195 var index = 0; 196 foreach(Vector3 v in trailPositions) 197 { 198 trail.SetPosition(index, v); 199 index++; 200 } 201 } 202 }
MouseControl
1 using UnityEngine; 2 using System.Collections; 3 4 public class ObjectKill : MonoBehaviour { 5 6 bool killed = false; 7 8 public GameObject[] prefab; 9 10 public float scale = 1f; 11 12 public void OnKill() 13 { 14 if (killed) return; 15 16 foreach(GameObject go in prefab) 17 { 18 GameObject ins = Instantiate(go, transform.position, Random.rotation) as GameObject; 19 20 Rigidbody rd = ins.GetComponent<Rigidbody>(); 21 if (rd != null) 22 { 23 rd.velocity = Random.onUnitSphere + Vector3.up; 24 rd.AddTorque(Random.onUnitSphere * scale, ForceMode.Impulse); 25 } 26 } 27 28 29 killed = true; 30 } 31 }
ObjectKill
1 using UnityEngine; 2 using System.Collections; 3 4 public class ParticleDestroy : MonoBehaviour { 5 6 private ParticleSystem ps; 7 8 void Start () { 9 10 ps = GetComponent<ParticleSystem>(); 11 Destroy(gameObject, ps.startLifetime + 0.5f); 12 13 } 14 }
ParticleDestroy
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 5 public class PrepareLevel : MonoBehaviour { 6 7 public GameObject GetReady; 8 public GameObject GO; 9 10 void Awake() 11 { 12 GetComponent<Timer>().timeAvailable = SharedSettings.ConfigTime; 13 } 14 15 void Start () { 16 17 GameObject.Find("GUI/LevelName/LevelName").GetComponent<Text>().text = SharedSettings.LevelName[SharedSettings.LoadLevel]; 18 StartCoroutine(PrepareRoutine()); 19 20 } 21 22 IEnumerator PrepareRoutine() 23 { 24 //等待1秒 25 yield return new WaitForSeconds(1.0f); 26 27 //显示GetReady 28 GetReady.SetActive(true); 29 30 //等待2秒 31 yield return new WaitForSeconds(2.0f); 32 GetReady.SetActive(false); 33 34 GO.SetActive(true); 35 36 yield return new WaitForSeconds(1.0f); 37 GO.SetActive(false); 38 } 39 }
PrepareLevel
1 using UnityEngine; 2 using System.Collections; 3 4 public class SharedSettings : MonoBehaviour { 5 6 //计时时间 7 public static int ConfigTime = 60; //seconds 8 9 10 //游戏难度 11 12 public static int LoadLevel = 2; 13 14 15 //游戏难度名字 16 17 public static string[] LevelName = new string[] { "Easy", "Medium", "Hard", "Extreme" }; 18 19 }
SharedSettings
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 5 public class TextFadeOut : MonoBehaviour { 6 7 //fade速度 8 public float speed = 0.5f; 9 Color color; 10 11 void Start () { 12 13 color = GetComponent<Text>().color; 14 15 } 16 17 void Update () { 18 19 if (gameObject.activeSelf) 20 { 21 color.a -= Time.deltaTime * speed; 22 GetComponent<Text>().color = color; 23 } 24 25 } 26 }
TextFadeOut
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 5 public class Timer : MonoBehaviour { 6 7 bool run = false; 8 bool showTimeLeft = true; 9 bool timeEnd = false; 10 11 float startTime = 0.0f; 12 float curTime = 0.0f; 13 string curStrTime = string.Empty; 14 bool pause = false; 15 16 public float timeAvailable = 30f; // 30 seconds 17 float showTime = 0; 18 19 public Text guiTimer; 20 public GameObject finishedUI; 21 22 void Start() 23 { 24 RunTimer(); 25 } 26 27 public void RunTimer() 28 { 29 run = true; 30 startTime = Time.time; 31 } 32 33 public void PauseTimer(bool b) 34 { 35 pause = b; 36 } 37 38 public void EndTimer() 39 { 40 41 } 42 43 void Update () { 44 45 if (pause) 46 { 47 startTime = startTime + Time.deltaTime; 48 return; 49 } 50 51 if (run) 52 { 53 curTime = Time.time - startTime; 54 } 55 56 if (showTimeLeft) 57 { 58 showTime = timeAvailable - curTime; 59 if (showTime <= 0) 60 { 61 timeEnd = true; 62 showTime = 0; 63 64 //弹出UI界面,告诉用户本轮游戏结束。 65 //暂停/停止游戏 66 finishedUI.SetActive(true); 67 } 68 } 69 70 int minutes = (int) (showTime / 60); 71 int seconds = (int) (showTime % 60); 72 int fraction = (int) ((showTime * 100) % 100); 73 74 curStrTime = string.Format("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 75 guiTimer.text = "Time: " + curStrTime; 76 77 } 78 }
Timer
视频:https://pan.baidu.com/s/1geAqJTL
项目:https://pan.baidu.com/s/1slq9QDf
时间: 2024-10-24 23:12:23