annotate game/Engine.java @ 47:695cf13c103a

Move some code.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Feb 2011 07:19:26 +0200
parents 3e8d1c30f573
children f13bab4cccd3
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.*;
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
18 import javax.sound.sampled.*;
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
19
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
20
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
21 class PathInfo
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
22 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
23 public int inPoint, inX, inY, outPoint, outX, outY;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
24
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
25 public PathInfo(int inPoint, int inX, int inY, int outPoint, int outX, int outY)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
26 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
27 this.inPoint = inPoint;
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
28 this.inX = inX;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
29 this.inY = inY;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
30
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
31 this.outPoint = outPoint;
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
32 this.outX = outX;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
33 this.outY = outY;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
34 }
27
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
35
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
36 public void print()
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
37 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
38 System.out.print("PathInfo: inP="+inPoint+", inX="+inX+", inY="+inY+"\n");
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
39 System.out.print(" outP="+outPoint+", outX="+outX+", outY="+outY+"\n\n");
27
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
40 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
41 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
43 /*
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
44 class AnimatedElement
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
45 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
46 float x, y, stime, value;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
47 Interpolate lerp;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
48 boolean active;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
49
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
50 public AnimatedElement(float x, float y, )
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
51 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
52 stime = 0;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
53 this.x = x;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
54 this.y = y;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
55
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
56 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
57
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
58 public animate(float time)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
59 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
60 if (!active)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
61 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
62 active = true;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
63 stime = time;
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 float t = (time - stime) / 10.0f;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
67 if (t < 100)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
68 value = lerp.getValue(t);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
69 else
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 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
73 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
74
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
75 public paint(Graphics2D g, );
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
76 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
77 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
78 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
79 */
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
81 class IDMWidget
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
82 {
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
83 public IDMWidget()
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
84 {
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
85 }
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
86
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
87 public void paint(Graphics2D g)
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
88 {
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
89 }
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
90
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
91 public boolean contains(float x, float y)
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
92 {
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
93 return false;
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
94 }
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
95
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
96 public void clicked()
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
97 {
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
98 }
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
99 }
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
100
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
101 class IDMButton
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
102 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
103 enum State { FOCUSED, PRESSED, NORMAL }
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
104 State state;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
105 BufferedImage imgUp, imgPressed, img;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
106 Point pos;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
107
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
108 public IDMButton(float x, float y, String text)
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
109 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
110 try
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
111 {
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
112 ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
113 imgUp = ImageIO.read(res.getStream());
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
114
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
115 res = new ResourceLoader("graphics/button1_down.png");
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
116 imgPressed = ImageIO.read(res.getStream());
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
117 }
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
118 catch (IOException e)
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
119 {
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
120 System.out.print(e.getMessage());
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
121 }
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
122
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
123 setState(State.NORMAL);
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
124 }
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
125
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
126 private void setState(State newState)
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
127 {
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
128 state = newState;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
129 if (state == State.PRESSED)
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
130 img = imgPressed;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
131 else
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
132 img = imgUp;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
133 }
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
134
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
135 public void paint(Graphics2D g)
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
136 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
137 g.drawImage(img, pos.x, pos.y, null);
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
138 }
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
139
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
140 public boolean contains(float x, float y)
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
141 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
142 return (x >= pos.x && y >= pos.y &&
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
143 x < pos.x + img.getWidth() &&
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
144 y < pos.y + img.getHeight());
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
145 }
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
146
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
147 public void clicked()
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
148 {
30
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
149 }
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
150 }
60a4579a79df Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
151
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
152
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
153 class GameBoard
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 {
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
155 public static final int boardSize = 9;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
156 public static final int boardMiddle = 4;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
157 Piece[][] board;
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
158
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
159 public boolean flagGameOver;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
160
41
a5fd4f74a767 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
161 Piece currPiece;
a5fd4f74a767 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
162 int currX, currY, currPoint;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
163
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
164 SoundManager soundManager;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
165 Sound sndPlaced;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
166
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
167 public GameBoard(SoundManager smgr)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
168 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
169 board = new Piece[boardSize][boardSize];
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
170
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
171 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
172
41
a5fd4f74a767 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
173 currX = boardMiddle;
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
174 currY = boardMiddle;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
175 currPoint = 0;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
176
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
177 pieceFinishTurn();
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
178
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
179 flagGameOver = false;
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
180
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
181 soundManager = smgr;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
182 sndPlaced = soundManager.getSound("sounds/placed.wav");
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
183 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
185 public void paint(Graphics2D g, int sx, int sy, float scale)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
186 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
187 for (int y = 0; y < boardSize; y++)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
188 for (int x = 0; x < boardSize; x++)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
189 if (board[x][y] != null)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
190 {
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
191 AffineTransform save = g.getTransform();
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
192
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
193 board[x][y].paint(g,
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
194 sx + (x * scale),
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
195 sy + (y * scale),
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
196 scale - scale / 10);
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
197
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
198 g.setTransform(save);
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
199 }
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
200 }
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
201
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
202 public void animate(float time)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
203 {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
204 for (int y = 0; y < boardSize; y++)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
205 for (int x = 0; x < boardSize; x++)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
206 if (board[x][y] != null)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
207 {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
208 board[x][y].animate(time);
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
209 }
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
210
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
211 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
212
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
213 private boolean isEmpty(int x, int y)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
214 {
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
215 return (x >= 0 && x < boardSize && y >= 0 &&
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
216 y < boardSize && board[x][y] == null);
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
217 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
218
32
e480579cc460 More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
219 public void pieceRotate(Piece.RotateDir dir)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
220 {
41
a5fd4f74a767 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
221 if (currPiece != null)
a5fd4f74a767 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
222 currPiece.rotate(dir);
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
223 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
224
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
225 private void pieceMoveTo(int point)
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
226 {
46
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
227 switch (point)
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
228 {
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
229 case 0: currY--; break;
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
230 case 1: currY--; break;
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
231
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
232 case 2: currX++; break;
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
233 case 3: currX++; break;
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
234
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
235 case 4: currY++; break;
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
236 case 5: currY++; break;
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
237
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
238 case 6: currX--; break;
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
239 case 7: currX--; break;
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
240 }
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
241 }
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
242
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
243 public void pieceFinishTurn()
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
244 {
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
245 while (true)
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
246 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
247 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize)
27
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
248 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
249 Piece curr = board[currX][currY];
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
250
27
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
251 if (curr == null)
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
252 {
46
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
253 // Create new piece
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
254 currPiece = new Piece(PieceType.ACTIVE);
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
255 board[currX][currY] = currPiece;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
256 return;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
257 }
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
258 else
27
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
259 if (curr.getType() == PieceType.START)
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
260 {
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
261 if (currPiece != null)
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
262 {
46
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
263 // Hit center starting piece, game over
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
264 flagGameOver = true;
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
265 currPiece = null;
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
266 System.out.print("GameOver!\n");
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
267 break;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
268 }
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
269 else
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
270 {
44
698c6bcc4aec Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
271 // Start piece as first piece means game is starting
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
272 pieceMoveTo(currPoint);
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
273 currPiece = new Piece(PieceType.ACTIVE);
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
274 board[currX][currY] = currPiece;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
275 return;
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
276 }
27
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
277 }
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
278 else
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
279 {
43
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
280 // Mark the current piece as locked
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
281 curr.setType(PieceType.LOCKED);
43
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
282
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
283 // Solve connection (with rotations) through the piece
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
284 currPoint = curr.getRotatedPoint(curr.getMatchingPoint(currPoint));
43
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
285
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
286 // Mark connection as active
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
287 curr.setConnectionState(currPoint, true);
27
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
288
43
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
289 // Solve exit point (with rotations)
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
290 currPoint = curr.getAntiRotatedPoint(curr.getConnection(currPoint));
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
291
9664bbb9d613 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
292 // Move to next position accordingly
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
293 pieceMoveTo(currPoint);
27
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
294 }
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
295 }
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
296 else
26adc2827983 More work on the internals.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
297 {
46
3e8d1c30f573 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
298 // Outside of the board, game over
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
299 flagGameOver = true;
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
300 currPiece = null;
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
301 System.out.print("GameOver!\n");
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
302 break;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
303 }
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
304 }
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
305
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
306 }
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
307
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
308 public boolean keyHandler(KeyEvent e)
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
309 {
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
310 switch (e.getKeyCode())
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
311 {
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
312 case KeyEvent.VK_LEFT:
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
313 case KeyEvent.VK_UP:
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
314 pieceRotate(Piece.RotateDir.LEFT);
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
315 return true;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
316
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
317 case KeyEvent.VK_RIGHT:
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
318 case KeyEvent.VK_DOWN:
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
319 pieceRotate(Piece.RotateDir.RIGHT);
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
320 return true;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
321
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
322 case KeyEvent.VK_ENTER:
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
323 soundManager.play(sndPlaced);
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
324 pieceFinishTurn();
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
325 return true;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
326 }
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
327 return false;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
328 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
329 }
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
330
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
331
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
332 public class Engine extends JPanel
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
333 implements Runnable, KeyListener, MouseListener
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
334 {
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
335 long startTime;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
336 float gameClock, gameFrames;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
337 Thread animThread;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
338 boolean animEnable = false;
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
339
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
340 Font fontMain, font1, font2;
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
341 GameBoard lauta = null;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
342 BufferedImage lautaBG = null, lautaBGScaled = null;
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
343 Dimension lautaDim;
22
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
344
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
345
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
346 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false);
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
347 SoundManager soundManager;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
348 Sound musa;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
349
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
350 public Engine()
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
351 {
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
352 // Initialize globals
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
353 System.out.print("Engine() constructor\n");
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
354
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
355 gameClock = 0;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
356 gameFrames = 0;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
357 startTime = new Date().getTime();
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
358
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
359 // Sound system
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
360 soundManager = new SoundManager(sfmt, 16);
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
361
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
362 // Load resources
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
363 try
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
364 {
24
1be98362e5e9 FPS measurement.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
365 ResourceLoader res = new ResourceLoader("graphics/board.jpg");
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
366 lautaBG = ImageIO.read(res.getStream());
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
367
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
368 try {
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
369 res = new ResourceLoader("graphics/font.ttf");
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
370 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream());
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
371 font1 = fontMain.deriveFont(24f);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
372 font2 = fontMain.deriveFont(32f);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
373 }
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
374 catch (FontFormatException e)
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
375 {
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
376 System.out.print("Could not initialize fonts.\n");
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
377 }
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
378
42
951a4d669af0 Initially working path solving algorithm.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
379 // musa = smgr.getSound("sounds/gamemusic.wav");
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
380 }
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
381 catch (IOException e)
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
382 {
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
383 JOptionPane.showMessageDialog(null,
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
384 e.getMessage(),
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
385 "Initialization error",
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
386 JOptionPane.ERROR_MESSAGE);
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
387
21
df494a65bf8c More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
388 System.out.print(e.getMessage());
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
389 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
390
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
391 // Initialize game components
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
392 lauta = new GameBoard(soundManager);
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
393 addKeyListener(this);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
394 addMouseListener(this);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
395
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
396 // Get initial focus
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
397 if (!hasFocus())
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
398 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
399 System.out.print("Engine(): requesting focus\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
400 requestFocus();
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
401 }
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
402
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
403 soundManager.play(musa);
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
404 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
405
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
406
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
407 public void paintComponent(Graphics g)
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
408 {
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
409 Graphics2D g2 = (Graphics2D) g;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
410
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
411 // Use antialiasing when rendering the game elements
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
412 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
413 RenderingHints.VALUE_ANTIALIAS_ON);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
414
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
415 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
416 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
417
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
418 // Rescale background if component size has changed
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
419 Dimension dim = getSize();
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
420 if (lautaDim == null || !dim.equals(lautaDim))
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
421 {
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
422 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
423 Graphics2D gimg = lautaBGScaled.createGraphics();
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
424 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
425 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
426
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
427 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
428 lautaDim = dim;
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
429
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
430 System.out.print("scale changed\n");
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
431 }
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
432
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
433 // Background, pieces
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
434 g2.drawImage(lautaBGScaled, 0, 0, null);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
435 lauta.paint(g2, 100, 150, 60);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
436
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
437 // Scores
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
438 g2.setFont(font1);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
439 g2.setPaint(Color.white);
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
440
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
441
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
442 // Other elements
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
443 long currTime = new Date().getTime();
32
e480579cc460 More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
444 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20);
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
445 }
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
446
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
447 public void startThreads()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
448 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
449 System.out.print("startThreads()\n");
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
450 if (animThread == null)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
451 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
452 animThread = new Thread(this);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
453 animEnable = true;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
454 animThread.start();
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
455 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
456 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
457
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
458 public void stopThreads()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
459 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
460 System.out.print("stopThreads()\n");
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
461
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
462 // Stop animations
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
463 if (animThread != null)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
464 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
465 animThread.interrupt();
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
466 animEnable = false;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
467 animThread = null;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
468 }
22
afde253ec705 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
469
37
3dc5ae9f1c80 Work on game logic.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
470 // Shut down sound manager
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
471 soundManager.close();
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
472 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
474 public void mousePressed(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
475 public void mouseEntered(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
476 public void mouseExited(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
477 public void mouseReleased(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
478
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
479 public void mouseClicked(MouseEvent e)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
480 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
481 System.out.print("mouseClicked()\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
482 if (!hasFocus())
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
483 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
484 System.out.print("requesting focus\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
485 requestFocus();
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
486 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
487 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
488
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
489 public void keyTyped(KeyEvent e)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
490 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
491 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
492
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
493 public void keyReleased(KeyEvent e)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
494 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
495 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
496
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
497 public void keyPressed(KeyEvent e)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
498 {
26
3d4cc47df31a Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
499 // Handle keyboard input
47
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
500 if (lauta.keyHandler(e))
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
501 return;
695cf13c103a Move some code.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
502
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
503 switch (e.getKeyCode())
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
504 {
35
aa15b2c556b4 Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
505 case KeyEvent.VK_ESCAPE:
aa15b2c556b4 Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
506 break;
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
507 }
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
508 }
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
509
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
510 public void run()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
511 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
512 while (animEnable)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
513 {
26
3d4cc47df31a Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
514 // Progress game animation clock
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
515 gameClock++;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
516
26
3d4cc47df31a Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
517 // Animate components
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
518 lauta.animate(gameClock);
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
519
26
3d4cc47df31a Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
520 // Repaint with a frame limiter
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
521 if (gameClock % 3 == 1)
24
1be98362e5e9 FPS measurement.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
522 {
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
523 repaint();
25
bbac3e4a4b9b Cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
524 gameFrames++;
24
1be98362e5e9 FPS measurement.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
525 }
32
e480579cc460 More work on debugging the path resolving.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
526
26
3d4cc47df31a Cleanups, fix piece rendering and rotation.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
527 // Sleep for a moment
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
528 try {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
529 Thread.sleep(10);
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
530 }
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
531 catch (InterruptedException x) {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
532 }
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
533 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
534 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 }