changeset 7:70714c229e23

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Jan 2011 20:23:21 +0200
parents be0bf7544069
children d8e7fd8f3ccf
files Ristipolku.java game/Interpolate.java game/Piece.java
diffstat 3 files changed, 83 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/Ristipolku.java	Fri Jan 28 18:42:16 2011 +0200
+++ b/Ristipolku.java	Fri Jan 28 20:23:21 2011 +0200
@@ -7,7 +7,9 @@
 import java.awt.*;
 import java.awt.geom.*;
 import java.awt.event.*;
-//import javax.swing.*;
+import java.awt.image.*;
+import java.awt.event.*;
+import javax.swing.*;
 import javax.imageio.*;
 import java.util.*;
 import java.io.*;
@@ -37,7 +39,9 @@
     public static final int boardMiddle = 4;
     Piece[][] board;
     Piece current;
-   
+    
+    int moveX, moveY, movePoint;
+    
     public GameBoard()
     {
         board = new Piece[boardSize][boardSize];
@@ -46,9 +50,10 @@
 
         moveX = boardMiddle;
         moveY = boardMiddle - 1;
+        movePoint = 0;
     }
 
-    public void paint(Graphics2D g, int sx, int sy, double scale)
+    public void paint(Graphics2D g, int sx, int sy, float scale)
     {
         for (int y = 0; y < boardSize; y++)
         for (int x = 0; x < boardSize; x++)
@@ -77,25 +82,46 @@
         else
             return null;
     }
-   
-    public Point resolvePath(int x, int y, int inpoint)
-    {
-    }
-   
+
     public void pieceRotate(boolean dir)
     {
         current.rotate(dir);
     }
 
+    public PathInfo resolvePath(int startX, int startY, int startPoint)
+    {
+        int x = startX, y = startY;
+        int point;
+
+        Piece curr = getPiece(startX, startY);
+        if (curr == null)
+            return null;
+            
+        while (curr != null)
+        {
+            
+        }
+        
+        return new PathInfo(startPoint, startX, startY, point, x, y);
+    }
+
     public void pieceFinishTurn()
     {
         if (current != null)
+        {
             current.setType(PieceType.LOCKED);
-      
-        if (isEmpty(cx, cy))
+            PathInfo i = resolvePath(moveX, moveY, movePoint);
+            
+            if (i != null)
+            {
+                
+            }
+        }
+
+        if (isEmpty(moveX, moveY))
         {
             current = new Piece(PieceType.ACTIVE);
-            lauta.setPiece(cx, cy, current);
+            lauta.setPiece(moveX, moveY, current);
         }
    }
 }
@@ -113,8 +139,9 @@
 
         try
         {
-            lautaBG = ImageIO.read(new File("tausta.png"));
+            lautaBG = ImageIO.read(new File("background.jpg"));
         }
+
         catch (IOException e)
         {
             JOptionPane.showMessageDialog(null,
@@ -134,7 +161,7 @@
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
 
-        lauta.paint(g2, 15, 15, 60);
+        lauta.paint(g2, 100, 150, 60);
     }
 
     public void keyPressed(KeyEvent e)
@@ -147,19 +174,19 @@
    
     public void keyTyped(KeyEvent e)
     {
-        switch (event.getKeyCode())
+        switch (e.getKeyCode())
         {
-            case VK_LEFT:
-            case VK_UP:
+            case KeyEvent.VK_LEFT:
+            case KeyEvent.VK_UP:
                 lauta.pieceRotate(false);
                 break;
 
-            case VK_RIGHT:
-            case VK_DOWN:
+            case KeyEvent.VK_RIGHT:
+            case KeyEvent.VK_DOWN:
                 lauta.pieceRotate(true);
                 break;
 
-            case VK_ENTER:
+            case KeyEvent.VK_ENTER:
                 lauta.pieceFinishTurn();
                 break;
         }
--- a/game/Interpolate.java	Fri Jan 28 18:42:16 2011 +0200
+++ b/game/Interpolate.java	Fri Jan 28 20:23:21 2011 +0200
@@ -8,19 +8,19 @@
 
 public class Interpolate
 {
-    double start, end, steps;
+    float start, end, steps;
 
-    public Interpolate(double start, double end, double steps)
+    public Interpolate(float start, float end, float steps)
     {
       this.start = start;
       this.end = end;
       this.steps = steps;
     }
     
-    public double getValue(double step)
+    public float getValue(float step)
     {
-      double n = step / steps;
-      double v = n * n * (3.0f - 2.0f * n);
+      float n = step / steps;
+      float v = n * n * (3.0f - 2.0f * n);
       return (start * v) + (end * (1.0f - v));
     }
 }
--- 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++)
         {