-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppRunner.java
More file actions
243 lines (216 loc) · 9.31 KB
/
AppRunner.java
File metadata and controls
243 lines (216 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
The Swing implementation and main method for PokéPath.
*/
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.*;
public class AppRunner {
private static Player player; // Add this at the top with other static fields
private static JPanel currentBattlePane;
private static JFrame jfrm;
private static CardLayout cards;
private static List<Entity> entityDex;
private static List<Usable> itemDex;
private static List<Attack> attackDex;
private static List<Intermission> intermissionDex;
private static BattleStats stats = new BattleStats();
public AppRunner() {
// Open game data file.
boolean ableToRun = true;
Scanner sc = null;
try {
sc = new Scanner(new File("Game Data.txt"));
} catch (FileNotFoundException e) {
System.out.println("Game Data file not found :(");
ableToRun = false;
}
if (ableToRun) {
String section = null;
attackDex = new ArrayList<>();
itemDex = new ArrayList<>();
entityDex = new ArrayList<>();
intermissionDex = new ArrayList<>();
// Read game data.
while (sc.hasNextLine()) {
String[] line = sc.nextLine().split("\t");
if (line.length == 1)
section = line[0];
else {
switch (section) {
case "Attack":
attackDex.add(new Attack(Integer.parseInt(line[0]), new Usable(line[1], Boolean.parseBoolean(line[2]), Boolean.parseBoolean(line[3]), new Effect(Integer.parseInt(line[4]), Integer.parseInt(line[5]), Integer.parseInt(line[6]), Boolean.parseBoolean(line[7])))));
break;
case "Item":
itemDex.add(new Usable(line[0], Boolean.parseBoolean(line[1]), Boolean.parseBoolean(line[2]), new Effect(Integer.parseInt(line[3]), Integer.parseInt(line[4]), Integer.parseInt(line[5]), Boolean.parseBoolean(line[6]))));
break;
case "Entity":
List<Attack> entityAttackList = new ArrayList<>();
int element;
element = Integer.parseInt(line[2]);
Entity entity = new Entity(line[0], Integer.parseInt(line[1]), element);
for (int i = 3; i < line.length - 2; i++) {
for (int j = 0; j < attackDex.size(); j++) {
Attack selectedAttack = attackDex.get(j);
if (line[i].equals(selectedAttack.getName())) {
entityAttackList.add(selectedAttack);
j = attackDex.size(); // Bad Practice :(
}
}
}
entity.setAttacks(entityAttackList);
entity.setDefaultPose(new ImageIcon(line[line.length - 2]));
entity.setAttackPose(new ImageIcon(line[line.length - 1]));
entityDex.add(entity);
break;
case "Intermission":
Usable itemOne = null;
Usable itemTwo = null;
int index = 0;
while ((itemOne == null || itemTwo == null) && index < itemDex.size()) {
Usable itemFound = itemDex.get(index);
if (line[3].equals(itemFound.getName()))
itemOne = itemFound;
else if (line[4].equals(itemFound.getName()))
itemTwo = itemFound;
index++;
}
intermissionDex.add(new Intermission(line[0], line[1], line[2], new Item(itemOne, Integer.parseInt(line[5])), new Item(itemTwo, Integer.parseInt(line[6])), line[7], line[8]));
break;
default:
System.out.println("Error: Section " + section + " not found.");
}
}
}
// Debugging prints.
// System.out.println(attackDex);
// System.out.println(itemDex);
// System.out.println(entityDex);
// System.out.println(intermissionDex);
// Create the frame.
jfrm = new JFrame("PokéPath! Definitely not copyrighted...");
jfrm.setSize(1500, 800);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setResizable(false);
cards = new CardLayout();
jfrm.setLayout(cards);
// Build Start Menu
JPanel startPane = PaneBuilder.buildStartPanel(jfrm, cards);
// Build Battle Menu -> Create Player and Random Enemy
createPlayer();
setUpNewBattle();
// Build Intermission Menu
JPanel intermissionPane = PaneBuilder.buildIntermissionPanel(jfrm, cards, intermissionDex, player);
// Build Death Menu
JPanel deathPane = PaneBuilder.buildDeathPanel(jfrm, cards);
// Add Panes to JFrame
jfrm.add(startPane, "Start");
jfrm.add(currentBattlePane, "Battle");
jfrm.add(intermissionPane, "Intermission");
jfrm.add(deathPane, "Death");
// Debugging tool to change screens instantly.
//changeScreen(cards, jfrm.getContentPane(), "Intermission");
// Make the frame visible.
jfrm.setVisible(true);
}
}
public static void main(String[] args) {
// Lambda expression (same as new Runnable() {...)
SwingUtilities.invokeLater(() -> {
new AppRunner();
});
}
public static void changeScreen(CardLayout cards, Container container, String string) {
// Changes screen to whichever screen is specified in "string"
cards.show(container, string);
}
public static BattleStats getBattleStats() {
return stats;
}
// Method to build a new battle panel
public static void setUpNewBattle() {
player.setCurrentHealth(player.getMaxHealth());
Enemy enemy = createRandomEnemy();
Encounter encounter = new Encounter(enemy, player);
currentBattlePane = PaneBuilder.buildBattlePanel(jfrm, cards, encounter);
addPane(currentBattlePane, "Battle");
}
//Returns BattlePane
//Example; Call This Then Envoke removeBattle()
//Removes Values on CurrentBattlePane
public static JPanel getCurrentBattlePane() {
return currentBattlePane;
}
//Returns entityDex
//Used When Creating a New Pane
public static List<Entity> getEntityDex() {
return entityDex;
}
//Returns ItemDex
//Used When Creating a New Pane
public static List<Usable> getItemDex() {
return itemDex;
}
//Adds Pane to jfrm
//jfrm Uses CardLayout, so Pane is now a part of CardLayout
public static void addPane(JPanel panel, String cardName) {
jfrm.add(panel, cardName); //Add Pane w/ Specified Name
}
//Method to Create Player
public static void createPlayer() {
player = null;
for (Entity entity : entityDex) {
if (entity.getName().equals("Player")) {
player = new Player(entity);
int element = (int) (Math.random() * 4);
player.setElement(element);
String searchAttackName = null;
switch (element) {
case 0:
searchAttackName = "Fire Breath";
break;
case 1:
searchAttackName = "Water Ball";
break;
case 2:
searchAttackName = "Lightning Strike";
break;
case 3:
searchAttackName = "Wood Thwack";
break;
default:
System.out.println("Error: Element " + element + " not found.");
}
for (Attack attack : attackDex) {
if (attack.getName().equals(searchAttackName)) {
player.getSelectedAttacks().add(attack);
break;
}
}
break;
}
}
//Add items like Health Potion, Fire Bottle, etc. to the player
for (Usable item : itemDex) {
if (item.getName().equals("Health Potion") || item.getName().equals("Fire Bottle")) {
player.addItem(new Item(item, 3)); // Add 3 items of each type
}
}
}
//Method to Create Random Enemy
public static Enemy createRandomEnemy() {
Enemy newEnemy = null;
//Randomly select an enemy from the entityDex
while (newEnemy == null) {
int roll = (int) (Math.random() * entityDex.size());
Entity chosenEntity = entityDex.get(roll);
if (!chosenEntity.getName().equals("Player")) {
newEnemy = new Enemy(chosenEntity);
}
}
return newEnemy;
}
}