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