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

Last change on this file since bdd63ba was 113d7cf, checked in by dportnoy <devnull@…>, 13 years ago

Implemented saving/loading of the map and the units on it. Also did some package refactoring (renamed com.medievaltech.game to com.medievaltech.unit and moved some classes into com.medievaltech.advancewars)

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[abe7b3d]1package com.medievaltech.advancewars;
2
[113d7cf]3import java.io.*;
4
[abe7b3d]5import com.medievaltech.advancewars.GameView.DrawingThread;
[5d9e7bb]6
7import android.app.Activity;
8import android.os.Bundle;
9import android.util.Log;
10import android.view.Menu;
11import android.view.MenuItem;
12import android.view.Window;
13import android.widget.TextView;
14
15public class Game extends Activity {
[113d7cf]16 private static final int MENU_SAVE = 1;
17 private static final int MENU_MAIN = 2;
18 private static final int MENU_EXIT = 3;
[5d9e7bb]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
[113d7cf]38 menu.add(0, MENU_SAVE, 0, R.string.menu_save);
39 menu.add(0, MENU_MAIN, 0, R.string.menu_main);
[5d9e7bb]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()) {
[113d7cf]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;
[5d9e7bb]66 case MENU_EXIT:
[113d7cf]67 finish();
[5d9e7bb]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) {
[205f525]82 Log.w("AdvanceWars", "We're inside onCreate");
[5d9e7bb]83
84 super.onCreate(savedInstanceState);
85
[205f525]86 Log.w("AdvanceWars", "the super constructor was called successfully");
[5d9e7bb]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();
[205f525]96 mGameView.mGame = this;
[5d9e7bb]97
98 mGameView.setTextView((TextView) findViewById(R.id.text));
99
100 if (savedInstanceState == null) { // we were just launched: set up a new game
[205f525]101 Log.w("AdvanceWars", "SIS is null");
[5d9e7bb]102
103 mThread.setState(AppState.RUNNING);
104 } else {
[205f525]105 Log.w("AdvanceWars", "SIS is nonnull");
[5d9e7bb]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);
[113d7cf]131
[5d9e7bb]132 super.onSaveInstanceState(outState);
[113d7cf]133 Log.w("AdvanceWars", "onSaveInstanceState called");
[5d9e7bb]134 }
135
136 @Override
137 protected void onStop() {
138 System.exit(1);
139 super.onStop();
140 }
141}
Note: See TracBrowser for help on using the repository browser.