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