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