annotate game/Engine.java @ 25:bbac3e4a4b9b

Cleanups, etc.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 01 Feb 2011 18:05:19 +0200
parents 1be98362e5e9
children 3d4cc47df31a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
2 * Ristipolku Game Engine
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 */
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
5 package game;
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
6
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 import java.awt.*;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 import java.awt.geom.*;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
9 import java.awt.event.*;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
10 import java.awt.image.*;
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
11 import java.awt.event.*;
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
12 import java.awt.font.*;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
13 import javax.imageio.*;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
14 import javax.swing.*;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
15 import java.util.*;
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
16 import java.io.*;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
17 import game.*;
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
18
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
19
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
20 class PathInfo
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
21 {
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
22 public int in, inX, inY, out, outX, outY;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
23
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
24 public PathInfo(int in, int inX, int inY, int out, int outX, int outY)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
25 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
26 this.in = in;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
27 this.inX = inX;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
28 this.inY = inY;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
29
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
30 this.out = out;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
31 this.outX = outX;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
32 this.outY = outY;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
33 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
34 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
36 /*
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
37 class AnimatedElement
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
38 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
39 float x, y, stime, value;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
40 Interpolate lerp;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
41 boolean active;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
42
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
43 public AnimatedElement(float x, float y, )
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
44 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
45 stime = 0;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
46 this.x = x;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
47 this.y = y;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
48
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
49 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
50
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
51 public animate(float time)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
52 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
53 if (!active)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
54 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
55 active = true;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
56 stime = time;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
57 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
58
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
59 float t = (time - stime) / 10.0f;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
60 if (t < 100)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
61 value = lerp.getValue(t);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
62 else
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
63 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
64
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
65 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
66 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
67
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
68 public paint(Graphics2D g, );
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
69 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
70 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
71 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
72 */
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
74 class GameBoard
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 {
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
76 public static final int boardSize = 9;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
77 public static final int boardMiddle = 4;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
78 Piece[][] board;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
79 Piece current;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
80 public boolean flagGameOver;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
81
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
82 int moveX, moveY, movePoint;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
83
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
84 public GameBoard()
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
85 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
86 board = new Piece[boardSize][boardSize];
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
87
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
88 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
89
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
90 moveX = boardMiddle;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
91 moveY = boardMiddle - 1;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
92 movePoint = 0;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
93
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
94 pieceFinishTurn();
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
95
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
96 flagGameOver = false;
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
97 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
99 public void paint(Graphics2D g, int sx, int sy, float scale)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
100 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
101 for (int y = 0; y < boardSize; y++)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
102 for (int x = 0; x < boardSize; x++)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
103 if (board[x][y] != null)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
104 {
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
105 AffineTransform save = g.getTransform();
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
106
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
107 board[x][y].paint(g,
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
108 sx + (x * scale),
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
109 sy + (y * scale),
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
110 scale - scale / 10);
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
111
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
112 g.setTransform(save);
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
113 }
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
114 }
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
115
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
116 public void animate(float time)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
117 {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
118 for (int y = 0; y < boardSize; y++)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
119 for (int x = 0; x < boardSize; x++)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
120 if (board[x][y] != null)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
121 {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
122 board[x][y].animate(time);
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
123 }
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
124
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
125 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
126
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
127 private boolean isEmpty(int x, int y)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
128 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
129 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
130 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
131
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
132 private Piece getPiece(int x, int y)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
133 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
134 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
135 return board[x][y];
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
136 else
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
137 return null;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
138 }
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
139
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
140 public void pieceRotate(boolean dir)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
141 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
142 if (current != null)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
143 current.rotate(dir);
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
144 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
145
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
146 public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark)
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
147 {
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
148 int x = startX, y = startY;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
149 int point = -1;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
150
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
151 Piece curr = getPiece(startX, startY);
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
152 if (curr == null)
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
153 return null;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
154
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
155 /*
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
156 while (curr != null)
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
157 {
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
158 // curr.(true);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
159 // elements.spawn("", );
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
160 }
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
161 */
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
162
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
163 return new PathInfo(startPoint, startX, startY, point, x, y);
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
164 }
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
165
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
166 public void pieceFinishTurn()
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
167 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
168 if (current != null)
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
169 {
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
170 current.setType(PieceType.LOCKED);
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
171 PathInfo i = resolvePath(moveX, moveY, movePoint, true);
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
172
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
173 if (i != null)
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
174 {
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
175 }
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
176 }
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
177
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
178 current = new Piece(PieceType.ACTIVE);
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
179 if (isEmpty(moveX, moveY))
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
180 {
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
181 board[moveX][moveY] = current;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
182 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
183 else
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
184 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
185 PathInfo i = resolvePath(moveX, moveY, movePoint, true);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
186 if (i != null)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
187 board[moveX][moveY] = current;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
188 else
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
189 flagGameOver = true;
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
190 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
191 }
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
192 }
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
193
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
194
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
195 public class Engine extends JPanel
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
196 implements Runnable, KeyListener, MouseListener
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
197 {
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
198 long startTime;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
199 float gameClock, gameFrames;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
200 Thread animThread;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
201 boolean animEnable = false;
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
202
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
203 Font fontMain, font1, font2;
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
204 GameBoard lauta = null;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
205 BufferedImage lautaBG = null, lautaBGScaled = null;
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
206 Dimension lautaDim;
22
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
207
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
208 SoundElement[] sounds;
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
209
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
210 public SoundElement snd(Sound snd)
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
211 {
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
212 return sounds[snd.ordinal()];
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
213 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
214
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
215 public Engine()
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
216 {
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
217 // Initialize globals
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
218 System.out.print("Engine() constructor\n");
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
219
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
220 gameClock = 0;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
221 gameFrames = 0;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
222 startTime = new Date().getTime();
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
223
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
224
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
225 // Load resources
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
226 try
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
227 {
24
1be98362e5e9 FPS measurement.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
228 ResourceLoader res = new ResourceLoader("graphics/board.jpg");
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
229 lautaBG = ImageIO.read(res.getStream());
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
230
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
231 try {
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
232 res = new ResourceLoader("graphics/font.ttf");
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
233 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream());
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
234 font1 = fontMain.deriveFont(24f);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
235 font2 = fontMain.deriveFont(32f);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
236 }
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
237 catch (FontFormatException e)
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
238 {
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
239 System.out.print("Could not initialize fonts.\n");
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
240 }
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
241
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
242 sounds = new SoundElement[16];
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
243 for (Sound s : Sound.values())
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
244 {
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
245 System.out.print(s +" = "+ s.ordinal() +"\n");
23
0741dc117808 Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
246 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isStreaming());
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
247 }
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
248 }
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
249 catch (IOException e)
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
250 {
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
251 /*
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
252 JOptionPane.showMessageDialog(null,
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
253 e.getMessage(),
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
254 "Initialization error",
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
255 JOptionPane.ERROR_MESSAGE);
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
256 */
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
257 System.out.print(e.getMessage());
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
258 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
259
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
260 lauta = new GameBoard();
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
261 addKeyListener(this);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
262 addMouseListener(this);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
263
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
264 // Get initial focus
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
265 if (!hasFocus())
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
266 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
267 System.out.print("Engine(): requesting focus\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
268 requestFocus();
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
269 }
22
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
270
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
271 snd(Sound.MUSIC_GAME1).loop(-1);
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
272 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
273
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
274
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
275 public void paintComponent(Graphics g)
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
276 {
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
277 Graphics2D g2 = (Graphics2D) g;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
278
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
279 // Use antialiasing when rendering the game elements
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
280 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
281 RenderingHints.VALUE_ANTIALIAS_ON);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
282
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
283 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
284 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
285
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
286 // Rescale background if component size has changed
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
287 Dimension dim = getSize();
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
288 if (lautaDim == null || !dim.equals(lautaDim))
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
289 {
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
290 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
291 Graphics2D gimg = lautaBGScaled.createGraphics();
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
292 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
293 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
294
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
295 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
296 lautaDim = dim;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
297
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
298 System.out.print("scale changed\n");
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
299 }
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
300
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
301 // Background, pieces
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
302 g2.drawImage(lautaBGScaled, 0, 0, null);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
303 lauta.paint(g2, 100, 150, 60);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
304
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
305 // Scores
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
306 g2.setFont(font1);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
307 g2.setPaint(Color.white);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
308
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
309
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
310 // Other elements
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
311 long currTime = new Date().getTime();
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
312 g2.drawString("fps = "+ ((currTime - startTime) / gameFrames), dim.width - 120, 20);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
313 }
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
314
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
315 public void startThreads()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
316 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
317 System.out.print("startThreads()\n");
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
318 if (animThread == null)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
319 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
320 animThread = new Thread(this);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
321 animEnable = true;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
322 animThread.start();
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
323 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
324 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
325
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
326 public void stopThreads()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
327 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
328 System.out.print("stopThreads()\n");
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
329 if (animThread != null)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
330 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
331 animThread.interrupt();
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
332 animEnable = false;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
333 animThread = null;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
334 }
22
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
335
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
336 for (Sound s : Sound.values())
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
337 {
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
338 if (snd(s) != null)
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
339 snd(s).stop();
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
340 }
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
341 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
343 public void mousePressed(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
344 public void mouseEntered(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
345 public void mouseExited(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
346 public void mouseReleased(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
347
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
348 public void mouseClicked(MouseEvent e)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
349 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
350 System.out.print("mouseClicked()\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
351 if (!hasFocus())
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
352 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
353 System.out.print("requesting focus\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
354 requestFocus();
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
355 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
356 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
357
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
358 public void keyTyped(KeyEvent e)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
359 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
360 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
361
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
362 public void keyReleased(KeyEvent e)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
363 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
364 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
365
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
366 public void keyPressed(KeyEvent e)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
367 {
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
368 switch (e.getKeyCode())
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
369 {
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
370 case KeyEvent.VK_LEFT:
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
371 case KeyEvent.VK_UP:
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
372 lauta.pieceRotate(false);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
373 break;
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
375 case KeyEvent.VK_RIGHT:
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
376 case KeyEvent.VK_DOWN:
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
377 lauta.pieceRotate(true);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
378 break;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
379
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
380 case KeyEvent.VK_ENTER:
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
381 lauta.pieceFinishTurn();
23
0741dc117808 Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
382 snd(Sound.PIECE_PLACED).stop();
0741dc117808 Remove sound code for later refactoring.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
383 snd(Sound.PIECE_PLACED).play();
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
384 break;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
385 }
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
386 }
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
387
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
388 public void run()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
389 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
390 while (animEnable)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
391 {
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
392 gameClock++;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
393
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
394 lauta.animate(gameClock);
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
395
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
396 if (gameClock % 3 == 1)
24
1be98362e5e9 FPS measurement.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
397 {
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
398 repaint();
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
399 gameFrames++;
24
1be98362e5e9 FPS measurement.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
400 }
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
401
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
402 try {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
403 Thread.sleep(10);
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
404 }
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
405
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
406 catch (InterruptedException x) {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
407 }
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
408 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
409 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 }