Changeset b660017 in advance-wars
- Timestamp:
- Jun 6, 2011, 5:09:12 PM (13 years ago)
- Branches:
- master
- Children:
- fea4b77
- Parents:
- 379005b
- Location:
- src/com/medievaltech/advancewars
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/medievaltech/advancewars/Game.java
r379005b rb660017 99 99 if (savedInstanceState == null) { // we were just launched: set up a new game 100 100 Log.w("AdvanceWars", "SIS is null"); 101 102 mThread.setState(AppState.RUNNING);103 101 } else { 104 102 Log.w("AdvanceWars", "SIS is nonnull"); 105 103 106 mThread.setState(AppState.RUNNING);107 108 104 mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState"); 109 105 } 110 }111 112 /**113 * Invoked when the Activity loses user focus.114 */115 @Override116 protected void onPause() {117 super.onPause();118 mGameView.getThread().pause(); // pause game when Activity pauses119 106 } 120 107 -
src/com/medievaltech/advancewars/GameView.java
r379005b rb660017 18 18 19 19 class DrawingThread extends Thread { 20 public AppState mAppState;21 20 public GameState mGameState; 22 21 … … 28 27 private int mCanvasWidth = 1; 29 28 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 36 29 /** Paint to draw the lines on screen. */ 37 30 private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1, mTilePaint2, mSelectionPaint, … … 53 46 54 47 public DrawingThread(SurfaceHolder surfaceHolder, Context context, Handler handler) { 55 // get handles to some important objects56 48 mSurfaceHolder = surfaceHolder; 57 mHandler = handler;58 49 59 50 mLinePaint = new Paint(); … … 126 117 public void doStart() { 127 118 synchronized (mSurfaceHolder) { 128 mLastTime = System.currentTimeMillis() + 100;129 setState(AppState.RUNNING);130 119 Log.i("AdvanceWars", "Player's turn starting now"); 131 120 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);141 121 } 142 122 } … … 149 129 c = mSurfaceHolder.lockCanvas(null); 150 130 synchronized(mSurfaceHolder) { 151 if(mAppState == AppState.RUNNING)152 updatePhysics();153 131 doDraw(c); 154 132 } … … 175 153 mRun = b; 176 154 } 177 178 /**179 * Sets the game mode. That is, whether we are running, paused, in the180 * failure state, in the victory state, etc.181 *182 * @see #setState(int, CharSequence)183 * @param mode one of the STATE_* constants184 */185 public void setState(AppState state) {186 synchronized (mSurfaceHolder) {187 setState(state, null);188 }189 }190 155 191 156 public void setGameState(GameState state) { 192 157 synchronized (mSurfaceHolder) { 193 158 mGameState = state; 194 }195 }196 197 /**198 * Sets the game mode. That is, whether we are running, paused, in the199 * failure state, in the victory state, etc.200 *201 * @param mode one of the STATE_* constants202 * @param message string to add to screen or null203 */204 public void setState(AppState mode, CharSequence message) {205 /*206 * This method optionally can cause a text message to be displayed207 * to the user when the mode changes. Since the View that actually208 * renders that text is part of the main View hierarchy and not209 * 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 main211 * 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 }238 159 } 239 160 } … … 248 169 Log.i("AdvanceWars", "width: "+mCanvasWidth+", height: "+mCanvasHeight); 249 170 } 250 }251 252 /**253 * Resumes from a pause.254 */255 public void unpause() {256 // Move the real time clock up to now257 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 pressed267 * @param msg the original event object268 * @return true269 */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 (okStart278 && (mAppState == AppState.READY || mAppState == AppState.LOSE || mAppState == AppState.WIN)) {279 // ready-to-start -> start280 doStart();281 return true;282 } else if (mAppState == AppState.PAUSE && okStart) {283 // paused -> running284 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 pressed298 * @param msg the original event object299 * @return true if the key was handled and consumed, or else false300 */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_CENTER307 || keyCode == KeyEvent.KEYCODE_SPACE) {308 handled = true;309 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT310 || keyCode == KeyEvent.KEYCODE_Q311 || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT312 || keyCode == KeyEvent.KEYCODE_W) {313 handled = true;314 }315 }316 }317 318 return handled;319 171 } 320 172 … … 345 197 break; 346 198 } 347 }348 349 /**350 * Figures the lander state (x, y, fuel, ...) based on the passage of351 * 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 physics359 // by 100ms or whatever.360 if (mLastTime > now) return;361 362 // DO SHIT HERE363 364 mLastTime = now+50;365 199 } 366 200 } … … 491 325 492 326 /** 493 * Standard override to get key-press events.494 */495 @Override496 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 can502 * turn off the engine or stop rotating.503 */504 @Override505 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 on511 * focus lost. e.g. user switches to take a call.512 */513 @Override514 public void onWindowFocusChanged(boolean hasWindowFocus) {515 if (!hasWindowFocus) thread.pause();516 }517 518 /**519 327 * Installs a pointer to the text view used for messages. 520 328 */ … … 534 342 */ 535 343 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 created538 344 thread.setRunning(true); 539 540 //if(thread.mAppState == AppState.PAUSE) 541 //thread.unpause(); 542 //else 543 thread.start(); 345 thread.start(); 544 346 } 545 347
Note:
See TracChangeset
for help on using the changeset viewer.