comparison game/Piece.java @ 6:be0bf7544069

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Jan 2011 18:42:16 +0200
parents 4890020bf856
children 70714c229e23
comparison
equal deleted inserted replaced
5:4890020bf856 6:be0bf7544069
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> 3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
4 */ 4 */
5 package game; 5 package game;
6 6
7 import java.awt.*; 7 import java.awt.*;
8 import java.awt.geom.*;
9 import java.awt.event.*;
10 import javax.swing.*;
11 import java.util.*; 8 import java.util.*;
9 import java.math.*;
12 10
13 11
14 public class Piece 12 public class Piece
15 { 13 {
16 static final int numConnections = 8; 14 static final int numConnections = 8;
20 int[] connections; 18 int[] connections;
21 PieceType type, oldType; 19 PieceType type, oldType;
22 20
23 boolean rotationChanged, rotationActive, 21 boolean rotationChanged, rotationActive,
24 typeChanged, typeActive; 22 typeChanged, typeActive;
25 double currAngle, newAngle; 23 double currAngle, newAngle, rotationTime, typeTime;
24 double throb;
25
26 26
27 public Piece(PieceType ptype) 27 public Piece(PieceType ptype)
28 { 28 {
29 // Initialize 29 // Initialize
30 connections = new int[numConnections]; 30 connections = new int[numConnections];
31 type = ptype; 31 type = ptype;
32 32
33 rotationChanged = false; 33 rotationChanged = false;
34 rotationActive = false;
35 currRotation = 0;
36 currAngle = 0;
37
34 typeChanged = false; 38 typeChanged = false;
35 rotationActive = false;
36 typeActive = false; 39 typeActive = false;
37 40
38 currRotation = 0; 41 throb = 0;
39 currAngle = 0;
40 42
41 43
42 // Initialize connections between endpoints of the paths inside the piece 44 // Initialize connections between endpoints of the paths inside the piece
43 for (int i = 0; i < numConnections; i++) 45 for (int i = 0; i < numConnections; i++)
44 connections[i] = -1; 46 connections[i] = -1;
45 47
48 // Randomize connections in the piece
46 Random rnd = new Random(); 49 Random rnd = new Random();
47 for (int i = 0; i < numConnections; i++) 50 for (int i = 0; i < numConnections; i++)
48 { 51 {
49 while (connections[i] < 0) 52 while (connections[i] < 0)
50 { 53 {
75 return connections[in]; 78 return connections[in];
76 } 79 }
77 80
78 public void rotate(boolean dir) 81 public void rotate(boolean dir)
79 { 82 {
80 if (type != PieceType.NORMAL) 83 // Only normal
84 if (type != PieceType.LOCKED && type != PieceType.ACTIVE)
81 return; 85 return;
82 86
83 newRotation = currRotation + (dir ? 1 : -1); 87 newRotation = currRotation + (dir ? 1 : -1);
84 88
85 if (newRotation < minRotation) 89 if (newRotation < minRotation)
86 newRotation = maxRotation; 90 newRotation = maxRotation;
87 else if (currRotation > maxRotation) 91 else if (currRotation > maxRotation)
88 newRotation = minRotation; 92 newRotation = minRotation;
89 93
90 rotationDir = dir; 94 newAngle =
91 rotationChanged = true; 95 rotationChanged = true;
92 } 96 }
93 97
94 public Point2D getPointCoords(double x, double y, double dim, int index) 98 public Point2D getPointCoords(double x, double y, double dim, int index)
95 { 99 {
120 rotationActive = true; 124 rotationActive = true;
121 } 125 }
122 126
123 if (rotationActive) 127 if (rotationActive)
124 { 128 {
125 double t = time - rotationTime; 129 double t = (time - rotationTime) / 10.0f;
126 rotationAngle = 130
131 if (t < Math.PI)
127 } 132 }
128 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;
129 } 146 }
130 147
131 public void paint(Graphics2D g, double x, double y, double dim) 148 public void paint(Graphics2D g, double x, double y, double dim)
132 { 149 {
133 AffineTransform save = g.getTransform();
134
135 AffineTransform tf = new AffineTransform(); 150 AffineTransform tf = new AffineTransform();
136 tf.rotate(Math.toRadians(rotationAngle)); 151 tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f);
137 g.transform(tf); 152 g.transform(tf);
138 153
139 switch (type) { 154 switch (type) {
140 case NORMAL: g.setPaint(Color.green); break; 155 case LOCKED: g.setPaint(Color.green); break;
141 case ACTIVE: g.setPaint(Color.red); break; 156 case ACTIVE: g.setPaint(Color.red); break;
142 case START: g.setPaint(Color.orange); break; 157 case START: g.setPaint(Color.orange); break;
143 } 158 }
159
144 g.fill(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10)); 160 g.fill(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10));
145 161
146 g.setPaint(Color.black); 162 g.setPaint(Color.black);
147 g.setStroke(new BasicStroke(4.0f)); 163 g.setStroke(new BasicStroke(4.0f));
148 g.draw(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10)); 164 g.draw(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10));
149 165
150 if (type == PieceType.START) 166 if (type == PieceType.START)
151 return; 167 return;
152 168
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
153 g.setStroke(new BasicStroke(6.0f)); 176 g.setStroke(new BasicStroke(6.0f));
154 // CubicCurve2D c = new CubicCurve2D.Double(); 177 // CubicCurve2D c = new CubicCurve2D.Double();
155 QuadCurve2D c = new QuadCurve2D.Double(); 178 QuadCurve2D c = new QuadCurve2D.Double();
156 179
157 for (int i = 0; i < numConnections / 2; i++) 180 for (int i = 0; i < numConnections / 2; i++)
158 { 181 {
159 Point2D start, cp1, cp2, end; 182 Point2D start, cp1, cp2, end;
160 start = getPoint(x, y, dim, i); 183
161 end = getPoint(x, y, dim, connections[i]); 184 start = getPointCoords(x, y, dim, i);
162 // cp1 = getPoint(x, y, dim, (i + 4) % 8); 185 end = getPointCoords(x, y, dim, connections[i]);
163 // cp2 = getPoint(x, y, dim, (connections[i] + 4) % 8); 186 cp1 = getPointCoords(x, y, dim, -1);
164 cp1 = getPoint(x, y, dim, -1);
165 187
166 c.setCurve(start, cp1, end); 188 c.setCurve(start, cp1, end);
167 g.draw(c); 189 g.draw(c);
168 } 190 }
169 g.setTransform(save);
170 } 191 }
171 } 192 }