1 | package com.medievaltech.advancewars;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 |
|
---|
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 |
|
---|
13 | import com.medievaltech.advancewars.GameView.*;
|
---|
14 | import com.medievaltech.advancewars.Enum.*;
|
---|
15 |
|
---|
16 | public class Game extends Activity {
|
---|
17 | private static final int MENU_SAVE = 1;
|
---|
18 | private static final int MENU_MAIN = 2;
|
---|
19 | private static final int MENU_EXIT = 3;
|
---|
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 |
|
---|
37 | menu.add(0, MENU_SAVE, 0, R.string.menu_save);
|
---|
38 | menu.add(0, MENU_MAIN, 0, R.string.menu_main);
|
---|
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()) {
|
---|
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;
|
---|
65 | case MENU_EXIT:
|
---|
66 | finish();
|
---|
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) {
|
---|
81 | Log.w("AdvanceWars", "We're inside onCreate");
|
---|
82 |
|
---|
83 | super.onCreate(savedInstanceState);
|
---|
84 |
|
---|
85 | Log.w("AdvanceWars", "the super constructor was called successfully");
|
---|
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();
|
---|
95 | mGameView.mGame = this;
|
---|
96 |
|
---|
97 | mGameView.setTextView((TextView) findViewById(R.id.text));
|
---|
98 |
|
---|
99 | if (savedInstanceState == null) { // we were just launched: set up a new game
|
---|
100 | Log.w("AdvanceWars", "SIS is null");
|
---|
101 | } else {
|
---|
102 | Log.w("AdvanceWars", "SIS is nonnull");
|
---|
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);
|
---|
117 |
|
---|
118 | super.onSaveInstanceState(outState);
|
---|
119 | Log.w("AdvanceWars", "onSaveInstanceState called");
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | protected void onStop() {
|
---|
124 | System.exit(1);
|
---|
125 | super.onStop();
|
---|
126 | }
|
---|
127 | } |
---|