Mercurial > hg > ristipolku
annotate game/Piece.java @ 27:26adc2827983
More work on the internals.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 01 Feb 2011 20:38:19 +0200 |
parents | 3d4cc47df31a |
children | 60a4579a79df |
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 { | |
84 return connections[in]; | |
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; | |
123 activeChanged = true; | |
124 } | |
27 | 125 |
126 public PieceType getType() | |
127 { | |
128 return type; | |
129 } | |
9 | 130 |
7 | 131 public void animate(float time) |
5 | 132 { |
133 if (rotationChanged) | |
134 { | |
135 rotationTime = time; | |
136 rotationActive = true; | |
7 | 137 rotationChanged = false; |
5 | 138 } |
1 | 139 |
5 | 140 if (rotationActive) |
141 { | |
11 | 142 float t = (time - rotationTime) / 2; |
143 | |
9 | 144 if (t < maxTime) |
7 | 145 currAngle = lerpRotation.getValue(t); |
146 else | |
147 { | |
148 currAngle = newAngle; | |
149 rotationActive = false; | |
150 } | |
5 | 151 } |
6 | 152 |
153 if (typeChanged) | |
154 { | |
155 typeTime = time; | |
156 typeActive = true; | |
7 | 157 typeChanged = false; |
6 | 158 } |
159 | |
160 if (typeActive) | |
161 { | |
162 } | |
163 | |
9 | 164 if (activeChanged) |
165 { | |
166 } | |
167 | |
168 throb = (time % 100) / 100.0f; | |
5 | 169 } |
1 | 170 |
7 | 171 public void paint(Graphics2D g, float x, float y, float dim) |
5 | 172 { |
11 | 173 g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); |
1 | 174 |
5 | 175 switch (type) { |
6 | 176 case LOCKED: g.setPaint(Color.green); break; |
5 | 177 case ACTIVE: g.setPaint(Color.red); break; |
178 case START: g.setPaint(Color.orange); break; | |
179 } | |
6 | 180 |
7 | 181 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
1 | 182 |
5 | 183 g.setPaint(Color.black); |
184 g.setStroke(new BasicStroke(4.0f)); | |
7 | 185 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
5 | 186 |
187 if (type == PieceType.START) | |
188 return; | |
189 | |
6 | 190 if (type == PieceType.ACTIVE) |
191 { | |
11 | 192 float offs1 = throb * 10.0f, |
193 offs2 = throb * 20.0f; | |
194 | |
7 | 195 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throb) )); |
6 | 196 g.setStroke(new BasicStroke(2.0f + throb * 2.0f)); |
11 | 197 g.draw(new RoundRectangle2D.Float(x - offs1, y - offs1, dim + offs2, dim + offs2, dim / 10, dim / 10)); |
6 | 198 } |
199 | |
5 | 200 g.setStroke(new BasicStroke(6.0f)); |
7 | 201 // CubicCurve2D c = new CubicCurve2D.Float(); |
202 QuadCurve2D c = new QuadCurve2D.Float(); | |
9 | 203 boolean[] drawn = new boolean[numConnections]; |
204 for (int i = 0; i < numConnections; i++) | |
205 if (!drawn[i]) | |
5 | 206 { |
207 Point2D start, cp1, cp2, end; | |
27 | 208 |
209 boolean act = active[i] || active[connections[i]]; | |
210 g.setPaint(act ? Color.white : Color.black); | |
9 | 211 |
6 | 212 start = getPointCoords(x, y, dim, i); |
213 end = getPointCoords(x, y, dim, connections[i]); | |
214 cp1 = getPointCoords(x, y, dim, -1); | |
1 | 215 |
9 | 216 c.setCurve(start, cp1, end); |
217 g.draw(c); | |
218 | |
219 drawn[i] = true; | |
220 drawn[connections[i]] = true; | |
5 | 221 } |
222 } | |
1 | 223 } |