comparison game/Engine.java @ 181:2e033eced6e5

Implement "new game" confirmation dialog.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 16 Mar 2017 12:23:02 +0200
parents fa9f29387cff
children cf0256f13612
comparison
equal deleted inserted replaced
180:fa9f29387cff 181:2e033eced6e5
99 } 99 }
100 } 100 }
101 } 101 }
102 102
103 103
104 class NewGameDialog extends IDMContainer
105 {
106 public NewGameDialog()
107 {
108 setPos(150f, 350f);
109 setSize(720f, 200f);
110 setModal(true);
111
112 add(new BtnYes(270f, 120f));
113 add(new BtnCancel(500f, 120f));
114 }
115
116 public void paint(Graphics2D g)
117 {
118 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f));
119 g.fill(new RoundRectangle2D.Float(getScaledX(), getScaledY(), getScaledWidth(), getScaledHeight(), 10, 10));
120
121 setTextFont(G.fonts[3], G.metrics[3]);
122 setTextPaint(Color.white);
123 setCurrPosScaledRel(25, 35);
124 drawString(g, "Really start a new game?\n");
125
126 super.paint(g);
127 }
128
129 public void clicked()
130 {
131 parent.remove(this);
132 }
133
134 class BtnYes extends IDMButton
135 {
136 public BtnYes(float x, float y)
137 {
138 super(x, y, KeyEvent.VK_Y, G.fonts[1], "Yes");
139 }
140
141 public void clicked()
142 {
143 sendMessage("NEWGAME");
144 parent.clicked();
145 }
146 }
147
148 class BtnCancel extends IDMButton
149 {
150 public BtnCancel(float x, float y)
151 {
152 super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "Cancel");
153 }
154
155 public void clicked()
156 {
157 parent.clicked();
158 }
159 }
160 }
161
162
104 class GameBoard extends IDMWidget 163 class GameBoard extends IDMWidget
105 { 164 {
106 static final int boardSize = 9; 165 static final int boardSize = 9;
107 static final int boardMiddle = 4; 166 static final int boardMiddle = 4;
108 Piece[][] board; 167 Piece[][] board;
109 float pscale, ptime; 168 float pscale, ptime;
110 169
111 public boolean flagGameOver, flagBoardIsDirty; 170 public boolean flagGameOver, flagBoardIsDirty;
112 int gameScore; 171 int gameScore;
172 int moves;
113 173
114 Piece currPiece, nextPiece; 174 Piece currPiece, nextPiece;
115 int currX, currY, currPoint; 175 int currX, currY, currPoint;
116 176
117 Sound sndPlaced; 177 Sound sndPlaced;
145 205
146 flagGameOver = false; 206 flagGameOver = false;
147 flagBoardIsDirty = true; 207 flagBoardIsDirty = true;
148 pieceFinishTurn(); 208 pieceFinishTurn();
149 gameScore = 0; 209 gameScore = 0;
210 moves = 0;
150 } 211 }
151 212
152 public void paintBackPlate(Graphics2D g) 213 public void paintBackPlate(Graphics2D g)
153 { 214 {
154 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.2f)); 215 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.2f));
435 } 496 }
436 } 497 }
437 498
438 // Compute and add score 499 // Compute and add score
439 gameScore += connections * connections; 500 gameScore += connections * connections;
501 moves++;
440 502
441 // If game over, clear the game 503 // If game over, clear the game
442 if (flagGameOver) 504 if (flagGameOver)
443 { 505 {
444 currPiece = null; 506 currPiece = null;
503 565
504 GameBoard lauta = null; 566 GameBoard lauta = null;
505 InputStream musa; 567 InputStream musa;
506 IDMWindow widgets; 568 IDMWindow widgets;
507 AboutBox aboutBox; 569 AboutBox aboutBox;
570 NewGameDialog newGameDialog;
508 571
509 public void dbg(String msg) 572 public void dbg(String msg)
510 { 573 {
511 System.out.print("Engine: " + msg); 574 System.out.print("Engine: " + msg);
512 } 575 }
560 widgets.add(new BtnSwapPiece(767f, 450f)); 623 widgets.add(new BtnSwapPiece(767f, 450f));
561 widgets.add(new BtnAbout (767f, 550f)); 624 widgets.add(new BtnAbout (767f, 550f));
562 widgets.add(new BtnNewGame (767f, 630f)); 625 widgets.add(new BtnNewGame (767f, 630f));
563 626
564 aboutBox = new AboutBox(); 627 aboutBox = new AboutBox();
628 newGameDialog = new NewGameDialog();
565 629
566 // Game 630 // Game
567 startNewGame(); 631 startNewGame();
568 632
569 // Initialize event listeners 633 // Initialize event listeners
781 super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "New Game"); 845 super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "New Game");
782 } 846 }
783 847
784 public void clicked() 848 public void clicked()
785 { 849 {
850 if (!lauta.flagGameOver && lauta.moves > 0)
851 {
852 if (!widgets.containsObject(newGameDialog))
853 widgets.add(newGameDialog);
854 }
855 else
786 sendMessage("NEWGAME"); 856 sendMessage("NEWGAME");
787 } 857 }
788 } 858 }
789 859
790 class BtnSwapPiece extends IDMButton 860 class BtnSwapPiece extends IDMButton