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