Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class Enemy : Entity
{[SerializeField] protected LayerMask whatIsPlayer;[Header("Move Info")]public float moveSpeed;public float idleTime;[Header("Attack Info")]public float attackDistance;#region 类public EnemyStateMachine stateMachine;#endregionprotected override void Awake(){base.Awake();stateMachine = new EnemyStateMachine();}protected override void Start(){base.Start();}protected override void Update(){base.Update();stateMachine.currentState.Update();//Debug.Log(IsPlayerDetected().collider.gameObject.name + "I see");//这串代码会报错,可能使版本的物体,因为在没有找到Player的时候物体是空的,NULL,你想让他在控制台上显示就报错了}public virtual RaycastHit2D IsPlayerDetected() => Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, 50, whatIsPlayer);//用于从射线投射获取信息的结构。//该函数的返回值可以变,可以只返回bool,也可以是碰到的结构protected override void OnDrawGizmos(){base.OnDrawGizmos();Gizmos.color = Color.yellow;Gizmos.DrawLine(transform.position, new Vector3(transform.position.x + attackDistance * facingDir, transform.position.y));}
}
Enemy_Skeleton.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditorInternal;
using UnityEngine;public class Enemy_Skeleton : Enemy
{#region 类Statepublic SkeletonIdleState idleState { get; private set; }public SkeletonMoveState moveState { get; private set; }public SkeletonBattleState battleState { get; private set; }#endregionprotected override void Awake(){base.Awake();idleState = new SkeletonIdleState(this, stateMachine, "Idle", this);moveState = new SkeletonMoveState(this,stateMachine, "Move", this);battleState = new SkeletonBattleState(this, stateMachine, "Move", this);}protected override void Start(){base.Start();stateMachine.Initialize(idleState);}protected override void Update(){base.Update();}
}
EnemyState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class EnemyState
{protected Enemy enemyBase;protected EnemyStateMachine stateMachine;protected Rigidbody2D rb;//小简化protected bool triggerCalled;private string animBoolName;protected float stateTimer;public EnemyState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName){this.enemyBase = _enemyBase;this.stateMachine = _stateMachine;this.animBoolName = _animBoolName;}public virtual void Enter(){triggerCalled = false;enemyBase.anim.SetBool(animBoolName, true);rb = enemyBase.rb;//小简化}public virtual void Update(){stateTimer -= Time.deltaTime;}public virtual void Exit(){enemyBase.anim.SetBool(animBoolName, false);}}
SkeletonGroundState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SkeletonGroundState : EnemyState
{protected Enemy_Skeleton enemy;public SkeletonGroundState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName,Enemy_Skeleton _enemy) : base(_enemyBase, _stateMachine, _animBoolName){enemy = _enemy;}public override void Enter(){base.Enter();}public override void Exit(){base.Exit();}public override void Update(){base.Update();if(enemy.IsPlayerDetected()){stateMachine.ChangeState(enemy.battleState);}}
}
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();stateTimer = enemy.idleTime;}public override void Exit(){base.Exit();}public override void Update(){base.Update();if(stateTimer < 0){stateMachine.ChangeState(enemy.moveState);}}
}
SkeletonMoveState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SkeletonMoveState : SkeletonGroundState
{public SkeletonMoveState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Skeleton _enemy) : base(_enemyBase, _stateMachine, _animBoolName, _enemy){}public override void Enter(){base.Enter();}public override void Exit(){base.Exit();}public override void Update(){base.Update();enemy.SetVelocity(enemy.moveSpeed * enemy.facingDir, rb.velocity.y);if(enemy.IsWallDetected()||!enemy.IsGroundDetected())//当撞墙或者没有路的时候翻转{enemy.Flip();stateMachine.ChangeState(enemy.idleState);}}
}
SkeletonBattleState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SkeletonBattleState : EnemyState
{private Transform player;//用于给Player定位,好判断怎么跟上他private Enemy_Skeleton enemy;private int moveDir;public SkeletonBattleState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName,Enemy_Skeleton _enemy ) : base(_enemyBase, _stateMachine, _animBoolName){enemy = _enemy;}public override void Enter(){base.Enter();player = GameObject.Find("Player").transform;//全局找Player位置}public override void Exit(){base.Exit();}public override void Update(){base.Update();if(enemy.IsPlayerDetected().distance < enemy.attackDistance){Debug.Log("I Attack");enemy.ZeroVelocity();return;}if(player.position.x > enemy.transform.position.x){moveDir = 1;}else if(player.position.x<enemy.transform.position.x){moveDir = -1;}enemy.SetVelocity(enemy.moveSpeed * moveDir, rb.velocity.y);}
}