1 | package com.medievaltech.advancewars;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 |
|
---|
5 | import android.app.Activity;
|
---|
6 | import android.os.Bundle;
|
---|
7 | import android.util.Log;
|
---|
8 | import android.view.Menu;
|
---|
9 | import android.view.MenuItem;
|
---|
10 | import android.view.Window;
|
---|
11 |
|
---|
12 | import com.medievaltech.advancewars.Enum.*;
|
---|
13 |
|
---|
14 | public class Game extends Activity {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Invoked during init to give the Activity a chance to set up its Menu.
|
---|
18 | *
|
---|
19 | * @param menu the Menu to which entries may be added
|
---|
20 | * @return true
|
---|
21 | */
|
---|
22 | @Override
|
---|
23 | public boolean onCreateOptionsMenu(Menu menu) {
|
---|
24 | super.onCreateOptionsMenu(menu);
|
---|
25 |
|
---|
26 | menu.add(0, MenuOption.END_TURN.ordinal(), 0, R.string.menu_end_turn);
|
---|
27 | menu.add(0, MenuOption.SAVE.ordinal(), 0, R.string.menu_save);
|
---|
28 | menu.add(0, MenuOption.MAIN.ordinal(), 0, R.string.menu_main);
|
---|
29 | menu.add(0, MenuOption.EXIT.ordinal(), 0, R.string.menu_exit);
|
---|
30 | return true;
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Invoked when the user selects an item from the Menu.
|
---|
35 | *
|
---|
36 | * @param item the Menu entry which was selected
|
---|
37 | * @return true if the Menu item was legit (and we consumed it), false
|
---|
38 | * otherwise
|
---|
39 | */
|
---|
40 | @Override
|
---|
41 | public boolean onOptionsItemSelected(MenuItem item) {
|
---|
42 | int i = item.getItemId();
|
---|
43 |
|
---|
44 | if(i == MenuOption.END_TURN.ordinal()) {
|
---|
45 | Static.turn = Turn.ENEMY_TURN;
|
---|
46 | }else if(i == MenuOption.SAVE.ordinal()) {
|
---|
47 | try {
|
---|
48 | PrintWriter p = new PrintWriter(new FileWriter(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
|
---|
49 | Static.map.save(p);
|
---|
50 | p.close();
|
---|
51 | }catch(IOException ioe) {
|
---|
52 | ioe.printStackTrace();
|
---|
53 | }
|
---|
54 | }else if(i == MenuOption.MAIN.ordinal()) {
|
---|
55 | Static.gameState = GameState.MAIN_MENU;
|
---|
56 | }else if(i == MenuOption.EXIT.ordinal()) {
|
---|
57 | finish();
|
---|
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("AdvanceWars", "We're inside onCreate");
|
---|
72 |
|
---|
73 | super.onCreate(savedInstanceState);
|
---|
74 |
|
---|
75 | Log.w("AdvanceWars", "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 | Static.game = this;
|
---|
84 |
|
---|
85 | if (savedInstanceState == null) { // we were just launched: set up a new game
|
---|
86 | Log.w("AdvanceWars", "SIS is null");
|
---|
87 | } else {
|
---|
88 | Log.w("AdvanceWars", "SIS is nonnull");
|
---|
89 |
|
---|
90 | Static.gameState = (GameState)savedInstanceState.getSerializable("gameState");
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Notification that something is about to happen, to give the Activity a
|
---|
96 | * chance to save state.
|
---|
97 | *
|
---|
98 | * @param outState a Bundle into which this Activity should save its state
|
---|
99 | */
|
---|
100 | @Override
|
---|
101 | protected void onSaveInstanceState(Bundle outState) {
|
---|
102 | outState.putSerializable("gameState", Static.gameState);
|
---|
103 |
|
---|
104 | super.onSaveInstanceState(outState);
|
---|
105 | Log.w("AdvanceWars", "onSaveInstanceState called");
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | protected void onStop() {
|
---|
110 | System.exit(1);
|
---|
111 | super.onStop();
|
---|
112 | }
|
---|
113 | } |
---|