// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2020-09-02 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using UnityEngine; using UnityEngine.AddressableAssets; namespace G { /// /// 地图拼接 /// public class Map_Compose : MonoBehaviour { [System.Serializable] public class LootGenConfig { public Vector3[] positions; public Vector3[] positions2; public float delay; public int weight; } [System.Serializable] public class BarricadeGenConfig { public Vector3[] positions; public float delay; public int weight; } #region Field private const int _MAX_LOOT = 32; private const int _MAX_OBJ = 16; private const int _MAX_ENEMY = 3; private const float _MAP_Z_SIZE = 10.5616f; public Transform[] map = new Transform[5]; public Transform loot; public Transform barricate; public Transform enemy; public Transform enemyHitFx; public int finalmap = 1; public LootGenConfig[] lootGenConfigs; public BarricadeGenConfig[] barricadeGenConfigs; private float[] _mapPosZ = new float[2]; private Transform[] _cloneMap = new Transform[2]; private Transform[] _loots = new Transform[_MAX_LOOT]; private Transform[] _barricade = new Transform[_MAX_OBJ]; private Transform[] _enemy = new Transform[_MAX_ENEMY]; private int _lootCount; private int _barricadeCount; private int _mapLength; private float _createDelay; private float _createTime = 100f; private float _genLootDelay = 5f; private float _genLootTime = 100f; private float _speed = -1.8f; private float _genBarricadeDelay = 5f; private int _lootGenWeight; private int _barricadeWeight; private bool _init; #endregion #region Method LootGenConfig GetRndLootGenConfig() { if (_lootGenWeight <= 0) for (int i = lootGenConfigs.Length - 1; i > 0; i--) { _lootGenWeight += lootGenConfigs[i].weight; } int curW = Random.Range(0, _lootGenWeight); for (int i = lootGenConfigs.Length - 1; i > 0; i--) { curW -= lootGenConfigs[i].weight; if (curW < 0) { return lootGenConfigs[i]; } } return null; } BarricadeGenConfig GetRndBarricadeGenConfig() { if (_barricadeWeight <= 0) for (int i = barricadeGenConfigs.Length - 1; i > 0; i--) { _barricadeWeight += barricadeGenConfigs[i].weight; } int curW = Random.Range(0, _barricadeWeight); for (int i = barricadeGenConfigs.Length - 1; i > 0; i--) { curW -= barricadeGenConfigs[i].weight; if (curW < 0) { return barricadeGenConfigs[i]; } } return null; } void CreateLoot(Vector3 position) { _loots[_lootCount].position = position; _loots[_lootCount].gameObject.SetActive(true); _lootCount = (_lootCount + 1) % _MAX_LOOT; } void CreateBarricade(Vector3 position, Quaternion rotation) { _barricade[_barricadeCount].SetPositionAndRotation(position, rotation); _barricadeCount = (_barricadeCount + 1) % _MAX_OBJ; } #endregion #region Unity public static Map_Compose Instance; private void Awake() { Instance = this; _createTime = Random.Range(1.5f, 2f); _createDelay = 0f; } private System.Collections.IEnumerator Start() { GameObject mapPrefab = null; if (map != null && map.Length > 0 && map[0]) { mapPrefab = map[0].gameObject; } else { var op = AssetProxy.Instance.TryGetTemporaryAssetAsync("basics_ride"); yield return op; mapPrefab = op.Result; } float z = 0f; for (int i = 0; i < 2; i++) { _mapPosZ[i] += z; z += _MAP_Z_SIZE; _cloneMap[i] = Object.Instantiate(mapPrefab, Vector3.forward * _mapPosZ[i], Quaternion.identity).transform; } for (int i = 0; i < _MAX_ENEMY; i++) { _enemy[i] = Object.Instantiate(enemy, (3 + i) * Vector3.one, Quaternion.identity); //_enemy[i].gameObject.SetActive(false); } for (int i = 0; i < _MAX_OBJ; i++) { _barricade[i] = Object.Instantiate(barricate, -2f * Vector3.forward, Quaternion.identity, this.transform); } _loots[0] = loot; for (int i = 1; i < _MAX_LOOT; i++) { _loots[i] = Object.Instantiate(loot, -200f * Vector3.forward, Quaternion.identity, this.transform); } _init = true; RideSceneLogic.Instance.init = true; } private void Update() { if (!_init) return; _createDelay += Time.deltaTime; _genLootDelay -= Time.deltaTime; _genBarricadeDelay -= Time.deltaTime; if (_mapLength < finalmap) { if (_genLootDelay <= 0f) { var config = GetRndLootGenConfig(); for (int i = 0; i < config.positions.Length; i++) { CreateLoot(config.positions[i]); } var negative_factor = 0; for (int i = 0; i < config.positions2.Length; i++) { CreateBarricade(config.positions2[i], Quaternion.Euler(0f, negative_factor + Random.Range(-24, 24), 0f)); negative_factor += 180; } _genLootDelay = config.delay; } if (_genBarricadeDelay <= 0f && barricadeGenConfigs != null && barricadeGenConfigs.Length > 0) { var config = GetRndBarricadeGenConfig(); var negative_factor = 0; for (int i = 0; i < config.positions.Length; i++) { CreateBarricade(config.positions[i], Quaternion.Euler(0f, negative_factor + Random.Range(-24, 24), 0f)); negative_factor += 180; } _genBarricadeDelay = config.delay; } } if (_mapLength < finalmap && _createDelay >= _createTime) { int rnd = Random.Range(0, 20); if (rnd < 2) { rnd = 2; } if (rnd >= 12) { //if (lootGenConfigs == null || lootGenConfigs.Length == 0) //{ // var ss_posX = Random.Range(-0.3f, 0.3f); // var rnd_posX = Random.Range(-0.06f, 0.06f); // for (int i = 0; i < 8; i++) // { // CreateLoot(new Vector3((ss_posX + rnd_posX * i), 0f, (2f + 0.3f * i))); // } //} } else if (rnd >= 6) { for (int j = 0; j < 3; j++) { if (!_enemy[j].gameObject.activeSelf) { int r = Random.Range(0, 2) * 2 - 1; _enemy[j].gameObject.SetActive(true); _enemy[j].position = new Vector3(r * 1.4f, 0f, Random.Range(-0.2f, 0.7f)); break; } } } else { //var ss_posX = Random.Range(-0.3f, 0.3f); //if (lootGenConfigs == null || lootGenConfigs.Length == 0) // CreateLoot(new Vector3(ss_posX, 0f, 3f)); //if (barricadeGenConfigs == null || barricadeGenConfigs.Length == 0) //{ // var rnd_posX = ss_posX - 0.18f * rnd * 0.5f; // var negative_factor = 0; // for (int k = 0; k < rnd + 2; k++) // { // CreateBarricade(new Vector3(rnd_posX, 0f, (2.2f + Random.Range(-0.1f, 0.1f))), Quaternion.Euler(0f, negative_factor + Random.Range(-24, 24), 0f)); // rnd_posX += 0.18f; // negative_factor += 180; // } //} } _createTime = Random.Range(1.6f, 2f); _createTime = 1.6f; _createDelay = 0f; } this.transform.Translate(Vector3.forward * (_speed * Time.deltaTime)); if (Time.frameCount % 30 == 0) { var progress = this.transform.position.z / (_MAP_Z_SIZE * finalmap); UI_InRide.Instance.GainComplete(-progress); } for (int i = 0; i < 2; i++) { _mapPosZ[i] += _speed * Time.deltaTime; if (_mapPosZ[i] < -_MAP_Z_SIZE) { _mapPosZ[i] += _MAP_Z_SIZE * 2F; _cloneMap[i].position = Vector3.forward * _mapPosZ[i]; _mapLength++; if (_mapLength >= finalmap) { for (int j = 0; j < _enemy.Length; j++) { _enemy[j].gameObject.SetActive(false); } GameObject.Find("horse_riding").GetComponent().RidingFinish(); } } _cloneMap[i].position = Vector3.forward * _mapPosZ[i]; } if (_mapLength >= finalmap) { //_speed = 0f; _speed += Time.deltaTime; if (_speed > 0f) _speed = 0f; } } #endregion } }