source: advance-wars/src/com/medievaltech/advancewars/Game.java@ 379005b

Last change on this file since 379005b was 379005b, checked in by dportnoy <devnull@…>, 14 years ago

Moved all the enums to an Enum.java file.

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[abe7b3d]1package com.medievaltech.advancewars;
2
[113d7cf]3import java.io.*;
4
[5d9e7bb]5import android.app.Activity;
6import android.os.Bundle;
7import android.util.Log;
8import android.view.Menu;
9import android.view.MenuItem;
10import android.view.Window;
11import android.widget.TextView;
12
[379005b]13import com.medievaltech.advancewars.GameView.*;
14import com.medievaltech.advancewars.Enum.*;
15
[5d9e7bb]16public 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
102 mThread.setState(AppState.RUNNING);
103 } else {
[205f525]104 Log.w("AdvanceWars", "SIS is nonnull");
[5d9e7bb]105
106 mThread.setState(AppState.RUNNING);
107
108 mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");
109 }
110 }
111
112 /**
113 * Invoked when the Activity loses user focus.
114 */
115 @Override
116 protected void onPause() {
117 super.onPause();
118 mGameView.getThread().pause(); // pause game when Activity pauses
119 }
120
121 /**
122 * Notification that something is about to happen, to give the Activity a
123 * chance to save state.
124 *
125 * @param outState a Bundle into which this Activity should save its state
126 */
127 @Override
128 protected void onSaveInstanceState(Bundle outState) {
129 outState.putSerializable("gameState", mGameView.getThread().mGameState);
[113d7cf]130
[5d9e7bb]131 super.onSaveInstanceState(outState);
[113d7cf]132 Log.w("AdvanceWars", "onSaveInstanceState called");
[5d9e7bb]133 }
134
135 @Override
136 protected void onStop() {
137 System.exit(1);
138 super.onStop();
139 }
140}
Note: See TracBrowser for help on using the repository browser.