/** * @(#)StoryLine.java 1.0.1 98/12/04 *
* Copyright (C) 1998 David E. Wexler *
* This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. See * license.txt for a full copy of the GNU GPL. *
* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. *
* To Contact the author send e-mail to vagabond@netdragon.com or send * snailmail to: 511 Bobcat Ct, Punta Gorda, FL 3398. */ import java.applet.Applet; import java.awt.*; import java.io.*; import java.lang.*; import java.net.*; import java.util.*; /** * StoryLine - Downloads and displays Information dealing with a plot line. *
* This Class simple makes a request for a file from the weapon, downloads * it and stores the information to be displayed. *
* @author David Wexler (vagabond@netdragons.com)
* @version 1.0.1
* @since Nelzan1.0.1
*/
public class StoryLine {
/**
* Type of Information the line is.
*/
private int[] type;
/**
* Information to be displayed.
*/
private String[] data;
/**
* Font to be Used.
*/
private Font font = new Font("Helvetica", Font.BOLD, 14);
/**
* Intance of Applet, for call backs.
*/
private Applet comp;
private int count;
private int subCount;
private int length;
private URL dataFile;
/**
* Creates and new instance of the class and downloads the correct file.
*
* @param level Player's Current level.
* @param c Instance of an Applet, for call backs.
*/
StoryLine(int level, Applet c) {
comp = c;
getFile(level);
}
/**
* Updates the length of the messages. It's best for it to look like the
* message is being download one line at a time.
*/
public void update() {
if (subCount++ > 2 && !canContinue()) {
count += 1;
subCount = 0;
}
}
/**
* Determins if the player can hit any key to continue.
*
* @return true if the number of lines being
* display is equal to the total number of lines,
* false otherwise.
*/
public boolean canContinue() {
if (count >= length)
return true;
return false;
}
/**
* Paints the story line information.
*
* @param g Graphics to paint to.
*/
public void paint(Graphics g) {
g.setFont(font);
for (int i = 0; i < count; i++) {
if (type[i] == 0) {
g.setColor(Color.cyan);
g.drawString("[System] " + data[i], 10, 17*i+15);
} else if (type[i] == 1) {
g.setColor(Color.red);
g.drawString("<< " + data[i], 10, 17*i+15);
} else if (type[i] == 2) {
g.setColor(Color.green);
g.drawString(">> " + data[i], 10, 17*i+15);
}
}
if (canContinue()) {
g.setColor(Color.cyan);
g.drawString("[System] Press Any Key To Continue", 10, 17*count+15);
}
}
/**
* Gets a file from the web and stores the information in arrays.
*
* @param i Level to download.
*/
private void getFile(int i) {
String url = comp.getCodeBase().toString() + "levels/" + i + ".plot";
try {
this.dataFile = new URL(url);
} catch ( MalformedURLException e) {
System.out.println("Bad URL: " + url);
}
String line; // Line Buffer
int c = -1;
try {
URLConnection conn = this.dataFile.openConnection();
conn.connect();
DataInputStream data = new DataInputStream(new BufferedInputStream(conn.getInputStream()));
while((line = data.readLine()) != null) {
// *** First line tells us how long the file is going to be.
if (c == -1) {
buildNew(toInt(line));
c = 0;
// *** All other lines contain info.
} else {
StringTokenizer Token = new StringTokenizer(line, "|", false);
type[c] = toInt(Token.nextToken());
if (type[c] != 3)
this.data[c] = Token.nextToken();
c += 1;
}
}
} catch (IOException e) {
System.out.println("IO Error:" + e.getMessage());
}
if (c != length)
System.err.println("Error: Size Missmatch, Number of Elements is wrong!");
}
/**
* Creates new arrays of the correct length so we can store
* Information.
*
* @param i Length required.
*/
private void buildNew(int i) {
length = i;
count = 0;
subCount = 0;
type = new int[i];
data = new String[i];
}
/**
* Converts a String to an int;
*
* @param s The String to be converted
* @return an int value from s
*/
private int toInt(String s) {
Integer cToI = new Integer(s);
return cToI.intValue();
}
}