// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : 2017-11-14 // // Last Modified By : Kimch // Last Modified On : // *********************************************************************** // // // *********************************************************************** #if SDK_WECHAT_WASM #define PLAY_PLATFORM_SOUND #endif using System.Collections.Generic; using UnityEngine; namespace G { /// /// /// public class EnemyHelper : MonoBehaviour { #region Field private const int ENEMY_COUNT_MAX = 32; private const int ENEMY_FX_COUNT_MAX = 16; /// /// /// public Transform shadow; /// /// /// public Transform ef_hit; /// /// /// public Transform ef_blood; /// /// /// public Transform ef_block; /// /// /// public Transform arrowmark; /// /// /// public Transform pt_itemdrop; /// /// /// public GameObject ef_dead; public Texture[] attribute_tex = new Texture[4]; public GameObject[] attribute_pt = new GameObject[4]; public Material[] blood_mat = new Material[5]; public Material split_material; private readonly Transform[] _shadows = new Transform[ENEMY_COUNT_MAX]; /// /// /// private readonly Transform[] _hitFx = new Transform[ENEMY_FX_COUNT_MAX]; /// /// /// private readonly Transform[] _bloodFx = new Transform[ENEMY_FX_COUNT_MAX]; /// /// /// private readonly GameObject[] _diedFx = new GameObject[ENEMY_FX_COUNT_MAX]; private Vector3 _originPos = Vector3.one * 3f; private Transform grab_blood; private int _efAttribute = -1; private int _effectIndex; private int _diedFxIndex; private float _limit_x = 2.65f; private float _limit_y_b = -2.6f; private float _limit_y_f = 2.5f; private int[] _grabForce = new int[] { 40, 40, 30, 80, 60, 80, 60 }; private int[] _kickForce = new int[] { -200, -10, -10, 240, 20, -160, -100 }; #endregion #region Property #endregion #region Method /// /// /// /// /// public int GetGrabForce(int style) { return _grabForce[style]; } /// /// /// /// /// public int GetKickForce(int style) { return _kickForce[style]; } public void FinishEfs() { } /// /// /// /// public void BloodAttribute(int index) { if (_efAttribute != index) { for (int i = 0; i < ENEMY_FX_COUNT_MAX; i++) { for (int j = 0; j < _bloodFx[i].childCount; j++) { _bloodFx[i].GetChild(j).gameObject.SetActive(index == j); } //_bloodFx[i].GetComponent().material = blood_mat[index]; } } _efAttribute = index; } /// /// /// /// /// /// /// /// public void EnemyDead(int destroy, Vector3 pos, Vector3 scale, Vector3 force, Texture tex) { if (GameLevel.Instance) { GameLevel.Instance.EnemyDead(); } _diedFxIndex = (_diedFxIndex + 1) % ENEMY_FX_COUNT_MAX; if (force != Vector3.zero) _diedFx[_diedFxIndex].transform.SetPositionAndRotation(pos + force * 0.1f, Quaternion.LookRotation(force)); else _diedFx[_diedFxIndex].transform.position = pos + force * 0.1f; //_destoryFx[_destroyIndex].GetComponent().TextureChange(tex, scale, destroy); _diedFx[_diedFxIndex].SetActive(true); } /// /// /// /// /// /// public int CreateShadow(Transform enemy, float size) { var shadowIndex = 0; for (int i = 0; i < ENEMY_COUNT_MAX; i++) { Vector3 position = _shadows[i].position; if (position.y > 2f) { shadowIndex = i; break; } } _shadows[shadowIndex].parent = this.transform; _shadows[shadowIndex].position = Vector3.one * -1f; _shadows[shadowIndex].GetComponent().PickParent(enemy, size); _shadows[shadowIndex].gameObject.SetActive(true); return shadowIndex; } /// /// /// /// public void DestroyShadow(int index) { if (_shadows[index]) _shadows[index].GetComponent().Finish(); } /// /// /// /// /// public void CreateBlood(Vector3 pos, Vector3 dir) { _bloodFx[_effectIndex].gameObject.SetActive(true); if (dir != Vector3.zero) _bloodFx[_effectIndex].SetPositionAndRotation(pos + Vector3.up * 0.05f, Quaternion.LookRotation(-dir)); else _bloodFx[_effectIndex].position = pos + Vector3.up * 0.05f; _hitFx[_effectIndex].gameObject.SetActive(true); _hitFx[_effectIndex].SetPositionAndRotation(pos + dir * 0.1f + Vector3.up * 0.05f, Quaternion.Euler(Vector3.up * GlobalUtils.RndAngle)); _effectIndex = (_effectIndex + 1) % ENEMY_FX_COUNT_MAX; } /// /// /// /// /// public void CreateBlood_Only(Vector3 pos, Vector3 dir) { _hitFx[_effectIndex].gameObject.SetActive(true); _hitFx[_effectIndex].SetPositionAndRotation(pos + dir * -0.1f + Vector3.up * 0.05f, Quaternion.Euler(Vector3.up * GlobalUtils.RndAngle)); _effectIndex = (_effectIndex + 1) % ENEMY_FX_COUNT_MAX; } /// /// /// /// /// public void CreateGrabBlood(Vector3 pos, Quaternion dir) { grab_blood.gameObject.SetActive(true); grab_blood.SetPositionAndRotation(pos, dir); } public void DirectionArrow() { } /// /// /// /// /// public void CreateBox(int index, int level, Vector3 pos) { if (index > 0) { pos.x = Mathf.Clamp(pos.x, 0f - _limit_x, _limit_x); pos.z = Mathf.Clamp(pos.z, _limit_y_b, _limit_y_f); EntityInfo ei = new EntityInfo() { itemId = index, level = level, x = pos.x, z = pos.z, y = 0.1f, }; EntityManager.Instance.CreateDropItem(ei); } } #endregion #region Sound public string trustSnd; public string smashSnd; public string[] slashSnds = new string[2]; public string blockSnd; public string blockBreakSnd; public string splitSnd; private int _rndsound; private float _delay; private AudioSource _audioSource; private readonly Dictionary _audioClips = new Dictionary(); public void PlaySound(string name) { #if !PLAY_PLATFORM_SOUND if (_audioClips.TryGetValue(name, out var audioClip)) PlaySound(audioClip); #else SoundProxy.PlayFxAsync(name); #endif } private void PlaySound(AudioClip clip) { if (_audioSource && clip) { _audioSource.clip = clip; _audioSource.Play(); } } public void PlaySoundOneShot(string name) { #if !PLAY_PLATFORM_SOUND if (_audioClips.TryGetValue(name, out var audioClip)) PlaySoundOneShot(audioClip); #else SoundProxy.PlayFxAsync(name); #endif } /// /// /// /// private void PlaySoundOneShot(AudioClip clip) { if (_audioSource && clip) { _audioSource.PlayOneShot(clip); } } /// /// /// /// public void SoundOn(int soundkind) { switch (soundkind) { case 4: PlaySoundOneShot(splitSnd); break; case 3: PlaySound(trustSnd); break; case 2: PlaySoundOneShot(smashSnd); break; case 1: if (_delay <= 0f) { _rndsound = Random.Range(0, 2); PlaySound(slashSnds[_rndsound]); } break; case 0: PlaySound(blockSnd); break; case -1: PlaySoundOneShot(blockBreakSnd); break; } _delay = 0.05f; } #endregion #region Unity public static EnemyHelper Instance; /// /// /// private void Awake() { Instance = this; _audioSource = this.GetComponent(); SoundProxy.Instance.SetAudioMixer(_audioSource, SoundProxy.FX_LAYER_INDEX); for (int i = 0; i < ENEMY_COUNT_MAX; i++) { _shadows[i] = Instantiate(shadow, _originPos, Quaternion.identity); _shadows[i].gameObject.SetActive(false); } for (int i = 0; i < ENEMY_FX_COUNT_MAX; i++) { _hitFx[i] = Instantiate(ef_hit, _originPos, Quaternion.identity, this.transform); _bloodFx[i] = Instantiate(ef_blood, _originPos, Quaternion.identity, this.transform); _diedFx[i] = Instantiate(ef_dead, this.transform); } } private System.Collections.IEnumerator Start() { grab_blood = Instantiate(ef_blood, _originPos, Quaternion.identity, this.transform); grab_blood.localScale = new Vector3(0.5f, 1.2f, 1.2f); #if !PLAY_PLATFORM_SOUND var handle = AssetProxy.Instance.TryGetTemporaryAssetsAsync("cha_sound"); yield return handle; foreach (var clip in handle.Result) { _audioClips.Add(clip.name, clip); } #else yield return null; #endif } private void FixedUpdate() { if (_delay > 0f) { _delay -= Time.deltaTime; } } #endregion } }