# HG changeset patch # User Matti Hamalainen # Date 1296253800 -7200 # Node ID a7751971c2a3612fce8afea6faa8edcb015f7b68 # Parent d8e7fd8f3ccf719a15d02f11631f90061a512689 More work. diff -r d8e7fd8f3ccf -r a7751971c2a3 Makefile --- a/Makefile Fri Jan 28 22:21:45 2011 +0200 +++ b/Makefile Sat Jan 29 00:30:00 2011 +0200 @@ -1,5 +1,5 @@ # Settings, directories -RUN=Ristipolku.class +RUN=RistipolkuApplet.class TARGETS=$(RUN) SUBDIR=game @@ -12,11 +12,15 @@ ### all: $(TARGETS) -#Ristipolku.class: game/Piece.class game/PieceType.class $(SUBDI %.class: %.java $(JAVAC) $< - + +game/%.class: game/%.java + $(JAVAC) $< + +RistipolkuApplet.class: game/Piece.class game/PieceType.class game/Engine.class game/Interpolate.class + run: $(RUN) $(APPLETVIEWER) $(patsubst %.class,%.html,$<) diff -r d8e7fd8f3ccf -r a7751971c2a3 Ristipolku.java --- a/Ristipolku.java Fri Jan 28 22:21:45 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,283 +0,0 @@ -/* - * Ristipolku - * (C) Copyright 2011 Matti 'ccr' Hämäläinen - * - * 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); - } - } -} diff -r d8e7fd8f3ccf -r a7751971c2a3 game/Engine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/Engine.java Sat Jan 29 00:30:00 2011 +0200 @@ -0,0 +1,330 @@ +/* + * Ristipolku Game Engine + * (C) Copyright 2011 Matti 'ccr' Hämäläinen + */ +package game; + +import java.awt.*; +import java.awt.geom.*; +import java.awt.event.*; +import java.awt.image.*; +import java.awt.event.*; +import javax.imageio.*; +import javax.swing.*; +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; + + pieceFinishTurn(); + + 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); + } + } + + public void animate(float time) + { + for (int y = 0; y < boardSize; y++) + for (int x = 0; x < boardSize; x++) + if (board[x][y] != null) + { + board[x][y].animate(time); + } + } + + 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) + { + if (current != null) + 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 Engine extends JPanel + implements Runnable, KeyListener +{ + Thread animThread; + boolean animEnable = false; + GameBoard lauta = null; + BufferedImage lautaBG = null, lautaBGScaled = null; + Dimension oldDim; + float clock; + + public Engine() + { + BufferedImage img; + clock = 0; + + System.out.print("Engine() constructor\n"); + + try + { + lautaBG = ImageIO.read(new File("board.png")); + } + catch (IOException e) + { + System.out.print("lol\n"); + JOptionPane.showMessageDialog(null, + "Could not load background image.", + "Initialization error", + JOptionPane.ERROR_MESSAGE); + } + +// Graphics2D g = img.getGraphics(); +// atrans = AffineTransform.getScaleInstance(img.getWidth(), 3); + + lauta = new GameBoard(); + addKeyListener(this); + } + + public void startThreads() + { + System.out.print("startThreads()\n"); + if (animThread == null) + { + animThread = new Thread(this); + animEnable = true; + animThread.start(); + } + } + + public void stopThreads() + { + System.out.print("stopThreads()\n"); + if (animThread != null) + { + animThread.interrupt(); + animEnable = false; + animThread = null; + } + } + + public void paintComponent(Graphics g) + { + Graphics2D g2 = (Graphics2D) g; + + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + Dimension dim = getSize(); + if (oldDim == null || !dim.equals(oldDim)) + { + lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); + Graphics2D gimg = lautaBGScaled.createGraphics(); + gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BICUBIC); + AffineTransform xform = AffineTransform.getScaleInstance(0.5f, 0.5f); + gimg.drawImage(lautaBG, xform, null); + oldDim = dim; + System.out.print("scale changed\n"); + } + + g2.drawImage(lautaBGScaled, 0, 0, null); + lauta.paint(g2, 100, 150, 60); + + if (!hasFocus()) + requestFocus(); + } + + public void keyTyped(KeyEvent e) + { + } + + public void keyReleased(KeyEvent e) + { + } + + public void keyPressed(KeyEvent e) + { + System.out.print("lol\n"); + 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++; + +// System.out.print("clock=" + clock + "\n"); + + lauta.animate(clock); + + repaint(); + + try { + Thread.sleep(10); + } + + catch (InterruptedException x) { + } + } + } +} diff -r d8e7fd8f3ccf -r a7751971c2a3 game/Piece.java --- a/game/Piece.java Fri Jan 28 22:21:45 2011 +0200 +++ b/game/Piece.java Sat Jan 29 00:30:00 2011 +0200 @@ -13,14 +13,16 @@ public class Piece { static final int numConnections = 8; - static final int minRotation = 0; - static final int maxRotation = 3; + static final float maxTime = 50.0f; + int currRotation; int[] connections; + boolean[] active; PieceType type, oldType; boolean rotationChanged, rotationActive, - typeChanged, typeActive; + typeChanged, typeActive, + activeChanged, activeActive; float currAngle, newAngle, rotationTime, typeTime; float throb; @@ -30,6 +32,7 @@ { // Initialize connections = new int[numConnections]; + active = new boolean[numConnections]; type = ptype; rotationChanged = false; @@ -86,16 +89,11 @@ if (type != PieceType.LOCKED && type != PieceType.ACTIVE) return; - currRotation = currRotation + (dir ? 1 : -1); + currRotation = currRotation + (dir ? -1 : 1); + newAngle = (float) (currRotation * Math.PI) / 2.0f; - if (currRotation < minRotation) - currRotation = maxRotation; - else if (currRotation > maxRotation) - currRotation = minRotation; - - newAngle = (float) (currRotation * Math.PI) / 2.0f; rotationChanged = true; - lerpRotation = new Interpolate(currAngle, newAngle, 100); + lerpRotation = new Interpolate(currAngle, newAngle, maxTime); } public Point2D getPointCoords(float x, float y, float dim, int index) @@ -119,6 +117,12 @@ return new Point2D.Float(x + ox * step, y + oy * step); } + public void setActiveConnection(int index) + { + active[index] = true; + activeChanged = true; + } + public void animate(float time) { if (rotationChanged) @@ -130,9 +134,9 @@ if (rotationActive) { - float t = (time - rotationTime) / 10.0f; + float t = (time - rotationTime); - if (t < 100) + if (t < maxTime) currAngle = lerpRotation.getValue(t); else { @@ -152,7 +156,12 @@ { } - throb = ((time / 10.0f) % 100) / 100.0f; + if (activeChanged) + { + + } + + throb = (time % 100) / 100.0f; } public void paint(Graphics2D g, float x, float y, float dim) @@ -161,6 +170,9 @@ tf.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); g.transform(tf); + if (type == PieceType.ACTIVE) + System.out.print("angle = " + currAngle + "\n"); + switch (type) { case LOCKED: g.setPaint(Color.green); break; case ACTIVE: g.setPaint(Color.red); break; @@ -183,20 +195,25 @@ 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.setPaint(Color.black); g.setStroke(new BasicStroke(6.0f)); // CubicCurve2D c = new CubicCurve2D.Float(); QuadCurve2D c = new QuadCurve2D.Float(); - - for (int i = 0; i < numConnections / 2; i++) + boolean[] drawn = new boolean[numConnections]; + for (int i = 0; i < numConnections; i++) + if (!drawn[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); + c.setCurve(start, cp1, end); + g.draw(c); + + drawn[i] = true; + drawn[connections[i]] = true; } } }