Mercurial > hg > ristipolku
view game/Piece.java @ 32:e480579cc460
More work on debugging the path resolving.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Feb 2011 18:22:16 +0200 |
parents | 60a4579a79df |
children | 6f6c551cc14c |
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.awt.geom.*; import java.util.*; import java.math.*; public class Piece { public enum RotateDir { LEFT, RIGHT } static final int numConnections = 8; static final float maxTime = 50.0f; int currRotation; int[] connections; boolean[] active; PieceType type, prevType; boolean rotationChanged, rotationActive, typeChanged, typeActive, activeChanged, activeActive; float currAngle, newAngle, rotationTime, typeTime; float throbTime; Interpolate lerpRotation; int point; public Piece(PieceType ptype) { // Initialize connections = new int[numConnections]; active = new boolean[numConnections]; type = ptype; rotationChanged = false; rotationActive = false; currRotation = 0; currAngle = 0; typeChanged = false; typeActive = false; throbTime = 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 (tmp != i && connections[tmp] < 0) { connections[i] = tmp; connections[tmp] = i; } } } } public Piece() { this(PieceType.NONE); } public void setType(PieceType ptype) { typeChanged = (prevType != ptype); prevType = type; type = ptype; } public int getConnection(int in) { return connections[(in + (currRotation * 2)) % 8]; } public void rotate(RotateDir dir) { // Only normal if (type != PieceType.LOCKED && type != PieceType.ACTIVE) return; currRotation = (currRotation + (dir == RotateDir.RIGHT ? 1 : -1)) % 4; newAngle = (float) ((currRotation * Math.PI) / 2.0f); lerpRotation = new Interpolate(newAngle, currAngle, maxTime); rotationChanged = true; } public Point2D getPointCoords(float x, float y, float dim, int index) { float ox = 0, oy = 0; float step = dim / 10; switch (index) { case 0: ox = 3.0f; oy = 0.4f; break; case 1: ox = 7.0f; oy = 0.4f; break; case 2: ox = 9.6f; oy = 3.0f; break; case 3: ox = 9.6f; oy = 7.0f; break; case 4: ox = 7.0f; oy = 9.6f; break; case 5: ox = 3.0f; oy = 9.6f; break; case 6: ox = 0.4f; oy = 7.0f; break; case 7: ox = 0.4f; oy = 3.0f; break; case 8: ox = 3.0f; oy = 3.0f; break; case 9: ox = 7.0f; oy = 3.0f; break; case 10: ox = 7.0f; oy = 3.0f; break; case 11: ox = 7.0f; oy = 7.0f; break; case 12: ox = 7.0f; oy = 7.0f; break; case 13: ox = 7.0f; oy = 7.0f; break; case 14: ox = 3.0f; oy = 7.0f; break; case 15: ox = 3.0f; oy = 3.0f; break; } return new Point2D.Float(x + ox * step, y + oy * step); } public void clearActiveConnections() { for (int i = 0; i < numConnections; i++) active[i] = false; activeChanged = true; } public void setActiveConnection(int index) { active[index] = true; active[connections[index]] = true; activeChanged = true; } public PieceType getType() { return type; } public void animate(float time) { if (rotationChanged) { rotationTime = time; rotationActive = true; rotationChanged = false; } if (rotationActive) { float t = (time - rotationTime) / 2; if (t < maxTime) currAngle = lerpRotation.getValue(t); else { currAngle = newAngle; rotationActive = false; } } if (typeChanged) { typeTime = time; typeActive = true; typeChanged = false; } if (typeActive) { } if (activeChanged) { } throbTime = (time % 100) / 100.0f; } public void paint(Graphics2D g, float x, float y, float dim) { g.rotate(currAngle, x + dim / 2.0f, y + dim / 2.0f); 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.Float(x, y, dim, dim, dim / 10, dim / 10)); g.setPaint(Color.black); g.setStroke(new BasicStroke(5.0f)); g.draw(new RoundRectangle2D.Float(x, y, dim, dim, dim / 10, dim / 10)); if (type == PieceType.START) return; if (type == PieceType.ACTIVE) { float offs1 = throbTime * 10.0f, offs2 = throbTime * 20.0f; g.setPaint(new Color(0.0f, 0.0f, 0.0f, (float) (1.0f - throbTime) )); g.setStroke(new BasicStroke(2.0f + throbTime * 2.0f)); g.draw(new RoundRectangle2D.Float(x - offs1, y - offs1, dim + offs2, dim + offs2, dim / 10, dim / 10)); } g.setStroke(new BasicStroke(5.0f)); CubicCurve2D c = new CubicCurve2D.Float(); // QuadCurve2D c = new QuadCurve2D.Float(); boolean[] drawn = new boolean[numConnections]; for (int i = 0; i < numConnections; i++) if (!drawn[i]) { Point2D start, cp1, cp2, end; boolean isActive = active[i] || active[connections[i]]; g.setPaint(isActive ? Color.white : Color.black); start = getPointCoords(x, y, dim, i); end = getPointCoords(x, y, dim, connections[i]); cp1 = getPointCoords(x, y, dim, i + 8); cp2 = getPointCoords(x, y, dim, connections[i] + 8); c.setCurve(start, cp1, cp2, end); g.draw(c); drawn[i] = true; drawn[connections[i]] = true; } } }