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