}
}
@Override
protected void loop() {
Display2D display=getDisplay();
Graphics2D g=display.getGraphics();
switch (gameState)
{
case IDLE:
animTime=0;
animTimeLastStep=0;
logoPos=0;
gameState=State.IDLE_ANIM;
break;
case IDLE_ANIM:
animTime+=timestep;
if (animTime-animTimeLastStep>=LOGO_ANIM_STEP)
{
animTimeLastStep=animTime;
logoPos++;
if (logoPos>100)
gameState=State.IDLE;
}
g.drawImage(mitrisLogo, 10-logoPos, 6, null);
break;
case GAME_START:
mitrisGame=new MITrisGame(width,height,timestep);
gameState=State.GAME;
break;
case GAME:
if (!controller.isConnected())
return;
//move piece down if it's time
mitrisGame.clockTick();
gameDisplayTime=mitrisGame.getTime();
sendBoardToDisplay(mitrisGame.getDisplayBoard(),display);
if (mitrisGame.isGameOver())
{
System.out.println(String.format("Game over! Lines cleared: %d Time: %3.1f",mitrisGame.getDisplayBoard().getNumCleared(),mitrisGame.getTime()));
gameState=State.GAME_END_1;
gameOverBoard=mitrisGame.getDisplayBoard();
animTime=0;
String extra="";
if (gameOverBoard.getLevel()>=5)
extra=" Great job!";
if (gameOverBoard.getLevel()>=8)
extra=" Amazing!!";
TwitterClient.tweet(String.format("Someone just played Tetris on the MIT Green Building! "+
"They cleared %d lines and lasted %1.1f seconds!%s #mittetris",
gameOverBoard.getNumCleared(),mitrisGame.getTime(),extra));
}
break;
case GAME_END_1:
animTime+=timestep;
if (animTime>GAME_END_WAIT)
{
gameState=State.GAME_END_2;
animTime=0;
animTimeLastStep=0;
if (gameOverBoard.getLevel()>=8)
{
gameState=State.GAME_END_ALT_2;
circTime=new double[] {-100,-100,-100,-100,-100};
circX=new double[5];
circY=new double[5];
circHue=new double[5];
circPos=0;
}
}
sendBoardToDisplay(gameOverBoard,display);
break;
case GAME_END_2:
animTime+=timestep;
if (animTime-animTimeLastStep>=ANIM_TIME_STEP)
{
animTimeLastStep=animTime;
if (gameOverBoard.isBoardEmpty())
{
gameState=State.IDLE;
}
gameOverBoard=gameOverBoard.shiftBoardDown();
}
sendBoardToDisplay(gameOverBoard,display);
break;
case GAME_END_ALT_2:
animTime+=timestep;
if (animTime>=CIRC_ANIM_FADE_WAIT_TIME)
{
if (animTime<=CIRC_ANIM_FADE_WAIT_TIME+CIRC_ANIM_FADE_TIME)
{
double brightness=1-(animTime-CIRC_ANIM_FADE_WAIT_TIME)/CIRC_ANIM_FADE_TIME;
sendBoardToDisplay(gameOverBoard,display,brightness);
}
}
else
sendBoardToDisplay(gameOverBoard,display);
if (animTime-animTimeLastStep>=CIRC_ADD_TIME && animTime<=CIRC_ANIM_STOPADD_TIME)
{
animTimeLastStep=animTime;
circX[circPos]=Math.random()*width;
circY[circPos]=Math.random()*height;
circTime[circPos]=animTime-0.3;
circHue[circPos]=Math.random();
circPos=(circPos+1)%circX.length;
}
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
for (int k=0; k<circX.length; k++)
{
g.setColor(new Color(Color.HSBtoRGB((float)circHue[k], 1, 1)));
g.setStroke(new BasicStroke(1.5f));
double circH=(animTime-circTime[k])*5;
double circW=circH/display.getPixelAspect();
g.draw(new Ellipse2D.Double(circX[k]-circW/2, circY[k]-circH/2, circW, circH));
}
if (animTime>=CIRC_ANIM_TOTAL_TIME)
gameState=State.IDLE;
}