EnemyGenerator.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using DatabaseModel;
  5. using ExtensionMethods;
  6. using UnityEngine;
  7. public class EnemyGenerator : Singleton<EnemyGenerator>
  8. {
  9. private static IDictionary<EnemyType, EnemyGenerator.EnemyPrefab> EnemyPrefabs
  10. {
  11. get
  12. {
  13. IDictionary<EnemyType, EnemyGenerator.EnemyPrefab> result;
  14. if ((result = EnemyGenerator._enemyPrefabs) == null)
  15. {
  16. result = (EnemyGenerator._enemyPrefabs = EnemyGenerator.LoadEnemyPrefabs());
  17. }
  18. return result;
  19. }
  20. }
  21. public static void PreloadEnemyPrefabs()
  22. {
  23. EnemyGenerator._enemyPrefabs = EnemyGenerator.LoadEnemyPrefabs();
  24. }
  25. private static IDictionary<EnemyType, EnemyGenerator.EnemyPrefab> LoadEnemyPrefabs()
  26. {
  27. return (from e in Asset.DeserializeFromFile<EnemyGenerator.EnemyGeneratorJsonObject[]>("Conf/", "EnemyGeneratorConfig")
  28. select new EnemyGenerator.EnemyPrefab
  29. {
  30. type = e.typeString.ToEnum<EnemyType>(false),
  31. prefab = Asset.LoadFromResources<GameObject>("Prefab/Enemys", e.prefabName),
  32. inUse = e.inUse
  33. }).ToDictionary((EnemyGenerator.EnemyPrefab e) => e.type);
  34. }
  35. public static EnemyType GetEnemyType(string name)
  36. {
  37. name = name.Replace("(Clone)", string.Empty);
  38. if (EnemyGenerator._enemyNameToType == null)
  39. {
  40. EnemyGenerator._enemyNameToType = EnemyGenerator.EnemyPrefabs.ToDictionary((KeyValuePair<EnemyType, EnemyGenerator.EnemyPrefab> e) => e.Value.prefab.name, (KeyValuePair<EnemyType, EnemyGenerator.EnemyPrefab> e) => e.Key);
  41. }
  42. return EnemyGenerator._enemyNameToType[name];
  43. }
  44. public GameObject GenerateEnemy(string name, Vector2? pos = null, bool withEffect = true, bool enemyPoint = true)
  45. {
  46. return this.GenerateEnemy(EnemyGenerator.GetEnemyType(name), pos, withEffect, enemyPoint);
  47. }
  48. public GameObject GenerateEnemy(EnemyType type, Vector2? pos = null, bool withEffect = true, bool enemyPoint = true)
  49. {
  50. return this.GenerateEnemy(EnemyGenerator.EnemyPrefabs[type], (pos == null) ? (Vector2)EnemyGenerator.EnemyPrefabs[type].prefab.transform.position : pos.Value, withEffect, enemyPoint);
  51. }
  52. private GameObject GenerateEnemy(EnemyGenerator.EnemyPrefab enemyPrefab, Vector2 pos, bool withEffect, bool enemyPoint)
  53. {
  54. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(enemyPrefab.prefab);
  55. EnemyAttribute component = gameObject.GetComponent<EnemyAttribute>();
  56. if (component != null)
  57. {
  58. gameObject.transform.position = new Vector3(pos.x, pos.y, LayerManager.ZNum.MMiddleE(component.rankType));
  59. gameObject.transform.localScale = Vector3.one;
  60. if (Application.isPlaying)
  61. {
  62. component.SetBasicData(EnemyAttrData.FindBySceneNameAndType(LevelManager.SceneName, enemyPrefab.type));
  63. pos.y = this.ClampOverGround(pos);
  64. if (enemyPoint || component.InTheWorld)
  65. {
  66. R.Ui.CreateEnemyPoint(component);
  67. }
  68. EnemyBaseAction component2 = component.GetComponent<EnemyBaseAction>();
  69. component2.AppearAtPosition(pos);
  70. if (withEffect)
  71. {
  72. component2.AppearEffect(pos);
  73. }
  74. }
  75. }
  76. else
  77. {
  78. gameObject.transform.position = new Vector3(pos.x, pos.y, LayerManager.ZNum.MMiddleE(EnemyAttribute.RankType.Normal));
  79. gameObject.transform.localScale = Vector3.one;
  80. }
  81. return gameObject;
  82. }
  83. private float ClampOverGround(Vector3 pos)
  84. {
  85. BoxCollider2D boxCollider2D = Physics2D.OverlapPoint(pos, LayerManager.GroundMask) as BoxCollider2D;
  86. if (boxCollider2D == null)
  87. {
  88. RaycastHit2D raycastHit2D = Physics2D.Raycast(pos, Vector2.down, GameArea.MapRange.height, LayerManager.GroundMask);
  89. pos.y -= raycastHit2D.distance;
  90. return pos.y;
  91. }
  92. return boxCollider2D.transform.position.y + boxCollider2D.size.y * boxCollider2D.transform.localScale.y / 2f;
  93. }
  94. private const string ConfigFileName = "EnemyGeneratorConfig";
  95. private const string EnemysPrefabPath = "Prefab/Enemys";
  96. private static IDictionary<string, EnemyType> _enemyNameToType;
  97. private static IDictionary<EnemyType, EnemyGenerator.EnemyPrefab> _enemyPrefabs;
  98. [Serializable]
  99. public class EnemyPrefab
  100. {
  101. public GameObject prefab;
  102. public EnemyType type;
  103. public bool inUse;
  104. }
  105. [Serializable]
  106. public class EnemyGeneratorJsonObject
  107. {
  108. public string prefabName;
  109. public string typeString;
  110. public bool inUse;
  111. }
  112. }