package com.medievaltech.advancewars; import java.io.*; import java.util.*; 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; public Player enemy; private Unit selectedUnit; private int mCanvasHeight = 1; private int mCanvasWidth = 1; private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1, mTilePaint2, mSelectionPaint, mUnitPaint1, mUnitPaint2; /** 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; 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); mUnitPaint1 = new Paint(); mUnitPaint1.setAntiAlias(true); mUnitPaint1.setARGB(255, 255, 0, 0); mUnitPaint2 = new Paint(); mUnitPaint2.setAntiAlias(true); mUnitPaint2.setARGB(255, 160, 160, 255); 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 iter = enemy.getControlledUnits().iterator(); Unit cur; int x, y; Log.i("AdvanceWars", "starting to move enemy units"); while(iter.hasNext()) { cur = iter.next(); x = cur.location.x; y = cur.location.y; Log.i("AdvanceWars", "moving enemy unit"); //any unit that's in the way is removed (needs to be changed eventuallyy) thread.mMap.getTile(x, y).removeUnit(); thread.mMap.getTile(x, y+1).addUnit(cur); } Log.i("AdvanceWars", "finished moving enemy units"); mTurn = Turn.YOUR_TURN; break; } } /** * Draws the ship, fuel/speed bars, and background to the provided * Canvas. */ private void doDraw(Canvas canvas) { canvas.drawColor(Color.BLACK); switch(mGameState) { case MAIN_MENU: wndMainMenu.draw(canvas); break; case BATTLE_MAP: mTextPaint.setTextSize(12); mMap.draw(canvas); if(selectedUnit != null) { for(Point p : selectedUnit.getMovementRange()) { canvas.drawRect(p.x*50+10, p.y*50+25, p.x*50+50+10, p.y*50+50+25, mSelectionPaint); } } mMap.drawUnits(canvas); break; } } } /** Pointer to the text view to display "Paused.." etc. */ private TextView mStatusText; /** The thread that actually draws the animation */ private DrawingThread thread; public Game mGame; public GameView(Context context, AttributeSet attrs) { super(context, attrs); // register our interest in hearing about changes to our surface SurfaceHolder holder = getHolder(); holder.addCallback(this); // create thread only; it's started in surfaceCreated() thread = new DrawingThread(holder, context, new Handler() { @Override public void handleMessage(Message m) { mStatusText.setVisibility(m.getData().getInt("viz")); mStatusText.setText(m.getData().getString("text")); } }); setFocusable(true); // make sure we get key events } @Override public boolean onTouchEvent(MotionEvent event) { Log.i("AdvanceWars", "Detected touch event"); if(event.getAction() == MotionEvent.ACTION_UP) { Log.i("AdvanceWars", "Detected UP touch action"); switch(thread.mGameState) { case MAIN_MENU: Log.i("AdvanceWars", "Switching to battle map"); if(thread.wndMainMenu.getGUIObject("btnNewGame").isClicked(event.getX(), event.getY())) { thread.mGameState = GameState.BATTLE_MAP; }else if(thread.wndMainMenu.getGUIObject("btnLoadGame").isClicked(event.getX(), event.getY())) { BufferedReader b; try { b = new BufferedReader(new FileReader(android.os.Environment.getExternalStorageDirectory()+"/save.txt")); int width = Integer.parseInt(b.readLine()); int height = Integer.parseInt(b.readLine()); String offset = b.readLine(); Log.i("GameSave", offset); int offsetX = Integer.parseInt(offset.substring(0, offset.indexOf("x"))); int offsetY = Integer.parseInt(offset.substring(offset.indexOf("x")+1)); thread.mMap = new Map(thread.grassTile, width, height, new Point(offsetX, offsetY)); Log.i("GameSave", "Created the map"); 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; } Log.i("AdvanceWars", "Touch event handling finished"); 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) { } } } }