Unity 麦扣 x 勇士传说 全解析 之 有限状态机(附各模块知识的链接,零基础也包学会的牢弟)(案例难度:★★☆☆☆)

        在编码前我一直有个疑问,为什么不是将方法写在一个一个类中,或者用的单例模式写个管理器来继承的方式来做怪物脚本,玩家控制和玩家动画控制的代码混在一起不说,与其他脚本之间的交互,让过于冗杂的代码不易阅读

        这节开始应用的有限状态机,似乎一定程度上解决了这个问题,但是之前的代码写都写了,所以只能将一部分放到状态机之中,将就写吧

1.有限状态机

思考,写一个基类状态机,应该有哪些状态机类,应该写成什么类型?

1.进入和退出肯定要有,也要有帧更新,还有因为该案例的移动方式是利用刚体的速度,所以还需要一个物理帧更新

而玩家和怪物要分别继承所以可以用抽象类,用接口也可以,缺点就是不能写一些基础的行为让子类继承

public abstract class BaseState 
{
    public abstract void OnEnter();
    public abstract void NormalUpdate();
    public abstract void PhysicsUpdate();
    public abstract void OnExit();        
}

野猪的巡逻状态 

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Xml;
using UnityEngine;

public class BoarPaxtrolState : BaseState {

    public override void OnEnter(Monster wcurrentMonster) {
        currentMonster = wcurrentMonster;
    }
    public override void NormalUpdate() {

        if ((currentMonster.headFor.x > 0 && currentMonster.physicscheck.isRightWall) 
            || (currentMonster.headFor.x < 0 && currentMonster. physicscheck.isLeftWall)
            ||!currentMonster.physicscheck.isGround) 
            {
            currentMonster.waitState = true;
            currentMonster.animator.SetBool("walk", false);
        }
    }

    public override void PhysicsUpdate() {

    }

    public override void OnExit() {
        currentMonster.animator.SetBool("walk",false);
    }


}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值