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