comparison game/Piece.java @ 32:e480579cc460

More work on debugging the path resolving.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 03 Feb 2011 18:22:16 +0200
parents 60a4579a79df
children 6f6c551cc14c
comparison
equal deleted inserted replaced
31:8dbc79294106 32:e480579cc460
10 import java.math.*; 10 import java.math.*;
11 11
12 12
13 public class Piece 13 public class Piece
14 { 14 {
15 public enum RotateDir { LEFT, RIGHT }
16
15 static final int numConnections = 8; 17 static final int numConnections = 8;
16 static final float maxTime = 50.0f; 18 static final float maxTime = 50.0f;
17 19
18 int currRotation; 20 int currRotation;
19 int[] connections; 21 int[] connections;
20 boolean[] active; 22 boolean[] active;
21 PieceType type, oldType; 23 PieceType type, prevType;
22 24
23 boolean rotationChanged, rotationActive, 25 boolean rotationChanged, rotationActive,
24 typeChanged, typeActive, 26 typeChanged, typeActive,
25 activeChanged, activeActive; 27 activeChanged, activeActive;
26 float currAngle, newAngle, rotationTime, typeTime; 28 float currAngle, newAngle, rotationTime, typeTime;
27 float throb; 29
28 30 float throbTime;
29 Interpolate lerpRotation; 31 Interpolate lerpRotation;
32
33 int point;
34
30 35
31 public Piece(PieceType ptype) 36 public Piece(PieceType ptype)
32 { 37 {
33 // Initialize 38 // Initialize
34 connections = new int[numConnections]; 39 connections = new int[numConnections];
41 currAngle = 0; 46 currAngle = 0;
42 47
43 typeChanged = false; 48 typeChanged = false;
44 typeActive = false; 49 typeActive = false;
45 50
46 throb = 0; 51 throbTime = 0;
47 52
48 53
49 // Initialize connections between endpoints of the paths inside the piece 54 // Initialize connections between endpoints of the paths inside the piece
50 for (int i = 0; i < numConnections; i++) 55 for (int i = 0; i < numConnections; i++)
51 connections[i] = -1; 56 connections[i] = -1;
72 this(PieceType.NONE); 77 this(PieceType.NONE);
73 } 78 }
74 79
75 public void setType(PieceType ptype) 80 public void setType(PieceType ptype)
76 { 81 {
77 typeChanged = (oldType != ptype); 82 typeChanged = (prevType != ptype);
78 oldType = type; 83 prevType = type;
79 type = ptype; 84 type = ptype;
80 } 85 }
81 86
82 public int getConnection(int in) 87 public int getConnection(int in)
83 { 88 {
84 return connections[(in + (currRotation * 2)) % 8]; 89 return connections[(in + (currRotation * 2)) % 8];
85 } 90 }
86 91
87 public void rotate(boolean dir) 92 public void rotate(RotateDir dir)
88 { 93 {
89 // Only normal 94 // Only normal
90 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) 95 if (type != PieceType.LOCKED && type != PieceType.ACTIVE)
91 return; 96 return;
92 97
93 currRotation = (currRotation + (dir ? 1 : -1)) % 4; 98 currRotation = (currRotation + (dir == RotateDir.RIGHT ? 1 : -1)) % 4;
94 newAngle = (float) ((currRotation * Math.PI) / 2.0f); 99 newAngle = (float) ((currRotation * Math.PI) / 2.0f);
95 lerpRotation = new Interpolate(newAngle, currAngle, maxTime); 100 lerpRotation = new Interpolate(newAngle, currAngle, maxTime);
96 rotationChanged = true; 101 rotationChanged = true;
97 } 102 }
98 103
100 { 105 {
101 float ox = 0, oy = 0; 106 float ox = 0, oy = 0;
102 float step = dim / 10; 107 float step = dim / 10;
103 108
104 switch (index) { 109 switch (index) {
105 case 0: ox = 3.0f; oy = 0.5f; break; 110 case 0: ox = 3.0f; oy = 0.4f; break;
106 case 1: ox = 7.0f; oy = 0.5f; break; 111 case 1: ox = 7.0f; oy = 0.4f; break;
107 case 2: ox = 9.5f; oy = 3.0f; break; 112 case 2: ox = 9.6f; oy = 3.0f; break;
108 case 3: ox = 9.5f; oy = 7.0f; break; 113 case 3: ox = 9.6f; oy = 7.0f; break;
109 case 4: ox = 7.0f; oy = 9.5f; break; 114 case 4: ox = 7.0f; oy = 9.6f; break;
110 case 5: ox = 3.0f; oy = 9.5f; break; 115 case 5: ox = 3.0f; oy = 9.6f; break;
111 case 6: ox = 0.5f; oy = 7.0f; break; 116 case 6: ox = 0.4f; oy = 7.0f; break;
112 case 7: ox = 0.5f; oy = 3.0f; break; 117 case 7: ox = 0.4f; oy = 3.0f; break;
113 118
114 case -1: ox = 5.0f; oy = 5.0f; break; 119 case 8: ox = 3.0f; oy = 3.0f; break;
120 case 9: ox = 7.0f; oy = 3.0f; break;
121 case 10: ox = 7.0f; oy = 3.0f; break;
122 case 11: ox = 7.0f; oy = 7.0f; break;
123 case 12: ox = 7.0f; oy = 7.0f; break;
124 case 13: ox = 7.0f; oy = 7.0f; break;
125 case 14: ox = 3.0f; oy = 7.0f; break;
126 case 15: ox = 3.0f; oy = 3.0f; break;
115 } 127 }
116 128
117 return new Point2D.Float(x + ox * step, y + oy * step); 129 return new Point2D.Float(x + ox * step, y + oy * step);
130 }
131
132 public void clearActiveConnections()
133 {
134 for (int i = 0; i < numConnections; i++)
135 active[i] = false;
136
137 activeChanged = true;
118 } 138 }
119 139
120 public void setActiveConnection(int index) 140 public void setActiveConnection(int index)
121 { 141 {
122 active[index] = true; 142 active[index] = true;
123 active[connections[index]] = true; 143 active[connections[index]] = true;
144
124 activeChanged = true; 145 activeChanged = true;
125 } 146 }
126 147
127 public PieceType getType() 148 public PieceType getType()
128 { 149 {
164 185
165 if (activeChanged) 186 if (activeChanged)
166 { 187 {
167 } 188 }
168 189
169 throb = (time % 100) / 100.0f; 190 throbTime = (time % 100) / 100.0f;
170 } 191 }
171 192
172 public void paint(Graphics2D g, float x, float y, float dim) 193 public void paint(Graphics2D g, float x, float y, float dim)
173 { 194 {
174 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); 195 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f);
180 } 201 }
181 202
182 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); 203 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10));
183 204
184 g.setPaint(Color.black); 205 g.setPaint(Color.black);
185 g.setStroke(new BasicStroke(4.0f)); 206 g.setStroke(new BasicStroke(5.0f));
186 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); 207 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10));
187 208
188 if (type == PieceType.START) 209 if (type == PieceType.START)
189 return; 210 return;
190 211
191 if (type == PieceType.ACTIVE) 212 if (type == PieceType.ACTIVE)
192 { 213 {
193 float offs1 = throb * 10.0f, 214 float offs1 = throbTime * 10.0f,
194 offs2 = throb * 20.0f; 215 offs2 = throbTime * 20.0f;
195 216
196 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throb) )); 217 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throbTime) ));
197 g.setStroke(new BasicStroke(2.0f + throb * 2.0f)); 218 g.setStroke(new BasicStroke(2.0f + throbTime * 2.0f));
198 g.draw(new RoundRectangle2D.Float(x - offs1, y - offs1, dim + offs2, dim + offs2, dim / 10, dim / 10)); 219 g.draw(new RoundRectangle2D.Float(x - offs1, y - offs1, dim + offs2, dim + offs2, dim / 10, dim / 10));
199 } 220 }
200 221
201 g.setStroke(new BasicStroke(6.0f)); 222 g.setStroke(new BasicStroke(5.0f));
202 // CubicCurve2D c = new CubicCurve2D.Float(); 223 CubicCurve2D c = new CubicCurve2D.Float();
203 QuadCurve2D c = new QuadCurve2D.Float(); 224 // QuadCurve2D c = new QuadCurve2D.Float();
204 boolean[] drawn = new boolean[numConnections]; 225 boolean[] drawn = new boolean[numConnections];
205 for (int i = 0; i < numConnections; i++) 226 for (int i = 0; i < numConnections; i++)
206 if (!drawn[i]) 227 if (!drawn[i])
207 { 228 {
208 Point2D start, cp1, cp2, end; 229 Point2D start, cp1, cp2, end;
209 230
210 boolean act = active[i] || active[connections[i]]; 231 boolean isActive = active[i] || active[connections[i]];
211 g.setPaint(act ? Color.white : Color.black); 232 g.setPaint(isActive ? Color.white : Color.black);
212 233
213 start = getPointCoords(x, y, dim, i); 234 start = getPointCoords(x, y, dim, i);
214 end = getPointCoords(x, y, dim, connections[i]); 235 end = getPointCoords(x, y, dim, connections[i]);
215 cp1 = getPointCoords(x, y, dim, -1); 236
216 237 cp1 = getPointCoords(x, y, dim, i + 8);
217 c.setCurve(start, cp1, end); 238 cp2 = getPointCoords(x, y, dim, connections[i] + 8);
239
240 c.setCurve(start, cp1, cp2, end);
218 g.draw(c); 241 g.draw(c);
219 242
220 drawn[i] = true; 243 drawn[i] = true;
221 drawn[connections[i]] = true; 244 drawn[connections[i]] = true;
222 } 245 }