source: advance-wars/src/com/medievaltech/advancewars/GameView.java@ bb2fa26

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

Add a menu for in-game actions.

  • Property mode set to 100644
File size: 13.1 KB
Line 
1package com.medievaltech.advancewars;
2
3import java.io.*;
4import java.util.*;
5
6import com.medievaltech.advancewars.Enum.*;
7import com.medievaltech.unit.*;
8import com.medievaltech.gui.*;
9
10import android.content.Context;
11import android.graphics.*;
12import android.os.*;
13import android.view.*;
14import android.view.ContextMenu.ContextMenuInfo;
15import android.util.AttributeSet;
16import android.util.Log;
17
18class GameView extends SurfaceView implements SurfaceHolder.Callback {
19
20 class DrawingThread extends Thread {
21
22 private Unit selectedUnit;
23
24 private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1,
25 mTilePaint2, mSelectionPaint, mUnitPaint1, mUnitPaint2;
26
27 /** Handle to the surface manager object we interact with */
28 private SurfaceHolder mSurfaceHolder;
29
30 public DrawingThread(SurfaceHolder surfaceHolder, Context context, Handler handler) {
31 mSurfaceHolder = surfaceHolder;
32
33 mLinePaint = new Paint();
34 initPaint(mLinePaint, 0, 255, 0);
35
36 mTextPaint = new Paint();
37 mTextPaint.setTextSize(12);
38 mTextPaint.setTextAlign(Paint.Align.CENTER);
39 initPaint(mTextPaint, 255, 255, 255);
40
41 mButtonPaint = new Paint();
42 mButtonPaint.setTextSize(20);
43 mButtonPaint.setTextAlign(Paint.Align.CENTER);
44 initPaint(mButtonPaint, 0, 0, 0);
45
46 mTilePaint1 = new Paint();
47 initPaint(mTilePaint1, 0, 255, 0);
48
49 mTilePaint2 = new Paint();
50 initPaint(mTilePaint2, 0, 0, 255);
51
52 mUnitPaint1 = new Paint();
53 initPaint(mUnitPaint1, 255, 0, 0);
54
55 mUnitPaint2 = new Paint();
56 initPaint(mUnitPaint2, 160, 160, 255);
57
58 mSelectionPaint = new Paint();
59 initPaint(mSelectionPaint, 255, 127, 0);
60
61 Player.neutralColor = new Paint();
62 initPaint(Player.neutralColor, 127, 127, 127);
63
64 Static.wndMainMenu = new com.medievaltech.gui.Window(0, 0, 320, 450);
65 Static.wndMainMenu.addGUIObject("txtTitle", new Text("Main Menu", 100, 30, 120, 20, mTextPaint));
66 Static.wndMainMenu.addGUIObject("btnNewGame", new Button("New Game", 100, 90, 120, 20, mLinePaint, mButtonPaint));
67 Static.wndMainMenu.addGUIObject("btnLoadGame", new Button("Load Game", 100, 125, 120, 20, mLinePaint, mButtonPaint));
68 Static.wndMainMenu.addGUIObject("btnMapEditor", new Button("Map Editor", 100, 160, 120, 20, mLinePaint, mButtonPaint));
69 Static.wndMainMenu.addGUIObject("btnQuit", new Button("Quit", 100, 195, 120, 20, mLinePaint, mButtonPaint));
70
71 Static.grassTile = new Tile(mTilePaint1, TerrainType.LAND);
72 Static.oceanTile = new Tile(mTilePaint2, TerrainType.SEA);
73
74 Static.map = new Map(Static.grassTile, 6, 8, new Point(10, 25));
75
76 boolean land = true;
77
78 for(int x=0; x<Static.map.getWidth(); x++) {
79 for(int y=0; y<Static.map.getHeight(); y++) {
80 if(land)
81 Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(x, y)));
82 else
83 Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(x, y)));
84 land = !land;
85 }
86 land = !land;
87 }
88
89 Static.human = new Player("Human", mUnitPaint1);
90 Static.enemy = new Player("Comp", mUnitPaint2);
91
92 Static.map.getTile(0, 0).addUnit(new Soldier(Static.enemy));
93 Static.map.getTile(2, 3).addUnit(new Soldier(Static.human));
94 Static.map.getTile(5, 6).addUnit(new Soldier(Static.human));
95
96 Static.map.getTile(4, 4).addBuilding(new City());
97
98 Static.turn = Turn.YOUR_TURN;
99
100 Static.gameState = GameState.MAIN_MENU;
101 }
102
103 public void initPaint(Paint p, int r, int g, int b) {
104 p.setAntiAlias(true);
105 p.setARGB(255, r, g, b);
106 }
107
108 /**
109 * Starts the game, setting parameters for the current difficulty.
110 */
111 // I don't think this gets called now. maybe we should call it in the thread constructor
112 public void doStart() {
113 synchronized (mSurfaceHolder) {
114 Log.i("AdvanceWars", "Player's turn starting now");
115 Static.gameState = GameState.MAIN_MENU;
116 }
117 }
118
119 @Override
120 public void run() {
121 while (Static.run) {
122 Canvas c = null;
123 try {
124 c = mSurfaceHolder.lockCanvas(null);
125 synchronized(mSurfaceHolder) {
126 doLogic();
127 doDraw(c);
128 }
129 } finally {
130 // do this in a finally so that if an exception is thrown
131 // during the above, we don't leave the Surface in an
132 // inconsistent state
133 if (c != null) {
134 mSurfaceHolder.unlockCanvasAndPost(c);
135 }
136 }
137 }
138 }
139
140 public void setGameState(GameState state) {
141 synchronized (mSurfaceHolder) {
142 Static.gameState = state;
143 }
144 }
145
146 /* Callback invoked when the surface dimensions change. */
147 public void setSurfaceSize(int width, int height) {
148 synchronized (mSurfaceHolder) {
149 Log.i("AdvanceWars", "width: "+width+", height: "+height);
150 }
151 }
152
153 private void doLogic() {
154 if(Static.turn == Turn.YOUR_TURN)
155 return;
156
157 switch(Static.gameState) {
158 case BATTLE_MAP:
159 Iterator<Unit> iter = Static.enemy.getControlledUnits().iterator();
160 Unit cur;
161 int x, y;
162
163 Log.i("AdvanceWars", "starting to move enemy units");
164 while(iter.hasNext()) {
165 cur = iter.next();
166 x = cur.location.x;
167 y = cur.location.y;
168
169 Log.i("AdvanceWars", "moving enemy unit");
170
171 //any unit that's in the way is removed (needs to be changed eventuallyy)
172 Static.map.getTile(x, y).removeUnit();
173 Static.map.getTile(x, y+1).addUnit(cur);
174 }
175 Log.i("AdvanceWars", "finished moving enemy units");
176
177
178 Static.turn = Turn.YOUR_TURN;
179 break;
180 }
181 }
182
183 /**
184 * Draws the ship, fuel/speed bars, and background to the provided
185 * Canvas.
186 */
187 private void doDraw(Canvas canvas) {
188 canvas.drawColor(Color.BLACK);
189
190 switch(Static.gameState) {
191 case MAIN_MENU:
192 Static.wndMainMenu.draw(canvas);
193 break;
194 case BATTLE_MAP:
195 mTextPaint.setTextSize(12);
196
197 Static.map.draw(canvas);
198
199 if(selectedUnit != null) {
200 for(Point p : selectedUnit.getMovementRange()) {
201 canvas.drawRect(p.x*50+10, p.y*50+25, p.x*50+50+10, p.y*50+50+25, mSelectionPaint);
202 }
203 }
204
205 Static.map.drawBuildings(canvas);
206 Static.map.drawUnits(canvas);
207
208 break;
209 }
210 }
211 }
212
213 public GameView(Context context, AttributeSet attrs) {
214 super(context, attrs);
215
216 // register our interest in hearing about changes to our surface
217 SurfaceHolder holder = getHolder();
218 holder.addCallback(this);
219
220 // create thread only; it's started in surfaceCreated()
221 Static.thread = new DrawingThread(holder, context, new Handler() {});
222
223 setFocusable(true); // make sure we get key events
224 }
225
226 @Override public boolean onTouchEvent(MotionEvent event) {
227 Log.i("AdvanceWars", "Detected touch event");
228
229 if(event.getAction() == MotionEvent.ACTION_UP) {
230 Log.i("AdvanceWars", "Detected UP touch action");
231 switch(Static.gameState) {
232 case MAIN_MENU:
233 if(Static.wndMainMenu.getGUIObject("btnNewGame").isClicked(event.getX(), event.getY())) {
234 Log.i("AdvanceWars", "Switching to battle map");
235 Static.gameState = GameState.BATTLE_MAP;
236 }else if(Static.wndMainMenu.getGUIObject("btnLoadGame").isClicked(event.getX(), event.getY())) {
237 BufferedReader b;
238 try {
239 b = new BufferedReader(new FileReader(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
240
241 int width = Integer.parseInt(b.readLine());
242 int height = Integer.parseInt(b.readLine());
243
244 String offset = b.readLine();
245 Log.i("GameSave", offset);
246 int offsetX = Integer.parseInt(offset.substring(0, offset.indexOf("x")));
247 int offsetY = Integer.parseInt(offset.substring(offset.indexOf("x")+1));
248
249 Static.map = new Map(Static.grassTile, width, height, new Point(offsetX, offsetY));
250
251 Log.i("GameSave", "Created the map");
252
253 for(int x=0; x<width; x++) {
254 String line = b.readLine();
255 Log.i("GameSave", line);
256 String[] arr = line.split(",");
257 for(int y=0; y<arr.length; y++) {
258 TerrainType type = TerrainType.values()[Integer.parseInt(arr[y])];
259 if(type.equals(TerrainType.LAND))
260 Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(10, 25)));
261 else
262 Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(10, 25)));
263 }
264 }
265
266 while(b.ready()) {
267 String unit = b.readLine();
268 Log.i("GameSave", unit);
269 int x = Integer.parseInt(unit.substring(0, unit.indexOf(",")));
270 int y = Integer.parseInt(unit.substring(unit.indexOf(",")+1));
271
272 Player humanPlayer = new Player("Human", Static.thread.mUnitPaint1);
273
274 Static.map.getTile(x, y).addUnit(new Soldier(humanPlayer));
275 }
276
277 b.close();
278 }catch(IOException ioe) {
279 ioe.printStackTrace();
280 }
281 Static.gameState = GameState.BATTLE_MAP;
282 }else if(Static.wndMainMenu.getGUIObject("btnQuit").isClicked(event.getX(), event.getY())) {
283 Static.game.finish();
284 }
285 break;
286 case BATTLE_MAP:
287 Log.i("AdvanceWars", "Touch event detected on battle map");
288
289 if(event.getX() >= Static.map.offset.x && event.getY() >= Static.map.offset.y) {
290 int x = ((int)event.getX() - Static.map.offset.x) / 50;
291 int y = ((int)event.getY() - Static.map.offset.y) / 50;
292
293 Log.i("AdvanceWars", "About to show context menu");
294 showContextMenu();
295
296 /*
297
298 Unit target = Static.map.getTile(x, y).currentUnit;
299
300 if(Static.thread.selectedUnit != null && Static.thread.selectedUnit.getMovementRange().contains(new Point(x, y))) {
301 //Display a menu allowing the player to move to the target or attack the target or
302 //get more info on the target
303
304 if(target == null || target == Static.thread.selectedUnit) {
305 Static.map.getTile(Static.thread.selectedUnit.location.x, Static.thread.selectedUnit.location.y).removeUnit();
306 Static.map.getTile(x, y).addUnit(Static.thread.selectedUnit);
307 }else {
308 // Display a context menu that gives the option of attacking the target
309 }
310 Static.thread.selectedUnit = null;
311 }else
312 Static.thread.selectedUnit = target;
313 */
314 }
315
316 Log.i("AdvanceWars", "Touch event handling finished");
317
318 break;
319 }
320 }else if(event.getAction() == MotionEvent.ACTION_DOWN) {
321
322 }
323
324 return true;
325 }
326
327 /* Callback invoked when the surface dimensions change. */
328 public void surfaceChanged(SurfaceHolder holder, int format, int width,
329 int height) {
330 Static.thread.setSurfaceSize(width, height);
331 }
332
333 /*
334 * Callback invoked when the Surface has been created and is ready to be
335 * used.
336 */
337 public void surfaceCreated(SurfaceHolder holder) {
338 Static.run = true;
339 Static.thread.start();
340 }
341
342 /*
343 * Callback invoked when the Surface has been destroyed and must no longer
344 * be touched. WARNING: after this method returns, the Surface/Canvas must
345 * never be touched again!
346 */
347 public void surfaceDestroyed(SurfaceHolder holder) {
348 // we have to tell thread to shut down & wait for it to finish, or else
349 // it might touch the Surface after we return and explode
350 boolean retry = true;
351 Static.run = false;
352 while (retry) {
353 try {
354 Static.thread.join();
355 retry = false;
356 } catch (InterruptedException e) {
357 }
358 }
359 }
360}
Note: See TracBrowser for help on using the repository browser.