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

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

Implemented turn and support for computer-controller movement of units. Right now, the computer moves a specific unit down one square when its his turn.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1package com.medievaltech.advancewars;
2
3import java.io.*;
4
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
13import com.medievaltech.advancewars.GameView.*;
14import com.medievaltech.advancewars.Enum.*;
15
16public class Game extends Activity {
17 public DrawingThread mThread;
18 private GameView mGameView;
19
20 /**
21 * Invoked during init to give the Activity a chance to set up its Menu.
22 *
23 * @param menu the Menu to which entries may be added
24 * @return true
25 */
26 @Override
27 public boolean onCreateOptionsMenu(Menu menu) {
28 super.onCreateOptionsMenu(menu);
29
30 menu.add(0, MenuOption.END_TURN.ordinal(), 0, R.string.menu_end_turn);
31 menu.add(0, MenuOption.SAVE.ordinal(), 0, R.string.menu_save);
32 menu.add(0, MenuOption.MAIN.ordinal(), 0, R.string.menu_main);
33 menu.add(0, MenuOption.EXIT.ordinal(), 0, R.string.menu_exit);
34 return true;
35 }
36
37 /**
38 * Invoked when the user selects an item from the Menu.
39 *
40 * @param item the Menu entry which was selected
41 * @return true if the Menu item was legit (and we consumed it), false
42 * otherwise
43 */
44 @Override
45 public boolean onOptionsItemSelected(MenuItem item) {
46 int i = item.getItemId();
47
48 if(i == MenuOption.END_TURN.ordinal()) {
49 mThread.mTurn = Turn.ENEMY_TURN;
50 }else if(i == MenuOption.SAVE.ordinal()) {
51 try {
52 PrintWriter p = new PrintWriter(new FileWriter(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
53 mThread.mMap.save(p);
54 p.close();
55 }catch(IOException ioe) {
56 ioe.printStackTrace();
57 }
58 }else if(i == MenuOption.MAIN.ordinal()) {
59 mThread.mGameState = GameState.MAIN_MENU;
60 }else if(i == MenuOption.EXIT.ordinal()) {
61 finish();
62 }
63
64 return true;
65 }
66
67 /**
68 * Invoked when the Activity is created.
69 *
70 * @param savedInstanceState a Bundle containing state saved from a previous
71 * execution, or null if this is a new execution
72 */
73 @Override
74 protected void onCreate(Bundle savedInstanceState) {
75 Log.w("AdvanceWars", "We're inside onCreate");
76
77 super.onCreate(savedInstanceState);
78
79 Log.w("AdvanceWars", "the super constructor was called successfully");
80
81 // turn off the window's title bar
82 requestWindowFeature(Window.FEATURE_NO_TITLE);
83
84 // tell system to use the layout defined in our XML file
85 setContentView(R.layout.main);
86
87 mGameView = (GameView) findViewById(R.id.lunar);
88 mThread = mGameView.getThread();
89 mGameView.mGame = this;
90
91 mGameView.setTextView((TextView) findViewById(R.id.text));
92
93 if (savedInstanceState == null) { // we were just launched: set up a new game
94 Log.w("AdvanceWars", "SIS is null");
95 } else {
96 Log.w("AdvanceWars", "SIS is nonnull");
97
98 mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");
99 }
100 }
101
102 /**
103 * Notification that something is about to happen, to give the Activity a
104 * chance to save state.
105 *
106 * @param outState a Bundle into which this Activity should save its state
107 */
108 @Override
109 protected void onSaveInstanceState(Bundle outState) {
110 outState.putSerializable("gameState", mGameView.getThread().mGameState);
111
112 super.onSaveInstanceState(outState);
113 Log.w("AdvanceWars", "onSaveInstanceState called");
114 }
115
116 @Override
117 protected void onStop() {
118 System.exit(1);
119 super.onStop();
120 }
121}
Note: See TracBrowser for help on using the repository browser.