Mercurial > hg > ristipolku
comparison game/Piece.java @ 7:70714c229e23
More work.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 28 Jan 2011 20:23:21 +0200 |
parents | be0bf7544069 |
children | a7751971c2a3 |
comparison
equal
deleted
inserted
replaced
6:be0bf7544069 | 7:70714c229e23 |
---|---|
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.*; | |
8 import java.util.*; | 9 import java.util.*; |
9 import java.math.*; | 10 import java.math.*; |
10 | 11 |
11 | 12 |
12 public class Piece | 13 public class Piece |
18 int[] connections; | 19 int[] connections; |
19 PieceType type, oldType; | 20 PieceType type, oldType; |
20 | 21 |
21 boolean rotationChanged, rotationActive, | 22 boolean rotationChanged, rotationActive, |
22 typeChanged, typeActive; | 23 typeChanged, typeActive; |
23 double currAngle, newAngle, rotationTime, typeTime; | 24 float currAngle, newAngle, rotationTime, typeTime; |
24 double throb; | 25 float throb; |
25 | 26 |
27 Interpolate lerpRotation; | |
26 | 28 |
27 public Piece(PieceType ptype) | 29 public Piece(PieceType ptype) |
28 { | 30 { |
29 // Initialize | 31 // Initialize |
30 connections = new int[numConnections]; | 32 connections = new int[numConnections]; |
82 { | 84 { |
83 // Only normal | 85 // Only normal |
84 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) | 86 if (type != PieceType.LOCKED && type != PieceType.ACTIVE) |
85 return; | 87 return; |
86 | 88 |
87 newRotation = currRotation + (dir ? 1 : -1); | 89 currRotation = currRotation + (dir ? 1 : -1); |
88 | 90 |
89 if (newRotation < minRotation) | 91 if (currRotation < minRotation) |
90 newRotation = maxRotation; | 92 currRotation = maxRotation; |
91 else if (currRotation > maxRotation) | 93 else if (currRotation > maxRotation) |
92 newRotation = minRotation; | 94 currRotation = minRotation; |
93 | 95 |
94 newAngle = | 96 newAngle = (float) (currRotation * Math.PI) / 2.0f; |
95 rotationChanged = true; | 97 rotationChanged = true; |
96 } | 98 lerpRotation = new Interpolate(currAngle, newAngle, 100); |
97 | 99 } |
98 public Point2D getPointCoords(double x, double y, double dim, int index) | 100 |
99 { | 101 public Point2D getPointCoords(float x, float y, float dim, int index) |
100 double ox = 0, oy = 0; | 102 { |
101 double step = dim / 10; | 103 float ox = 0, oy = 0; |
104 float step = dim / 10; | |
102 | 105 |
103 switch (index) { | 106 switch (index) { |
104 case 0: ox = 3.0f; oy = 0.5f; break; | 107 case 0: ox = 3.0f; oy = 0.5f; break; |
105 case 1: ox = 7.0f; oy = 0.5f; break; | 108 case 1: ox = 7.0f; oy = 0.5f; break; |
106 case 2: ox = 9.5f; oy = 3.0f; break; | 109 case 2: ox = 9.5f; oy = 3.0f; break; |
111 case 7: ox = 0.5f; oy = 3.0f; break; | 114 case 7: ox = 0.5f; oy = 3.0f; break; |
112 | 115 |
113 case -1: ox = 5.0f; oy = 5.0f; break; | 116 case -1: ox = 5.0f; oy = 5.0f; break; |
114 } | 117 } |
115 | 118 |
116 return new Point2D.Double(x + ox * step, y + oy * step); | 119 return new Point2D.Float(x + ox * step, y + oy * step); |
117 } | 120 } |
118 | 121 |
119 public void animate(double time) | 122 public void animate(float time) |
120 { | 123 { |
121 if (rotationChanged) | 124 if (rotationChanged) |
122 { | 125 { |
123 rotationTime = time; | 126 rotationTime = time; |
124 rotationActive = true; | 127 rotationActive = true; |
128 rotationChanged = false; | |
125 } | 129 } |
126 | 130 |
127 if (rotationActive) | 131 if (rotationActive) |
128 { | 132 { |
129 double t = (time - rotationTime) / 10.0f; | 133 float t = (time - rotationTime) / 10.0f; |
130 | 134 |
131 if (t < Math.PI) | 135 if (t < 100) |
136 currAngle = lerpRotation.getValue(t); | |
137 else | |
138 { | |
139 currAngle = newAngle; | |
140 rotationActive = false; | |
141 } | |
132 } | 142 } |
133 | 143 |
134 if (typeChanged) | 144 if (typeChanged) |
135 { | 145 { |
136 typeTime = time; | 146 typeTime = time; |
137 typeActive = true; | 147 typeActive = true; |
148 typeChanged = false; | |
138 } | 149 } |
139 | 150 |
140 if (typeActive) | 151 if (typeActive) |
141 { | 152 { |
142 | |
143 } | 153 } |
144 | 154 |
145 throb = ((time / 10.0f) % 100) / 100.0f; | 155 throb = ((time / 10.0f) % 100) / 100.0f; |
146 } | 156 } |
147 | 157 |
148 public void paint(Graphics2D g, double x, double y, double dim) | 158 public void paint(Graphics2D g, float x, float y, float dim) |
149 { | 159 { |
150 AffineTransform tf = new AffineTransform(); | 160 AffineTransform tf = new AffineTransform(); |
151 tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); | 161 tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); |
152 g.transform(tf); | 162 g.transform(tf); |
153 | 163 |
155 case LOCKED: g.setPaint(Color.green); break; | 165 case LOCKED: g.setPaint(Color.green); break; |
156 case ACTIVE: g.setPaint(Color.red); break; | 166 case ACTIVE: g.setPaint(Color.red); break; |
157 case START: g.setPaint(Color.orange); break; | 167 case START: g.setPaint(Color.orange); break; |
158 } | 168 } |
159 | 169 |
160 g.fill(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10)); | 170 g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
161 | 171 |
162 g.setPaint(Color.black); | 172 g.setPaint(Color.black); |
163 g.setStroke(new BasicStroke(4.0f)); | 173 g.setStroke(new BasicStroke(4.0f)); |
164 g.draw(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10)); | 174 g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); |
165 | 175 |
166 if (type == PieceType.START) | 176 if (type == PieceType.START) |
167 return; | 177 return; |
168 | 178 |
169 if (type == PieceType.ACTIVE) | 179 if (type == PieceType.ACTIVE) |
170 { | 180 { |
171 g.setPaint(Color(0, 0, 0, 1.0f - throb)); | 181 g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throb) )); |
172 g.setStroke(new BasicStroke(2.0f + throb * 2.0f)); | 182 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)); | 183 g.draw(new RoundRectangle2D.Float(x - throb * 10.0f, y - throb * 10.0f, dim + throb * 20.0f, dim + throb * 20.0f, dim / 10, dim / 10)); |
174 } | 184 } |
175 | 185 |
176 g.setStroke(new BasicStroke(6.0f)); | 186 g.setStroke(new BasicStroke(6.0f)); |
177 // CubicCurve2D c = new CubicCurve2D.Double(); | 187 // CubicCurve2D c = new CubicCurve2D.Float(); |
178 QuadCurve2D c = new QuadCurve2D.Double(); | 188 QuadCurve2D c = new QuadCurve2D.Float(); |
179 | 189 |
180 for (int i = 0; i < numConnections / 2; i++) | 190 for (int i = 0; i < numConnections / 2; i++) |
181 { | 191 { |
182 Point2D start, cp1, cp2, end; | 192 Point2D start, cp1, cp2, end; |
183 | 193 |