RabbitHuntGameMode.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class RabbitHuntGameMode : ChallengeGameMode
  6. {
  7. public RabbitHuntGameMode(GameMgr gameMgr) : base(gameMgr) {
  8. this.animalTypeID = 0;
  9. animalPrefab = animalsBaseT.Find("Rabbit").gameObject;
  10. baseCreateDistance = 5;
  11. AddSelectLevelView();
  12. }
  13. //动物的创建列表(数组元素表示动物颜色样式)
  14. int[] animalCreateList;
  15. //动物创建列表的当前索引值
  16. int animalCreateIndex = 0;
  17. //设置关卡级别
  18. public override void SetLevel(int level) {
  19. if (level == 1) {
  20. animalCreateList = new int[]{1, 1};
  21. arrowCount = 5;
  22. }
  23. else if (level == 2) {
  24. animalCreateList = new int[]{2, 2};
  25. arrowCount = 6;
  26. }
  27. else if (level == 3) {
  28. animalCreateList = new int[]{3, 3};
  29. arrowCount = 7;
  30. }
  31. else if (level == 4) {
  32. animalCreateList = new int[]{1, 1, 2, 2};
  33. arrowCount = 8;
  34. }
  35. else if (level == 5) {
  36. animalCreateList = new int[]{1, 1, 1, 3, 3, 3};
  37. arrowCount = 10;
  38. }
  39. if (animalCreateList != null) animalCount = animalCreateList.Length;
  40. CreateAnimal();
  41. }
  42. float baseCreateDistance = 0;
  43. float plusCreateDistance = 3;
  44. void CreateAnimal() {
  45. if (animalCreateIndex >= animalCreateList.Length) return;
  46. int animalStyleID = animalCreateList[animalCreateIndex++];
  47. //计算初始生成位置,后面的兔子生成越来越远,在标准方向的±30°内生成
  48. #region
  49. float createDistance = baseCreateDistance;
  50. baseCreateDistance += plusCreateDistance;
  51. Vector3 standardPointer = animalsBaseT.forward;
  52. Vector3 displace = standardPointer * createDistance;
  53. displace = Quaternion.AngleAxis(30 - Random.value * 60, Vector3.up) * displace;
  54. Vector3 newPos = animalsBaseT.position + displace;
  55. #endregion
  56. GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT);
  57. animalObject.SetActive(true);
  58. Rabbit rabbit = animalObject.GetComponent<Rabbit>();
  59. rabbit.baseDistance = createDistance;
  60. rabbit.baseDistanceMoveRange = plusCreateDistance;
  61. rabbit.hp = animalStyleID;
  62. rabbit.ChangeColorByType(animalStyleID);
  63. rabbit.RotateByWorldY(Random.value * 360);
  64. rabbit.onDie += delegate(Rabbit rb) {
  65. animalCount--;
  66. animalSet.Remove(rb);
  67. CreateAnimal();
  68. };
  69. animalSet.Add(rabbit);
  70. if (animalSet.Count < maxAnimalCountAtTheSameTime) {
  71. CreateAnimal();
  72. }
  73. }
  74. }