diff game/Piece.java @ 106:41c6cca69d60

Make new pieces appear gradually, and same effect for swapping.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 03 Mar 2011 21:21:35 +0200
parents eb2e72dd8cae
children 4c0dec72e2f0
line wrap: on
line diff
--- a/game/Piece.java	Thu Mar 03 18:48:17 2011 +0200
+++ b/game/Piece.java	Thu Mar 03 21:21:35 2011 +0200
@@ -26,11 +26,11 @@
             typeChanged, typeActive,
             stateChanged, stateActive;
 
-    float   currAngle, newAngle,
-            rotationTime, rotationSpeed,
-            typeTime, throbTime;
+    float   currAngle, rotationTime, rotationSpeed,
+            typeTime, typeValue, throbTime;
 
-    Interpolate lerpRotation;
+    Interpolate lerpRotation,
+                lerpType;
 
 
     public Piece(PieceType ptype)
@@ -50,6 +50,7 @@
       
         throbTime = 0;
 
+        lerpType = new Interpolate(1.0f, 0.0f, maxTime);
 
         // Initialize connections between endpoints of the paths inside the piece
         for (int i = 0; i < numConnections; i++)
@@ -77,9 +78,15 @@
         this(PieceType.NONE);
     }
 
+
+    public void changed()
+    {
+        typeChanged = true;
+    }
+    
     public void setType(PieceType ptype)
     {
-        typeChanged = (prevType != ptype);
+//        typeChanged = (prevType != ptype) && (ptype == PieceType.LOCKED);
         prevType = type;
         type = ptype;
     }
@@ -153,8 +160,7 @@
             return;
 
         currRotation = (currRotation + (dir == RotateDir.RIGHT ? 1 : -1));
-        newAngle = getAngle(currRotation);
-        lerpRotation = new Interpolate(newAngle, currAngle, maxTime);
+        lerpRotation = new Interpolate(getAngle(currRotation), currAngle, maxTime);
         rotationChanged = true;
     }
 
@@ -223,7 +229,7 @@
                 currAngle = lerpRotation.getValue(t);
             else
             {
-                currAngle = newAngle;
+                currAngle = lerpRotation.start;
                 rotationActive = false;
             }
         }
@@ -237,6 +243,15 @@
         
         if (typeActive)
         {
+            float t = (time - typeTime) * 2.0f;
+
+            if (t < maxTime)
+                typeValue = lerpType.getValue(t);
+            else
+            {
+                typeValue = lerpType.start;
+                typeActive = false;
+            }
         }
         
         if (stateChanged)
@@ -249,6 +264,15 @@
     public void paint(Graphics2D g, float x, float y, float dim)
     {
         AffineTransform save = g.getTransform();
+        Composite csave = g.getComposite();
+
+        // Change compositing alpha for the whole piece drawing
+        // when the piece is being "introduced".
+        if (typeActive)
+        {
+            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, typeValue));
+        }
+
 
         // Transform drawing by current angle
         g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f);
@@ -320,5 +344,6 @@
         } // !PieceType.START
 
         g.setTransform(save);
+        g.setComposite(csave);
     }
 }