comparison game/Piece.java @ 3:b8fd19e2d879

Indentation.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Jan 2011 16:59:36 +0200
parents 1785f66a7beb
children 4890020bf856
comparison
equal deleted inserted replaced
2:1785f66a7beb 3:b8fd19e2d879
11 import java.util.*; 11 import java.util.*;
12 12
13 13
14 public class Piece 14 public class Piece
15 { 15 {
16 static final int numConnections = 8; 16 static final int numConnections = 8;
17 static final int minRotation = 0; 17 static final int minRotation = 0;
18 static final int maxRotation = 3; 18 static final int maxRotation = 3;
19 int currRotation; 19 int currRotation;
20 int[] connections; 20 int[] connections;
21 PieceType type, oldType; 21 PieceType type, oldType;
22 22
23 boolean rotationChanged, rotationActive, 23 boolean rotationChanged, rotationActive,
24 typeChanged, typeActive; 24 typeChanged, typeActive;
25 double currAngle, newAngle; 25 double currAngle, newAngle;
26 26
27 public Piece(PieceType ptype)
28 {
29 // Initialize
30 connections = new int[numConnections];
31 type = ptype;
32
33 rotationChanged = false;
34 typeChanged = false;
35 rotationActive = false;
36 typeActive = false;
37
38 currRotation = 0;
39 currAngle = 0;
40
41
42 // Initialize connections between endpoints of the paths inside the piece
43 for (int i = 0; i < numConnections; i++)
44 connections[i] = -1;
45
46 Random rnd = new Random();
47 for (int i = 0; i < numConnections; i++)
48 {
49 while (connections[i] < 0)
50 {
51 int tmp = rnd.nextInt(numConnections);
52 if (connections[tmp] < 0)
53 {
54 connections[i] = tmp;
55 connections[tmp] = i;
56 }
57 }
58 }
59 }
60
61 public Piece()
62 {
63 this(PieceType.NONE);
64 }
65
66 public void setType(PieceType ptype)
67 {
68 typeChanged = (oldType != ptype);
69 oldType = type;
70 type = ptype;
71 }
72
73 public int getConnection(int in)
74 {
75 return connections[in];
76 }
27 77
28 public Piece(PieceType ptype) 78 public void rotate(boolean dir)
29 { 79 {
30 // Initialize 80 if (type != PieceType.NORMAL)
31 connections = new int[numConnections]; 81 return;
32 type = ptype;
33 82
34 rotationChanged = false; 83 newRotation = currRotation + (dir ? 1 : -1);
35 typeChanged = false;
36 rotationActive = false;
37 typeActive = false;
38
39 84
40 currRotation = 0; 85 if (newRotation < minRotation)
86 newRotation = maxRotation;
87 else if (currRotation > maxRotation)
88 newRotation = minRotation;
41 89
42 // Initialize connections between endpoints of the paths inside the piece 90 rotationDir = dir;
43 for (int i = 0; i < numConnections; i++) 91 rotationChanged = true;
44 connections[i] = -1; 92 }
45
46 Random rnd = new Random();
47 for (int i = 0; i < numConnections; i++)
48 {
49 int tmp = rnd.nextInt(numConnections);
50 if (connections[tmp] < 0 && connections[i] < 0)
51 {
52 connections[i] = tmp;
53 connections[tmp] = i;
54 }
55 }
56 }
57
58 public Piece()
59 {
60 this(PieceType.NONE);
61 }
62
63 public void setType(PieceType ptype)
64 {
65 oldType = type;
66 type = ptype;
67 }
68
69 public int getConnection(int in)
70 {
71 return connections[in];
72 }
73
74 public void rotate(boolean dir)
75 {
76 if (type != PieceType.NORMAL)
77 return;
78
79 newRotation = currRotation + (dir ? 1 : -1);
80
81 if (newRotation < minRotation)
82 newRotation = maxRotation;
83 else if (currRotation > maxRotation)
84 newRotation = minRotation;
85
86 rotationDir = dir;
87 rotationChanged = true;
88 }
89 93
90 public Point2D getPoint(double x, double y, double dim, int index) 94 public Point2D getPoint(double x, double y, double dim, int index)
91 { 95 {
92 double ox = 0, oy = 0; 96 double ox = 0, oy = 0;
93 double step = dim / 10; 97 double step = dim / 10;