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
RevLine 
[abe7b3d]1package com.medievaltech.advancewars;
[5d9e7bb]2
[113d7cf]3import java.io.*;
[c3ad11c]4import java.util.*;
[113d7cf]5
[379005b]6import com.medievaltech.advancewars.Enum.*;
[113d7cf]7import com.medievaltech.unit.*;
[abe7b3d]8import com.medievaltech.gui.*;
[205f525]9
[5d9e7bb]10import android.content.Context;
11import android.graphics.*;
12import android.os.*;
13import android.view.*;
[bb2fa26]14import android.view.ContextMenu.ContextMenuInfo;
[5d9e7bb]15import android.util.AttributeSet;
16import android.util.Log;
17
18class GameView extends SurfaceView implements SurfaceHolder.Callback {
19
[4666fae]20 class DrawingThread extends Thread {
21
[c3ad11c]22 private Unit selectedUnit;
[fea4b77]23
24 private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1,
[c3ad11c]25 mTilePaint2, mSelectionPaint, mUnitPaint1, mUnitPaint2;
[5d9e7bb]26
27 /** Handle to the surface manager object we interact with */
28 private SurfaceHolder mSurfaceHolder;
29
[205f525]30 public DrawingThread(SurfaceHolder surfaceHolder, Context context, Handler handler) {
[5d9e7bb]31 mSurfaceHolder = surfaceHolder;
[2e798d9]32
[5d9e7bb]33 mLinePaint = new Paint();
[4666fae]34 initPaint(mLinePaint, 0, 255, 0);
[5d9e7bb]35
36 mTextPaint = new Paint();
37 mTextPaint.setTextSize(12);
[205f525]38 mTextPaint.setTextAlign(Paint.Align.CENTER);
[4666fae]39 initPaint(mTextPaint, 255, 255, 255);
[5d9e7bb]40
41 mButtonPaint = new Paint();
42 mButtonPaint.setTextSize(20);
43 mButtonPaint.setTextAlign(Paint.Align.CENTER);
[4666fae]44 initPaint(mButtonPaint, 0, 0, 0);
[5d9e7bb]45
[2e798d9]46 mTilePaint1 = new Paint();
[4666fae]47 initPaint(mTilePaint1, 0, 255, 0);
[2e798d9]48
49 mTilePaint2 = new Paint();
[4666fae]50 initPaint(mTilePaint2, 0, 0, 255);
[2e798d9]51
[c3ad11c]52 mUnitPaint1 = new Paint();
[4666fae]53 initPaint(mUnitPaint1, 255, 0, 0);
[c3ad11c]54
55 mUnitPaint2 = new Paint();
[4666fae]56 initPaint(mUnitPaint2, 160, 160, 255);
[1a1e8c7]57
[ebaddd9]58 mSelectionPaint = new Paint();
[4666fae]59 initPaint(mSelectionPaint, 255, 127, 0);
[ebaddd9]60
[41c11dd]61 Player.neutralColor = new Paint();
[4666fae]62 initPaint(Player.neutralColor, 127, 127, 127);
[41c11dd]63
[4666fae]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));
[205f525]70
[4666fae]71 Static.grassTile = new Tile(mTilePaint1, TerrainType.LAND);
72 Static.oceanTile = new Tile(mTilePaint2, TerrainType.SEA);
[2e798d9]73
[4666fae]74 Static.map = new Map(Static.grassTile, 6, 8, new Point(10, 25));
[2e798d9]75
76 boolean land = true;
77
[41c11dd]78 for(int x=0; x<Static.map.getWidth(); x++) {
79 for(int y=0; y<Static.map.getHeight(); y++) {
[2e798d9]80 if(land)
[4666fae]81 Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(x, y)));
[2e798d9]82 else
[4666fae]83 Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(x, y)));
[2e798d9]84 land = !land;
85 }
86 land = !land;
87 }
88
[4666fae]89 Static.human = new Player("Human", mUnitPaint1);
90 Static.enemy = new Player("Comp", mUnitPaint2);
[fea4b77]91
[4666fae]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));
[41c11dd]95
96 Static.map.getTile(4, 4).addBuilding(new City());
[ebaddd9]97
[4666fae]98 Static.turn = Turn.YOUR_TURN;
[fea4b77]99
[4666fae]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);
[5d9e7bb]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) {
[205f525]114 Log.i("AdvanceWars", "Player's turn starting now");
[4666fae]115 Static.gameState = GameState.MAIN_MENU;
[5d9e7bb]116 }
117 }
118
119 @Override
120 public void run() {
[4666fae]121 while (Static.run) {
[5d9e7bb]122 Canvas c = null;
123 try {
124 c = mSurfaceHolder.lockCanvas(null);
125 synchronized(mSurfaceHolder) {
[fea4b77]126 doLogic();
[5d9e7bb]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) {
[4666fae]142 Static.gameState = state;
[5d9e7bb]143 }
144 }
145
146 /* Callback invoked when the surface dimensions change. */
147 public void setSurfaceSize(int width, int height) {
148 synchronized (mSurfaceHolder) {
[4666fae]149 Log.i("AdvanceWars", "width: "+width+", height: "+height);
[5d9e7bb]150 }
151 }
[fea4b77]152
153 private void doLogic() {
[4666fae]154 if(Static.turn == Turn.YOUR_TURN)
[fea4b77]155 return;
156
[4666fae]157 switch(Static.gameState) {
[fea4b77]158 case BATTLE_MAP:
[4666fae]159 Iterator<Unit> iter = Static.enemy.getControlledUnits().iterator();
[c3ad11c]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)
[41c11dd]172 Static.map.getTile(x, y).removeUnit();
173 Static.map.getTile(x, y+1).addUnit(cur);
[c3ad11c]174 }
175 Log.i("AdvanceWars", "finished moving enemy units");
176
177
[4666fae]178 Static.turn = Turn.YOUR_TURN;
[fea4b77]179 break;
180 }
181 }
[5d9e7bb]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
[4666fae]190 switch(Static.gameState) {
[dd3e793]191 case MAIN_MENU:
[4666fae]192 Static.wndMainMenu.draw(canvas);
[dd3e793]193 break;
194 case BATTLE_MAP:
195 mTextPaint.setTextSize(12);
196
[41c11dd]197 Static.map.draw(canvas);
[dd3e793]198
[b97a618]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 }
[ebaddd9]203 }
204
[41c11dd]205 Static.map.drawBuildings(canvas);
206 Static.map.drawUnits(canvas);
[b97a618]207
[dd3e793]208 break;
209 }
[5d9e7bb]210 }
211 }
[bb2fa26]212
[5d9e7bb]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()
[4666fae]221 Static.thread = new DrawingThread(holder, context, new Handler() {});
[5d9e7bb]222
223 setFocusable(true); // make sure we get key events
224 }
225
226 @Override public boolean onTouchEvent(MotionEvent event) {
[205f525]227 Log.i("AdvanceWars", "Detected touch event");
[5d9e7bb]228
229 if(event.getAction() == MotionEvent.ACTION_UP) {
[205f525]230 Log.i("AdvanceWars", "Detected UP touch action");
[4666fae]231 switch(Static.gameState) {
[dd3e793]232 case MAIN_MENU:
[4666fae]233 if(Static.wndMainMenu.getGUIObject("btnNewGame").isClicked(event.getX(), event.getY())) {
[bb2fa26]234 Log.i("AdvanceWars", "Switching to battle map");
[4666fae]235 Static.gameState = GameState.BATTLE_MAP;
236 }else if(Static.wndMainMenu.getGUIObject("btnLoadGame").isClicked(event.getX(), event.getY())) {
[113d7cf]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
[4666fae]249 Static.map = new Map(Static.grassTile, width, height, new Point(offsetX, offsetY));
[113d7cf]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))
[4666fae]260 Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(10, 25)));
[113d7cf]261 else
[4666fae]262 Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(10, 25)));
[113d7cf]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
[4666fae]272 Player humanPlayer = new Player("Human", Static.thread.mUnitPaint1);
[c3ad11c]273
[41c11dd]274 Static.map.getTile(x, y).addUnit(new Soldier(humanPlayer));
[113d7cf]275 }
276
277 b.close();
278 }catch(IOException ioe) {
279 ioe.printStackTrace();
280 }
[4666fae]281 Static.gameState = GameState.BATTLE_MAP;
282 }else if(Static.wndMainMenu.getGUIObject("btnQuit").isClicked(event.getX(), event.getY())) {
283 Static.game.finish();
[205f525]284 }
[5d9e7bb]285 break;
[dd3e793]286 case BATTLE_MAP:
[205f525]287 Log.i("AdvanceWars", "Touch event detected on battle map");
[b97a618]288
[41c11dd]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;
[b97a618]292
[bb2fa26]293 Log.i("AdvanceWars", "About to show context menu");
294 showContextMenu();
295
296 /*
297
[41c11dd]298 Unit target = Static.map.getTile(x, y).currentUnit;
[bdd63ba]299
[4666fae]300 if(Static.thread.selectedUnit != null && Static.thread.selectedUnit.getMovementRange().contains(new Point(x, y))) {
[bb2fa26]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
[4666fae]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);
[bdd63ba]307 }else {
[bb2fa26]308 // Display a context menu that gives the option of attacking the target
[bdd63ba]309 }
[4666fae]310 Static.thread.selectedUnit = null;
[bdd63ba]311 }else
[4666fae]312 Static.thread.selectedUnit = target;
[bb2fa26]313 */
[b97a618]314 }
315
[c3ad11c]316 Log.i("AdvanceWars", "Touch event handling finished");
317
[5d9e7bb]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) {
[4666fae]330 Static.thread.setSurfaceSize(width, height);
[5d9e7bb]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) {
[4666fae]338 Static.run = true;
339 Static.thread.start();
[5d9e7bb]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;
[4666fae]351 Static.run = false;
[5d9e7bb]352 while (retry) {
353 try {
[4666fae]354 Static.thread.join();
[5d9e7bb]355 retry = false;
356 } catch (InterruptedException e) {
357 }
358 }
359 }
360}
Note: See TracBrowser for help on using the repository browser.