feat: 添加了受击反馈mod chore: 优化了隐藏角色的代码

This commit is contained in:
m0_75251201
2025-11-02 11:43:21 +08:00
parent 2af09007f9
commit 2b7943339c
48 changed files with 253 additions and 36 deletions

View File

@@ -1,7 +1,116 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Duckov;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
using Random = UnityEngine.Random;
namespace HitFeedback
{
public class ModBehaviour:Duckov.Modding.ModBehaviour
{
public const string AudioFolderName = "audio";
public string audioFolderPath;
public List<string> audioFilePath = new List<string>();
public Health health;
public Config config=new Config();
private void Update()
{
if (Input.GetKeyDown(KeyCode.F8))
{
PlayRandomAudioClip();
}
}
protected override void OnAfterSetup()
{
LevelManager.OnLevelInitialized += OnSceneLoaded;
audioFolderPath=Path.Combine(info.path,AudioFolderName);
FindWavFiles();
}
protected override void OnBeforeDeactivate()
{
LevelManager.OnLevelInitialized -= OnSceneLoaded;
}
private void FindWavFiles()
{
audioFilePath.Clear();
if (!Directory.Exists(audioFolderPath))
{
return;
}
try
{
string[] files = Directory.GetFiles(audioFolderPath, "*.wav", SearchOption.TopDirectoryOnly);
if (files.Length > 0)
{
foreach (string filePath in files)
{
audioFilePath.Add(filePath);
}
}
}
catch (UnauthorizedAccessException ex)
{
Debug.LogError($"Error: Access to '{audioFolderPath}' is denied. {ex.Message}");
}
catch (DirectoryNotFoundException ex)
{
Debug.LogError($"Error: Directory '{audioFolderPath}' not found. {ex.Message}");
}
catch (Exception ex)
{
Debug.LogError($"An unexpected error occurred: {ex.Message}");
}
}
private void OnSceneLoaded()
{
TryAddListener();
}
private void TryAddListener()
{
if (health)
{
health.OnHurtEvent.RemoveListener(OnHurtEvent);
}
health = CharacterMainControl.Main?.Health;
if (health)
{
health.OnHurtEvent.AddListener(OnHurtEvent);
}
}
private void OnHurtEvent(DamageInfo damageInfo)
{
PlayRandomAudioClip();
}
public void PlayRandomAudioClip()
{
if (audioFilePath.Count > 0)
{
var randomIndex = Random.Range(0, audioFilePath.Count);
var filePath = audioFilePath[randomIndex];
AudioManager.PostCustomSFX(filePath);
}
else
{
Debug.LogWarning("Mod Feedback: No audio clips loaded to play.");
}
}
}
}