annotate game/Engine.java @ 55:974ec36c562e

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