Mercurial > hg > ristipolku
annotate game/Piece.java @ 30:60a4579a79df dev-0_1
Moar work.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 02 Feb 2011 22:45:53 +0200 |
parents | 26adc2827983 |
children | e480579cc460 |
rev | line source |
---|---|
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.*; | |
7 | 8 import java.awt.geom.*; |
1 | 9 import java.util.*; |
6 | 10 import java.math.*; |
1 | 11 |
12 | |
13 public class Piece | |
14 { | |
3 | 15 static final int numConnections = 8; |
9 | 16 static final float maxTime = 50.0f; |
17 | |
3 | 18 int currRotation; |
19 int[] connections; | |
9 | 20 boolean[] active; |
3 | 21 PieceType type, oldType; |
1 | 22 |
3 | 23 boolean rotationChanged, rotationActive, |
9 | 24 typeChanged, typeActive, |
25 activeChanged, activeActive; | |
7 | 26 float currAngle, newAngle, rotationTime, typeTime; |
27 float throb; | |
6 | 28 |
7 | 29 Interpolate lerpRotation; |
1 | 30 |
3 | 31 public Piece(PieceType ptype) |
32 { | |
33 // Initialize | |
34 connections = new int[numConnections]; | |
9 | 35 active = new boolean[numConnections]; |
3 | 36 type = ptype; |
1 | 37 |
3 | 38 rotationChanged = false; |
6 | 39 rotationActive = false; |
40 currRotation = 0; | |
41 currAngle = 0; | |
42 | |
3 | 43 typeChanged = false; |
44 typeActive = false; | |
1 | 45 |
6 | 46 throb = 0; |
1 | 47 |
48 | |
3 | 49 // Initialize connections between endpoints of the paths inside the piece |
50 for (int i = 0; i < numConnections; i++) | |
51 connections[i] = -1; | |
1 | 52 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
53 |
6 | 54 // Randomize connections in the piece |
3 | 55 Random rnd = new Random(); |
56 for (int i = 0; i < numConnections; i++) | |
57 { | |
58 while (connections[i] < 0) | |
59 { | |
60 int tmp = rnd.nextInt(numConnections); | |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
61 if (tmp != i && connections[tmp] < 0) |
3 | 62 { |
63 connections[i] = tmp; | |
64 connections[tmp] = i; | |
65 } | |
66 } | |
67 } | |
68 } | |
69 | |
70 public Piece() | |
71 { | |
72 this(PieceType.NONE); | |
73 } | |
1 | 74 |
3 | 75 public void setType(PieceType ptype) |
76 { | |
77 typeChanged = (oldType != ptype); | |
78 oldType = type; | |
79 type = ptype; | |
80 } | |
1 | 81 |
3 | 82 public int getConnection(int in) |
83 { | |
30 | 84 return connections[(in + (currRotation * 2)) % 8]; |
3 | 85 } |
5 | 86 |
3 | 87 public void rotate(boolean dir) |
88 { | |
6 | 89 // Only normal |
90 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) | |
3 | 91 return; |
1 | 92 |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
93 currRotation = (currRotation + (dir ? 1 : -1)) % 4; |
11 | 94 newAngle = (float) ((currRotation * Math.PI) / 2.0f); |
26
3d4cc47df31a
Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents:
21
diff
changeset
|
95 lerpRotation = new Interpolate(newAngle, currAngle, maxTime); |
3 | 96 rotationChanged = true; |
5 | 97 } |
1 | 98 |
7 | 99 public Point2D getPointCoords(float x, float y, float dim, int index) |
5 | 100 { |
7 | 101 float ox = 0, oy = 0; |
102 float step = dim / 10; | |
1 | 103 |
5 | 104 switch (index) { |
105 case 0: ox = 3.0f; oy = 0.5f; break; | |
106 case 1: ox = 7.0f; oy = 0.5f; break; | |
107 case 2: ox = 9.5f; oy = 3.0f; break; | |
108 case 3: ox = 9.5f; oy = 7.0f; break; | |
109 case 4: ox = 7.0f; oy = 9.5f; break; | |
110 case 5: ox = 3.0f; oy = 9.5f; break; | |
111 case 6: ox = 0.5f; oy = 7.0f; break; | |
112 case 7: ox = 0.5f; oy = 3.0f; break; | |
1 | 113 |
5 | 114 case -1: ox = 5.0f; oy = 5.0f; break; |
115 } | |
1 | 116 |
7 | 117 return new Point2D.Float(x + ox * step, y + oy * step); |
5 | 118 } |
1 | 119 |
9 | 120 public void setActiveConnection(int index) |
121 { | |
122 active[index] = true; | |
30 | 123 active[connections[index]] = true; |
9 | 124 activeChanged = true; |
125 } | |
27 | 126 |
127 public PieceType getType() | |
128 { | |
129 return type; | |
130 } | |
9 | 131 |
7 | 132 public void animate(float time) |
5 | 133 { |
134 if (rotationChanged) | |
135 { | |
136 rotationTime = time; | |
137 rotationActive = true; | |
7 | 138 rotationChanged = false; |
5 | 139 } |
1 | 140 |
5 | 141 if (rotationActive) |
142 { | |
11 | 143 float t = (time - rotationTime) / 2; |
144 | |
9 | 145 if (t < maxTime) |
7 | 146 currAngle = lerpRotation.getValue(t); |
147 else | |
148 { | |
149 currAngle = newAngle; | |
150 rotationActive = false; | |
151 } | |
5 | 152 } |
6 | 153 |
154 if (typeChanged) | |
155 { | |
156 typeTime = time; | |
157 typeActive = true; | |
7 | 158 typeChanged = false; |
6 | 159 } |
160 | |
161 if (typeActive) | |
162 { | |
163 } | |
164 | |
9 | 165 if (activeChanged) |
166 { | |
167 } | |
168 | |
169 throb = (time % 100) / 100.0f; | |
5 | 170 } |
1 | 171 |
7 | 172 public void paint(Graphics2D g, float x, float y, float dim) |
5 | 173 { |
11 | 174 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); |
1 | 175 |
5 | 176 switch (type) { |
6 | 177 case LOCKED: g.setPaint(Color.green); break; |
5 | 178 case ACTIVE: g.setPaint(Color.red); break; |
179 case START: g.setPaint(Color.orange); break; | |
180 } | |
6 | 181 |
7 | 182 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
1 | 183 |
5 | 184 g.setPaint(Color.black); |
185 g.setStroke(new BasicStroke(4.0f)); | |
7 | 186 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
5 | 187 |
188 if (type == PieceType.START) | |
189 return; | |
190 | |
6 | 191 if (type == PieceType.ACTIVE) |
192 { | |
11 | 193 float offs1 = throb * 10.0f, |
194 offs2 = throb * 20.0f; | |
195 | |
7 | 196 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throb) )); |
6 | 197 g.setStroke(new BasicStroke(2.0f + throb * 2.0f)); |
11 | 198 g.draw(new RoundRectangle2D.Float(x - offs1, y - offs1, dim + offs2, dim + offs2, dim / 10, dim / 10)); |
6 | 199 } |
200 | |
5 | 201 g.setStroke(new BasicStroke(6.0f)); |
7 | 202 // CubicCurve2D c = new CubicCurve2D.Float(); |
203 QuadCurve2D c = new QuadCurve2D.Float(); | |
9 | 204 boolean[] drawn = new boolean[numConnections]; |
205 for (int i = 0; i < numConnections; i++) | |
206 if (!drawn[i]) | |
5 | 207 { |
208 Point2D start, cp1, cp2, end; | |
27 | 209 |
210 boolean act = active[i] || active[connections[i]]; | |
211 g.setPaint(act ? Color.white : Color.black); | |
9 | 212 |
6 | 213 start = getPointCoords(x, y, dim, i); |
214 end = getPointCoords(x, y, dim, connections[i]); | |
215 cp1 = getPointCoords(x, y, dim, -1); | |
1 | 216 |
9 | 217 c.setCurve(start, cp1, end); |
218 g.draw(c); | |
219 | |
220 drawn[i] = true; | |
221 drawn[connections[i]] = true; | |
5 | 222 } |
223 } | |
1 | 224 } |