package com.medievaltech.advancewars; import java.io.*; import com.medievaltech.advancewars.Enum.*; import com.medievaltech.unit.*; import com.medievaltech.gui.*; import android.content.Context; import android.graphics.*; import android.os.*; import android.view.*; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; class GameView extends SurfaceView implements SurfaceHolder.Callback { class DrawingThread extends Thread { public GameState mGameState; //maybe make this private and make an accessor for it public Map mMap; public Tile grassTile, oceanTile; public Turn mTurn; //temporary variable to let the enemy control a specific unit //ownership of units hasn't been implemented yet public Unit enemyUnit; private int mCanvasHeight = 1; private int mCanvasWidth = 1; private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1, mTilePaint2, mSelectionPaint, mUnitPaint; /** Indicate whether the surface has been created & is ready to draw */ private boolean mRun = false; /** Handle to the surface manager object we interact with */ private SurfaceHolder mSurfaceHolder; private com.medievaltech.gui.Window wndMainMenu; private Unit selectedUnit; public DrawingThread(SurfaceHolder surfaceHolder, Context context, Handler handler) { mSurfaceHolder = surfaceHolder; mLinePaint = new Paint(); mLinePaint.setAntiAlias(true); mLinePaint.setARGB(255, 0, 255, 0); mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setARGB(255, 255, 255, 255); mTextPaint.setTextSize(12); mTextPaint.setTextAlign(Paint.Align.CENTER); mButtonPaint = new Paint(); mButtonPaint.setAntiAlias(true); mButtonPaint.setARGB(255, 0, 0, 0); mButtonPaint.setTextSize(20); mButtonPaint.setTextAlign(Paint.Align.CENTER); mTilePaint1 = new Paint(); mTilePaint1.setAntiAlias(true); mTilePaint1.setARGB(255, 0, 255, 0); mTilePaint2 = new Paint(); mTilePaint2.setAntiAlias(true); mTilePaint2.setARGB(255, 0, 0, 255); mUnitPaint = new Paint(); mUnitPaint.setAntiAlias(true); mUnitPaint.setARGB(255, 255, 0, 0); mSelectionPaint = new Paint(); mSelectionPaint.setAntiAlias(true); mSelectionPaint.setARGB(255, 255, 127, 0); wndMainMenu = new com.medievaltech.gui.Window(0, 0, 320, 450);; wndMainMenu.addGUIObject("txtTitle", new Text("Main Menu", 100, 30, 120, 20, mTextPaint)); wndMainMenu.addGUIObject("btnNewGame", new Button("New Game", 100, 90, 120, 20, mLinePaint, mButtonPaint)); wndMainMenu.addGUIObject("btnLoadGame", new Button("Load Game", 100, 125, 120, 20, mLinePaint, mButtonPaint)); wndMainMenu.addGUIObject("btnMapEditor", new Button("Map Editor", 100, 160, 120, 20, mLinePaint, mButtonPaint)); wndMainMenu.addGUIObject("btnQuit", new Button("Quit", 100, 195, 120, 20, mLinePaint, mButtonPaint)); grassTile = new Tile(mTilePaint1, TerrainType.LAND); oceanTile = new Tile(mTilePaint2, TerrainType.SEA); mMap = new Map(grassTile, 6, 8, new Point(10, 25)); boolean land = true; for(int x=0; x= thread.mMap.offset.x && event.getY() >= thread.mMap.offset.y) { int x = ((int)event.getX() - thread.mMap.offset.x) / 50; int y = ((int)event.getY() - thread.mMap.offset.y) / 50; Unit target = thread.mMap.getTile(x, y).currentUnit; if(thread.selectedUnit != null && thread.selectedUnit.getMovementRange().contains(new Point(x, y))) { if(target == null || target == thread.selectedUnit) { thread.mMap.getTile(thread.selectedUnit.location.x, thread.selectedUnit.location.y).removeUnit(); thread.mMap.getTile(x, y).addUnit(thread.selectedUnit); }else { // the target contains another unit. If the unit is enemy-controlled, attack it } thread.selectedUnit = null; }else thread.selectedUnit = target; } break; } }else if(event.getAction() == MotionEvent.ACTION_DOWN) { } return true; } /** * Fetches the animation thread corresponding to this LunarView. * * @return the animation thread */ public DrawingThread getThread() { return thread; } /** * Installs a pointer to the text view used for messages. */ public void setTextView(TextView textView) { mStatusText = textView; } /* Callback invoked when the surface dimensions change. */ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { thread.setSurfaceSize(width, height); } /* * Callback invoked when the Surface has been created and is ready to be * used. */ public void surfaceCreated(SurfaceHolder holder) { thread.setRunning(true); thread.start(); } /* * Callback invoked when the Surface has been destroyed and must no longer * be touched. WARNING: after this method returns, the Surface/Canvas must * never be touched again! */ public void surfaceDestroyed(SurfaceHolder holder) { // we have to tell thread to shut down & wait for it to finish, or else // it might touch the Surface after we return and explode boolean retry = true; thread.setRunning(false); while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { } } } }