Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.29 KB | None | 0 0
  1. /**
  2. * Text User Interface for Battle scenes
  3. */
  4. import java.util.Random;
  5. public class BattleGUI
  6. {
  7. private static Random generator = new Random();
  8. public static void displayStats(Person p)
  9. {
  10. if(p instanceof Player)
  11. {
  12. System.out.println(" _________\n | STATS |\n ¯¯¯¯¯¯¯¯¯");
  13. System.out.print("LEVEL: " + ((Player)p).getLVL());
  14. System.out.println(" EXP: " + ((Player)p).getEXP() + "/" + ((Player)p).getEXPtoLVL());
  15. System.out.println("GOLD: " + ((Player)p).getMoney());
  16. System.out.printf("ATK:%2d (%+3d) DEF:%2d (%+3d)\nSPD:%2d (%+3d) HP: %d/%d (%+3d)\n", p.getAttack() + ((Player)p).getWeapon().getAttack() + ((Player)p).getArmor().getAttack(), ((Player)p).getWeapon().getAttack() + ((Player)p).getArmor().getAttack(), p.getDefense() + ((Player)p).getWeapon().getDefense() + ((Player)p).getArmor().getDefense(), ((Player)p).getWeapon().getDefense() + ((Player)p).getArmor().getDefense(),p.getSpeed() + ((Player)p).getWeapon().getSpeed() + ((Player)p).getArmor().getSpeed(), ((Player)p).getWeapon().getSpeed() + ((Player)p).getArmor().getSpeed(), p.getCurrentHealth(), p.getHealth() + ((Player)p).getWeapon().getHealth() + ((Player)p).getArmor().getHealth(), ((Player)p).getWeapon().getHealth() + ((Player)p).getArmor().getHealth());
  17. System.out.println("WEAPON: " + ((Player)p).getWeapon().getName());
  18. System.out.println("ARMOR: " + ((Player)p).getArmor().getName());
  19. }
  20. else
  21. {
  22. System.out.printf("ATK:%3d DEF:%3d\nSPD:%3d HP: %3d/%-3d\n", p.getAttack(), p.getDefense(), p.getSpeed(), p.getCurrentHealth(), p.getHealth());
  23. }
  24. }
  25. public static void displayStats(BuffItem i)
  26. {
  27. if(i.getAttack() > 0)
  28. {
  29. System.out.print("ATK: +" + i.getAttack() + " ");
  30. }
  31. if(i.getDefense() != 0)
  32. {
  33. System.out.print("DEF: +" + i.getDefense() + " ");
  34. }
  35. if(i.getSpeed() != 0)
  36. {
  37. System.out.print("SPD: +" + i.getSpeed() + " ");
  38. }
  39. if(i.getHealth() != 0)
  40. {
  41. System.out.print("HP: +" + i.getHealth() + " ");
  42. }
  43. if(i instanceof EquipItem)
  44. {
  45. System.out.println("while equipped.");
  46. }
  47. if(i instanceof ConsumableItem)
  48. {
  49. System.out.println("upon use.");
  50. }
  51. }
  52. public static void displayHealthbar(Person p, int healthBarSize)
  53. {
  54. System.out.printf("%-10s ", p.getName());
  55. System.out.print("[");
  56. for(int x = 0; x < (p.getCurrentHealth() * 1.0 / p.getHealth()) * (healthBarSize - 2); x++)
  57. {
  58. System.out.print("■");
  59. }
  60. for(int x = 0; x < (healthBarSize - 2) - ((p.getCurrentHealth() * 1.0 / p.getHealth()) * (healthBarSize - 2)); x++)
  61. {
  62. System.out.print(" ");
  63. if(x % 3 == 0 && x != 0)
  64. {
  65. System.out.print(" ");
  66. }
  67. }
  68. System.out.print("] ");
  69. System.out.println(p.getCurrentHealth()+ "/" + p.getHealth());
  70. }
  71. public static void displayBattle(Player p, Enemy e)
  72. {
  73. System.out.println("____________________________________________________________________\n____________________________________________________________________");
  74. System.out.println(" __________\n | BATTLE |\n ¯¯¯¯¯¯¯¯¯¯");
  75. displayHealthbar(e, 30);
  76. System.out.println();
  77. System.out.println(e.getSprite() + "\n");
  78. displayHealthbar(p, 30);
  79. System.out.println();
  80. }
  81. public static String promptBattleInventory(Player p)
  82. {
  83. while(true)
  84. {
  85. InventoryGUI.displayBattleInventory(p);
  86. if(p.getInventory().length == 0)
  87. {
  88. System.out.println("Nothing here except dust and spiders.");
  89. String[] inventoryOptionsNull = {"EXIT"};
  90. TUI.closedResponsePrompt("Options: EXIT", inventoryOptionsNull);
  91. return "exit";
  92. }
  93. String[] inventoryOptions = {"EXAMINE", "EXIT"};
  94. String input = TUI.closedResponsePrompt("Options: EXAMINE, EXIT", inventoryOptions);
  95. if(input.equals("exit"))
  96. {
  97. return "exit";
  98. }
  99. else if(input.equals("examine"))
  100. {
  101. este:while(true)
  102. {
  103. int numInput = TUI.closedIntResponse("Input a number to select an item to EXAMINE", p.getInventory().length + 1, 0);
  104. InventoryGUI.display(p.getInventory()[numInput-1]);
  105. estes:while(true)
  106. {
  107. if(p.getInventory()[numInput-1] instanceof EquipItem)
  108. {
  109. String[] itemOptions;
  110. String itemInput;
  111. if(!(p.isEquipped((EquipItem)(p.getInventory()[numInput-1]))))
  112. {
  113. itemOptions = new String[] {"EQUIP", "DISCARD", "EXIT"};
  114. itemInput = TUI.closedResponsePrompt("Options: EQUIP, DISCARD, EXIT", itemOptions);
  115. }
  116. else
  117. {
  118. itemOptions = new String[]{"UNEQUIP","DISCARD","EXIT"};
  119. itemInput = TUI.closedResponsePrompt("Options: UNEQUIP, DISCARD, EXIT", itemOptions);
  120. }
  121. if(itemInput.equals("equip"))
  122. {
  123. if (((EquipItem)p.getInventory()[numInput - 1]).getType() == 0)
  124. {
  125. p.equipWeapon((EquipItem)(p.getInventory()[numInput - 1]));
  126. }
  127. else
  128. {
  129. p.equipArmor((EquipItem)(p.getInventory()[numInput - 1]));
  130. }
  131. return "You equipped the " + p.getInventory()[numInput - 1].getName();
  132. }
  133. if(itemInput.equals("unequip"))
  134. {
  135. if (((EquipItem)p.getInventory()[numInput - 1]).getType() == 0)
  136. {
  137. p.unequipWeapon();
  138. }
  139. else
  140. {
  141. p.unequipArmor();
  142. }
  143. return "You unequipped the " + p.getInventory()[numInput - 1].getName();
  144. }
  145. if(itemInput.equals("discard"))
  146. {
  147. if(((EquipItem)(p.getInventory()[numInput - 1])).getType() == 0 && ((EquipItem)(p.getInventory()[numInput - 1])).isEquipped())
  148. {
  149. p.unequipWeapon();
  150. }
  151. else if(((EquipItem)(p.getInventory()[numInput - 1])).getType() == 1 && ((EquipItem)(p.getInventory()[numInput - 1])).isEquipped())
  152. {
  153. p.unequipArmor();
  154. }
  155. System.out.println("You discarded the " + p.getInventory()[numInput - 1].getName());
  156. p.removeItem(numInput-1);
  157. break este;
  158. }
  159. if(itemInput.equals("exit"))
  160. {
  161. break este;
  162. }
  163. }
  164. if(p.getInventory()[numInput - 1] instanceof ConsumableItem)
  165. {
  166. String[] itemOptions = {"USE", "DISCARD", "EXIT"};
  167. String itemInput = TUI.closedResponsePrompt("Options: USE, EXIT", itemOptions);
  168. if(itemInput.equals("use"))
  169. {
  170. int healthBoost = p.getCurrentHealth() + ((ConsumableItem)(p.getInventory()[numInput - 1])).getHealth();
  171. if(healthBoost + (p.getCurrentHealth()) > p.getHealth())
  172. {
  173. healthBoost = p.getHealth() - p.getCurrentHealth();
  174. }
  175. p.setCurrentHealth(p.getCurrentHealth() + healthBoost);
  176. String s = "You used 1 " + p.getInventory()[numInput - 1].getName();
  177. p.removeItem(numInput - 1);
  178. return s;
  179. }
  180. if(itemInput.equals("discard"))
  181. {
  182. String s = "You discarded 1 " + p.getInventory()[numInput - 1].getName();
  183. p.removeItem(numInput-1);
  184. return s;
  185. }
  186. if(itemInput.equals("exit"))
  187. {
  188. break este;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. public static String promptBattle(Player p, Enemy e)
  197. {
  198. String[] options = {"FIGHT", "CHECK", p.getName().toUpperCase(), "INVENTORY"};
  199. displayBattle(p, e);
  200. for(int x = 0; x < e.getEncounter().length() + 4; x++)
  201. {
  202. System.out.print("*");
  203. }
  204. System.out.println("\n* " + e.getEncounter() + " *");
  205. for(int x = 0; x < e.getEncounter().length() + 4; x++)
  206. {
  207. System.out.print("*");
  208. }
  209. System.out.println();
  210. mainLoop:while(true)
  211. {
  212. String input = TUI.closedResponsePrompt("Options: FIGHT, CHECK, " + p.getName().toUpperCase() + ", INVENTORY", options);
  213. if(input.equals("fight"))
  214. {
  215. boolean goesFirst = (p.getSpeed() >= e.getSpeed());
  216. if(goesFirst)
  217. {
  218. int enemyDamage = (p.getAttack() + p.getWeapon().getAttack() - e.getDefense());
  219. if(enemyDamage <= 0)
  220. {
  221. enemyDamage = 1;
  222. }
  223. e.setCurrentHealth(e.getCurrentHealth() - enemyDamage);
  224. if(e.getCurrentHealth() < 0)
  225. {
  226. e.setCurrentHealth(0);
  227. }
  228. }
  229. if(e.getCurrentHealth() != 0)
  230. {
  231. int playerDamage = (e.getAttack() - p.getArmor().getDefense() - p.getDefense());
  232. if(playerDamage <= 0)
  233. {
  234. playerDamage = 1;
  235. }
  236. p.setCurrentHealth(p.getCurrentHealth() - playerDamage);
  237. if(p.getCurrentHealth() < 0)
  238. {
  239. p.setCurrentHealth(0);
  240. }
  241. }
  242. if(e.getCurrentHealth() != 0 && !goesFirst)
  243. {
  244. int enemyDamage = (p.getAttack() + p.getWeapon().getAttack() - e.getDefense());
  245. if(enemyDamage <= 0)
  246. {
  247. enemyDamage = 1;
  248. }
  249. e.setCurrentHealth(e.getCurrentHealth() - enemyDamage);
  250. if(e.getCurrentHealth() < 0)
  251. {
  252. e.setCurrentHealth(0);
  253. }
  254. }
  255. displayBattle(p, e);
  256. if(goesFirst)
  257. {
  258. System.out.println("You attacked!");
  259. int enemyDamage = (p.getAttack() + p.getWeapon().getAttack() - e.getDefense());
  260. if(enemyDamage <= 0)
  261. {
  262. enemyDamage = 1;
  263. }
  264. System.out.println("Opposing " + e.getName() + " took " + enemyDamage + " damage!");
  265. if(e.getCurrentHealth() <= 0)
  266. {
  267. e.setCurrentHealth(e.getHealth());
  268. System.out.print("You defeated the " + e.getName() + "! ");
  269. System.out.println(e.getDeath());
  270. System.out.println("You found " + e.getMoney() + " gold!");
  271. p.setMoney(p.getMoney() + e.getMoney());
  272. System.out.println("You gained " + e.getEXP() + " EXP!");
  273. p.setEXP(p.getEXP() + e.getEXP());
  274. p.checkToLevel();
  275. if(!(e.getLoot().equals(EnemyResources.noLoot)))
  276. {
  277. int lootValue = generator.nextInt(e.getLoot().length);
  278. Entity dropt = e.getLoot()[lootValue].getItem();
  279. if(dropt instanceof ConsumableItem)
  280. {
  281. if(e.getLoot()[lootValue].getAmount() > 1)
  282. {
  283. System.out.println("You obtained " + e.getLoot()[lootValue].getAmount() + " " + dropt.getName() + "s!");
  284. for(int x = 0; x < e.getLoot()[lootValue].getAmount(); x++)
  285. {
  286. p.addItem(((ConsumableItem)(dropt)));
  287. }
  288. break mainLoop;
  289. }
  290. else
  291. {
  292. System.out.println("You obtained 1 " + dropt.getName() + "!");
  293. p.addItem((ConsumableItem)(dropt));
  294. break mainLoop;
  295. }
  296. }
  297. System.out.println("You obtained " + dropt.getName() + "!");
  298. p.addItem(((BuffItem)(dropt)));
  299. }
  300. break mainLoop;
  301. }
  302. }
  303. System.out.println("The opposing " + e.getName() + " attacked!");
  304. int playerDamage = (e.getAttack() - p.getArmor().getDefense() - p.getDefense());
  305. if(playerDamage <= 0)
  306. {
  307. playerDamage = 1;
  308. }
  309. if(p.getCurrentHealth() < 0)
  310. {
  311. p.setCurrentHealth(0);
  312. }
  313. System.out.println("You took " + playerDamage + " damage!");
  314. if(p.getCurrentHealth() <= 0)
  315. {
  316. System.out.println("You were defeated by the " + e.getName() + ".");
  317. if(p.getMoney() > 3)
  318. {
  319. System.out.println("You lost " + (p.getMoney()/3) + " gold and some dignity.");
  320. p.setMoney(p.getMoney() - p.getMoney()/3);
  321. }
  322. TUI.closedResponsePrompt("Options: CONTINUE", new String[] {"CONTINUE"});
  323. p.setCurrentHealth(p.getHealth()/3);
  324. return "loss";
  325. }
  326. if(!goesFirst)
  327. {
  328. System.out.println("You attacked!");
  329. int enemyDamage = (p.getAttack() + p.getWeapon().getAttack() - e.getDefense());
  330. if(enemyDamage <= 0)
  331. {
  332. enemyDamage = 1;
  333. }
  334. System.out.println("Opposing " + e.getName() + " took " + enemyDamage + " damage!");
  335. if(e.getCurrentHealth() <= 0)
  336. {
  337. e.setCurrentHealth(e.getHealth());
  338. System.out.print("You defeated the " + e.getName() + "! ");
  339. System.out.println(e.getDeath());
  340. System.out.println("You found " + e.getMoney() + " gold!");
  341. p.setMoney(p.getMoney() + e.getMoney());
  342. System.out.println("You gained " + e.getEXP() + " EXP!");
  343. p.setEXP(p.getEXP() + e.getEXP());
  344. p.checkToLevel();
  345. if(!(e.getLoot().equals(EnemyResources.noLoot)))
  346. {
  347. int lootValue = generator.nextInt(e.getLoot().length);
  348. Entity dropt = e.getLoot()[lootValue].getItem();
  349. if(dropt instanceof ConsumableItem)
  350. {
  351. if(e.getLoot()[lootValue].getAmount() > 1)
  352. {
  353. System.out.println("You obtained " + e.getLoot()[lootValue].getAmount() + " " + dropt.getName() + "s!");
  354. for(int x = 0; x < e.getLoot()[lootValue].getAmount(); x++)
  355. {
  356. p.addItem(((ConsumableItem)(dropt)));
  357. }
  358. break mainLoop;
  359. }
  360. else
  361. {
  362. System.out.println("You obtained 1 " + dropt.getName() + "!");
  363. p.addItem((ConsumableItem)(dropt));
  364. break mainLoop;
  365. }
  366. }
  367. System.out.println("You obtained " + dropt.getName() + "!");
  368. p.addItem(((BuffItem)(dropt)));
  369. }
  370. break mainLoop;
  371. }
  372. }
  373. }
  374. else if(input.equals("check"))
  375. {
  376. System.out.println("Name: " + e.getName());
  377. System.out.println("Info: " + e.getDesc());
  378. displayStats(e);
  379. }
  380. else if(input.equals(p.getName().toLowerCase()))
  381. {
  382. System.out.println("____________________________________________________________________\n____________________________________________________________________");
  383. System.out.println(p.getSprite());
  384. System.out.println("Name: " + p.getName());
  385. System.out.println("Info: Yes, that unicode abomination is in fact you.");
  386. displayStats(p);
  387. String[] selfCheckOptions = {"EXIT"};
  388. TUI.closedResponsePrompt("Options: EXIT", selfCheckOptions);
  389. displayBattle(p, e);
  390. }
  391. else if(input.equals("inventory"))
  392. {
  393. String s = promptBattleInventory(p);
  394. if(s.equals("exit"))
  395. {
  396. displayBattle(p, e);
  397. }
  398. else
  399. {
  400. if(e.getCurrentHealth() != 0)
  401. {
  402. int playerDamage = (e.getAttack() - p.getArmor().getDefense() - p.getDefense());
  403. if(playerDamage <= 0)
  404. {
  405. playerDamage = 1;
  406. }
  407. p.setCurrentHealth(p.getCurrentHealth() - playerDamage);
  408. if(p.getCurrentHealth() < 0)
  409. {
  410. p.setCurrentHealth(0);
  411. }
  412. }
  413. displayBattle(p, e);
  414. System.out.println(s);
  415. System.out.println("The opposing " + e.getName() + " attacked!");
  416. int playerDamage = (e.getAttack() - p.getArmor().getDefense() - p.getDefense());
  417. if(playerDamage <= 0)
  418. {
  419. playerDamage = 1;
  420. }
  421. System.out.println("You took " + playerDamage + " damage!");
  422. if(p.getCurrentHealth() <= 0)
  423. {
  424. System.out.println("You were defeated by the " + e.getName() + ".");
  425. if(p.getMoney() > 3)
  426. {
  427. System.out.println("You lost " + (p.getMoney()/3) + " gold and some dignity.");
  428. p.setMoney(p.getMoney() - p.getMoney()/3);
  429. }
  430. TUI.closedResponsePrompt("Options: CONTINUE", new String[] {"CONTINUE"});
  431. p.setCurrentHealth(p.getHealth()/3);
  432. return "loss";
  433. }
  434. }
  435. }
  436. }
  437. while(true)
  438. {
  439. String[] endOptions = {"CONTINUE", p.getName().toUpperCase(), "INVENTORY"};
  440. String inputEnd = TUI.closedResponsePrompt("Options: CONTINUE, " + p.getName().toUpperCase() + ", INVENTORY", endOptions);
  441. if(inputEnd.equals("inventory"))
  442. {
  443. InventoryGUI.promptInventory(p);
  444. }
  445. else if(inputEnd.equals(p.getName().toLowerCase()))
  446. {
  447. System.out.println("____________________________________________________________________\n____________________________________________________________________");
  448. System.out.println(p.getSprite());
  449. System.out.println("Name: " + p.getName());
  450. System.out.println("Info: Yes, that unicode abomination is in fact you.");
  451. displayStats(p);
  452. String[] selfCheckOptions = {"EXIT"};
  453. TUI.closedResponsePrompt("Options: EXIT", selfCheckOptions);
  454. }
  455. else if(inputEnd.equals("continue"))
  456. {
  457. return "win";
  458. }
  459. }
  460. }
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement