source: advance-wars/src/com/example/advancewars/Game.java@ c088db6

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

Added lots of code from the android blackjack project.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1package com.example.advancewars;
2
3import com.example.advancewars.R;
4import com.example.advancewars.GameView;
5import com.example.advancewars.GameView.DrawingThread;
6import android.app.Activity;
7import android.os.Bundle;
8import android.util.Log;
9import android.view.Menu;
10import android.view.MenuItem;
11import android.view.Window;
12import android.widget.TextView;
13import java.io.*;
14
15public class Game extends Activity {
16 private static final int MENU_EXIT = 1;
17 private static final int MENU_NEW = 2;
18
19 public enum State{BUST, ACTIVE, DOUBLEDOWN};
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_EXIT, 0, R.string.menu_exit);
38 return true;
39 }
40
41 /**
42 * Invoked when the user selects an item from the Menu.
43 *
44 * @param item the Menu entry which was selected
45 * @return true if the Menu item was legit (and we consumed it), false
46 * otherwise
47 */
48 @Override
49 public boolean onOptionsItemSelected(MenuItem item) {
50 switch (item.getItemId()) {
51 case MENU_EXIT:
52 this.finish();
53 break;
54 case MENU_NEW:
55 if(mThread.mGameState == GameState.COMP_TURN)
56 mGameView.newRound();
57 break;
58 }
59
60 return true;
61 }
62
63 /**
64 * Invoked when the Activity is created.
65 *
66 * @param savedInstanceState a Bundle containing state saved from a previous
67 * execution, or null if this is a new execution
68 */
69 @Override
70 protected void onCreate(Bundle savedInstanceState) {
71 Log.w("Blackjack", "We're inside onCreate");
72
73 super.onCreate(savedInstanceState);
74
75 Log.w("Blackjack", "the super constructor was called successfully");
76
77 // turn off the window's title bar
78 requestWindowFeature(Window.FEATURE_NO_TITLE);
79
80 // tell system to use the layout defined in our XML file
81 setContentView(R.layout.main);
82
83 mGameView = (GameView) findViewById(R.id.lunar);
84 mThread = mGameView.getThread();
85
86 mGameView.setTextView((TextView) findViewById(R.id.text));
87
88 if (savedInstanceState == null) { // we were just launched: set up a new game
89 Log.w("Blackjack", "SIS is null");
90
91 mThread.setState(AppState.RUNNING);
92 } else {
93 Log.w("Blackjack", "SIS is nonnull");
94
95 mThread.setState(AppState.RUNNING);
96
97 mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");
98
99 mGameView.getThread().mPlayerWins = savedInstanceState.getInt("playerWins");
100 mGameView.getThread().mPlayerLosses = savedInstanceState.getInt("playerLosses");
101 }
102 }
103
104 /**
105 * Invoked when the Activity loses user focus.
106 */
107 @Override
108 protected void onPause() {
109 super.onPause();
110 mGameView.getThread().pause(); // pause game when Activity pauses
111 }
112
113 /**
114 * Notification that something is about to happen, to give the Activity a
115 * chance to save state.
116 *
117 * @param outState a Bundle into which this Activity should save its state
118 */
119 @Override
120 protected void onSaveInstanceState(Bundle outState) {
121 outState.putSerializable("gameState", mGameView.getThread().mGameState);
122 outState.putSerializable("playerWins", mGameView.getThread().mPlayerWins);
123 outState.putSerializable("playerLosses", mGameView.getThread().mPlayerLosses);
124
125 super.onSaveInstanceState(outState);
126 Log.w("Blackjack", "onSaveInstanceState called");
127 }
128
129 @Override
130 protected void onStop() {
131 System.exit(1);
132 super.onStop();
133 }
134}
Note: See TracBrowser for help on using the repository browser.