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