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