[abe7b3d] | 1 | package com.medievaltech.advancewars;
|
---|
| 2 |
|
---|
[113d7cf] | 3 | import java.io.*;
|
---|
| 4 |
|
---|
[5d9e7bb] | 5 | import android.app.Activity;
|
---|
| 6 | import android.os.Bundle;
|
---|
| 7 | import android.util.Log;
|
---|
| 8 | import android.view.Menu;
|
---|
| 9 | import android.view.MenuItem;
|
---|
| 10 | import android.view.Window;
|
---|
| 11 | import android.widget.TextView;
|
---|
| 12 |
|
---|
[379005b] | 13 | import com.medievaltech.advancewars.GameView.*;
|
---|
| 14 | import com.medievaltech.advancewars.Enum.*;
|
---|
| 15 |
|
---|
[5d9e7bb] | 16 | public class Game extends Activity {
|
---|
[113d7cf] | 17 | private static final int MENU_SAVE = 1;
|
---|
| 18 | private static final int MENU_MAIN = 2;
|
---|
| 19 | private static final int MENU_EXIT = 3;
|
---|
[5d9e7bb] | 20 |
|
---|
| 21 | /** A handle to the thread that's actually running the animation. */
|
---|
| 22 | public DrawingThread mThread;
|
---|
| 23 |
|
---|
| 24 | /** A handle to the View in which the game is running. */
|
---|
| 25 | private GameView mGameView;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Invoked during init to give the Activity a chance to set up its Menu.
|
---|
| 29 | *
|
---|
| 30 | * @param menu the Menu to which entries may be added
|
---|
| 31 | * @return true
|
---|
| 32 | */
|
---|
| 33 | @Override
|
---|
| 34 | public boolean onCreateOptionsMenu(Menu menu) {
|
---|
| 35 | super.onCreateOptionsMenu(menu);
|
---|
| 36 |
|
---|
[113d7cf] | 37 | menu.add(0, MENU_SAVE, 0, R.string.menu_save);
|
---|
| 38 | menu.add(0, MENU_MAIN, 0, R.string.menu_main);
|
---|
[5d9e7bb] | 39 | menu.add(0, MENU_EXIT, 0, R.string.menu_exit);
|
---|
| 40 | return true;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Invoked when the user selects an item from the Menu.
|
---|
| 45 | *
|
---|
| 46 | * @param item the Menu entry which was selected
|
---|
| 47 | * @return true if the Menu item was legit (and we consumed it), false
|
---|
| 48 | * otherwise
|
---|
| 49 | */
|
---|
| 50 | @Override
|
---|
| 51 | public boolean onOptionsItemSelected(MenuItem item) {
|
---|
| 52 | switch (item.getItemId()) {
|
---|
[113d7cf] | 53 | case MENU_SAVE:
|
---|
| 54 | try {
|
---|
| 55 | PrintWriter p = new PrintWriter(new FileWriter(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
|
---|
| 56 | mThread.mMap.save(p);
|
---|
| 57 | p.close();
|
---|
| 58 | }catch(IOException ioe) {
|
---|
| 59 | ioe.printStackTrace();
|
---|
| 60 | }
|
---|
| 61 | break;
|
---|
| 62 | case MENU_MAIN:
|
---|
| 63 | mThread.mGameState = GameState.MAIN_MENU;
|
---|
| 64 | break;
|
---|
[5d9e7bb] | 65 | case MENU_EXIT:
|
---|
[113d7cf] | 66 | finish();
|
---|
[5d9e7bb] | 67 | break;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | return true;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * Invoked when the Activity is created.
|
---|
| 75 | *
|
---|
| 76 | * @param savedInstanceState a Bundle containing state saved from a previous
|
---|
| 77 | * execution, or null if this is a new execution
|
---|
| 78 | */
|
---|
| 79 | @Override
|
---|
| 80 | protected void onCreate(Bundle savedInstanceState) {
|
---|
[205f525] | 81 | Log.w("AdvanceWars", "We're inside onCreate");
|
---|
[5d9e7bb] | 82 |
|
---|
| 83 | super.onCreate(savedInstanceState);
|
---|
| 84 |
|
---|
[205f525] | 85 | Log.w("AdvanceWars", "the super constructor was called successfully");
|
---|
[5d9e7bb] | 86 |
|
---|
| 87 | // turn off the window's title bar
|
---|
| 88 | requestWindowFeature(Window.FEATURE_NO_TITLE);
|
---|
| 89 |
|
---|
| 90 | // tell system to use the layout defined in our XML file
|
---|
| 91 | setContentView(R.layout.main);
|
---|
| 92 |
|
---|
| 93 | mGameView = (GameView) findViewById(R.id.lunar);
|
---|
| 94 | mThread = mGameView.getThread();
|
---|
[205f525] | 95 | mGameView.mGame = this;
|
---|
[5d9e7bb] | 96 |
|
---|
| 97 | mGameView.setTextView((TextView) findViewById(R.id.text));
|
---|
| 98 |
|
---|
| 99 | if (savedInstanceState == null) { // we were just launched: set up a new game
|
---|
[205f525] | 100 | Log.w("AdvanceWars", "SIS is null");
|
---|
[5d9e7bb] | 101 | } else {
|
---|
[205f525] | 102 | Log.w("AdvanceWars", "SIS is nonnull");
|
---|
[5d9e7bb] | 103 |
|
---|
| 104 | mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Notification that something is about to happen, to give the Activity a
|
---|
| 110 | * chance to save state.
|
---|
| 111 | *
|
---|
| 112 | * @param outState a Bundle into which this Activity should save its state
|
---|
| 113 | */
|
---|
| 114 | @Override
|
---|
| 115 | protected void onSaveInstanceState(Bundle outState) {
|
---|
| 116 | outState.putSerializable("gameState", mGameView.getThread().mGameState);
|
---|
[113d7cf] | 117 |
|
---|
[5d9e7bb] | 118 | super.onSaveInstanceState(outState);
|
---|
[113d7cf] | 119 | Log.w("AdvanceWars", "onSaveInstanceState called");
|
---|
[5d9e7bb] | 120 | }
|
---|
| 121 |
|
---|
| 122 | @Override
|
---|
| 123 | protected void onStop() {
|
---|
| 124 | System.exit(1);
|
---|
| 125 | super.onStop();
|
---|
| 126 | }
|
---|
| 127 | } |
---|