comparison game/Piece.java @ 9:a7751971c2a3

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 29 Jan 2011 00:30:00 +0200
parents 70714c229e23
children 4bacc98973f5
comparison
equal deleted inserted replaced
8:d8e7fd8f3ccf 9:a7751971c2a3
11 11
12 12
13 public class Piece 13 public class Piece
14 { 14 {
15 static final int numConnections = 8; 15 static final int numConnections = 8;
16 static final int minRotation = 0; 16 static final float maxTime = 50.0f;
17 static final int maxRotation = 3; 17
18 int currRotation; 18 int currRotation;
19 int[] connections; 19 int[] connections;
20 boolean[] active;
20 PieceType type, oldType; 21 PieceType type, oldType;
21 22
22 boolean rotationChanged, rotationActive, 23 boolean rotationChanged, rotationActive,
23 typeChanged, typeActive; 24 typeChanged, typeActive,
25 activeChanged, activeActive;
24 float currAngle, newAngle, rotationTime, typeTime; 26 float currAngle, newAngle, rotationTime, typeTime;
25 float throb; 27 float throb;
26 28
27 Interpolate lerpRotation; 29 Interpolate lerpRotation;
28 30
29 public Piece(PieceType ptype) 31 public Piece(PieceType ptype)
30 { 32 {
31 // Initialize 33 // Initialize
32 connections = new int[numConnections]; 34 connections = new int[numConnections];
35 active = new boolean[numConnections];
33 type = ptype; 36 type = ptype;
34 37
35 rotationChanged = false; 38 rotationChanged = false;
36 rotationActive = false; 39 rotationActive = false;
37 currRotation = 0; 40 currRotation = 0;
84 { 87 {
85 // Only normal 88 // Only normal
86 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) 89 if (type != PieceType.LOCKED && type != PieceType.ACTIVE)
87 return; 90 return;
88 91
89 currRotation = currRotation + (dir ? 1 : -1); 92 currRotation = currRotation + (dir ? -1 : 1);
90
91 if (currRotation < minRotation)
92 currRotation = maxRotation;
93 else if (currRotation > maxRotation)
94 currRotation = minRotation;
95
96 newAngle = (float) (currRotation * Math.PI) / 2.0f; 93 newAngle = (float) (currRotation * Math.PI) / 2.0f;
94
97 rotationChanged = true; 95 rotationChanged = true;
98 lerpRotation = new Interpolate(currAngle, newAngle, 100); 96 lerpRotation = new Interpolate(currAngle, newAngle, maxTime);
99 } 97 }
100 98
101 public Point2D getPointCoords(float x, float y, float dim, int index) 99 public Point2D getPointCoords(float x, float y, float dim, int index)
102 { 100 {
103 float ox = 0, oy = 0; 101 float ox = 0, oy = 0;
117 } 115 }
118 116
119 return new Point2D.Float(x + ox * step, y + oy * step); 117 return new Point2D.Float(x + ox * step, y + oy * step);
120 } 118 }
121 119
120 public void setActiveConnection(int index)
121 {
122 active[index] = true;
123 activeChanged = true;
124 }
125
122 public void animate(float time) 126 public void animate(float time)
123 { 127 {
124 if (rotationChanged) 128 if (rotationChanged)
125 { 129 {
126 rotationTime = time; 130 rotationTime = time;
128 rotationChanged = false; 132 rotationChanged = false;
129 } 133 }
130 134
131 if (rotationActive) 135 if (rotationActive)
132 { 136 {
133 float t = (time - rotationTime) / 10.0f; 137 float t = (time - rotationTime);
134 138
135 if (t < 100) 139 if (t < maxTime)
136 currAngle = lerpRotation.getValue(t); 140 currAngle = lerpRotation.getValue(t);
137 else 141 else
138 { 142 {
139 currAngle = newAngle; 143 currAngle = newAngle;
140 rotationActive = false; 144 rotationActive = false;
150 154
151 if (typeActive) 155 if (typeActive)
152 { 156 {
153 } 157 }
154 158
155 throb = ((time / 10.0f) % 100) / 100.0f; 159 if (activeChanged)
160 {
161
162 }
163
164 throb = (time % 100) / 100.0f;
156 } 165 }
157 166
158 public void paint(Graphics2D g, float x, float y, float dim) 167 public void paint(Graphics2D g, float x, float y, float dim)
159 { 168 {
160 AffineTransform tf = new AffineTransform(); 169 AffineTransform tf = new AffineTransform();
161 tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); 170 tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f);
162 g.transform(tf); 171 g.transform(tf);
172
173 if (type == PieceType.ACTIVE)
174 System.out.print("angle = " + currAngle + "\n");
163 175
164 switch (type) { 176 switch (type) {
165 case LOCKED: g.setPaint(Color.green); break; 177 case LOCKED: g.setPaint(Color.green); break;
166 case ACTIVE: g.setPaint(Color.red); break; 178 case ACTIVE: g.setPaint(Color.red); break;
167 case START: g.setPaint(Color.orange); break; 179 case START: g.setPaint(Color.orange); break;
181 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throb) )); 193 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throb) ));
182 g.setStroke(new BasicStroke(2.0f + throb * 2.0f)); 194 g.setStroke(new BasicStroke(2.0f + throb * 2.0f));
183 g.draw(new RoundRectangle2D.Float(x - throb * 10.0f, y - throb * 10.0f, dim + throb * 20.0f, dim + throb * 20.0f, dim / 10, dim / 10)); 195 g.draw(new RoundRectangle2D.Float(x - throb * 10.0f, y - throb * 10.0f, dim + throb * 20.0f, dim + throb * 20.0f, dim / 10, dim / 10));
184 } 196 }
185 197
198 g.setPaint(Color.black);
186 g.setStroke(new BasicStroke(6.0f)); 199 g.setStroke(new BasicStroke(6.0f));
187 // CubicCurve2D c = new CubicCurve2D.Float(); 200 // CubicCurve2D c = new CubicCurve2D.Float();
188 QuadCurve2D c = new QuadCurve2D.Float(); 201 QuadCurve2D c = new QuadCurve2D.Float();
189 202 boolean[] drawn = new boolean[numConnections];
190 for (int i = 0; i < numConnections / 2; i++) 203 for (int i = 0; i < numConnections; i++)
204 if (!drawn[i])
191 { 205 {
192 Point2D start, cp1, cp2, end; 206 Point2D start, cp1, cp2, end;
193 207
194 start = getPointCoords(x, y, dim, i); 208 start = getPointCoords(x, y, dim, i);
195 end = getPointCoords(x, y, dim, connections[i]); 209 end = getPointCoords(x, y, dim, connections[i]);
196 cp1 = getPointCoords(x, y, dim, -1); 210 cp1 = getPointCoords(x, y, dim, -1);
197 211
198 c.setCurve(start, cp1, end); 212 c.setCurve(start, cp1, end);
199 g.draw(c); 213 g.draw(c);
214
215 drawn[i] = true;
216 drawn[connections[i]] = true;
200 } 217 }
201 } 218 }
202 } 219 }