隨筆 - 1  文章 - 37  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿(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 閱讀(481) 評論(0)  編輯  收藏 所屬分類: Mir3gAnyWhere

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


          網站導航:
           
          主站蜘蛛池模板: 革吉县| 延安市| 额尔古纳市| 通江县| 偃师市| 平利县| 克什克腾旗| 灵石县| 昌图县| 双城市| 仪陇县| 上蔡县| 桃江县| 搜索| 尼玛县| 杭州市| 庄河市| 宜兰县| 桐柏县| 武邑县| 保康县| 灵台县| 保德县| 曲靖市| 赤峰市| 云安县| 镇宁| 博客| 仙桃市| 雅江县| 阿图什市| 南京市| 柳江县| 儋州市| 盐津县| 津市市| 南岸区| 扶余县| 永济市| 巴青县| 即墨市|