Mercurial > hg > ristipolku
annotate game/Piece.java @ 34:6f6c551cc14c
Fix piece connections rendering.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Feb 2011 20:37:34 +0200 |
parents | e480579cc460 |
children | 3dc5ae9f1c80 |
rev | line source |
---|---|
1 | 1 /* |
2 * Ristipolku | |
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> | |
4 */ | |
5 package game; | |
6 | |
7 import java.awt.*; | |
7 | 8 import java.awt.geom.*; |
1 | 9 import java.util.*; |
6 | 10 import java.math.*; |
1 | 11 |
12 | |
13 public class Piece | |
14 { | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
15 public enum RotateDir { LEFT, RIGHT } |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
16 |
3 | 17 static final int numConnections = 8; |
9 | 18 static final float maxTime = 50.0f; |
19 | |
3 | 20 int currRotation; |
21 int[] connections; | |
9 | 22 boolean[] active; |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
23 PieceType type, prevType; |
1 | 24 |
3 | 25 boolean rotationChanged, rotationActive, |
9 | 26 typeChanged, typeActive, |
27 activeChanged, activeActive; | |
7 | 28 float currAngle, newAngle, rotationTime, typeTime; |
6 | 29 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
30 float throbTime; |
7 | 31 Interpolate lerpRotation; |
1 | 32 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
33 int point; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
34 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
35 |
3 | 36 public Piece(PieceType ptype) |
37 { | |
38 // Initialize | |
39 connections = new int[numConnections]; | |
9 | 40 active = new boolean[numConnections]; |
3 | 41 type = ptype; |
1 | 42 |
3 | 43 rotationChanged = false; |
6 | 44 rotationActive = false; |
45 currRotation = 0; | |
46 currAngle = 0; | |
47 | |
3 | 48 typeChanged = false; |
49 typeActive = false; | |
1 | 50 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
51 throbTime = 0; |
1 | 52 |
53 | |
3 | 54 // Initialize connections between endpoints of the paths inside the piece |
55 for (int i = 0; i < numConnections; i++) | |
56 connections[i] = -1; | |
1 | 57 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
58 |
6 | 59 // Randomize connections in the piece |
3 | 60 Random rnd = new Random(); |
61 for (int i = 0; i < numConnections; i++) | |
62 { | |
63 while (connections[i] < 0) | |
64 { | |
65 int tmp = rnd.nextInt(numConnections); | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
66 if (tmp != i && connections[tmp] < 0) |
3 | 67 { |
68 connections[i] = tmp; | |
69 connections[tmp] = i; | |
70 } | |
71 } | |
72 } | |
73 } | |
74 | |
75 public Piece() | |
76 { | |
77 this(PieceType.NONE); | |
78 } | |
1 | 79 |
3 | 80 public void setType(PieceType ptype) |
81 { | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
82 typeChanged = (prevType != ptype); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
83 prevType = type; |
3 | 84 type = ptype; |
85 } | |
1 | 86 |
3 | 87 public int getConnection(int in) |
88 { | |
30 | 89 return connections[(in + (currRotation * 2)) % 8]; |
3 | 90 } |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
91 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
92 public void rotate(RotateDir dir) |
3 | 93 { |
6 | 94 // Only normal |
95 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) | |
3 | 96 return; |
1 | 97 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
98 currRotation = (currRotation + (dir == RotateDir.RIGHT ? 1 : -1)) % 4; |
11 | 99 newAngle = (float) ((currRotation * Math.PI) / 2.0f); |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
100 lerpRotation = new Interpolate(newAngle, currAngle, maxTime); |
3 | 101 rotationChanged = true; |
5 | 102 } |
1 | 103 |
7 | 104 public Point2D getPointCoords(float x, float y, float dim, int index) |
5 | 105 { |
7 | 106 float ox = 0, oy = 0; |
107 float step = dim / 10; | |
1 | 108 |
5 | 109 switch (index) { |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
110 // Normal line starting and ending points |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
111 case 0: ox = 3.0f; oy = 0.4f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
112 case 1: ox = 7.0f; oy = 0.4f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
113 case 2: ox = 9.6f; oy = 3.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
114 case 3: ox = 9.6f; oy = 7.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
115 case 4: ox = 7.0f; oy = 9.6f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
116 case 5: ox = 3.0f; oy = 9.6f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
117 case 6: ox = 0.4f; oy = 7.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
118 case 7: ox = 0.4f; oy = 3.0f; break; |
1 | 119 |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
120 // Matching control points for each point above (+8) |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
121 case 8: ox = 3.0f; oy = 3.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
122 case 9: ox = 7.0f; oy = 3.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
123 case 10: ox = 7.0f; oy = 3.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
124 case 11: ox = 7.0f; oy = 7.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
125 case 12: ox = 7.0f; oy = 7.0f; break; |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
126 case 13: ox = 3.0f; oy = 7.0f; break; |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
127 case 14: ox = 3.0f; oy = 7.0f; break; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
128 case 15: ox = 3.0f; oy = 3.0f; break; |
5 | 129 } |
1 | 130 |
7 | 131 return new Point2D.Float(x + ox * step, y + oy * step); |
5 | 132 } |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
133 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
134 public void clearActiveConnections() |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
135 { |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
136 for (int i = 0; i < numConnections; i++) |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
137 active[i] = false; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
138 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
139 activeChanged = true; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
140 } |
1 | 141 |
9 | 142 public void setActiveConnection(int index) |
143 { | |
144 active[index] = true; | |
30 | 145 active[connections[index]] = true; |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
146 |
9 | 147 activeChanged = true; |
148 } | |
27 | 149 |
150 public PieceType getType() | |
151 { | |
152 return type; | |
153 } | |
9 | 154 |
7 | 155 public void animate(float time) |
5 | 156 { |
157 if (rotationChanged) | |
158 { | |
159 rotationTime = time; | |
160 rotationActive = true; | |
7 | 161 rotationChanged = false; |
5 | 162 } |
1 | 163 |
5 | 164 if (rotationActive) |
165 { | |
11 | 166 float t = (time - rotationTime) / 2; |
167 | |
9 | 168 if (t < maxTime) |
7 | 169 currAngle = lerpRotation.getValue(t); |
170 else | |
171 { | |
172 currAngle = newAngle; | |
173 rotationActive = false; | |
174 } | |
5 | 175 } |
6 | 176 |
177 if (typeChanged) | |
178 { | |
179 typeTime = time; | |
180 typeActive = true; | |
7 | 181 typeChanged = false; |
6 | 182 } |
183 | |
184 if (typeActive) | |
185 { | |
186 } | |
187 | |
9 | 188 if (activeChanged) |
189 { | |
190 } | |
191 | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
192 throbTime = (time % 100) / 100.0f; |
5 | 193 } |
1 | 194 |
7 | 195 public void paint(Graphics2D g, float x, float y, float dim) |
5 | 196 { |
11 | 197 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); |
1 | 198 |
5 | 199 switch (type) { |
6 | 200 case LOCKED: g.setPaint(Color.green); break; |
5 | 201 case ACTIVE: g.setPaint(Color.red); break; |
202 case START: g.setPaint(Color.orange); break; | |
203 } | |
6 | 204 |
7 | 205 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
1 | 206 |
5 | 207 |
208 if (type == PieceType.START) | |
209 return; | |
210 | |
6 | 211 if (type == PieceType.ACTIVE) |
212 { | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
213 float offs1 = throbTime * 10.0f, |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
214 offs2 = throbTime * 20.0f; |
11 | 215 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
216 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throbTime) )); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
217 g.setStroke(new BasicStroke(2.0f + throbTime * 2.0f)); |
11 | 218 g.draw(new RoundRectangle2D.Float(x - offs1, y - offs1, dim + offs2, dim + offs2, dim / 10, dim / 10)); |
6 | 219 } |
220 | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
221 g.setStroke(new BasicStroke(5.0f)); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
222 CubicCurve2D c = new CubicCurve2D.Float(); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
223 // QuadCurve2D c = new QuadCurve2D.Float(); |
9 | 224 boolean[] drawn = new boolean[numConnections]; |
225 for (int i = 0; i < numConnections; i++) | |
226 if (!drawn[i]) | |
5 | 227 { |
228 Point2D start, cp1, cp2, end; | |
27 | 229 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
230 boolean isActive = active[i] || active[connections[i]]; |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
231 g.setPaint(isActive ? Color.white : Color.black); |
9 | 232 |
6 | 233 start = getPointCoords(x, y, dim, i); |
234 end = getPointCoords(x, y, dim, connections[i]); | |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
235 |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
236 cp1 = getPointCoords(x, y, dim, i + 8); |
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
237 cp2 = getPointCoords(x, y, dim, connections[i] + 8); |
1 | 238 |
32
e480579cc460
More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
239 c.setCurve(start, cp1, cp2, end); |
9 | 240 g.draw(c); |
241 | |
242 drawn[i] = true; | |
243 drawn[connections[i]] = true; | |
5 | 244 } |
34
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
245 |
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
246 g.setPaint(Color.black); |
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
247 g.setStroke(new BasicStroke(5.0f)); |
6f6c551cc14c
Fix piece connections rendering.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
248 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
5 | 249 } |
1 | 250 } |