view game/Piece.java @ 6:be0bf7544069

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Jan 2011 18:42:16 +0200
parents 4890020bf856
children 70714c229e23
line wrap: on
line source

/*
 * Ristipolku
 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
 */
package game;

import java.awt.*;
import java.util.*;
import java.math.*;


public class Piece
{
    static final int numConnections = 8;
    static final int minRotation = 0;
    static final int maxRotation = 3;
    int currRotation;
    int[] connections;
    PieceType type, oldType;

    boolean rotationChanged, rotationActive,
            typeChanged, typeActive;
    double currAngle, newAngle, rotationTime, typeTime;
    double throb;


    public Piece(PieceType ptype)
    {
        // Initialize
        connections = new int[numConnections];
        type = ptype;

        rotationChanged = false;
        rotationActive = false;
        currRotation = 0;
        currAngle = 0;

        typeChanged = false;
        typeActive = false;
      
        throb = 0;


        // Initialize connections between endpoints of the paths inside the piece
        for (int i = 0; i < numConnections; i++)
            connections[i] = -1;

        // Randomize connections in the piece
        Random rnd = new Random();
        for (int i = 0; i < numConnections; i++)
        {
            while (connections[i] < 0)
            {
                int tmp = rnd.nextInt(numConnections);
                if (connections[tmp] < 0)
                {
                    connections[i] = tmp;
                    connections[tmp] = i;
                }
            }
        }
    }

    public Piece()
    {
        this(PieceType.NONE);
    }

    public void setType(PieceType ptype)
    {
        typeChanged = (oldType != ptype);
        oldType = type;
        type = ptype;
    }

    public int getConnection(int in)
    {
        return connections[in];
    }

    public void rotate(boolean dir)
    {
        // Only normal 
        if (type != PieceType.LOCKED && type != PieceType.ACTIVE)
            return;

        newRotation = currRotation + (dir ? 1 : -1);

        if (newRotation < minRotation)
            newRotation = maxRotation;
        else if (currRotation > maxRotation)
            newRotation = minRotation;

        newAngle = 
        rotationChanged = true;
    }

    public Point2D getPointCoords(double x, double y, double dim, int index)
    {
        double ox = 0, oy = 0;
        double step = dim / 10;
      
        switch (index) {
            case  0: ox = 3.0f; oy = 0.5f; break;
            case  1: ox = 7.0f; oy = 0.5f; break;
            case  2: ox = 9.5f; oy = 3.0f; break;
            case  3: ox = 9.5f; oy = 7.0f; break;
            case  4: ox = 7.0f; oy = 9.5f; break;
            case  5: ox = 3.0f; oy = 9.5f; break;
            case  6: ox = 0.5f; oy = 7.0f; break;
            case  7: ox = 0.5f; oy = 3.0f; break;

            case -1: ox = 5.0f; oy = 5.0f; break;
        }

        return new Point2D.Double(x + ox * step, y + oy * step);
    }

    public void animate(double time)
    {
        if (rotationChanged)
        {
            rotationTime = time;
            rotationActive = true;
        }

        if (rotationActive)
        {
            double t = (time - rotationTime) / 10.0f;
            
            if (t < Math.PI)
        }
        
        if (typeChanged)
        {
            typeTime = time;
            typeActive = true;
        }
        
        if (typeActive)
        {
            
        }
        
        throb = ((time / 10.0f) % 100) / 100.0f;
    }

    public void paint(Graphics2D g, double x, double y, double dim)
    {
        AffineTransform tf = new AffineTransform();
        tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f);
        g.transform(tf);

        switch (type) {
            case LOCKED:  g.setPaint(Color.green); break;
            case ACTIVE:  g.setPaint(Color.red); break;
            case START:   g.setPaint(Color.orange); break;
        }

        g.fill(new RoundRectangle2D.Double(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));

        if (type == PieceType.START)
            return;

        if (type == PieceType.ACTIVE)
        {
            g.setPaint(Color(0, 0, 0, 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.setStroke(new BasicStroke(6.0f));
//      CubicCurve2D c = new CubicCurve2D.Double();
        QuadCurve2D c = new QuadCurve2D.Double();

        for (int i = 0; i < numConnections / 2; i++)
        {
            Point2D start, cp1, cp2, end;

            start = getPointCoords(x, y, dim, i);
            end   = getPointCoords(x, y, dim, connections[i]);
            cp1   = getPointCoords(x, y, dim, -1);

           c.setCurve(start, cp1, end);
           g.draw(c);
        }
    }
}