2025-05-18 01:04:31 +08:00

430 lines
9.6 KiB
C#

// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2017-11-14
//
// Last Modified By : Kimch
// Last Modified On :
// ***********************************************************************
// <copyright file= "EnemyHelper" company=""></copyright>
// <summary></summary>
// ***********************************************************************
#if SDK_WECHAT_WASM
#define PLAY_PLATFORM_SOUND
#endif
using System.Collections.Generic;
using UnityEngine;
namespace G
{
/// <summary>
///
/// </summary>
public class EnemyHelper : MonoBehaviour
{
#region Field
private const int ENEMY_COUNT_MAX = 32;
private const int ENEMY_FX_COUNT_MAX = 16;
/// <summary>
///
/// </summary>
public Transform shadow;
/// <summary>
///
/// </summary>
public Transform ef_hit;
/// <summary>
///
/// </summary>
public Transform ef_blood;
/// <summary>
///
/// </summary>
public Transform ef_block;
/// <summary>
///
/// </summary>
public Transform arrowmark;
/// <summary>
///
/// </summary>
public Transform pt_itemdrop;
/// <summary>
///
/// </summary>
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];
/// <summary>
///
/// </summary>
private readonly Transform[] _hitFx = new Transform[ENEMY_FX_COUNT_MAX];
/// <summary>
///
/// </summary>
private readonly Transform[] _bloodFx = new Transform[ENEMY_FX_COUNT_MAX];
/// <summary>
///
/// </summary>
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
/// <summary>
///
/// </summary>
/// <param name="style"></param>
/// <returns></returns>
public int GetGrabForce(int style)
{
return _grabForce[style];
}
/// <summary>
///
/// </summary>
/// <param name="style"></param>
/// <returns></returns>
public int GetKickForce(int style)
{
return _kickForce[style];
}
public void FinishEfs()
{
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
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<Renderer>().material = blood_mat[index];
}
}
_efAttribute = index;
}
/// <summary>
///
/// </summary>
/// <param name="destroy"></param>
/// <param name="pos"></param>
/// <param name="scale"></param>
/// <param name="force"></param>
/// <param name="tex"></param>
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<Mon_Destroy>().TextureChange(tex, scale, destroy);
_diedFx[_diedFxIndex].SetActive(true);
}
/// <summary>
///
/// </summary>
/// <param name="enemy"></param>
/// <param name="size"></param>
/// <returns></returns>
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<Shadow>().PickParent(enemy, size);
_shadows[shadowIndex].gameObject.SetActive(true);
return shadowIndex;
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
public void DestroyShadow(int index)
{
if (_shadows[index])
_shadows[index].GetComponent<Shadow>().Finish();
}
/// <summary>
///
/// </summary>
/// <param name="pos"></param>
/// <param name="dir"></param>
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;
}
/// <summary>
///
/// </summary>
/// <param name="pos"></param>
/// <param name="dir"></param>
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;
}
/// <summary>
///
/// </summary>
/// <param name="pos"></param>
/// <param name="dir"></param>
public void CreateGrabBlood(Vector3 pos, Quaternion dir)
{
grab_blood.gameObject.SetActive(true);
grab_blood.SetPositionAndRotation(pos, dir);
}
public void DirectionArrow()
{
}
/// <summary>
///
/// </summary>
/// <param name="level"></param>
/// <param name="pos"></param>
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<string, AudioClip> _audioClips = new Dictionary<string, AudioClip>();
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
}
/// <summary>
///
/// </summary>
/// <param name="clip"></param>
private void PlaySoundOneShot(AudioClip clip)
{
if (_audioSource && clip)
{
_audioSource.PlayOneShot(clip);
}
}
/// <summary>
///
/// </summary>
/// <param name="soundkind"></param>
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;
/// <summary>
///
/// </summary>
private void Awake()
{
Instance = this;
_audioSource = this.GetComponent<AudioSource>();
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<AudioClip>("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
}
}