package com.chrislee.tetris;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
public class TetrisView extends View implements Runnable{
final static int SCREEN_WIDTH = 320;
final static int SCREEN_HEIGHT = 455;
final int STATE_MENU = 0;
final int STATE_PLAY = 1;
final int STATE_PAUSE = 2;
final int STATE_OVER = 3;
// final int MENU_NEWGAME = 10;
// final int MENU_SETTING = 11;
// final int MENU_HELP = 12;
//
// final int SETTING_MUSIC_ON = 21;
// final int SETTING_MUSIC_OFF = 22;
// final int SETTING_LEVEL = 23;
// final int OPTION_RESUME = 31;
// final int OPTION_MENU = 32;
// final int OPTION_QUIT = 33;
public static final int MAX_LEVEL = 6;
public static final String TAG = "TetrisView";
public static final String DATAFILE = "save.dt";
int mGamestate = STATE_PLAY;
int mScore = 0;
int mSpeed = 1;
int mDeLine = 0;
boolean mIsCombo = false; //
boolean mIsPaused = false;
boolean mIsVoice = true;
long mMoveDelay = 600;
long mLastMove = 0;
// int mAtPause = OPTION_RESUME;
private Context mContext = null;
private Paint mPaint = new Paint();
RefreshHandler mRefreshHandler = null;
//RefreshHandler mRefreshHandler = null;
TileView mCurrentTile = null;
TileView mNextTile = null;
Court mCourt = null;
ResourceStore mResourceStore = null;
///////////
MusicPlayer mMPlayer = null;
public TetrisView(Context context) {
super(context);
init(context);
// TODO Auto-generated constructor stub
}
protected void init(Context context)
{
mContext = context;
mCurrentTile = new TileView(context);
Log.i("tetris","mCurrentTile builed");
mNextTile = new TileView(context);
mCourt = new Court(context);
mRefreshHandler = new RefreshHandler(this);
mResourceStore = new ResourceStore(context);
////////////////////////////////////////
mMPlayer = new MusicPlayer(context);
//
setLevel(1);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
setFocusable(true);
new Thread(this).start();
}
public void logic()
{
switch(mGamestate)
{
case STATE_MENU:
//
mGamestate = STATE_PLAY;
break;
case STATE_PLAY:
//startGame();
playGame();
break;
case STATE_PAUSE:
// if(mAtPause == OPTION_RESUME)
// {
// mIsPaused = false;
// mGamestate = STATE_PLAY;
// break;
// }
//if(mPusedChoose = )
break;
case STATE_OVER:
// startGame();
// mGamestate = STATE_PLAY;
break;
default:;
}
}
public void startGame()
{
mGamestate = STATE_PLAY;
mCourt.clearCourt();
mCurrentTile = new TileView(mContext);
mNextTile = new TileView(mContext);
// mSpeed = speed choosed
// mScore
// mLine
setLevel(1);
mScore = 0;
mDeLine = 0;
mIsPaused = false;
mIsCombo = false;
playGame();
}
public void playGame()
{
long now = System.currentTimeMillis();
if(now - mLastMove > mMoveDelay)
{
if(mIsPaused)
{
return;
}
if(mIsCombo)
{
mCourt.placeTile(mCurrentTile);
//////
mMPlayer.playMoveVoice();
if(mCourt.isGameOver() )
{
mGamestate = STATE_OVER;
return;
}
int line = mCourt.removeLines();
if(line > 0 )
{
mMPlayer.playBombVoice();
}
mDeLine += line;
countScore(line);
mCurrentTile = mNextTile;
mNextTile = new TileView(mContext);
mIsCombo = false;
}
moveDown();
mLastMove = now;
}
}
private void countScore(int line)
{
switch(line)
{
case 1: mScore += 100;break;
case 2: mScore += 300;break;
case 3: mScore += 600;break;
case 4: mScore += 1000;break;
default: ;
}
if(mScore >= 2000 && mScore <4000)
{
setLevel(2);
}
else if(mScore >= 4000 && mScore < 6000)
{
setLevel(3);
}
else if(mScore >= 6000 && mScore < 8000)
{
setLevel(4);
}
else if(mScore >= 8000 && mScore < 10000)
{
setLevel(5);
}
else if(mScore >= 10000)
{
setLevel(6);
}
}
protected void onDraw(Canvas canvas)
{
switch(mGamestate)
{
case STATE_MENU:
paintMenu(canvas);
break;
case STATE_PLAY:
paintGame(canvas);
break;
case STATE_PAUSE:
paintPause(canvas);
break;
case STATE_OVER:
paintOver(canvas);
break;
default:;
}
}
public boolean isGameOver()
{
return mCourt.isGameOver();
}
public boolean onKeyDown(int keyCode,KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_DPAD_UP:
if(mGamestate == STATE_PLAY)
{
if(!mIsPaused)
{
rotate();
mMPlayer.playMoveVoice();
}
}
else if(mGamestate == STATE_PAUSE)
{
}
else if(mGamestate == STATE_MENU)
{
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if(mGamestate == STATE_PLAY)
{
if(!mIsPaused)
{
moveDown();
mMPlayer.playMoveVoice();
}
}
else if(mGamestate == STATE_PAUSE)
{
}
else if(mGamestate == STATE_MENU)
{
}
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
if(mGamestate == STATE_PLAY)
{
if(!mIsPaused)
{
moveLeft();
mMPlayer.playMoveVoice();
}
}
else if(mGamestate == STATE_PAUSE)
{
}
else if(mGamestate == STATE_MENU)
{
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if(mGamestate == STATE_PLAY)
{
if(!mIsPaused)
{
moveRight();
mMPlayer.playMoveVoice();
}
}
else if(mGamestate == STATE_PAUSE)
{
}
else if(mGamestate == STATE_MENU)
{
}
break;
case KeyEvent.KEYCODE_ENTER: ;
case KeyEvent.KEYCODE_DPAD_CENTER:
if(mGamestate == STATE_PLAY)
{
if(!mIsPaused)
{
fastDrop();
mMPlayer.playMoveVoice();
}
}
else if(mGamestate == STATE_PAUSE)
{
}
else if(mGamestate == STATE_MENU)
{
}
break;
//
case KeyEvent.KEYCODE_S:
if(mGamestate == STATE_PLAY)
{
mIsPaused = true;
}
else if(mGamestate == STATE_PAUSE)
{
mIsPaused = false;
}
else if(mGamestate == STATE_MENU)
{
}
break;
case KeyEvent.KEYCODE_SPACE:
mIsPaused = !mIsPaused;
if(mIsPaused)
{
mRefreshHandler.pause();
}
else
{
mRefreshHandler.resume();
}
break;
default: ;
}
return super.onKeyDown(keyCode,event);
}
private void rotate()
{
// check
if(!mIsCombo)
mCurrentTile.rotateOnCourt(mCourt);
}
private void moveDown()
{
if(!mIsCombo)
{
if( ! mCurrentTile.moveDownOnCourt(mCourt) )
mIsCombo = true;
}
}
private void moveLeft()
{
if(!mIsCombo)
{
mCurrentTile.moveLeftOnCourt(mCourt);
}
}
private void moveRight()
{
if(!mIsCombo)
{
mCurrentTile.moveRightOnCourt(mCourt);
}
}
private void fastDrop()
{
if(!mIsCombo)
{
mCurrentTile.fastDropOnCourt(mCourt);
mIsCombo = true;
}
}
private void paintMenu(Canvas canvas)
{
DrawTool.paintImage(canvas,mResourceStore.getMenuBackground(),0,0);
DrawTool.paintImage(canvas,mResourceStore.getMenu(),0,SCREEN_HEIGHT/2 - mResourceStore.getMenu().getHeight()/2 );
}
private void paintGame(Canvas canvas)
{
mCourt.paintCourt(canvas);
mCurrentTile.paintTile(canvas);
//mNextTile.paintTile(canvas);
mPaint.setTextSize(20);
paint