Source code: jdstar/Komander.java
1 /*
2 * Copyright (C) 1999-2001 Max Gilead <gilead@linart.pl>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 package jdstar;
19
20 import huf.io.FileFilter;
21 import javax.swing.JLabel;
22 import javax.swing.JFileChooser;
23 import javax.swing.JFrame;
24 import javax.swing.JOptionPane;
25 import javax.swing.JScrollPane;
26 import javax.swing.JTextArea;
27 import java.awt.Dimension;
28 import java.awt.Font;
29 import java.io.IOException;
30 import java.io.File;
31 import java.io.InputStream;
32 import java.io.FileInputStream;
33 import java.io.BufferedInputStream;
34 import java.io.FileNotFoundException;
35
36 class Komander
37 {
38 private Board board = null;
39 private Input input = null;
40 private Display display = null;
41 private JFrame frame = null;
42 private JLabel status = null;
43 private GameLoop loop = null;
44
45 Komander(Board b, Input i, Display d, GameLoop l, JLabel s, JFrame f)
46 {
47 board = b;
48 input = i;
49 display = d;
50 loop = l;
51 status = s;
52 frame = f;
53 }
54
55 void k_null_atStart()
56 {
57 int _ = Board.EMPTY;
58 int X = Board.WALL;
59 int p = Board.PLAYER;
60 int b = Board.BUDDY;
61 int i = Board.ITEM;
62 board.set(new int[][] {
63 { _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ },
64 { _, X, X, _, X, X, _, _, _, X, X, _, _, X, _, _, _, _, _, _, _, _, _ },
65 { _, _, X, _, X, i, X, _, X, _, _, _, _, X, _, _, _, _, _, _, _, _, _ },
66 { _, _, X, _, X, i, X, _, X, _, _, _, X, X, X, _, X, X, _, _, _, X, _ },
67 { _, _, X, _, X, i, X, _, _, X, _, _, _, X, _, _, _, _, X, _, X, _, _ },
68 { _, _, X, _, X, i, X, _, _, _, X, _, _, X, _, _, _, X, X, _, X, _, _ },
69 { _, _, X, _, X, i, X, _, _, _, X, _, _, X, _, _, X, i, X, _, X, _, _ },
70 { _, X, _, _, X, X, _, _, X, X, _, _, _, _, X, _, X, X, X, _, X, _, _ },
71 { p, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, b } });
72 //loop.start(); // will start far too long with game loop running
73 display.repaint();
74 }
75 void k_null_board_finished()
76 {
77 // positioning player in the middle of the tile
78 board.player.X = board.player.x + 0.5;
79 board.player.Y = board.player.y + 0.5;
80 loop.stop();
81 JOptionPane.showMessageDialog(null,
82 "Congratulations! You have finished this board!",
83 "Ypieee!!!", JOptionPane.INFORMATION_MESSAGE);
84 if (!Board.NO_NEXT_LEVEL.equals(board.nextLevel))
85 {
86 try
87 {
88 board.load(JDStar.LEVELS_DIRECTORY + board.nextLevel);
89 display.refreshBoard();
90 display.revalidate();
91 frame.setTitle("JDStar "+ JDStar.VERSION +": "+ board.nextLevel);
92 status.setText("Board loaded");
93 status.revalidate();
94 frame.pack();
95 }
96 catch(InvalidBoardException ibe)
97 {
98 JOptionPane.showMessageDialog(null, ibe.getMessage(), "Unable to load board",
99 JOptionPane.ERROR_MESSAGE);
100 k_null_atStart();
101 }
102 display.repaint();
103 loop.start();
104 }
105 }
106
107 void k_game_restart()
108 {
109 board.resetBoard();
110 display.refreshBoard();
111 display.revalidate();
112 status.setText("Board restarted");
113 status.revalidate();
114 loop.start();
115 }
116 void k_game_load_board()
117 {
118 loop.stop();
119
120 JFileChooser chooser = new JFileChooser(JDStar.LEVELS_DIRECTORY);
121 FileFilter filterJDS = new FileFilter(new String[] { "ds" }, "JDStar boards");
122 chooser.addChoosableFileFilter(filterJDS);
123 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
124 int returnVal = chooser.showOpenDialog(null);
125 if (returnVal == JFileChooser.APPROVE_OPTION)
126 {
127 //System.out.println("Loading board "+ chooser.getSelectedFile().getName());
128 try
129 {
130 board.load(chooser.getSelectedFile().getAbsolutePath());
131 display.refreshBoard();
132 display.revalidate();
133 frame.setTitle("JDStar "+ JDStar.VERSION +": "+ chooser.getSelectedFile().getName());
134 status.setText("Board loaded");
135 status.revalidate();
136 frame.pack();
137 }
138 catch(InvalidBoardException ibe)
139 {
140 JOptionPane.showMessageDialog(null, ibe.getMessage(), "Unable to load board",
141 JOptionPane.ERROR_MESSAGE);
142 k_null_atStart();
143 }
144 display.repaint();
145 loop.start();
146 }
147 }
148
149 void k_tilesets_loadTileset(Tileset t)
150 {
151 board.tileset = t;
152 board.player.refreshBoard();
153 board.buddy.refreshBoard();
154 display.refreshBoard();
155 display.repaint();
156 }
157
158 void k_help_rules()
159 {
160 JOptionPane.showMessageDialog(null,
161 "The goal is to collect all the items with your\n"+
162 "collector object (called 'player'). You can use the\n"+
163 "obstacle object (called 'buddy') to help you out when\n"+
164 "needed. Use the keyboard or mouse to move the current\n"+
165 "object and the special key to toggle control between\n"+
166 "player and buddy.\n"+
167 "\n"+
168 "Game is very simple so it's best to simply experiment\n"+
169 "by playing a little. Use Game|Load board from the menu\n"+
170 "to try different levels. See if you can do them all!\n",
171 "Game rules", JOptionPane.INFORMATION_MESSAGE);
172 }
173 void k_help_general()
174 {
175 File f = new File(JDStar.README_FILE);
176 FileInputStream fin = null;
177 try
178 {
179 fin = new FileInputStream(f);
180 }
181 catch(FileNotFoundException fnfe)
182 {
183 // not internal because this file could have been deleted in the meantime
184 throw new CriticalException("README file not found");
185 }
186 BufferedInputStream bin = new BufferedInputStream(fin);
187
188 String help = null;
189 try
190 {
191 help = readString(bin);
192 }
193 catch(IOException ioe)
194 {
195 // not internal because this file could have been deleted in the meantime
196 throw new CriticalException("README file unreadable");
197 }
198 JTextArea text = new JTextArea(help);
199 text.setEditable(false);
200 text.setFont(new Font("Monospaced", Font.PLAIN, 12));
201 text.setCaretPosition(0);
202
203 JScrollPane scroll = new JScrollPane(text);
204 scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
205 scroll.setPreferredSize(new Dimension(500, 300));
206 scroll.setMinimumSize(new Dimension(300, 100));
207 JOptionPane.showMessageDialog(null, scroll, "General help", JOptionPane.INFORMATION_MESSAGE);
208 }
209 void k_help_about()
210 {
211 JOptionPane.showMessageDialog(null,
212 "JDStar version "+ JDStar.VERSION +"\n"+
213 "\n"+
214 "Copyright (C) 2001 Max Gilead <gilead@sf.net>\n"+
215 "\n"+
216 "\n"+
217 "This program is free software; you can redistribute it and/or\n"+
218 "modify it under the terms of the GNU General Public License\n"+
219 "as published by the Free Software Foundation; either version 2\n"+
220 "of the License, or (at your option) any later version.\n"+
221 "\n"+
222 "This program is distributed in the hope that it will be useful,\n"+
223 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"+
224 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"+
225 "GNU General Public License for more details.\n"+
226 "\n"+
227 " You should have received a copy of the GNU General Public License\n"+
228 "along with this program; if not, write to the Free Software\n"+
229 "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n",
230 "About", JOptionPane.INFORMATION_MESSAGE);
231 }
232
233 private String readString(InputStream in)
234 throws IOException
235 {
236 StringBuffer s = new StringBuffer();
237 int b = 0;
238 while( (b = in.read()) != -1)
239 s.append((char)b); // inefficient but should be OK for such short files
240 return new String(s);
241 }
242 }
243