view Ristipolku.java @ 8:d8e7fd8f3ccf

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Jan 2011 22:21:45 +0200
parents 70714c229e23
children
line wrap: on
line source

/*
 * Ristipolku
 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
 *
 * Ohjelmointiprojekti 2010-2011 Java-kurssille T740306.
 */
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.util.*;
import java.io.*;
import game.*;

import javax.sound.sampled.*;


class PathInfo
{
    public int in, inX, inY, out, outX, outY; 
   
    public PathInfo(int in, int inX, int inY, int out, int outX, int outY)
    {
        this.in = in;
        this.inX = inX;
        this.inY = inY;

        this.out = out;
        this.outX = outX;
        this.outY = outY;
    }
}

/*
class AnimatedElement
{
    float x, y, stime, value;
    Interpolate lerp;
    boolean active;
    
    public AnimatedElement(float x, float y, )
    {
        stime = 0;
        this.x = x;
        this.y = y;
        
    }
    
    public animate(float time)
    {
        if (!active)
        {
            active = true;
            stime = time;
        }
        
        float t = (time - stime) / 10.0f;
        if (t < 100)
            value = lerp.getValue(t);
        else
        {
            
        }
    }
    
    public paint(Graphics2D g, );
    {
    }
}
*/

class GameBoard
{
    public static final int boardSize = 9;
    public static final int boardMiddle = 4;
    Piece[][] board;
    Piece current;
    public boolean flagGameOver;
    
    int moveX, moveY, movePoint;
    
    public GameBoard()
    {
        board = new Piece[boardSize][boardSize];

        board[boardMiddle][boardMiddle] = new Piece(PieceType.START);

        moveX = boardMiddle;
        moveY = boardMiddle - 1;
        movePoint = 0;
        
        
        
        flagGameOver = false;
    }

    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++)
        if (board[x][y] != null)
        {
            AffineTransform save = g.getTransform();

            board[x][y].paint(g,
                sx + (x * scale),
                sy + (y * scale),
                scale - scale / 10);

            g.setTransform(save);
        }
    }
   
    private boolean isEmpty(int x, int y)
    {
        return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null);
    }

    private Piece getPiece(int x, int y)
    {
        if (x >= 0 && x < boardSize && y >= 0 && y < boardSize)
            return board[x][y];
        else
            return null;
    }

    public void pieceRotate(boolean dir)
    {
        current.rotate(dir);
    }

    public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark)
    {
        int x = startX, y = startY;
        int point = -1;

        Piece curr = getPiece(startX, startY);
        if (curr == null)
            return null;
            
        while (curr != null)
        {
//            curr.(true);
//            elements.spawn("", );
        }
        
        return new PathInfo(startPoint, startX, startY, point, x, y);
    }

    public void pieceFinishTurn()
    {
        if (current != null)
        {
            current.setType(PieceType.LOCKED);
            PathInfo i = resolvePath(moveX, moveY, movePoint, true);
            
            if (i != null)
            {
            }
        }

        current = new Piece(PieceType.ACTIVE);
        if (isEmpty(moveX, moveY))
        {
            board[moveX][moveY] = current;
        }
        else
        {
            PathInfo i = resolvePath(moveX, moveY, movePoint, true);
            if (i != null)
                board[moveX][moveY] = current;
            else
                flagGameOver = true;
        }
   }
}


public class Ristipolku extends JPanel
                        implements Runnable, KeyListener
{
    Thread animThread;
    boolean animEnable = false;
    GameBoard lauta = null;
    BufferedImage lautaBG = null;
    float clock;

    public void init()
    {
        clock = 0;

        try
        {
            lautaBG = ImageIO.read(new File("board.png"));
        }

        catch (IOException e)
        {
            JOptionPane.showMessageDialog(null,
                "Could not load background image.",
                "Initialization error",
                JOptionPane.ERROR_MESSAGE);
        }
        
        startThreads();
        
        lauta = new GameBoard();
        addKeyListener(this);
        
        startThreads();
    }

    public void startThreads()
    {
        if (animThread == null)
        {
            animThread = new Thread(this);
            animEnable = true;
            animThread.start();
        }
    }
    
    public void stopThreads()
    {
        if (animThread != null)
        {
            animThread.interrupt();
            animEnable = false;
            animThread = null;
        }
    }

    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

        lauta.paint(g2, 100, 150, 60);
    }

    public void keyPressed(KeyEvent e)
    {
    }
   
    public void keyReleased(KeyEvent e)
    {
    }
   
    public void keyTyped(KeyEvent e)
    {
        switch (e.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
            case KeyEvent.VK_UP:
                lauta.pieceRotate(false);
                break;

            case KeyEvent.VK_RIGHT:
            case KeyEvent.VK_DOWN:
                lauta.pieceRotate(true);
                break;

            case KeyEvent.VK_ENTER:
                lauta.pieceFinishTurn();
                break;
        }
    }
    
    public void run()
    {
        while (animEnable)
        {
            clock++;
            
            Thread.sleep(100);
        }
    }
}