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