<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 1  文章 - 37  trackbacks - 0
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    留言簿(16)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    test

    搜索

    •  

    最新評論

    人物角色有多種動作:上馬,下馬,走,跑,物理攻擊,魔法攻擊,施放魔法,交易 .....等等
    每個動作間隔為600毫秒,任務有可能會執行失敗。

    /**
     * 角色動作調度器
     * 
    @author Donf Yang
     
    */

    public final class ActionDispatcher {

        
    /**
         * 上一次動作的執行時間
         
    */

        
    private static long previousActionTime = 0;

        
    /**
         * 每次動作執行時間間隔
         
    */

        
    public static final int ACTION_INTERVAL = 600;

        
    /**
         * 上一個ActionMission,不受調度器控制,當currentActionMission不可執行時,調度該Mission [未實現]
         
    */

        
    private static ActionMission previousActionMission = null;
        
    /**
         * 下一個ActionMission,不受調度器控制,當continueDispatch時,優先調度
         
    */

        
    private static ActionMission nextActionMission = null;

        
    private static ActionMission currentActionMission = null;

        
    private static final String ISDISPATCHLOCKME = "IL";
        
    private static final String ACTIONLOCKME = "AL";

        
    private static Logger log = LoggerFactory.getLogger(ActionDispatcher.class);

        
    private static boolean isActionExecuting = false;

        
    /**
         * dispatch之前,先判斷是否可以被調度isDispatchable(); 如果不能被調度,調用setNextActionMission();
         * 
         * 
    @param action
         * 
    @throws Exception
         
    */

        
    public static void dispatch(ActionMission action) throws Exception {

            
    if (!isDispatchable()) {
                
    return;
            }

            
    try {
                setDispatching(
    true);
                currentActionMission 
    = action;
                
    /*
                 * 如果Action時間為0,說明該動作沒有執行
                 
    */

                
    long _l = action.doMission();
                
    if (_l == 0{
                    setDispatching(
    false);
                }
     
            }
     catch (Exception e) {
                setDispatching(
    false);
                
    throw e;
            }

        }


        
    /**
         * GameProcess接受到服務器返回的動作執行成功指令
         
    */

        
    public static void actionGood() {
            currentActionMission.returnActionGood();
            
    try {
                continueDispatch();
            }
     catch (Exception e) {
                setDispatching(
    false);
                e.printStackTrace();
            }


        }


        
    /**
         * GameProcess接受到服務器返回的動作執行失敗指令
         
    */

        
    public static void actionFail() {
            currentActionMission.returnActionFail();
            
    try {
                continueDispatch();
            }
     catch (Exception e) {
                setDispatching(
    false);
                e.printStackTrace();
            }

        }


        
    private static void continueDispatch() throws Exception {
            
    synchronized (ACTIONLOCKME) {
                
    if (nextActionMission != null{
                    currentActionMission 
    = nextActionMission;
                    nextActionMission 
    = null;
                }

            }

            
    try {
                
    if (currentActionMission.isContinueExecutable()) {
                    setDispatching(
    true);
                    
    /*
                     * 如果Action時間為0,說明該動作沒有執行
                     
    */

                    
    long _l = currentActionMission.continueDoMission();
                    
    if (_l == 0{
                        setDispatching(
    false);
                    }

                }
     else {
                    setDispatching(
    false);
                }

            }
     catch (Exception e) {
                setDispatching(
    false);
                
    throw e;
            }


        }


        
    /**
         * 設置當前是否有Mission正在被執行的狀態
         * 
    @param dispatching
         
    */

        
    private static void setDispatching(boolean dispatching) {
            
    synchronized (ISDISPATCHLOCKME) {
                isActionExecuting 
    = dispatching;
            }

        }


        
    /**
         * Mission是否可以被馬上執行,由當前是否有Mission正在被執行決定
         * 
    @return
         
    */

        
    public static boolean isDispatchable() {
            
    synchronized (ISDISPATCHLOCKME) {
                
    return !isActionExecuting;
            }

        }


        
    public static void setNextActionMission(ActionMission actionMission) {
            
    synchronized (ACTIONLOCKME) {
                nextActionMission 
    = actionMission;
            }

        }


        
    public static void setPreviousActionMission(ActionMission actionMission) {
            
    synchronized (ACTIONLOCKME) {
                previousActionMission 
    = actionMission;
            }

        }

        
        
    /**
         * 設置調度時間,600毫秒
         * 
    @return
         
    */

        
    public static long dispatchActionTime(){
            
    long _l = ActionDispatcher.generateCurrentActionTime();
            
    if (_l - previousActionTime < ActionDispatcher.ACTION_INTERVAL) {
                
    try {
                    Thread.sleep((ActionDispatcher.ACTION_INTERVAL 
    - (_l - previousActionTime)));
                }
     catch (Exception e) {
                    e.printStackTrace();
                }

                previousActionTime 
    = ActionDispatcher.generateCurrentActionTime();
            }
     else {
                previousActionTime 
    = _l;
            }

            
    return previousActionTime;
        }


        
    /**
         * 為Action生成當前時間
         * 
    @return
         
    */

        
    public static long generateCurrentActionTime() {
            Calendar now 
    = Calendar.getInstance();
            
    return now.get(Calendar.HOUR_OF_DAY) * 3600000 + now.get(Calendar.MINUTE) * 60000
                    
    + now.get(Calendar.SECOND) * 1000 + now.get(Calendar.MILLISECOND);
        }


    }


     

    /**
     * ActionMission,可調度的動作任務,該任務可以是一個簡單的移動,也可以包含N層任務的AI流程
     * 
    @author Donf Yang
     
    */

    public interface ActionMission {

        
    /**
         * 首次執行Mission
         * 
    @return 返回Action執行時的當前時間,如果為0,說明沒有被執行
         * 
    @throws Exception
         
    */

        
    public long doMission() throws Exception;

        
    /**
         * 繼續執行Mission
         * 
    @return 返回Action執行時的當前時間,如果為0,說明沒有被執行
         * 
    @throws Exception
         
    */

        
    public long continueDoMission() throws Exception;
        
        
    /**
         * 服務器返回動作執行成功,該方法不能有過多的動作邏輯
         
    */

        
    public void returnActionGood();
        
        
    /**
         * 服務器返回動作執行失敗,該方法不能有過多的動作邏輯
         
    */

        
    public void returnActionFail();

        
    /**
         * 是否需要(可以)繼續執行
         * 
    @return
         
    */

        
    public boolean isContinueExecutable();

    }


    動作的回調接口

    /**
     * 
     * ControllableAction回調接口 <br />
     * 1. 停止當前動作<br />
     * 2. 當動作完成時,得到通知
     * 
     * 
    @author Donf Yang
     * @2008-7-8 下午09:31:44
     
    */

    public interface ActionObserver {

        
    public void onControllableActionFinished(ControllableAction action);
        
    }

    /**
     * 
     * 所有可控制的動作接口需要繼承該接口,用于回調 <br />
     * 
     * 
    @author Donf Yang
     * @2008-7-8 下午09:32:31
     
    */

    public interface ControllableAction {
        
        
    public void stopAction();

    }
    posted on 2008-07-06 18:05 Phrancol Yang 閱讀(486) 評論(0)  編輯  收藏 所屬分類: Mir3gAnyWhere

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产精品久久久久久亚洲影视 | 日韩免费电影网站| 久久久精品2019免费观看| 日本XXX黄区免费看| 亚洲精品岛国片在线观看| 78成人精品电影在线播放日韩精品电影一区亚洲 | 亚洲日韩在线视频| 一个人看的免费观看日本视频www 一个人看的免费视频www在线高清动漫 | 国产三级免费观看| 精品亚洲成a人片在线观看少妇| 亚洲精品无码mⅴ在线观看| 亚洲精品无码日韩国产不卡av| 国内精品乱码卡1卡2卡3免费| 中文文字幕文字幕亚洲色| 久久九九久精品国产免费直播| 一二三四在线播放免费观看中文版视频 | 亚洲冬月枫中文字幕在线看| 韩国免费A级毛片久久| 韩国二级毛片免费播放| 精品亚洲成a人片在线观看| 最近免费中文字幕大全高清大全1| 亚洲精品午夜视频| 午夜视频在线免费观看| 亚洲日本一区二区一本一道 | 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲成AV人片一区二区密柚| 污污视频免费观看网站| 成人免费无码大片a毛片| 亚洲六月丁香六月婷婷色伊人| 成人超污免费网站在线看| 四虎影视永久在线精品免费| 在线观看91精品国产不卡免费| 亚洲最大成人网色香蕉| 日本v片免费一区二区三区| 国产免费牲交视频免费播放| 亚洲一本综合久久| 久久www免费人成精品香蕉| 亚洲系列国产精品制服丝袜第| 天天干在线免费视频| 亚洲欧美成人av在线观看| 国产精品亚洲玖玖玖在线观看|