/*"CombinedMap" class
version 1.0*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
public class CombinedMap extends JApplet implements ActionListener
{
int Place = 0, temp = 0, temp1 = 0;
JFrame mapFrame = new JFrame ("Town Map");
JFrame WorldFrame = new JFrame ("World Map");
JFrame ChoiceFrame = new JFrame ("Town Choice Window");
JFrame WChoiceFrame = new JFrame ("World Choice Window");
JFrame CharStatsFrame = new JFrame ("Character Stats Window");
JFrame BuildingFrame = new JFrame ();
JButton[] WorldB = new JButton [4];
JButton[] ChoiceB = new JButton [4];
JButton[] ItemB = new JButton [10];
JButton InnB = new JButton ();
JLabel BGLabel = new JLabel ();
JLabel BGInLabel = new JLabel ();
JLabel[] CharStatsLabel = new JLabel [8];
Icon[] BG = new ImageIcon [3];
Icon[] MAP = new ImageIcon [11];
Icon[] Inside = new ImageIcon [2];
String tempstring;
Icon display = new ImageIcon ();
String CharName = "", record = "";
int[] Count = new int [2];
int Item2Buy = 4, ItemUGot = 0, ATK = 10, Gold = 0, LVL = 1, HealorNot = 0, Potion = 3;
int[] ItemLeft = new int [10];
String[] ItemName = new String [10]; //will be hardcoded into townmap.java
int[] HP = new int [3];
int[] decision = new int [2];
int[] WMV = new int [4];
public int[] init (int[] decisionvariable) throws IOException
{
setSize (0, 0);
//initialize values
decision [0] = decisionvariable [0];
decision [1] = decisionvariable [1];
ItemName [0] = "Potion: 20 Gold";
ItemName [1] = "Stat Item 1: 5 Gold"; //Replace with ACTUAL items
ItemName [2] = "Stat Item 2: 15 Gold";
ItemName [3] = "Stat Item 3: 30 Gold";
ItemName [4] = "Stat Item 4: 60 Gold";
ItemName [5] = "Stat Item 5: 120 Gold";
ItemName [6] = "Stat Item 6: 240 Gold";
ItemName [7] = "Stat Item 7: 580 Gold";
ItemName [8] = "Stat Item 8: 1160 Gold";
ItemName [9] = "Stat Item 9: 2320 Gold";
Count [0] = 0;
Count [1] = 0;
HP [0] = 0;
HP [1] = 100;
HP [2] = 100;
temp = 0;
temp1 = 0;
Place = 0;
for (int i = 0 ; i < 3 ; i++)
{
BG [i] = new ImageIcon ("map/Town Map - " + i + ".jpg");
}
for (int i = 0 ; i < 2 ; i++)
{
Inside [i] = new ImageIcon ("map/inside " + i + ".jpg");
}
for (int i = 0 ; i < 11 ; i++)
{
MAP [i] = new ImageIcon ("map/Map - " + i + ".jpg");
}
//THE ORDER OF ITEMS FOR LOADING IS++LVL,HP [0],HP [1], HP [2], ATK, Gold, Potion, Item2Buy, ItemLeft x 10;
//Load Character Choice & Pic
try
{
FileReader fr1 = new FileReader ("char.txt");
BufferedReader br1 = new BufferedReader (fr1);
record = "";
while ((record = br1.readLine ()) != null)
{
tempstring = record;
temp = Integer.parseInt (tempstring);
}
}
catch (IOException e)
{
}
//set character pics + name
if (temp == 1)
{
CharName = "Eliwood";
display = new ImageIcon ("eliwood_sprites/eliwood_mug.gif");
}
else if (temp == 2)
{
CharName = "Hector";
display = new ImageIcon ("hector_sprites/hector_mug.gif");
}
else if (temp == 3)
{
CharName = "Lyndis ";
display = new ImageIcon ("lyn_sprites/lyn_mug.gif");
}
temp = 0;
//if player chooses LOAD GAME, otherwise preset values are used for character
if (decision [0] == 2)
{
// Load Charstats Info
try
{
FileReader fr2 = new FileReader ("charstats.txt");
BufferedReader br2 = new BufferedReader (fr2);
record = "";
while ((record = br2.readLine ()) != null)
{
if (Count [0] == 0)
{
tempstring = record;
LVL = Integer.parseInt (tempstring);
}
else if (Count [0] == 1)
{
tempstring = record;
HP [0] = Integer.parseInt (tempstring);
}
else if (Count [0] == 2)
{
tempstring = record;
HP [1] = Integer.parseInt (tempstring);
}
else if (Count [0] == 3)
{
tempstring = record;
HP [2] = Integer.parseInt (tempstring);
}
else if (Count [0] == 4)
{
tempstring = record;
ATK = Integer.parseInt (tempstring);
}
else if (Count [0] == 5)
{
tempstring = record;
Gold = Integer.parseInt (tempstring);
}
else if (Count [0] == 6)
{
tempstring = record;
Potion = Integer.parseInt (tempstring);
}
else if (Count [0] == 7)
{
tempstring = record;
Item2Buy = Integer.parseInt (tempstring);
}
else if (Count [0] > 7)
{
tempstring = record;
ItemLeft [Count [1]] = Integer.parseInt (tempstring);
Count [1]++;
}
Count [0]++;
}
}
catch (IOException e)
{
}
}
//set Inn button to show if character can be healed
if (HP [0] > 0)
{
HealorNot = 1;
}
Count [0] = 0;
//Load World Map Stats
try
{
FileReader fr3 = new FileReader ("wms.txt");
BufferedReader br3 = new BufferedReader (fr3);
record = "";
while ((record = br3.readLine ()) != null)
{
tempstring = record;
WMV [Count [0]] = Integer.parseInt (tempstring);
Count [0]++;
}
}
catch (IOException e)
{
}
//Actual Game Code
while (temp == 0)
{
temp1 = 0;
//go to town if main says so
if (decision [1] == 0)
{
//[ Choice Window ]
Container introContents = new Container ();
introContents.setLayout (new FlowLayout ());
String[] buttonLabels = {"[ Inn ]", "[ Shop ]", "[ Leave Town ]", "[ Save & Quit ]"};
for (int i = 0 ; i < 4 ; i++)
{
ChoiceB [i] = new JButton (buttonLabels [i]);
ChoiceB [i].addActionListener (this);
introContents.add (ChoiceB [i]);
}
ChoiceFrame.getContentPane ().add (introContents);
ChoiceFrame.setSize (600, 75);
ChoiceFrame.setLocation (100, 550);
ChoiceFrame.setVisible (true);
//[ Main Window ]
mapFrame.getContentPane ().setLayout (new FlowLayout ());
mapFrame.getContentPane ().add (BGLabel);
BGLabel.setHorizontalTextPosition (SwingConstants.CENTER);
BGLabel.setIcon (BG [0]);
mapFrame.setSize (600, 400);
mapFrame.setLocation (100, 150);
mapFrame.setVisible (true);
}
//go to world map if main says so
else if (decision [1] > 0 || Place >= 0)
{
//[ Choice Window ]
Container introContents = new Container ();
introContents.setLayout (new FlowLayout ());
String[] buttonLabels = {"[ Backward ]", "[ Forward ]", "[ Battle ]", "[ Town ]"};
for (int i = 0 ; i < 4 ; i++)
{
WorldB [i] = new JButton (buttonLabels [i]);
WorldB [i].addActionListener (this);
introContents.add (WorldB [i]);
}
WChoiceFrame.getContentPane ().add (introContents);
WChoiceFrame.setSize (600, 75);
WChoiceFrame.setLocation (100, 550);
WChoiceFrame.setVisible (true);
//[ Main Window ]
WorldFrame.getContentPane ().setLayout (new FlowLayout ());
WorldFrame.getContentPane ().add (BGLabel);
BGLabel.setHorizontalTextPosition (SwingConstants.CENTER);
BGLabel.setIcon (MAP [0]);
WorldFrame.setSize (600, 400);
WorldFrame.setLocation (100, 150);
WorldFrame.setVisible (true);
}
//[ Char Stats Window ]
Container statContents = new Container ();
statContents.setLayout (new FlowLayout ());
for (int i = 0 ; i < 8 ; i++)
{
CharStatsLabel [i] = new JLabel ();
statContents.add (CharStatsLabel [i]);
}
CharStatsFrame.getContentPane ().add (statContents);
CharStatsFrame.setSize (600, 100);
CharStatsFrame.setLocation (100, 50);
CharStatsFrame.setVisible (true);
CharStatsLabel [0].setIcon (display);
CharStatsLabel [1].setText (CharName);
CharStatsLabel [2].setText (" | Level: " + LVL);
CharStatsLabel [3].setText (" | Health: " + HP [2] + "/" + HP [1]);
CharStatsLabel [4].setText (" | Attack: " + ATK);
CharStatsLabel [5].setText (" | Gold: " + Gold);
CharStatsLabel [6].setText (" | Potions: " + Potion + " |");
CharStatsLabel [7].setText (" ");
while (temp1 == 0)
{
}
}
//Save the char stats
InputStreamReader converter = new InputStreamReader (System.in);
BufferedReader console = new BufferedReader (converter);
FileWriter FileName = new FileWriter ("charstats.txt");
PrintWriter NewPrintWriter = new PrintWriter (FileName, true);
NewPrintWriter.println (LVL);
NewPrintWriter.println (HP [0]);
NewPrintWriter.println (HP [1]);
NewPrintWriter.println (HP [2]);
NewPrintWriter.println (ATK);
NewPrintWriter.println (Gold);
NewPrintWriter.println (Potion);
NewPrintWriter.println (Item2Buy);
NewPrintWriter.println (ItemLeft [0]);
NewPrintWriter.println (ItemLeft [1]);
NewPrintWriter.println (ItemLeft [2]);
NewPrintWriter.println (ItemLeft [3]);
NewPrintWriter.println (ItemLeft [4]);
NewPrintWriter.println (ItemLeft [5]);
NewPrintWriter.println (ItemLeft [6]);
NewPrintWriter.println (ItemLeft [7]);
NewPrintWriter.println (ItemLeft [8]);
NewPrintWriter.println (ItemLeft [9]);
NewPrintWriter.close ();
//Save the world stats (ie bosses beaten, place for battle
InputStreamReader converter1 = new InputStreamReader (System.in);
BufferedReader console1 = new BufferedReader (converter1);
FileWriter FileName1 = new FileWriter ("wms.txt");
PrintWriter NewPrintWriter1 = new PrintWriter (FileName1, true);
NewPrintWriter1.println (WMV [0]);
NewPrintWriter1.println (WMV [1]);
NewPrintWriter1.println (WMV [2]);
if (decision [1] >= 0)
NewPrintWriter1.println (WMV [3]);
else
NewPrintWriter1.println (0);
NewPrintWriter1.close ();
return decision;
}
//Buttons
public void actionPerformed (ActionEvent e)
{
//*TOWN BUTTONS*
//Enter Inn
if (e.getSource () == ChoiceB [0])
{
BGLabel.setIcon (BG [1]);
//[ The Inn ]
BuildingFrame.setVisible (false);
BGInLabel.setIcon (Inside [0]);
Container indoorContents = new Container ();
indoorContents.setLayout (new FlowLayout ());
indoorContents.add (BGInLabel);
if (HealorNot == 1)
{
InnB = new JButton ("[ Heal: 50 Gold ]");
InnB.addActionListener (this);
indoorContents.add (InnB);
}
BuildingFrame = new JFrame ("The Inn");
BuildingFrame.getContentPane ().add (indoorContents);
BuildingFrame.setSize (250, 285 + (30 * HealorNot));
BuildingFrame.setLocation (700, 150);
BuildingFrame.setVisible (true);
}
//Enter Shop
else if (e.getSource () == ChoiceB [1])
{
BGLabel.setIcon (BG [2]);
//[ The Shop ]
BuildingFrame.setVisible (false);
BGInLabel.setIcon (Inside [1]);
Container indoorContents = new Container ();
indoorContents.setLayout (new FlowLayout ());
indoorContents.add (BGInLabel);
ItemB [0] = new JButton (ItemName [0]);
ItemB [0].addActionListener (this);
indoorContents.add (ItemB [0]);
for (int i = 1 ; i < 10 ; i++)
{
if (ItemLeft [i] == 0)
{
ItemB [i] = new JButton (ItemName [i]);
ItemB [i].addActionListener (this);
indoorContents.add (ItemB [i]);
}
}
BuildingFrame = new JFrame ("The Shop");
BuildingFrame.getContentPane ().add (indoorContents);
BuildingFrame.setSize (250, 285 + (30 * Item2Buy));
BuildingFrame.setLocation (700, 150);
BuildingFrame.setVisible (true);
}
//Exit Town Map
else if (e.getSource () == ChoiceB [2])
{
if (Place == 1 || Place == 2)
BuildingFrame.setVisible (false);
Place = 0;
temp1 = 1;
decision [1] = 1;
//close town windows upon exit
CharStatsFrame.setVisible (false);
mapFrame.setVisible (false);
ChoiceFrame.setVisible (false);
}
//Save & Quit
else if (e.getSource () == ChoiceB [3])
{
if (Place == 1 || Place == 2)
BuildingFrame.setVisible (false);
decision [1] = -1;
temp1 = 1;
temp = 1;
//close town windows upon exit
CharStatsFrame.setVisible (false);
mapFrame.setVisible (false);
ChoiceFrame.setVisible (false);
}
//*WORLD MAP BUTTONS*
//Backward
if (e.getSource () == WorldB [0])
{
if (Place > 1)
{
if ((Place - 1) == 3 && WMV [0] == 1)
{
Place -= 2;
}
if ((Place - 1) == 6 && WMV [1] == 1)
{
Place -= 2;
}
if ((Place - 1) == 10 && WMV [1] == 1)
{
Place -= 2;
}
else
Place -= 1;
}
//Refresh Map
if (Place == 1)
BGLabel.setIcon (MAP [1]);
else if (Place == 2)
BGLabel.setIcon (MAP [2]);
else if (Place == 3)
BGLabel.setIcon (MAP [3]);
else if (Place == 4)
BGLabel.setIcon (MAP [4]);
else if (Place == 5)
BGLabel.setIcon (MAP [5]);
else if (Place == 6)
BGLabel.setIcon (MAP [6]);
else if (Place == 7)
BGLabel.setIcon (MAP [7]);
else if (Place == 8)
BGLabel.setIcon (MAP [8]);
else if (Place == 9)
BGLabel.setIcon (MAP [9]);
else if (Place == 10)
BGLabel.setIcon (MAP [10]);
}
//Forward
else if (e.getSource () == WorldB [1])
{
if (Place < 10)
{
if ((Place + 1) == 3 && WMV [0] == 1)
{
Place += 2;
}
else if ((Place + 1) == 6 && WMV [1] == 1)
{
Place += 2;
}
else if ((Place + 1) == 10 && WMV [2] == 1)
{
}
else
Place += 1;
}
//Refresh Map
if (Place == 1)
BGLabel.setIcon (MAP [1]);
else if (Place == 2)
BGLabel.setIcon (MAP [2]);
else if (Place == 3)
BGLabel.setIcon (MAP [3]);
else if (Place == 4)
BGLabel.setIcon (MAP [4]);
else if (Place == 5)
BGLabel.setIcon (MAP [5]);
else if (Place == 6)
BGLabel.setIcon (MAP [6]);
else if (Place == 7)
BGLabel.setIcon (MAP [7]);
else if (Place == 8)
BGLabel.setIcon (MAP [8]);
else if (Place == 9)
BGLabel.setIcon (MAP [9]);
else if (Place == 10)
BGLabel.setIcon (MAP [10]);
}
//Enter Battle
else if (e.getSource () == WorldB [2])
{
if (Place != 0)
{
WMV [3] = Place;
decision [1] = Place;
temp1 = 1;
//close world windows upon exit
CharStatsFrame.setVisible (false);
WorldFrame.setVisible (false);
WChoiceFrame.setVisible (false);
}
}
//Enter Town
else if (e.getSource () == WorldB [3])
{
Place = -1;
WMV [3] = 0;
temp1 = 1;
decision [1] = 0;
//close world windows upon exit
CharStatsFrame.setVisible (false);
WorldFrame.setVisible (false);
WChoiceFrame.setVisible (false);
}
}
}
|