状态机

  • 不同状态下的相同操作
游戏状态机
播放器状态机
  • 灯开关示例
    /**
     * 灯
     */
    function Light()
    {
        
        var _state = new StateOFF();
        
        /**
         * 更改状态
         */
        function state(v)
        {
            _state = v;
        }
        
        
        /**
         * 开
         */
        function open()
        {
            _state.open(this)
        }
        
        
        /**
         * 关
         */
        function close()
        {
            _state.close(this)
        }
        
        
        return{
            
            state:state,
            open:open,
            close:close
        }
    }
    
    
    
    /**
     * 灯开着的状态
     */
    function StateON()
    {
        
        function open(light)
        {
            console.log('本次操作被忽略,灯处于打开状态')
        }
        
        function close(light)
        {
            light.state(new StateOFF())
            console.log('关灯成功')
        }
        
        return{
            
            open:open,
            close:close
        }
    }
    
    
    
    /**
     * 灯关着的状态
     */
    function StateOFF()
    {
        
        function open(light)
        {
            light.state(new StateON())
            console.log('开灯成功')
        }
        
        function close(light)
        {
            console.log('本次操作被忽略,灯处于关闭状态')
        }
        
        return{
            
            open:open,
            close:close
        }
    }
    
    
    
    /**
     * 测试应用
     */
    var light = new Light();
    
    /**
     * 侦听用户交互
     */
    window.addEventListener('click',function(e)
    {
        
        if(e.target.id == 'on')
        {
            light.open()
        }
        else
        {
            light.close()
        }
    })
  • 游戏状态机示例
    /**
     * 游戏人物
     */
    function Person()
    {
        
        /**
         * 默认状态
         */
        var _state = new StateStand();
        
        
        /**
         * 切换状态
         */
        function state(v)
        {
            _state = v;
        }
        
        
        /**
         * 上
         */
        function top()
        {
            _state.top(this)
        }
        
        
        /**
         * 下
         */
        function bottom()
        {
            _state.bottom(this)
        }
        
        
        return{
            
            state:state,
            top:top,
            bottom:bottom
        }
        
    }
    
    
    
    /**
     * 站立状态
     */
    function StateStand()
    {
        
        /**
         * 上
         */
        function top(p)
        {
            console.log('切换到跳跃状态');
            p.state(new StateJump())
        }
        
        
        /**
         * 下
         */
        function bottom(p)
        {
            console.log('切换到下蹲状态')
            p.state(new StateSquat())
        }
        
        return{
            
            top:top,
            bottom:bottom
        }
    }
    
    
    /**
     * 跳跃
     */
    function StateJump()
    {
        /**
         * 上
         */
        function top(p)
        {
            console.log('此操作忽略,跳跃中不能再次跳跃')
        }
        
        /**
         * 下
         */
        function bottom(p)
        {
            console.log('切换到下斩状态')
            p.state(new StateBehead())
        }
        
        return{
            
            top:top,
            bottom:bottom
        }
    }
    
    
    /**
     * 下斩
     */
    function StateBehead()
    {
        /**
         * 上
         */
        function top(p)
        {
            console.log('切换到站立状态')
            p.state(new StateStand())
        }
        
        /**
         * 下
         */
        function bottom()
        {
            console.log('此操作忽略,下斩中不能再次下斩')
        }
        
        return{
            
            top:top,
            bottom:bottom
        }
    }
    
    
    /**
     * 下蹲
     */
    function StateSquat()
    {
        /**
         * 上
         */
        function top(p)
        {
            console.log('请按下键释放')
        }
        
        /**
         * 下
         */
        function bottom(p)
        {
            console.log('切换到站立状态')
            p.state(new StateStand())
        }
        
        return{
            
            top:top,
            bottom:bottom
        }
    }
    
    
    
    
    
    /**
     * 游戏状态机应用
     */
    var person = new Person();
    
    
    window.addEventListener('keydown',function(e)
    {
        
        if(e.keyCode == '38')
        {
            person.top();
        }
        
        if(e.keyCode == '40')
        {
            person.bottom();
        }
    })
  • 播放器状态机
    /**
     * 播放器
     */
    function Player(video)
    {
        
        
        /**
         * 默认状态
         */
        var _state = new StateStop();
        
        
        /**
         * 切换状态
         */
        function state(v)
        {
            _state = v;
        }
        
        
        /**
         * 播放
         */
        function play()
        {
            _state.play(this)
        }
        
        
        /**
         * 暂停
         */
        function pause()
        {
            _state.pause(this)
        }
        
        
        /**
         * 停止
         */
        function stop()
        {
            _state.stop(this)
        }
        
        
        return{
            
            video:video,
            state:state,
            play:play,
            pause:pause,
            stop:stop
        }
        
    }
    
    
    
    /**
     * 停止状态
     */
    function StateStop()
    {
        
        /**
         * 播放
         */
        function play(p)
        {
            
            console.log('切换到播放状态')
            p.state(new StatePlay());
            p.video.play();
        }
        
        
        /**
         * 暂停
         */
        function pause(p)
        {
            console.log('此操作被忽略,停止中不可暂停')
        }
        
        
        /**
         * 停止
         */
        function stop(p)
        {
            console.log('此操作被忽略,停止中不可再次停止')
        }
        
        
        return{
            
            play:play,
            pause:pause,
            stop:stop
        }
        
    }
    
    
    /**
     * 播放状态
     */
    function StatePlay()
    {
        
        /**
         * 播放
         */
        function play(p)
        {
            console.log('此操作被忽略,播放中不可再次播放')
        }
        
        
        /**
         * 暂停
         */
        function pause(p)
        {
            console.log('切换到暂停状态')
            p.state(new StatePause());
            p.video.pause();
        }
        
        
        /**
         * 停止
         */
        function stop(p)
        {
            console.log('切换到停止状态')
            p.state(new StateStop());
            //p.video.stop();
        }
        
        
        return{
            
            play:play,
            pause:pause,
            stop:stop
        }
        
    }
    
    
    /**
     * 暂停状态
     */
    function StatePause()
    {
        
        /**
         * 播放
         */
        function play(p)
        {
            console.log('切换到播放状态')
            p.state(new StatePlay());
            p.video.play();
        }
        
        
        /**
         * 暂停
         */
        function pause(p)
        {
            console.log('此操作被忽略,暂停中不可再次暂停')
        }
        
        
        /**
         * 停止
         */
        function stop(p)
        {
            console.log('切换到停止状态')
            p.state(new StateStop());
            //p.video.stop();
        }
        
        
        return{
            
            play:play,
            pause:pause,
            stop:stop
        }
        
    }
    
    
    
    
    /**
     * 测试播放状态机
     */
    
    var player = new Player(document.getElementsByTagName('video')[0]);
    
    
    window.addEventListener('click',function(e)
    {
        
        if(e.target.id == 'play')
        {
            player.play()
        }
        
        
        if(e.target.id == 'pause')
        {
            player.pause()
        }
        
        
        if(e.target.id == 'stop')
        {
            player.stop()   
        }
        
    })
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 状态机是无论科研探索还是科技应用方面都非常重要的一种分析工具。几乎在所有涉及到随时间演化的问题中,都可以找到状态机...
    Esmool阅读 4,531评论 0 26
  • 这个系列最终实现的状态机并不是一个标准的状态机,把状态机的很多标准的概念进行了简化,对概念的东西做了减法,实现了具...
    iPolaris阅读 34,941评论 2 40
  • 上一篇文章写完之后,我一直在想做一个游戏示例,想将之前学到的状态机模式运用在其中。然而一直遇到各种奇怪的bug(之...
    qufl阅读 2,570评论 0 52
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,156评论 19 139
  • 『代码github地址』 标签: 有限状态机,Akka fsm,squirrel-foundation,java状...
    醉叁重阅读 30,234评论 3 24