Mercurial > hg > ristipolku
annotate game/Engine.java @ 119:52d79502447c
Make few variables un-public.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 31 May 2011 20:22:18 +0300 |
parents | 973f567c7b9d |
children | fa1b86b632aa |
rev | line source |
---|---|
0 | 1 /* |
9 | 2 * Ristipolku Game Engine |
0 | 3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
4 */ | |
9 | 5 package game; |
6 | |
0 | 7 import java.awt.*; |
8 import java.awt.geom.*; | |
1 | 9 import java.awt.event.*; |
7 | 10 import java.awt.image.*; |
11 import java.awt.event.*; | |
25 | 12 import java.awt.font.*; |
9 | 13 import javax.imageio.*; |
7 | 14 import javax.swing.*; |
1 | 15 import java.util.*; |
6 | 16 import java.io.*; |
1 | 17 import game.*; |
37 | 18 import javax.sound.sampled.*; |
21 | 19 |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
20 |
75 | 21 class AboutBox extends IDMWidget |
8 | 22 { |
75 | 23 BufferedImage aboutImg; |
24 boolean aboutSecret; | |
25 | |
76 | 26 IDMPoint currPos, currOffs; |
27 Paint textPaint; | |
28 Font textFont; | |
29 FontMetrics textMetrics; | |
30 | |
31 | |
75 | 32 public AboutBox() |
33 { | |
34 try { | |
35 ResourceLoader res = new ResourceLoader("graphics/girl.jpg"); | |
36 aboutImg = ImageIO.read(res.getStream()); | |
37 } | |
38 catch (IOException e) | |
39 { | |
40 } | |
41 | |
42 aboutSecret = false; | |
43 | |
44 setPos(150f, 150f); | |
81
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
45 setSize(675f, 420f); |
75 | 46 } |
47 | |
48 public void setTextFont(Font font, FontMetrics metrics) | |
49 { | |
50 textFont = font; | |
51 textMetrics = metrics; | |
52 } | |
53 | |
54 public void setTextPaint(Paint paint) | |
8 | 55 { |
75 | 56 textPaint = paint; |
57 } | |
58 | |
59 public void setCurrPos(IDMPoint npos) | |
60 { | |
61 currPos = npos; | |
62 currOffs = new IDMPoint(0, 0); | |
63 } | |
64 | |
65 public void setCurrPos(float x, float y) | |
66 { | |
67 setCurrPos(new IDMPoint(x, y)); | |
8 | 68 } |
69 | |
75 | 70 public void drawString(Graphics2D g, String text) |
8 | 71 { |
75 | 72 Paint savePaint = g.getPaint(); |
73 g.setPaint(textPaint); | |
76 | 74 g.setFont(textFont); |
75 | 75 |
76 int i = 0; | |
77 while (i < text.length()) | |
8 | 78 { |
75 | 79 int p = text.indexOf("\n", i); |
80 boolean linefeed; | |
81 String str; | |
80
3d34258ccc2c
Render empty lines correctly.
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
82 if (p >= i) |
75 | 83 { |
84 str = text.substring(i, p); | |
85 i = p + 1; | |
86 linefeed = true; | |
87 } | |
88 else | |
89 { | |
90 str = text.substring(i); | |
91 i += str.length(); | |
92 linefeed = false; | |
93 } | |
94 | |
95 g.drawString(str, currPos.x + currOffs.x, currPos.y + currOffs.y); | |
96 | |
97 if (linefeed) | |
98 { | |
99 currOffs.x = 0; | |
76 | 100 currOffs.y += textMetrics.getHeight(); |
75 | 101 } |
102 else | |
103 { | |
104 currOffs.x += textMetrics.stringWidth(str); | |
105 } | |
8 | 106 } |
75 | 107 |
108 g.setPaint(savePaint); | |
109 } | |
110 | |
111 public void paint(Graphics2D g) | |
112 { | |
113 int x = getScaledX(), y = getScaledY(), | |
114 w = getScaledWidth(), h = getScaledHeight(); | |
115 | |
115 | 116 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f)); |
75 | 117 g.fill(new RoundRectangle2D.Float(x, y, w, h, 10, 10)); |
118 | |
76 | 119 setTextFont(G.fonts[3], G.metrics[3]); |
75 | 120 setTextPaint(Color.white); |
8 | 121 |
76 | 122 setCurrPos(x + 20, y + 30); |
105
75015dfd47ef
Move version number info to G.java.
Matti Hamalainen <ccr@tnsp.org>
parents:
93
diff
changeset
|
123 drawString(g, "RistiPolku (CrossPaths) v"+ G.version +"\n"); |
75 | 124 |
76 | 125 setTextFont(G.fonts[1], G.metrics[1]); |
75 | 126 if (aboutSecret) |
127 { | |
115 | 128 g.drawImage(aboutImg, x + 20, y + 55, |
129 aboutImg.getWidth(), aboutImg.getHeight(), null); | |
75 | 130 |
76 | 131 setCurrPos(x + 225, y + 75); |
75 | 132 drawString(g, |
133 "Dedicated to my\n" + | |
76 | 134 "favorite woman\n" + |
75 | 135 "in the world."); |
136 | |
76 | 137 setCurrPos(x + 370, y + 175); |
75 | 138 drawString(g, "- Matti"); |
139 } | |
8 | 140 else |
141 { | |
75 | 142 setTextPaint(Color.yellow); |
143 drawString(g, "(c) Copyright 2011 Matti 'ccr' Hämäläinen\n"); | |
144 | |
145 setTextPaint(Color.white); | |
146 drawString(g, "Programming project for Java-course\n" + | |
81
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
147 "T740306 taught by Kari Laitinen." + |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
148 "\n \n"); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
149 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
150 setTextPaint(Color.red); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
151 drawString(g, "Controls:\n"); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
152 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
153 IDMPoint old = currOffs.copy(); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
154 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
155 setTextPaint(Color.white); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
156 drawString(g, |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
157 "Arrow keys / mouse wheel\n"+ |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
158 "Enter / mouse click\n"+ |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
159 "Space bar\n"); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
160 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
161 currPos.x += 330; |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
162 currOffs.y = old.y; |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
163 drawString(g, |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
164 "- Rotate piece\n" + |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
165 "- Place/lock piece\n" + |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
166 "- Swap piece\n"); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
167 |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
168 currPos.x -= 330; |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
169 setTextPaint(Color.green); |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
170 drawString(g, |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
171 "\nObjective: Create a path long as possible by rotating\n"+ |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
172 "and placing pieces. More points will be awarded for\n"+ |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
173 "advancing the path by several segments per turn." |
8932b1300ba1
Adjust about box size, add instructions.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
174 ); |
8 | 175 } |
176 } | |
75 | 177 |
178 public boolean keyPressed(KeyEvent e) | |
179 { | |
180 if (e.getKeyCode() == KeyEvent.VK_L) | |
181 { | |
182 aboutSecret = true; | |
183 } | |
184 else | |
185 { | |
186 clicked(); | |
187 aboutSecret = false; | |
188 } | |
189 return true; | |
190 } | |
8 | 191 |
75 | 192 public void clicked() |
8 | 193 { |
75 | 194 parent.remove(this); |
8 | 195 } |
196 } | |
75 | 197 |
0 | 198 |
48 | 199 class GameBoard extends IDMWidget |
0 | 200 { |
75 | 201 static final int boardSize = 9; |
202 static final int boardMiddle = 4; | |
2 | 203 Piece[][] board; |
117
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
204 float pscale, ptime; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
205 |
8 | 206 public boolean flagGameOver; |
119
52d79502447c
Make few variables un-public.
Matti Hamalainen <ccr@tnsp.org>
parents:
117
diff
changeset
|
207 int gameScore; |
54 | 208 |
119
52d79502447c
Make few variables un-public.
Matti Hamalainen <ccr@tnsp.org>
parents:
117
diff
changeset
|
209 Piece currPiece, nextPiece; |
41 | 210 int currX, currY, currPoint; |
9 | 211 |
47 | 212 Sound sndPlaced; |
213 | |
75 | 214 public GameBoard(IDMPoint pos, float ps) |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
215 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
216 super(pos); |
75 | 217 pscale = ps; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
218 |
108
ae66d21d4747
Disable piece placement sound.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
219 // sndPlaced = G.smgr.getSound("sounds/placed.wav"); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
220 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
221 startNewGame(); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
222 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
223 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
224 public void startNewGame() |
2 | 225 { |
226 board = new Piece[boardSize][boardSize]; | |
227 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); | |
228 | |
41 | 229 currX = boardMiddle; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
230 currY = boardMiddle; |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
231 currPoint = 0; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
232 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
233 currPiece = null; |
54 | 234 nextPiece = new Piece(PieceType.ACTIVE); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
235 |
65
dc936dba124e
Set game over-flag correctly, so that we reset everything after a game over
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
236 flagGameOver = false; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
237 pieceFinishTurn(); |
55 | 238 gameScore = 0; |
2 | 239 } |
0 | 240 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
241 public void paintBackPlate(Graphics2D g) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
242 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
243 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.2f)); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
244 g.setStroke(new BasicStroke(5.0f)); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
245 g.draw(new RoundRectangle2D.Float(getScaledX(), getScaledY(), |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
246 boardSize * pscale, boardSize * pscale, |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
247 pscale / 5, pscale / 5)); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
248 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
249 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
250 public void paint(Graphics2D g) |
2 | 251 { |
252 for (int y = 0; y < boardSize; y++) | |
253 for (int x = 0; x < boardSize; x++) | |
254 if (board[x][y] != null) | |
255 { | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
256 board[x][y].paint(g, |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
257 getScaledX() + (x * pscale), |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
258 getScaledY() + (y * pscale), |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
259 pscale - pscale / 10); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
260 } |
117
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
261 |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
262 if (!flagGameOver) |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
263 { |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
264 if (nextPiece != null) |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
265 { |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
266 // Draw next piece |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
267 AffineTransform save = g.getTransform(); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
268 nextPiece.paint(g, 830, 325, 90); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
269 g.setTransform(save); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
270 } |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
271 } |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
272 else |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
273 { |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
274 // Game over text |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
275 String text = "Game Over!"; |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
276 int textWidth = G.metrics[2].stringWidth(text); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
277 g.setFont(G.fonts[2]); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
278 |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
279 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
280 g.drawString(text, (G.screenDim.width - textWidth) / 2 + 5, G.screenDim.height / 2 + 5); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
281 |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
282 double f = Math.sin(ptime * 0.1) * 4.0; |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
283 g.setPaint(Color.white); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
284 g.drawString(text, (G.screenDim.width - textWidth) / 2 + (float) f, G.screenDim.height / 2 + (float) f); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
285 } |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
286 |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
287 // Score |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
288 g.setFont(G.fonts[2]); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
289 g.setPaint(Color.white); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
290 g.drawString(""+ String.format("%05d", gameScore), G.screenDim.width - 230, 220); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
291 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
292 } |
75 | 293 |
294 public boolean contains(float x, float y) | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
295 { |
76 | 296 return (x >= getScaledX() && |
75 | 297 y >= getScaledY() && |
298 x < getScaledX() + boardSize * pscale && | |
299 y < getScaledY() + boardSize * pscale); | |
2 | 300 } |
9 | 301 |
302 public void animate(float time) | |
303 { | |
117
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
304 ptime = time; |
9 | 305 for (int y = 0; y < boardSize; y++) |
306 for (int x = 0; x < boardSize; x++) | |
307 if (board[x][y] != null) | |
308 { | |
309 board[x][y].animate(time); | |
310 } | |
311 } | |
1 | 312 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
313 public void pieceRotate(Piece.RotateDir dir) |
2 | 314 { |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
315 if (currPiece != null && !flagGameOver) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
316 { |
41 | 317 currPiece.rotate(dir); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
318 } |
2 | 319 } |
1 | 320 |
75 | 321 // Change coordinates based on the "outgoing" |
322 // piece connection point. | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
323 private void pieceMoveTo(int point) |
7 | 324 { |
46 | 325 switch (point) |
326 { | |
327 case 0: currY--; break; | |
328 case 1: currY--; break; | |
329 | |
330 case 2: currX++; break; | |
331 case 3: currX++; break; | |
332 | |
333 case 4: currY++; break; | |
334 case 5: currY++; break; | |
335 | |
336 case 6: currX--; break; | |
337 case 7: currX--; break; | |
338 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
339 } |
9 | 340 |
54 | 341 public void pieceCreateNew() |
342 { | |
343 currPiece = nextPiece; | |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
344 currPiece.changed(); |
54 | 345 nextPiece = new Piece(PieceType.ACTIVE); |
346 } | |
347 | |
348 public void pieceSwapCurrent() | |
349 { | |
56 | 350 if (!flagGameOver) |
351 { | |
352 Piece tmp = currPiece; | |
353 currPiece = nextPiece; | |
354 nextPiece = tmp; | |
355 board[currX][currY] = currPiece; | |
106
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
356 currPiece.changed(); |
41c6cca69d60
Make new pieces appear gradually, and same effect for swapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
357 nextPiece.changed(); |
56 | 358 } |
54 | 359 } |
360 | |
75 | 361 // Check one piece, set connections, find the new placement |
362 // based on piece rotations etc. | |
363 private boolean pieceCheck(Piece piece) | |
48 | 364 { |
53 | 365 if (piece == null) |
48 | 366 { |
367 // Create new piece | |
56 | 368 pieceCreateNew(); |
48 | 369 board[currX][currY] = currPiece; |
370 return true; | |
371 } | |
372 else | |
53 | 373 if (piece.getType() == PieceType.START) |
48 | 374 { |
375 if (currPiece != null) | |
376 { | |
377 // Hit center starting piece, game over | |
378 flagGameOver = true; | |
379 return true; | |
380 } | |
381 else | |
382 { | |
383 // Start piece as first piece means game is starting | |
384 pieceMoveTo(currPoint); | |
54 | 385 pieceCreateNew(); |
48 | 386 board[currX][currY] = currPiece; |
387 return true; | |
388 } | |
389 } | |
390 | |
391 // Mark the current piece as locked | |
53 | 392 piece.setType(PieceType.LOCKED); |
48 | 393 |
394 // Solve connection (with rotations) through the piece | |
53 | 395 currPoint = piece.getRotatedPoint(piece.getMatchingPoint(currPoint)); |
48 | 396 |
397 // Mark connection as active | |
53 | 398 piece.setConnectionState(currPoint, true); |
48 | 399 |
400 // Solve exit point (with rotations) | |
53 | 401 currPoint = piece.getAntiRotatedPoint(piece.getConnection(currPoint)); |
48 | 402 |
403 // Move to next position accordingly | |
404 pieceMoveTo(currPoint); | |
405 return false; | |
406 } | |
407 | |
75 | 408 // Finish one move/turn of the game, resolve path and find placement |
409 // of the next piece, or set "game over" state if required. | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
410 public void pieceFinishTurn() |
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
411 { |
48 | 412 boolean finished = false; |
55 | 413 int connections = 0; |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
414 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
415 if (currPiece != null) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
416 { |
75 | 417 G.smgr.play(sndPlaced); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
418 } |
55 | 419 |
48 | 420 while (!finished) |
7 | 421 { |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
422 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) |
27 | 423 { |
55 | 424 connections++; |
48 | 425 finished = pieceCheck(board[currX][currY]); |
27 | 426 } |
427 else | |
428 { | |
46 | 429 // Outside of the board, game over |
48 | 430 finished = true; |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
431 flagGameOver = true; |
7 | 432 } |
433 } | |
53 | 434 |
66 | 435 // Compute and add score |
55 | 436 gameScore += connections * connections; |
437 | |
66 | 438 // If game over, clear the game |
53 | 439 if (flagGameOver) |
440 { | |
441 currPiece = null; | |
442 } | |
42
951a4d669af0
Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents:
41
diff
changeset
|
443 } |
66 | 444 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
445 public boolean mouseWheelMoved(MouseWheelEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
446 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
447 int notches = e.getWheelRotation(); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
448 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
449 if (notches < 0) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
450 pieceRotate(Piece.RotateDir.LEFT); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
451 else |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
452 pieceRotate(Piece.RotateDir.RIGHT); |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
453 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
454 return true; |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
455 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
456 |
75 | 457 public void clicked() |
458 { | |
77
d17db72ac770
Fix the game continuing after gameover bug again due to refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
459 if (!flagGameOver) |
d17db72ac770
Fix the game continuing after gameover bug again due to refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents:
76
diff
changeset
|
460 pieceFinishTurn(); |
75 | 461 } |
47 | 462 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
463 public boolean keyPressed(KeyEvent e) |
47 | 464 { |
54 | 465 if (flagGameOver) |
466 return false; | |
467 | |
47 | 468 switch (e.getKeyCode()) |
469 { | |
470 case KeyEvent.VK_LEFT: | |
471 case KeyEvent.VK_UP: | |
472 pieceRotate(Piece.RotateDir.LEFT); | |
473 return true; | |
474 | |
475 case KeyEvent.VK_RIGHT: | |
476 case KeyEvent.VK_DOWN: | |
477 pieceRotate(Piece.RotateDir.RIGHT); | |
478 return true; | |
479 | |
480 case KeyEvent.VK_ENTER: | |
481 pieceFinishTurn(); | |
482 return true; | |
483 } | |
484 return false; | |
485 } | |
1 | 486 } |
487 | |
488 | |
9 | 489 public class Engine extends JPanel |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
490 implements Runnable, KeyListener, |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
491 MouseListener, MouseWheelListener |
1 | 492 { |
25 | 493 long startTime; |
494 float gameClock, gameFrames; | |
75 | 495 |
8 | 496 Thread animThread; |
497 boolean animEnable = false; | |
25 | 498 |
4 | 499 GameBoard lauta = null; |
69
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
500 InputStream musa; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
501 IDMContainer widgets; |
75 | 502 AboutBox aboutBox; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
503 |
9 | 504 public Engine() |
2 | 505 { |
25 | 506 // Initialize globals |
507 System.out.print("Engine() constructor\n"); | |
508 | |
37 | 509 // Sound system |
116
858b9644a61e
Adjusts number of max simultaneous sounds.
Matti Hamalainen <ccr@tnsp.org>
parents:
115
diff
changeset
|
510 G.smgr = new SoundManager(new AudioFormat(22050, 16, 1, true, false), 16); |
9 | 511 |
25 | 512 // Load resources |
4 | 513 try |
514 { | |
24 | 515 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
75 | 516 G.lautaBG = ImageIO.read(res.getStream()); |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
517 |
25 | 518 try { |
519 res = new ResourceLoader("graphics/font.ttf"); | |
75 | 520 |
521 G.fonts = new Font[G.numFonts]; | |
522 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
523 G.fonts[1] = G.fonts[0].deriveFont(24f); | |
524 G.fonts[2] = G.fonts[0].deriveFont(64f); | |
76 | 525 G.fonts[3] = G.fonts[0].deriveFont(32f); |
75 | 526 |
25 | 527 } |
528 catch (FontFormatException e) | |
529 { | |
530 System.out.print("Could not initialize fonts.\n"); | |
531 } | |
37 | 532 |
69
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
533 res = new ResourceLoader("sounds/gamemusic.wav"); |
163232ec225b
Stream music data instead of loading to memory.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
534 musa = res.getStream(); |
4 | 535 } |
536 catch (IOException e) | |
537 { | |
538 JOptionPane.showMessageDialog(null, | |
21 | 539 e.getMessage(), |
4 | 540 "Initialization error", |
541 JOptionPane.ERROR_MESSAGE); | |
37 | 542 |
21 | 543 System.out.print(e.getMessage()); |
18 | 544 } |
545 | |
52 | 546 // Create IDM GUI widgets |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
547 widgets = new IDMContainer(); |
52 | 548 |
117
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
549 lauta = new GameBoard(new IDMPoint(95, 130), 63); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
550 widgets.add(lauta); |
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
551 |
75 | 552 widgets.add(new BtnSwapPiece(767f, 450f)); |
553 widgets.add(new BtnAbout (767f, 570f)); | |
554 widgets.add(new BtnNewGame (767f, 650f)); | |
50 | 555 |
75 | 556 aboutBox = new AboutBox(); |
557 | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
558 // Game |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
559 startNewGame(); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
560 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
561 // Initialize event listeners |
18 | 562 addKeyListener(this); |
563 addMouseListener(this); | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
564 addMouseWheelListener(this); |
18 | 565 |
93 | 566 // Start playing background music |
567 G.smgr.play(musa); | |
568 | |
18 | 569 // Get initial focus |
570 if (!hasFocus()) | |
571 { | |
572 System.out.print("Engine(): requesting focus\n"); | |
573 requestFocus(); | |
4 | 574 } |
93 | 575 |
8 | 576 } |
577 | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
578 public void startNewGame() |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
579 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
580 gameClock = 0; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
581 gameFrames = 0; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
582 startTime = new Date().getTime(); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
583 lauta.startNewGame(); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
584 } |
25 | 585 |
586 public void paintComponent(Graphics g) | |
587 { | |
588 Graphics2D g2 = (Graphics2D) g; | |
589 | |
52 | 590 // Rescale if parent component size has changed |
25 | 591 Dimension dim = getSize(); |
75 | 592 if (G.screenDim == null || !dim.equals(G.screenDim)) |
25 | 593 { |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
594 // Rescale IDM GUI widgets |
75 | 595 widgets.setScale(dim.width / 1024.0f, dim.height / 768.0f); |
596 G.screenDim = dim; | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
597 |
52 | 598 // Rescale background image |
87
6afbf88c8bda
Change background image type from RGBA to RGB only, improves rendering speed.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
599 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); |
75 | 600 Graphics2D gimg = G.lautaBGScaled.createGraphics(); |
25 | 601 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
602 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | |
603 | |
75 | 604 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
605 lauta.paintBackPlate(gimg); |
25 | 606 |
607 System.out.print("scale changed\n"); | |
608 } | |
609 | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
610 // Get font metrics against current Graphics2D context |
75 | 611 if (G.metrics == null) |
612 { | |
613 G.metrics = new FontMetrics[G.numFonts]; | |
614 for (int i = 0; i < G.numFonts; i++) | |
615 G.metrics[i] = g2.getFontMetrics(G.fonts[i]); | |
616 } | |
55 | 617 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
618 // Draw background image, pieces, widgets |
75 | 619 g2.drawImage(G.lautaBGScaled, 0, 0, null); |
86
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
620 |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
621 // Use antialiasing when rendering the game elements |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
622 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
623 RenderingHints.VALUE_ANTIALIAS_ON); |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
624 |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
625 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
626 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
454a9fcd13ca
Turn antialiasing rendering hints on only for the needed elements to improve rendering speed and CPU usage.
Matti Hamalainen <ccr@tnsp.org>
parents:
82
diff
changeset
|
627 |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
628 widgets.paint(g2); |
55 | 629 |
75 | 630 |
631 // Frames per second counter | |
632 g2.setFont(G.fonts[1]); | |
25 | 633 long currTime = new Date().getTime(); |
117
973f567c7b9d
Change how gameboard is rendered, make it more OO-sensible.
Matti Hamalainen <ccr@tnsp.org>
parents:
116
diff
changeset
|
634 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), G.screenDim.width - 120, 20); |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
635 |
75 | 636 gameFrames++; |
25 | 637 } |
638 | |
8 | 639 public void startThreads() |
640 { | |
9 | 641 System.out.print("startThreads()\n"); |
8 | 642 if (animThread == null) |
643 { | |
644 animThread = new Thread(this); | |
645 animEnable = true; | |
646 animThread.start(); | |
647 } | |
648 } | |
649 | |
650 public void stopThreads() | |
651 { | |
9 | 652 System.out.print("stopThreads()\n"); |
37 | 653 |
654 // Stop animations | |
8 | 655 if (animThread != null) |
656 { | |
657 animThread.interrupt(); | |
658 animEnable = false; | |
659 animThread = null; | |
660 } | |
22 | 661 |
37 | 662 // Shut down sound manager |
75 | 663 G.smgr.close(); |
2 | 664 } |
0 | 665 |
75 | 666 public void mouseEntered(MouseEvent e) |
667 { | |
668 widgets.mouseEntered(e); | |
669 } | |
670 public void mouseExited(MouseEvent e) | |
671 { | |
672 widgets.mouseExited(e); | |
673 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
674 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
675 public void mousePressed(MouseEvent e) |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
676 { |
75 | 677 if (widgets.containsObject(aboutBox)) |
678 aboutBox.mousePressed(e); | |
679 else | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
680 widgets.mousePressed(e); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
681 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
682 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
683 public void mouseReleased(MouseEvent e) |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
684 { |
75 | 685 if (widgets.containsObject(aboutBox)) |
686 aboutBox.mouseReleased(e); | |
687 else | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
688 widgets.mouseReleased(e); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
689 } |
18 | 690 |
691 public void mouseClicked(MouseEvent e) | |
692 { | |
693 if (!hasFocus()) | |
694 { | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
695 System.out.print("Requesting focus\n"); |
18 | 696 requestFocus(); |
697 } | |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
698 } |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
699 |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
700 public void mouseWheelMoved(MouseWheelEvent e) |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
701 { |
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
702 lauta.mouseWheelMoved(e); |
18 | 703 } |
704 | |
75 | 705 public void keyTyped(KeyEvent e) { } |
706 public void keyReleased(KeyEvent e) { } | |
1 | 707 |
9 | 708 public void keyPressed(KeyEvent e) |
2 | 709 { |
75 | 710 // Handle keyboard input |
711 if (widgets.containsObject(aboutBox)) | |
712 aboutBox.keyPressed(e); | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
713 else |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
714 widgets.keyPressed(e); |
2 | 715 } |
8 | 716 |
717 public void run() | |
718 { | |
719 while (animEnable) | |
720 { | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
721 // Progress game animation clock |
25 | 722 gameClock++; |
9 | 723 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
724 // Animate components |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
725 lauta.animate(gameClock); |
62
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
726 if (lauta.nextPiece != null) |
caf67c7e0814
Fix a possible null pointer exception under certain circumstances.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
727 lauta.nextPiece.animate(gameClock); |
9 | 728 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
729 // Repaint with a frame limiter |
59
fd10a9422b60
Cleanups, bugfixes, etc etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
730 if (gameClock % 4 == 1) |
10 | 731 repaint(); |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
732 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
733 // Sleep for a moment |
9 | 734 try { |
735 Thread.sleep(10); | |
736 } | |
737 catch (InterruptedException x) { | |
738 } | |
8 | 739 } |
740 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
741 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
742 class BtnNewGame extends IDMButton |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
743 { |
51 | 744 public BtnNewGame(float x, float y) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
745 { |
75 | 746 super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "New Game"); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
747 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
748 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
749 public void clicked() |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
750 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
751 startNewGame(); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
752 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
48
diff
changeset
|
753 } |
50 | 754 |
755 class BtnSwapPiece extends IDMButton | |
756 { | |
51 | 757 public BtnSwapPiece(float x, float y) |
50 | 758 { |
75 | 759 super(x, y, KeyEvent.VK_SPACE, G.fonts[1], "Swap"); |
50 | 760 } |
761 | |
762 public void clicked() | |
763 { | |
54 | 764 lauta.pieceSwapCurrent(); |
50 | 765 } |
766 } | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
767 |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
768 class BtnAbout extends IDMButton |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
769 { |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
770 public BtnAbout(float x, float y) |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
771 { |
75 | 772 super(x, y, KeyEvent.VK_A, G.fonts[1], "About"); |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
773 } |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
774 |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
775 public void clicked() |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
776 { |
75 | 777 if (!widgets.containsObject(aboutBox)) |
778 widgets.add(aboutBox); | |
60
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
779 } |
4a984e3b27d2
Cleanups. Add a ad-hoc about box, it's ugly.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
780 } |
0 | 781 } |