diff 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
line wrap: on
line diff
--- a/game/Piece.java	Fri Jan 28 18:42:16 2011 +0200
+++ b/game/Piece.java	Fri Jan 28 20:23:21 2011 +0200
@@ -5,6 +5,7 @@
 package game;
 
 import java.awt.*;
+import java.awt.geom.*;
 import java.util.*;
 import java.math.*;
 
@@ -20,9 +21,10 @@
 
     boolean rotationChanged, rotationActive,
             typeChanged, typeActive;
-    double currAngle, newAngle, rotationTime, typeTime;
-    double throb;
+    float currAngle, newAngle, rotationTime, typeTime;
+    float throb;
 
+    Interpolate lerpRotation;
 
     public Piece(PieceType ptype)
     {
@@ -84,21 +86,22 @@
         if (type != PieceType.LOCKED && type != PieceType.ACTIVE)
             return;
 
-        newRotation = currRotation + (dir ? 1 : -1);
+        currRotation = currRotation + (dir ? 1 : -1);
 
-        if (newRotation < minRotation)
-            newRotation = maxRotation;
+        if (currRotation < minRotation)
+            currRotation = maxRotation;
         else if (currRotation > maxRotation)
-            newRotation = minRotation;
+            currRotation = minRotation;
 
-        newAngle = 
+        newAngle = (float) (currRotation * Math.PI) / 2.0f;
         rotationChanged = true;
+        lerpRotation = new Interpolate(currAngle, newAngle, 100);
     }
 
-    public Point2D getPointCoords(double x, double y, double dim, int index)
+    public Point2D getPointCoords(float x, float y, float dim, int index)
     {
-        double ox = 0, oy = 0;
-        double step = dim / 10;
+        float ox = 0, oy = 0;
+        float step = dim / 10;
       
         switch (index) {
             case  0: ox = 3.0f; oy = 0.5f; break;
@@ -113,39 +116,46 @@
             case -1: ox = 5.0f; oy = 5.0f; break;
         }
 
-        return new Point2D.Double(x + ox * step, y + oy * step);
+        return new Point2D.Float(x + ox * step, y + oy * step);
     }
 
-    public void animate(double time)
+    public void animate(float time)
     {
         if (rotationChanged)
         {
             rotationTime = time;
             rotationActive = true;
+            rotationChanged = false;
         }
 
         if (rotationActive)
         {
-            double t = (time - rotationTime) / 10.0f;
+            float t = (time - rotationTime) / 10.0f;
             
-            if (t < Math.PI)
+            if (t < 100)
+                currAngle = lerpRotation.getValue(t);
+            else
+            {
+                currAngle = newAngle;
+                rotationActive = false;
+            }
         }
         
         if (typeChanged)
         {
             typeTime = time;
             typeActive = true;
+            typeChanged = false;
         }
         
         if (typeActive)
         {
-            
         }
         
         throb = ((time / 10.0f) % 100) / 100.0f;
     }
 
-    public void paint(Graphics2D g, double x, double y, double dim)
+    public void paint(Graphics2D g, float x, float y, float dim)
     {
         AffineTransform tf = new AffineTransform();
         tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f);
@@ -157,25 +167,25 @@
             case START:   g.setPaint(Color.orange); break;
         }
 
-        g.fill(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10));
+        g.fill(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10));
 
         g.setPaint(Color.black);
         g.setStroke(new BasicStroke(4.0f));
-        g.draw(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10));
+        g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10));
 
         if (type == PieceType.START)
             return;
 
         if (type == PieceType.ACTIVE)
         {
-            g.setPaint(Color(0, 0, 0, 1.0f - throb));
+            g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throb) ));
             g.setStroke(new BasicStroke(2.0f + throb * 2.0f));
-            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));
+            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));
         }
 
         g.setStroke(new BasicStroke(6.0f));
-//      CubicCurve2D c = new CubicCurve2D.Double();
-        QuadCurve2D c = new QuadCurve2D.Double();
+//      CubicCurve2D c = new CubicCurve2D.Float();
+        QuadCurve2D c = new QuadCurve2D.Float();
 
         for (int i = 0; i < numConnections / 2; i++)
         {