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