Changeset b660017 in advance-wars


Ignore:
Timestamp:
Jun 6, 2011, 5:09:12 PM (13 years ago)
Author:
dportnoy <devnull@…>
Branches:
master
Children:
fea4b77
Parents:
379005b
Message:

Removed the AppState enum and a bunch of code associated with it. This was all from the demo app that was used as a starting point for blackjack and looks like it's only useful for games that run in real-time, as opposed to turn-based.

Location:
src/com/medievaltech/advancewars
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/com/medievaltech/advancewars/Game.java

    r379005b rb660017  
    9999        if (savedInstanceState == null) {       // we were just launched: set up a new game
    100100                Log.w("AdvanceWars", "SIS is null");
    101                
    102                 mThread.setState(AppState.RUNNING);
    103101        } else {
    104102            Log.w("AdvanceWars", "SIS is nonnull");
    105103           
    106             mThread.setState(AppState.RUNNING);
    107            
    108104            mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");
    109105        }
    110     }
    111 
    112     /**
    113      * Invoked when the Activity loses user focus.
    114      */
    115     @Override
    116     protected void onPause() {
    117         super.onPause();
    118         mGameView.getThread().pause(); // pause game when Activity pauses
    119106    }
    120107
  • src/com/medievaltech/advancewars/GameView.java

    r379005b rb660017  
    1818       
    1919    class DrawingThread extends Thread {       
    20         public AppState mAppState;
    2120        public GameState mGameState;
    2221
     
    2827        private int mCanvasWidth = 1;
    2928
    30         /** Message handler used by thread to interact with TextView */
    31         private Handler mHandler;
    32 
    33         /** Used to figure out elapsed time between frames */
    34         private long mLastTime;
    35 
    3629        /** Paint to draw the lines on screen. */
    3730        private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1, mTilePaint2, mSelectionPaint,
     
    5346       
    5447        public DrawingThread(SurfaceHolder surfaceHolder, Context context, Handler handler) {
    55             // get handles to some important objects
    5648            mSurfaceHolder = surfaceHolder;
    57             mHandler = handler;
    5849           
    5950            mLinePaint = new Paint();
     
    126117        public void doStart() {
    127118            synchronized (mSurfaceHolder) {
    128                 mLastTime = System.currentTimeMillis() + 100;
    129                 setState(AppState.RUNNING);
    130119                Log.i("AdvanceWars", "Player's turn starting now");
    131120                mGameState = GameState.MAIN_MENU;
    132             }
    133         }
    134 
    135         /**
    136          * Pauses the physics update & animation.
    137          */
    138         public void pause() {
    139             synchronized (mSurfaceHolder) {
    140                 if (mAppState == AppState.RUNNING) setState(AppState.PAUSE);
    141121            }
    142122        }
     
    149129                    c = mSurfaceHolder.lockCanvas(null);
    150130                    synchronized(mSurfaceHolder) {
    151                         if(mAppState == AppState.RUNNING)
    152                                 updatePhysics();
    153131                        doDraw(c);
    154132                    }
     
    175153            mRun = b;
    176154        }
    177 
    178         /**
    179          * Sets the game mode. That is, whether we are running, paused, in the
    180          * failure state, in the victory state, etc.
    181          *
    182          * @see #setState(int, CharSequence)
    183          * @param mode one of the STATE_* constants
    184          */
    185         public void setState(AppState state) {
    186             synchronized (mSurfaceHolder) {
    187                 setState(state, null);
    188             }
    189         }
    190155       
    191156        public void setGameState(GameState state) {
    192157            synchronized (mSurfaceHolder) {
    193158                mGameState = state;
    194             }
    195         }
    196 
    197         /**
    198          * Sets the game mode. That is, whether we are running, paused, in the
    199          * failure state, in the victory state, etc.
    200          *
    201          * @param mode one of the STATE_* constants
    202          * @param message string to add to screen or null
    203          */
    204         public void setState(AppState mode, CharSequence message) {
    205             /*
    206              * This method optionally can cause a text message to be displayed
    207              * to the user when the mode changes. Since the View that actually
    208              * renders that text is part of the main View hierarchy and not
    209              * owned by this thread, we can't touch the state of that View.
    210              * Instead we use a Message + Handler to relay commands to the main
    211              * thread, which updates the user-text View.
    212              */
    213             synchronized (mSurfaceHolder) {
    214                 mAppState = mode;
    215 
    216                 if (mAppState == AppState.RUNNING) {
    217                     Message msg = mHandler.obtainMessage();
    218                     Bundle b = new Bundle();
    219                     b.putString("text", "");
    220                     b.putInt("viz", GameView.INVISIBLE);
    221                     msg.setData(b);
    222                     mHandler.sendMessage(msg);
    223                 } else {
    224                     CharSequence str = "";
    225                     str = "Mode probably changed";
    226 
    227                     if (message != null) {
    228                         str = message + "\n" + str;
    229                     }
    230 
    231                     Message msg = mHandler.obtainMessage();
    232                     Bundle b = new Bundle();
    233                     b.putString("text", str.toString());
    234                     b.putInt("viz", GameView.VISIBLE);
    235                     msg.setData(b);
    236                     //mHandler.sendMessage(msg);
    237                 }
    238159            }
    239160        }
     
    248169                Log.i("AdvanceWars", "width: "+mCanvasWidth+", height: "+mCanvasHeight);
    249170            }
    250         }
    251 
    252         /**
    253          * Resumes from a pause.
    254          */
    255         public void unpause() {
    256             // Move the real time clock up to now
    257             synchronized (mSurfaceHolder) {
    258                 mLastTime = System.currentTimeMillis() + 100;
    259             }
    260             setState(AppState.RUNNING);
    261         }
    262 
    263         /**
    264          * Handles a key-down event.
    265          *
    266          * @param keyCode the key that was pressed
    267          * @param msg the original event object
    268          * @return true
    269          */
    270         boolean doKeyDown(int keyCode, KeyEvent msg) {
    271             synchronized (mSurfaceHolder) {
    272                 boolean okStart = false;
    273                 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) okStart = true;
    274                 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) okStart = true;
    275                 if (keyCode == KeyEvent.KEYCODE_S) okStart = true;
    276 
    277                 if (okStart
    278                         && (mAppState == AppState.READY || mAppState == AppState.LOSE || mAppState == AppState.WIN)) {
    279                     // ready-to-start -> start
    280                     doStart();
    281                     return true;
    282                 } else if (mAppState == AppState.PAUSE && okStart) {
    283                     // paused -> running
    284                     unpause();
    285                     return true;
    286                 } else if (mAppState == AppState.RUNNING) {
    287                     return true;
    288                 }
    289 
    290                 return false;
    291             }
    292         }
    293 
    294         /**
    295          * Handles a key-up event.
    296          *
    297          * @param keyCode the key that was pressed
    298          * @param msg the original event object
    299          * @return true if the key was handled and consumed, or else false
    300          */
    301         boolean doKeyUp(int keyCode, KeyEvent msg) {
    302             boolean handled = false;
    303 
    304             synchronized (mSurfaceHolder) {
    305                 if (mAppState == AppState.RUNNING) {
    306                     if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
    307                             || keyCode == KeyEvent.KEYCODE_SPACE) {
    308                         handled = true;
    309                     } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT
    310                             || keyCode == KeyEvent.KEYCODE_Q
    311                             || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
    312                             || keyCode == KeyEvent.KEYCODE_W) {
    313                         handled = true;
    314                     }
    315                 }
    316             }
    317 
    318             return handled;
    319171        }
    320172
     
    345197                        break;
    346198                }
    347         }
    348 
    349         /**
    350          * Figures the lander state (x, y, fuel, ...) based on the passage of
    351          * realtime. Does not invalidate(). Called at the start of draw().
    352          * Detects the end-of-game and sets the UI to the next state.
    353          */
    354         private void updatePhysics() {
    355             long now = System.currentTimeMillis();
    356 
    357             // Do nothing if mLastTime is in the future.
    358             // This allows the game-start to delay the start of the physics
    359             // by 100ms or whatever.
    360             if (mLastTime > now) return;
    361            
    362             // DO SHIT HERE
    363 
    364             mLastTime = now+50;
    365199        }
    366200    }
     
    491325
    492326    /**
    493      * Standard override to get key-press events.
    494      */
    495     @Override
    496     public boolean onKeyDown(int keyCode, KeyEvent msg) {
    497         return thread.doKeyDown(keyCode, msg);
    498     }
    499 
    500     /**
    501      * Standard override for key-up. We actually care about these, so we can
    502      * turn off the engine or stop rotating.
    503      */
    504     @Override
    505     public boolean onKeyUp(int keyCode, KeyEvent msg) {
    506         return thread.doKeyUp(keyCode, msg);
    507     }
    508 
    509     /**
    510      * Standard window-focus override. Notice focus lost so we can pause on
    511      * focus lost. e.g. user switches to take a call.
    512      */
    513     @Override
    514     public void onWindowFocusChanged(boolean hasWindowFocus) {
    515         if (!hasWindowFocus) thread.pause();
    516     }
    517 
    518     /**
    519327     * Installs a pointer to the text view used for messages.
    520328     */
     
    534342     */
    535343    public void surfaceCreated(SurfaceHolder holder) {
    536         // start the thread here so that we don't busy-wait in run()
    537         // waiting for the surface to be created
    538344        thread.setRunning(true);
    539        
    540         //if(thread.mAppState == AppState.PAUSE)
    541                 //thread.unpause();
    542         //else
    543                 thread.start();
     345        thread.start();
    544346    }
    545347
Note: See TracChangeset for help on using the changeset viewer.