Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
AudioManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance;
[SerializeField] private float sfxMinimumDistance;
[SerializeField] private AudioSource[] sfx;
[SerializeField] private AudioSource[] bgm;
public bool playBgm;
private int bgmIndex;
private void Awake()
{
if (instance != null)
{
Destroy(instance.gameObject);
}
else
instance = this;
}
private void Update()
{
if (!playBgm)
StopAllBGM();
else
{
if (!bgm[bgmIndex].isPlaying)
PlayBGM(bgmIndex);
}
}
public void PlaySFX(int _sfxIndex,Transform _source)
{
if (sfx[_sfxIndex].isPlaying)//防止出现多个相同物体同时发出声音
{
return;
}
if (_source != null && Vector2.Distance(PlayerManager.instance.player.transform.position, _source.position) > sfxMinimumDistance)//防止东西很远的情况下发出声音
return;
if(_sfxIndex < sfx.Length)
{
sfx[_sfxIndex].pitch = Random.Range(.85f, 1.1f);
sfx[_sfxIndex].Play();
}
}
public void StopSFX(int _sfxIndex)
{
sfx[_sfxIndex].Stop();
}
public void PlayRandomBGM()
{
bgmIndex = Random.Range(0, bgm.Length);
PlayBGM(bgmIndex);
}
public void PlayBGM(int _bgmIndex)
{
bgmIndex = _bgmIndex;
StopAllBGM();
if (_bgmIndex < sfx.Length)
{
bgm[_bgmIndex].Play();
}
}
public void StopAllBGM()
{
for(int i = 0; i < bgm.Length; i++)
{
bgm[i].Stop();
}
}
}
SkeletonIdleState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkeletonIdleState : SkeletonGroundState
{
public SkeletonIdleState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Skeleton _enemy) : base(_enemyBase, _stateMachine, _animBoolName, _enemy)
{
}
public override void Enter()
{
base.Enter();
AudioManager.instance.PlaySFX(24, enemy.transform);
stateTimer = enemy.idleTime;
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
if(stateTimer < 0)
{
stateMachine.ChangeState(enemy.moveState);
}
}
}
playerAnimationTriggers.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerAnimationTriggers : MonoBehaviour
{
private Player player => GetComponentInParent<Player>();//获得夫组件上的实际存在的Player组件
private void AnimationTrigger()
{
player.AnimationTrigger();
}
private void AttackTrigger()
{
AudioManager.instance.PlaySFX(2,null);
Collider2D[] colliders = Physics2D.OverlapCircleAll(player.attackCheck.position, player.attackCheckRadius);//创建一个碰撞器组,保存所有圈所碰到的碰撞器
//https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Physics2D.OverlapCircleAll.html
foreach(var hit in colliders)//https://blog.csdn.net/m0_52358030/article/details/121722077
{
if(hit.GetComponent<Enemy>()!=null)
{
EnemyStats _target = hit.GetComponent<EnemyStats>();
if(_target != null)//修复无敌人时触发DoDamge出错的bug
player.stats.DoDamage(_target);//提供一个可以从player调用敌人受伤函数的入口,通过传入受伤敌人的组件
if(Inventory.instance.GetEquipment(EquipmentType.Weapon) != null)//攻击带装备特效
Inventory.instance.GetEquipment(EquipmentType.Weapon).Effect(_target.transform);
// hit.GetComponent<Enemy>().DamageImpact();
}
}
}
private void ThrowSword()
{
SkillManager.instance.sword.CreateSword();
}
}