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 {
|
3
|
15 static final int numConnections = 8;
|
|
16 static final int minRotation = 0;
|
|
17 static final int maxRotation = 3;
|
|
18 int currRotation;
|
|
19 int[] connections;
|
|
20 PieceType type, oldType;
|
1
|
21
|
3
|
22 boolean rotationChanged, rotationActive,
|
|
23 typeChanged, typeActive;
|
7
|
24 float currAngle, newAngle, rotationTime, typeTime;
|
|
25 float throb;
|
6
|
26
|
7
|
27 Interpolate lerpRotation;
|
1
|
28
|
3
|
29 public Piece(PieceType ptype)
|
|
30 {
|
|
31 // Initialize
|
|
32 connections = new int[numConnections];
|
|
33 type = ptype;
|
1
|
34
|
3
|
35 rotationChanged = false;
|
6
|
36 rotationActive = false;
|
|
37 currRotation = 0;
|
|
38 currAngle = 0;
|
|
39
|
3
|
40 typeChanged = false;
|
|
41 typeActive = false;
|
1
|
42
|
6
|
43 throb = 0;
|
1
|
44
|
|
45
|
3
|
46 // Initialize connections between endpoints of the paths inside the piece
|
|
47 for (int i = 0; i < numConnections; i++)
|
|
48 connections[i] = -1;
|
1
|
49
|
6
|
50 // Randomize connections in the piece
|
3
|
51 Random rnd = new Random();
|
|
52 for (int i = 0; i < numConnections; i++)
|
|
53 {
|
|
54 while (connections[i] < 0)
|
|
55 {
|
|
56 int tmp = rnd.nextInt(numConnections);
|
|
57 if (connections[tmp] < 0)
|
|
58 {
|
|
59 connections[i] = tmp;
|
|
60 connections[tmp] = i;
|
|
61 }
|
|
62 }
|
|
63 }
|
|
64 }
|
|
65
|
|
66 public Piece()
|
|
67 {
|
|
68 this(PieceType.NONE);
|
|
69 }
|
1
|
70
|
3
|
71 public void setType(PieceType ptype)
|
|
72 {
|
|
73 typeChanged = (oldType != ptype);
|
|
74 oldType = type;
|
|
75 type = ptype;
|
|
76 }
|
1
|
77
|
3
|
78 public int getConnection(int in)
|
|
79 {
|
|
80 return connections[in];
|
|
81 }
|
5
|
82
|
3
|
83 public void rotate(boolean dir)
|
|
84 {
|
6
|
85 // Only normal
|
|
86 if (type != PieceType.LOCKED && type != PieceType.ACTIVE)
|
3
|
87 return;
|
1
|
88
|
7
|
89 currRotation = currRotation + (dir ? 1 : -1);
|
1
|
90
|
7
|
91 if (currRotation < minRotation)
|
|
92 currRotation = maxRotation;
|
3
|
93 else if (currRotation > maxRotation)
|
7
|
94 currRotation = minRotation;
|
1
|
95
|
7
|
96 newAngle = (float) (currRotation * Math.PI) / 2.0f;
|
3
|
97 rotationChanged = true;
|
7
|
98 lerpRotation = new Interpolate(currAngle, newAngle, 100);
|
5
|
99 }
|
1
|
100
|
7
|
101 public Point2D getPointCoords(float x, float y, float dim, int index)
|
5
|
102 {
|
7
|
103 float ox = 0, oy = 0;
|
|
104 float step = dim / 10;
|
1
|
105
|
5
|
106 switch (index) {
|
|
107 case 0: ox = 3.0f; oy = 0.5f; break;
|
|
108 case 1: ox = 7.0f; oy = 0.5f; break;
|
|
109 case 2: ox = 9.5f; oy = 3.0f; break;
|
|
110 case 3: ox = 9.5f; oy = 7.0f; break;
|
|
111 case 4: ox = 7.0f; oy = 9.5f; break;
|
|
112 case 5: ox = 3.0f; oy = 9.5f; break;
|
|
113 case 6: ox = 0.5f; oy = 7.0f; break;
|
|
114 case 7: ox = 0.5f; oy = 3.0f; break;
|
1
|
115
|
5
|
116 case -1: ox = 5.0f; oy = 5.0f; break;
|
|
117 }
|
1
|
118
|
7
|
119 return new Point2D.Float(x + ox * step, y + oy * step);
|
5
|
120 }
|
1
|
121
|
7
|
122 public void animate(float time)
|
5
|
123 {
|
|
124 if (rotationChanged)
|
|
125 {
|
|
126 rotationTime = time;
|
|
127 rotationActive = true;
|
7
|
128 rotationChanged = false;
|
5
|
129 }
|
1
|
130
|
5
|
131 if (rotationActive)
|
|
132 {
|
7
|
133 float t = (time - rotationTime) / 10.0f;
|
6
|
134
|
7
|
135 if (t < 100)
|
|
136 currAngle = lerpRotation.getValue(t);
|
|
137 else
|
|
138 {
|
|
139 currAngle = newAngle;
|
|
140 rotationActive = false;
|
|
141 }
|
5
|
142 }
|
6
|
143
|
|
144 if (typeChanged)
|
|
145 {
|
|
146 typeTime = time;
|
|
147 typeActive = true;
|
7
|
148 typeChanged = false;
|
6
|
149 }
|
|
150
|
|
151 if (typeActive)
|
|
152 {
|
|
153 }
|
|
154
|
|
155 throb = ((time / 10.0f) % 100) / 100.0f;
|
5
|
156 }
|
1
|
157
|
7
|
158 public void paint(Graphics2D g, float x, float y, float dim)
|
5
|
159 {
|
|
160 AffineTransform tf = new AffineTransform();
|
6
|
161 tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f);
|
5
|
162 g.transform(tf);
|
1
|
163
|
5
|
164 switch (type) {
|
6
|
165 case LOCKED: g.setPaint(Color.green); break;
|
5
|
166 case ACTIVE: g.setPaint(Color.red); break;
|
|
167 case START: g.setPaint(Color.orange); break;
|
|
168 }
|
6
|
169
|
7
|
170 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10));
|
1
|
171
|
5
|
172 g.setPaint(Color.black);
|
|
173 g.setStroke(new BasicStroke(4.0f));
|
7
|
174 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10));
|
5
|
175
|
|
176 if (type == PieceType.START)
|
|
177 return;
|
|
178
|
6
|
179 if (type == PieceType.ACTIVE)
|
|
180 {
|
7
|
181 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throb) ));
|
6
|
182 g.setStroke(new BasicStroke(2.0f + throb * 2.0f));
|
7
|
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));
|
6
|
184 }
|
|
185
|
5
|
186 g.setStroke(new BasicStroke(6.0f));
|
7
|
187 // CubicCurve2D c = new CubicCurve2D.Float();
|
|
188 QuadCurve2D c = new QuadCurve2D.Float();
|
1
|
189
|
5
|
190 for (int i = 0; i < numConnections / 2; i++)
|
|
191 {
|
|
192 Point2D start, cp1, cp2, end;
|
6
|
193
|
|
194 start = getPointCoords(x, y, dim, i);
|
|
195 end = getPointCoords(x, y, dim, connections[i]);
|
|
196 cp1 = getPointCoords(x, y, dim, -1);
|
1
|
197
|
5
|
198 c.setCurve(start, cp1, end);
|
|
199 g.draw(c);
|
|
200 }
|
|
201 }
|
1
|
202 }
|