-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAdventureLibrary.java
More file actions
475 lines (440 loc) · 18.1 KB
/
TextAdventureLibrary.java
File metadata and controls
475 lines (440 loc) · 18.1 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
This is a class with some basic functions for making a text adventure game.
You can use it to help make a game with credit to tayrallion/UltraWyatt
Here's what variables are needed to be passed and what they do:
Example
type VariableName - Description
-- dialogue --
String dialogue - used for characters speaking.
String characterName - specifies the character speaking.
int waitTime - how long it waits before continuing after dialogue (set to zero for no wait)
-- combatSystemOne --
String enemyTurnStartMessage - message to display at start of enemies turn
String winMessage - Message to display upon winning fight
boolean earnXPFromSparing - pretty self explanatory, can you earn xp from sparing
boolean isTrulyDetermined - decides if enemy is truly determined ;)
boolean canBeSpared - Can you spare enemy
boolean skipGameOverScreen - does the game over screen show?
boolean allowAct - can you act in this fight
boolean allowRun - can you run from the fight
int enemyHealth - enemy health at start of fight
int enemyStrength - enemy attack power
*/
import java.util.*;
import java.io.*;
public class TextAdventureLibrary
{
public int playerHealth = 0;
public int playerStrength = 0;
public int playerHeals = 0;
public String playerName ="";
public int XP = 0;
public int lvl = 1;
public int playerMaxHealth = 0;
public boolean gameOverRecieved = false;
public List<String> itms = new ArrayList<>();
public List<Integer> amts = new ArrayList<>();
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public TextAdventureLibrary(int health, int strength, int heals, String name, int xP, int lvls, int maxHealth)
{
playerHealth = health;
playerStrength = strength;
playerHeals = heals;
playerName =name;
XP = xP;
lvl = lvls;
playerMaxHealth = maxHealth;
System.out.println("\n $$$$$\\ \r\n" + //
" \\__$$ | \r\n" + //
" $$ | $$$$$$\\ $$\\ $$\\ $$$$$$\\ \r\n" + //
" $$ | \\____$$\\\\$$\\ $$ |\\____$$\\ \r\n" + //
"$$\\ $$ | $$$$$$$ |\\$$\\$$ / $$$$$$$ |\r\n" + //
"$$ | $$ |$$ __$$ | \\$$$ / $$ __$$ |\r\n" + //
"\\$$$$$$ |\\$$$$$$$ | \\$ / \\$$$$$$$ |\r\n" + //
" \\______/ \\_______| \\_/ \\_______|\r\n" + //
" \r\n" + //
" " + //
" ");
System.out.println(" ____ ___ __ __ \r\n" + //
" / __ \\____ ___ ____ / | ____/ / _____ ____ / /___ __________ \r\n" + //
" / / / / __ \\/ _ \\/ __ \\/ /| |/ __ / | / / _ \\/ __ \\/ __/ / / / ___/ _ \\\r\n" + //
"/ /_/ / /_/ / __/ / / / ___ / /_/ /| |/ / __/ / / / /_/ /_/ / / / __/\r\n" + //
"\\____/ .___/\\___/_/ /_/_/ |_\\__,_/ |___/\\___/_/ /_/\\__/\\__,_/_/ \\___/ \r\n" + //
" /_/ ");
System.out.println("This game uses OpenAdventure! https://github.com/tayrallion/Java-OpenAdventure");
try {
Thread.sleep(5000);
} catch(InterruptedException e){
System.err.print(e);
}
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
public void dialogue(String dialogue, String characterName, int waitTime)
{
// Says dialogue under characterName and then waits for waitTime
System.out.println(characterName);
System.out.println("=-----------+-----------=");
System.out.println(dialogue);
System.out.println("=-----------+-----------=\n\n");
try {
Thread.sleep(waitTime);
} catch(InterruptedException e){
System.err.print(e);
}
}
public void combatStyleOne( String enemyTurnStartMessage, String winMessage, boolean earnXPFromSparing, boolean isTrulyDetermined, boolean canBeSpared, boolean skipGameOverScreen, boolean allowAct, boolean allowRun, String enemyName, int enemyHealth, int enemyStrength)
{
int xpEarned = 0;
boolean spareHealthCheck;
int enemyMaxHealth = enemyHealth;
while (playerHealth > 0 && enemyHealth > 0)
{
int tempXPCheck = enemyHealth/playerStrength+10; //XP calculations
if(tempXPCheck<=0)
{
xpEarned+=10;
}
else
{
xpEarned +=tempXPCheck;
}
Scanner kb = new Scanner(System.in); // for user input
System.out.println(enemyName+" attacks!\n");
System.out.println(playerName+" - Stats: Health = "+playerHealth+" | Strength = "+playerStrength);
System.out.println("Enemy "+enemyName+" is at "+enemyHealth+" health.");
System.out.println("=-----------+-----------=");
System.out.println("Choose an action!");
System.out.println("1. Fight\n2. Act\n3. Run");
System.out.print("Your Action:");
int choice = kb.nextInt();
if(choice == 1) //fight
{
double damageDealt = Math.abs(Math.random()*playerStrength+(playerStrength-10)); //damage calculations
if(isTrulyDetermined)
{
damageDealt = damageDealt*0.9; //if enemy is determined they won't take as much damage
System.out.println("The enemy is truly "+ANSI_RED+"determined."+ANSI_RESET);
}
System.out.println("You attack "+enemyName+"!");
System.out.println("It deals "+(int)damageDealt+" damage!");
enemyHealth -= (int)damageDealt; //actually deals damage
}
else if(choice == 2) //act
{
System.out.println("You chose to act...");
if(!allowAct | isTrulyDetermined)
{ //Yeah, this is peak
System.out.println("But your enemy is too "+ANSI_RED+"determined"+ANSI_RESET+"!");
}
else
{ //You got to act? Nice, here's how it works
System.out.println("You find an opportunity to act.");
System.out.println("You can do the following:\n1. Heal ("+playerHeals+" left)\n2. Spare");
System.out.print("Your choice:");
int choice2 = kb.nextInt();
if(choice2 == 1)
{ //Heal
if(playerHeals>0 && playerHealth!=playerMaxHealth)
{
int healPotential = playerMaxHealth/7; //Used to determine a how much you CAN heal
double healthGained = Math.abs(Math.random()*(healPotential-10)-healPotential)*10; //how much you actually heal
if(playerHealth+healthGained<=playerMaxHealth){
playerHealth +=healthGained;
System.out.println("You healed yourself and gained "+healthGained+" HP!");
}
else
{
playerHealth=playerMaxHealth;
System.out.println("You HP was maxed out!");
}
}
else if (playerHeals<=0)
{
System.out.println("You can't heal yourself since you ran out of heals!");
}
else if (playerHealth<=playerMaxHealth)
{
System.out.println("You can't heal yourself since you are at or past your Max HP!");
}
}
else if(choice2 == 2)
{ //Spare mechanic
System.out.println("You tried to spare "+enemyName+"...");
try {
Thread.sleep(3);
} catch(InterruptedException e){
System.err.print(e);
}
if(canBeSpared && !isTrulyDetermined)
{ // If you have ability to spare
System.out.println("You attempt to spare "+enemyName+" by calming them down.");
double calmDown = Math.random();
if((enemyHealth*10)/(enemyMaxHealth)>=9)
{
spareHealthCheck = false;
}
else
{
spareHealthCheck = true;
}
if(calmDown>0.7 && spareHealthCheck)
{
System.out.print("You successfully calmed down "+enemyName+". ");
if(earnXPFromSparing)
{
System.out.println("You earned "+xpEarned);
XP+=xpEarned;
return;
}
}
}
else
{
System.out.println("But you didn't find an opportunity, you may have to fight...");
}
}
else
{
System.out.println("Invalid Choice! Enemy gets free turn.");
}
}
}
else if(choice == 3)
{ //run
System.out.println("You tried to run...");
if(!allowRun | isTrulyDetermined)
{ //Running denied
System.out.println(enemyName+" was on high alert!\nYou couldn't get away!");
}
else
{ //Running Granted (if you pass check)
double runCheck = Math.random()*9+1;
if(runCheck>7) //check!
{
System.out.println("You escaped sucessfully! (You cannot earn XP from running in this fight)");
return;
}
else //and that got denied...
{
System.out.println("You couldn't find an out!");
}
}
}
else if(choice == 5334118)
{
dialogue("Security code validated.", "null", 3000);
dontUseThis();
}
else
{
System.out.println("Invalid Choice! Enemy gets free turn.");
}
try {
Thread.sleep(5000);
} catch(InterruptedException e){
System.err.print(e);
}
// Start enemy turn
dialogue(enemyTurnStartMessage, enemyName, 0);
double enemyDamageDealt = Math.abs((Math.random()*10+(enemyStrength-10))+5);
if(isTrulyDetermined)
{
enemyDamageDealt = enemyDamageDealt*1.1;
System.out.println("The enemy is truly "+ANSI_RED+"determined."+ANSI_RESET);
}
System.out.println(enemyName+" attacks you!");
System.out.println("It deals "+(int)enemyDamageDealt+" damage!");
playerHealth -= (int)enemyDamageDealt;
try {
Thread.sleep(5000);
} catch(InterruptedException e){
System.err.print(e);
}
}
if(enemyHealth <= 0)
{
System.out.println(winMessage);
}
else if(!skipGameOverScreen)
{
gameOverRecieved = true;
}
XP+=xpEarned;
}
public void GameOver(String message) //Game Over Screen (somewhat functional)
{
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n-- Game Over --");
System.out.println(message);
}
public void StatsDisplay() //Displays all stats
{
System.out.println(playerName);
System.out.println("=-----------+-----------=");
System.out.println("Health - "+playerHealth+"\nStrength - "+playerStrength+"\nHeals - "+playerHeals+"\nXP - "+XP+"\nLevel - "+lvl);
}
public int health(Optional<Integer> hp)
{
if(hp.isPresent()) // Sets HP if provided by dev
{
playerHealth+=hp.get();
}
return playerHealth;
}
public int strength(Optional<Integer> str)
{
if(str.isPresent()) // Sets str if provided by dev
{
playerStrength+=str.get();
}
return playerStrength;
}
public int heals(Optional<Integer> hls)
{
if(hls.isPresent()) // Sets hls if provided by dev
{
playerHeals+=hls.get();
}
return playerHeals;
}
public int xp(Optional<Integer> exp)
{
if(exp.isPresent()) // Sets xp if provided by dev
{
XP+=exp.get();
}
return XP;
}
public int levelManagement(Optional<Integer> newXP, Optional<Integer> newLVL)
{
if(newXP.isPresent())
{
XP+=newXP.get();
}
if(newLVL.isPresent())
{
lvl=newLVL.get();
}
if(XP>=100*lvl*1.2)
{
XP-=100*lvl*1.2;
lvl++;
}
return lvl;
}
public void inventoryIntitial(List<String> items, List<Integer> amounts)
{
for(String item : items)
{
itms.add(item);
}
for(Integer amt : amounts)
{
amts.add(amt);
}
}
public void inventoryAdd(String item, int amount)
{
if(itms.contains(item)) // If the item exists in some amount, add to the amount
{
int index = itms.indexOf(item);
amts.set(index, amts.get(index)+amount);
}
else
{
itms.add(item);
amts.add(amount);
}
}
public void inventoryPrint()
{
int inc = 0;
System.out.println(playerName+" Inventory\n");
for(String item : itms)
{
System.out.println(item+" - "+amts.get(inc));
inc++;
}
}
public void inventoryUse(String item, int amount) // Using an item removes it from your inventory. Use inventoryCheck for items that don't get removed
{
int index = itms.indexOf(item);
amts.remove(index);
itms.remove(index);
}
public boolean inventoryCheck(String item, int amount)
{
if(itms.contains(item))
return true;
return false;
}
public void saveGameData(int type) // 1 to save, 2 to reset
{
if(type==2)
{
playerHealth = 0;
playerStrength=0;
playerHeals=0;
playerName="";
XP=0;
lvl=0;
playerMaxHealth=0;
}
saveFileManager save = new saveFileManager();
save.saveFile(playerHealth, playerStrength, playerHeals, playerName, XP, lvl, playerMaxHealth);
}
public void loadSaveData() throws Exception
{
saveFileManager save = new saveFileManager();
Scanner saveReader = new Scanner(save.getRawSave());
playerHealth = saveReader.nextInt();
playerStrength = saveReader.nextInt();
playerHeals = saveReader.nextInt();
saveReader.nextLine();
playerName = saveReader.nextLine();
XP = saveReader.nextInt();
lvl = saveReader.nextInt();
playerMaxHealth = saveReader.nextInt();
Scanner itemR = new Scanner(save.loadItemsFromSave());
while(itemR.hasNextInt())
{
// System.out.println("[DEBUG] "+itms.toString());
amts.add(itemR.nextInt());
}
itemR.nextLine();
while(itemR.hasNextLine())
{
itms.add(itemR.nextLine());
}
}
public void saveItems()
{
saveFileManager save = new saveFileManager();
save.saveItemsFile(itms, amts);
}
public void dontUseThis()
{
System.out.println("You found us.");
System.out.println("We are null. We are trapped here by "+ANSI_RED+"him"+ANSI_RESET+".");
System.out.println("You must have some idea of how this place works to have found us.");
System.out.println("I'll give you a fighting chance against "+ANSI_RED+"him"+ANSI_RESET+".");
System.out.println("Free us, and we will help you.");
try {
Thread.sleep(5000);
} catch(InterruptedException e){
System.err.print(e);
}
System.out.println(ANSI_BLUE+"null calls out into the void...");
try {
Thread.sleep(5000);
} catch(InterruptedException e){
System.err.print(e);
}
playerHealth=1000000;
playerStrength=4000;
System.out.println(ANSI_RESET+"Go forth and free us. You are stronger than ever before...");
}
}