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

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

Moved many widely used variables to the Static class.

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