- Timestamp:
- Oct 20, 2011, 3:25:50 PM (13 years ago)
- Branches:
- master
- Children:
- bb2fa26
- Parents:
- 331d180
- Location:
- src/com/medievaltech/advancewars
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/medievaltech/advancewars/Game.java
r331d180 r4666fae 9 9 import android.view.MenuItem; 10 10 import android.view.Window; 11 import android.widget.TextView;12 11 13 import com.medievaltech.advancewars.GameView.*;14 12 import com.medievaltech.advancewars.Enum.*; 15 13 16 14 public class Game extends Activity { 17 public DrawingThread mThread; 18 private GameView mGameView; 19 15 20 16 /** 21 17 * Invoked during init to give the Activity a chance to set up its Menu. … … 47 43 48 44 if(i == MenuOption.END_TURN.ordinal()) { 49 mThread.mTurn = Turn.ENEMY_TURN;45 Static.turn = Turn.ENEMY_TURN; 50 46 }else if(i == MenuOption.SAVE.ordinal()) { 51 47 try { … … 57 53 } 58 54 }else if(i == MenuOption.MAIN.ordinal()) { 59 mThread.mGameState = GameState.MAIN_MENU;55 Static.gameState = GameState.MAIN_MENU; 60 56 }else if(i == MenuOption.EXIT.ordinal()) { 61 57 finish(); … … 85 81 setContentView(R.layout.main); 86 82 87 mGameView = (GameView) findViewById(R.id.lunar); 88 mThread = mGameView.getThread(); 89 mGameView.mGame = this; 90 91 mGameView.setTextView((TextView) findViewById(R.id.text)); 83 Static.game = this; 92 84 93 85 if (savedInstanceState == null) { // we were just launched: set up a new game … … 96 88 Log.w("AdvanceWars", "SIS is nonnull"); 97 89 98 mGameView.getThread().mGameState = (GameState)savedInstanceState.getSerializable("gameState");90 Static.gameState = (GameState)savedInstanceState.getSerializable("gameState"); 99 91 } 100 92 } … … 108 100 @Override 109 101 protected void onSaveInstanceState(Bundle outState) { 110 outState.putSerializable("gameState", mGameView.getThread().mGameState);102 outState.putSerializable("gameState", Static.gameState); 111 103 112 104 super.onSaveInstanceState(outState); -
src/com/medievaltech/advancewars/GameView.java
r331d180 r4666fae 14 14 import android.util.AttributeSet; 15 15 import android.util.Log; 16 import android.widget.TextView;17 16 18 17 class GameView extends SurfaceView implements SurfaceHolder.Callback { 19 18 20 class DrawingThread extends Thread { 21 public GameState mGameState; 22 23 //maybe make this private and make an accessor for it 24 //public Map mMap; 25 26 public Tile grassTile, oceanTile; 27 28 public Turn mTurn; 29 30 public Player enemy; 19 class DrawingThread extends Thread { 20 31 21 private Unit selectedUnit; 32 33 private int mCanvasHeight = 1;34 private int mCanvasWidth = 1;35 22 36 23 private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1, 37 24 mTilePaint2, mSelectionPaint, mUnitPaint1, mUnitPaint2; 38 25 39 /** Indicate whether the surface has been created & is ready to draw */40 private boolean mRun = false;41 42 26 /** Handle to the surface manager object we interact with */ 43 27 private SurfaceHolder mSurfaceHolder; 44 28 45 private com.medievaltech.gui.Window wndMainMenu;46 47 29 public DrawingThread(SurfaceHolder surfaceHolder, Context context, Handler handler) { 48 30 mSurfaceHolder = surfaceHolder; 49 31 50 32 mLinePaint = new Paint(); 51 mLinePaint.setAntiAlias(true); 52 mLinePaint.setARGB(255, 0, 255, 0); 33 initPaint(mLinePaint, 0, 255, 0); 53 34 54 35 mTextPaint = new Paint(); 55 mTextPaint.setAntiAlias(true);56 mTextPaint.setARGB(255, 255, 255, 255);57 36 mTextPaint.setTextSize(12); 58 37 mTextPaint.setTextAlign(Paint.Align.CENTER); 38 initPaint(mTextPaint, 255, 255, 255); 59 39 60 40 mButtonPaint = new Paint(); 61 mButtonPaint.setAntiAlias(true);62 mButtonPaint.setARGB(255, 0, 0, 0);63 41 mButtonPaint.setTextSize(20); 64 42 mButtonPaint.setTextAlign(Paint.Align.CENTER); 43 initPaint(mButtonPaint, 0, 0, 0); 65 44 66 45 mTilePaint1 = new Paint(); 67 mTilePaint1.setAntiAlias(true); 68 mTilePaint1.setARGB(255, 0, 255, 0); 46 initPaint(mTilePaint1, 0, 255, 0); 69 47 70 48 mTilePaint2 = new Paint(); 71 mTilePaint2.setAntiAlias(true); 72 mTilePaint2.setARGB(255, 0, 0, 255); 49 initPaint(mTilePaint2, 0, 0, 255); 73 50 74 51 mUnitPaint1 = new Paint(); 75 mUnitPaint1.setAntiAlias(true); 76 mUnitPaint1.setARGB(255, 255, 0, 0); 52 initPaint(mUnitPaint1, 255, 0, 0); 77 53 78 54 mUnitPaint2 = new Paint(); 79 mUnitPaint2.setAntiAlias(true); 80 mUnitPaint2.setARGB(255, 160, 160, 255); 55 initPaint(mUnitPaint2, 160, 160, 255); 81 56 82 57 mSelectionPaint = new Paint(); 83 mSelectionPaint.setAntiAlias(true); 84 mSelectionPaint.setARGB(255, 255, 127, 0); 58 initPaint(mSelectionPaint, 255, 127, 0); 85 59 86 60 Player.neutralColor = new Paint(); 87 Player.neutralColor.setAntiAlias(true); 88 Player.neutralColor.setARGB(255, 127, 127, 127); 89 90 wndMainMenu = new com.medievaltech.gui.Window(0, 0, 320, 450);; 91 wndMainMenu.addGUIObject("txtTitle", new Text("Main Menu", 100, 30, 120, 20, mTextPaint)); 92 wndMainMenu.addGUIObject("btnNewGame", new Button("New Game", 100, 90, 120, 20, mLinePaint, mButtonPaint)); 93 wndMainMenu.addGUIObject("btnLoadGame", new Button("Load Game", 100, 125, 120, 20, mLinePaint, mButtonPaint)); 94 wndMainMenu.addGUIObject("btnMapEditor", new Button("Map Editor", 100, 160, 120, 20, mLinePaint, mButtonPaint)); 95 wndMainMenu.addGUIObject("btnQuit", new Button("Quit", 100, 195, 120, 20, mLinePaint, mButtonPaint)); 96 97 grassTile = new Tile(mTilePaint1, TerrainType.LAND); 98 oceanTile = new Tile(mTilePaint2, TerrainType.SEA); 99 100 Static.map = new Map(grassTile, 6, 8, new Point(10, 25)); 61 initPaint(Player.neutralColor, 127, 127, 127); 62 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)); 69 70 Static.grassTile = new Tile(mTilePaint1, TerrainType.LAND); 71 Static.oceanTile = new Tile(mTilePaint2, TerrainType.SEA); 72 73 Static.map = new Map(Static.grassTile, 6, 8, new Point(10, 25)); 101 74 102 75 boolean land = true; … … 105 78 for(int y=0; y<Static.map.getHeight(); y++) { 106 79 if(land) 107 Static.map.setTile(x, y, new Tile( grassTile, new Point(x, y)));80 Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(x, y))); 108 81 else 109 Static.map.setTile(x, y, new Tile( oceanTile, new Point(x, y)));82 Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(x, y))); 110 83 land = !land; 111 84 } … … 113 86 } 114 87 115 Player humanPlayer= new Player("Human", mUnitPaint1);116 enemy = new Player("Comp", mUnitPaint2);117 118 Static.map.getTile(0, 0).addUnit(new Soldier( enemy));119 Static.map.getTile(2, 3).addUnit(new Soldier( humanPlayer));120 Static.map.getTile(5, 6).addUnit(new Soldier( humanPlayer));88 Static.human = new Player("Human", mUnitPaint1); 89 Static.enemy = new Player("Comp", mUnitPaint2); 90 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)); 121 94 122 95 Static.map.getTile(4, 4).addBuilding(new City()); 123 96 124 mTurn = Turn.YOUR_TURN; 125 126 mGameState = GameState.MAIN_MENU; 97 Static.turn = Turn.YOUR_TURN; 98 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); 127 105 } 128 106 … … 134 112 synchronized (mSurfaceHolder) { 135 113 Log.i("AdvanceWars", "Player's turn starting now"); 136 mGameState = GameState.MAIN_MENU;114 Static.gameState = GameState.MAIN_MENU; 137 115 } 138 116 } … … 140 118 @Override 141 119 public void run() { 142 while ( mRun) {120 while (Static.run) { 143 121 Canvas c = null; 144 122 try { … … 158 136 } 159 137 } 160 161 /**162 * Used to signal the thread whether it should be running or not.163 * Passing true allows the thread to run; passing false will shut it164 * down if it's already running. Calling start() after this was most165 * recently called with false will result in an immediate shutdown.166 *167 * @param b true to run, false to shut down168 */169 public void setRunning(boolean b) {170 mRun = b;171 }172 138 173 139 public void setGameState(GameState state) { 174 140 synchronized (mSurfaceHolder) { 175 mGameState = state;141 Static.gameState = state; 176 142 } 177 143 } … … 179 145 /* Callback invoked when the surface dimensions change. */ 180 146 public void setSurfaceSize(int width, int height) { 181 // synchronized to make sure these all change atomically182 147 synchronized (mSurfaceHolder) { 183 mCanvasWidth = width; 184 mCanvasHeight = height; 185 186 Log.i("AdvanceWars", "width: "+mCanvasWidth+", height: "+mCanvasHeight); 148 Log.i("AdvanceWars", "width: "+width+", height: "+height); 187 149 } 188 150 } 189 151 190 152 private void doLogic() { 191 if( mTurn == Turn.YOUR_TURN)153 if(Static.turn == Turn.YOUR_TURN) 192 154 return; 193 155 194 switch( mGameState) {156 switch(Static.gameState) { 195 157 case BATTLE_MAP: 196 Iterator<Unit> iter = enemy.getControlledUnits().iterator();158 Iterator<Unit> iter = Static.enemy.getControlledUnits().iterator(); 197 159 Unit cur; 198 160 int x, y; … … 213 175 214 176 215 mTurn = Turn.YOUR_TURN;177 Static.turn = Turn.YOUR_TURN; 216 178 break; 217 179 } … … 225 187 canvas.drawColor(Color.BLACK); 226 188 227 switch( mGameState) {189 switch(Static.gameState) { 228 190 case MAIN_MENU: 229 wndMainMenu.draw(canvas);191 Static.wndMainMenu.draw(canvas); 230 192 break; 231 193 case BATTLE_MAP: … … 249 211 250 212 /** Pointer to the text view to display "Paused.." etc. */ 251 private TextView mStatusText; 252 253 /** The thread that actually draws the animation */ 254 private DrawingThread thread; 255 256 public Game mGame; 213 //private TextView mStatusText; 257 214 258 215 public GameView(Context context, AttributeSet attrs) { … … 264 221 265 222 // create thread only; it's started in surfaceCreated() 266 thread = new DrawingThread(holder, context, new Handler() { 267 @Override 268 public void handleMessage(Message m) { 269 mStatusText.setVisibility(m.getData().getInt("viz")); 270 mStatusText.setText(m.getData().getString("text")); 271 } 272 }); 223 Static.thread = new DrawingThread(holder, context, new Handler() {}); 273 224 274 225 setFocusable(true); // make sure we get key events … … 280 231 if(event.getAction() == MotionEvent.ACTION_UP) { 281 232 Log.i("AdvanceWars", "Detected UP touch action"); 282 switch( thread.mGameState) {233 switch(Static.gameState) { 283 234 case MAIN_MENU: 284 235 Log.i("AdvanceWars", "Switching to battle map"); 285 if( thread.wndMainMenu.getGUIObject("btnNewGame").isClicked(event.getX(), event.getY())) {286 thread.mGameState = GameState.BATTLE_MAP;287 }else if( thread.wndMainMenu.getGUIObject("btnLoadGame").isClicked(event.getX(), event.getY())) {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())) { 288 239 BufferedReader b; 289 240 try { … … 298 249 int offsetY = Integer.parseInt(offset.substring(offset.indexOf("x")+1)); 299 250 300 Static.map = new Map( thread.grassTile, width, height, new Point(offsetX, offsetY));251 Static.map = new Map(Static.grassTile, width, height, new Point(offsetX, offsetY)); 301 252 302 253 Log.i("GameSave", "Created the map"); … … 309 260 TerrainType type = TerrainType.values()[Integer.parseInt(arr[y])]; 310 261 if(type.equals(TerrainType.LAND)) 311 Static.map.setTile(x, y, new Tile( thread.grassTile, new Point(10, 25)));262 Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(10, 25))); 312 263 else 313 Static.map.setTile(x, y, new Tile( thread.oceanTile, new Point(10, 25)));264 Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(10, 25))); 314 265 } 315 266 } … … 321 272 int y = Integer.parseInt(unit.substring(unit.indexOf(",")+1)); 322 273 323 Player humanPlayer = new Player("Human", mGame.mThread.mUnitPaint1);274 Player humanPlayer = new Player("Human", Static.thread.mUnitPaint1); 324 275 325 276 Static.map.getTile(x, y).addUnit(new Soldier(humanPlayer)); … … 330 281 ioe.printStackTrace(); 331 282 } 332 thread.mGameState = GameState.BATTLE_MAP;333 }else if( thread.wndMainMenu.getGUIObject("btnQuit").isClicked(event.getX(), event.getY())) {334 mGame.finish();283 Static.gameState = GameState.BATTLE_MAP; 284 }else if(Static.wndMainMenu.getGUIObject("btnQuit").isClicked(event.getX(), event.getY())) { 285 Static.game.finish(); 335 286 } 336 287 break; … … 344 295 Unit target = Static.map.getTile(x, y).currentUnit; 345 296 346 if( thread.selectedUnit != null &&thread.selectedUnit.getMovementRange().contains(new Point(x, y))) {347 if(target == null || target == thread.selectedUnit) {348 Static.map.getTile( thread.selectedUnit.location.x,thread.selectedUnit.location.y).removeUnit();349 Static.map.getTile(x, y).addUnit( thread.selectedUnit);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); 350 301 }else { 351 302 // the target contains another unit. If the unit is enemy-controlled, attack it 352 303 } 353 thread.selectedUnit = null;304 Static.thread.selectedUnit = null; 354 305 }else 355 thread.selectedUnit = target;306 Static.thread.selectedUnit = target; 356 307 } 357 308 … … 366 317 return true; 367 318 } 368 369 /**370 * Fetches the animation thread corresponding to this LunarView.371 *372 * @return the animation thread373 */374 public DrawingThread getThread() {375 return thread;376 }377 378 /**379 * Installs a pointer to the text view used for messages.380 */381 public void setTextView(TextView textView) {382 mStatusText = textView;383 }384 319 385 320 /* Callback invoked when the surface dimensions change. */ 386 321 public void surfaceChanged(SurfaceHolder holder, int format, int width, 387 322 int height) { 388 323 Static.thread.setSurfaceSize(width, height); 389 324 } 390 325 … … 394 329 */ 395 330 public void surfaceCreated(SurfaceHolder holder) { 396 thread.setRunning(true);397 331 Static.run = true; 332 Static.thread.start(); 398 333 } 399 334 … … 407 342 // it might touch the Surface after we return and explode 408 343 boolean retry = true; 409 thread.setRunning(false);344 Static.run = false; 410 345 while (retry) { 411 346 try { 412 347 Static.thread.join(); 413 348 retry = false; 414 349 } catch (InterruptedException e) { -
src/com/medievaltech/advancewars/Static.java
r331d180 r4666fae 1 1 package com.medievaltech.advancewars; 2 3 import com.medievaltech.advancewars.Enum.*; 4 import com.medievaltech.advancewars.GameView.DrawingThread; 2 5 3 6 public class Static { 4 7 public static Map map; 8 9 public static GameState gameState; 10 11 public static Tile grassTile, oceanTile; 12 13 public static Turn turn; 14 15 public static Player human, enemy; 16 17 public static DrawingThread thread; 18 19 public static Game game; 20 21 public static boolean run = false; 22 23 public static com.medievaltech.gui.Window wndMainMenu; 5 24 }
Note:
See TracChangeset
for help on using the changeset viewer.