Skip to content

Commit

Permalink
Merge pull request #92 from B1G-SAM/ShopImplementation
Browse files Browse the repository at this point in the history
Partly add shop things
  • Loading branch information
B1G-SAM committed Apr 7, 2024
2 parents 95e4b27 + e865387 commit b4dd134
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 38 deletions.
26 changes: 17 additions & 9 deletions src/main/java/command/fight/FightingCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command.fight;
import command.Command;
import map.BaseMap;
import map.ShopMap;

import java.util.Scanner;

Expand All @@ -20,15 +21,22 @@ public void execute() {

@Override
public void execute(Scanner in) {
currentMap.enableFight(in);
BaseMap.currentMap = mapIndex.get(FIRST_MAP_IDENTITY);
if (currentMap.getEntityDeath()){
int xPos = storedMaps.get(BaseMap.currentMap).getInteractX();
int yPos = storedMaps.get(BaseMap.currentMap).getInteractY();
storedMaps.get(BaseMap.currentMap).clearSpot(xPos, yPos);
currentMap.handleLootingByPlayer();
} else if (currentMap.getPlayerDeath()){
currentMap.handleDeath();
if (currentMap instanceof map.battleinterface.BattleInterface) {
currentMap.enableFight(in);
BaseMap.currentMap = mapIndex.get(FIRST_MAP_IDENTITY);
if (currentMap.getEntityDeath()) {
int xPos = storedMaps.get(BaseMap.currentMap).getInteractX();
int yPos = storedMaps.get(BaseMap.currentMap).getInteractY();
storedMaps.get(BaseMap.currentMap).clearSpot(xPos, yPos);
currentMap.handleLootingByPlayer();
} else if (currentMap.getPlayerDeath()) {
currentMap.handleDeath();
}
}

if (currentMap instanceof ShopMap) {
currentMap.enableFight(in);
BaseMap.currentMap = mapIndex.get(FIRST_MAP_IDENTITY);
}
}
}
23 changes: 14 additions & 9 deletions src/main/java/command/mapmove/InteractingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.Objects;

import static main.CalculaChroniclesOfTheAlgorithmicKingdom.PLAYER_INVENTORY;
import static map.BaseMap.storedMaps;
import static map.BaseMap.mapIndex;
import static map.MapGenerator.CENTAUR;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void execute() {
break;
case DEMON:
monster = new Demon(15, 15, 15, xPos, yPos, 15, 15);
textBox.setNextDialogue("*the " + monster.getName() + " stares at you menacingly*");
textBox.setNextDialogue("*the " + monster.getName() + " growls at you menacingly*");
battleMap = new BattleInterface(playerStatus, textBox, monster);
battleMap.initMap(30, monster.getHeight());
storedMaps.add(battleMap);
Expand All @@ -70,7 +71,7 @@ public void execute() {
break;
case DRAGON:
monster = new Dragon(20, 20, 20, xPos, yPos, 20, 20);
textBox.setNextDialogue("*the " + monster.getName() + " stares at you menacingly*");
textBox.setNextDialogue("*the " + monster.getName() + " breathes a ball of flame menacingly*");
battleMap = new BattleInterface(playerStatus, textBox, monster);
battleMap.initMap(30, monster.getHeight());
storedMaps.add(battleMap);
Expand All @@ -79,7 +80,7 @@ public void execute() {
break;
case GOBLIN:
monster = new Goblin(25, 25, 25, xPos, yPos, 25, 25);
textBox.setNextDialogue("*the " + monster.getName() + " stares at you menacingly*");
textBox.setNextDialogue("*the " + monster.getName() + " laughs maniacally*");
battleMap = new BattleInterface(playerStatus, textBox, monster);
battleMap.initMap(30, monster.getHeight());
storedMaps.add(battleMap);
Expand All @@ -88,7 +89,7 @@ public void execute() {
break;
case GRYPHON:
monster = new Gryphon(30, 30, 30, xPos, yPos, 30, 30);
textBox.setNextDialogue("*the " + monster.getName() + " stares at you menacingly*");
textBox.setNextDialogue("*the " + monster.getName() + " screams at you loudly*");
battleMap = new BattleInterface(playerStatus, textBox, monster);
battleMap.initMap(30, monster.getHeight());
storedMaps.add(battleMap);
Expand All @@ -98,17 +99,21 @@ public void execute() {
case SHOP: //some shopkeeper
ShopMap shopMap;
ShopKeeper shopkeeper = new ShopKeeper("src/main/resources/ShopKeeper/ShopKeeper",
"Hi welcome to my shop!");
shopMap = new ShopMap(playerStatus, textBox, shopkeeper);
"*meow* Hi welcome to my shop! *meow*");
shopMap = new ShopMap(playerStatus, textBox, shopkeeper, PLAYER_INVENTORY);
shopMap.initMap(30, 0); // Set appropriate width and height
//shopMap.printShopItems();
shopkeeper.addConsumable(20, 0, "The caffeine is so strong, it heals wounds",
"Cup of Coffee", 10);
shopkeeper.addConsumable(0, 100, "Gun with a single round. Why does a cat have a " +
"gun anyway?", "Desert Eagle", 50);

storedMaps.add(shopMap);

mapIndex.put(SHOP, storedMaps.size() - 1);
BaseMap.currentMap = mapIndex.get(SHOP);

//((ShopMap) battleMap).interact(textBox); // Interact with the shop
textBox.setNextNarration(shopkeeper.getDefaultMessgage());
textBox.setNextNarration("You are greeted by a cat with oddly small eyes.\n");
textBox.setNextInstruction("To enter the shop enter [fight]. To leave now, enter [run].");
break;

default:
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/interactable/ShopKeeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

public class ShopKeeper extends InteractableEntity{
protected ArrayList<ShopItem> shopItems;
protected String defaultMessgage; //whatever the guy says to introduce his items
protected String defaultMessage; //whatever the guy says to introduce his items
protected String filePath;


public ShopKeeper(String filePathway, String message){
//addConsumable();
this.filePath = filePathway;
this.defaultMessgage = message;

this.defaultMessage = message;
this.shopItems = new ArrayList<>();
}

public ArrayList<ShopItem> getShopItems() {
Expand All @@ -27,24 +27,28 @@ public void addConsumable(int heal, int damage, String itemDescription, String i
shopItems.add(newConsumable);
}

public String getDefaultMessgage() {
return defaultMessgage;
public String getDefaultMessage() {
return defaultMessage;
}


public String formatShop() {
StringBuilder formattedList = new StringBuilder();
for (int i = 0; i < shopItems.size(); i += 1) {
ShopItem item = shopItems.get(i);
formattedList.append(i + 1).append(". ")
.append(item.getName())
.append(" - ")
.append(item.getDescription())
.append("\n");
if (shopItems != null) {
StringBuilder formattedList = new StringBuilder();
for (int i = 0; i < shopItems.size(); i += 1) {
ShopItem item = shopItems.get(i);
formattedList.append(i + 1).append(". ")
.append(item.getName())
.append(" - ")
.append(item.getDescription())
.append("\n");
}
return formattedList.toString();
}
return formattedList.toString();
return "The shop is empty";
}


@Override
public int getHealth() {
return 0;
Expand Down
62 changes: 56 additions & 6 deletions src/main/java/map/ShopMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

import interactable.ShopKeeper;
import filereader.FileReader;
import inventoryitems.ShopItem;
import textbox.PlayerStatus;
import textbox.TextBox;
import ui.Ui;

import java.util.ArrayList;
import java.util.Scanner;


public class ShopMap extends BaseMap{
protected PlayerStatus currentPlayer;
protected TextBox currentTextBox;
protected ShopKeeper currentEntity;
protected PlayerInventory inventory;

public ShopMap(PlayerStatus player, TextBox text, ShopKeeper shopKeeper){
public ShopMap(PlayerStatus player, TextBox text, ShopKeeper shopKeeper, PlayerInventory bag){
this.currentPlayer = player;
this.currentTextBox = text;
this.currentEntity = shopKeeper;
this.inventory = bag;
//this.currentMap = new ArrayList<>(height);
//loadShopMap();
}
Expand All @@ -37,17 +42,62 @@ public void initMap(int givenWidth, int givenHeight) {


public void queueTextBox(){
currentTextBox.setNextNarration("Welcome to " + currentEntity.getName() + "'s shop");
currentTextBox.setNextDialogue(currentEntity.getDefaultMessgage() + "\n" + currentEntity.formatShop());
currentTextBox.setNextInstruction("Enter the index of the item you wish to purchase");
currentTextBox.setNextNarration("You are greeted by a cat with oddly small eyes.\n");
currentTextBox.setNextDialogue(currentEntity.getDefaultMessage() + "\n" + currentEntity.formatShop());
currentTextBox.setNextInstruction("Give the shop keeper an [INDEX] to view the item and purchase" +
" or enter [exit]" +
" to leave the shop.");
}


@Override
public void enableFight() {

public void enableFight(){

}


@Override
public void enableFight(Scanner in) {
String answerCommand = "";
Ui ui = new Ui();
queueTextBox();
while (!answerCommand.equalsIgnoreCase("exit")) {

ui.printPlayerStatus(currentPlayer);
ui.printTextBox(currentTextBox);

answerCommand = in.nextLine().trim();

// Check if the input is numeric
if (answerCommand.matches("\\d+")) {
int index = Integer.parseInt(answerCommand) - 1;
ArrayList<ShopItem> shopItems = currentEntity.getShopItems();

if (index >= 0 && index < shopItems.size()) {
ShopItem item = shopItems.get(index);
if (currentPlayer.getPlayerMoney() >= item.getPrice()) {
int currentMoney = currentPlayer.getPlayerMoney();
currentPlayer.setPlayerMoney(currentMoney - item.getPrice());
currentTextBox.setNextNarration("NEW ITEM ADDED TO INVENTORY");
inventory.addItems(item);
} else {
currentTextBox.setNextNarration("You are greeted by a cat with oddly small eyes.\n");
}
} else {
currentTextBox.setNextError("Invalid index. Please enter a valid item index or 'exit'.");
}
} else if (!answerCommand.equalsIgnoreCase("exit")) {
currentTextBox.setNextError("Invalid command. Please enter a valid item index or 'exit'.");
}

currentTextBox.setNextDialogue(currentEntity.getDefaultMessage() + "\n" + currentEntity.formatShop());
currentTextBox.setNextInstruction("Give the shopkeeper an [INDEX] to view the item and purchase or " +
"enter [exit]" +
" to leave the shop.");
}
}


@Override
public boolean getEntityDeath() {
return false;
Expand Down

0 comments on commit b4dd134

Please sign in to comment.