Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' into features/tutorial_dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoan12 authored Dec 6, 2022
2 parents d57c06c + 75a7b8a commit f4cc2a1
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/mg105/entities/Consumable.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
public interface Consumable {

/**
* This method allow a character to use an item on themselves. It does NOT remove an item from the inventory.
*
* @param character the battleCharacter to use the item on
*This method allow a character to use an item on themselves. It does NOT remove an item from the inventory.
* @param state the state of the game
* @param characterName the name of the character to use the item on
*/
void consume(@NotNull BattleCharacter character);
void consume(@NotNull GameState state, @NotNull String characterName);
}
24 changes: 23 additions & 1 deletion src/main/java/com/mg105/entities/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public BattleCharacter getPartyAliveMember(@NotNull String characterName) {
return null;
}

/**
* Adds the battleCharacter to the party
* @param character the ALIVE character to add to the party
*/
public void addPartyMemberToAlive(BattleCharacter character){
this.party.add(character);
}

/**
* Returns the character in the party based on the given name
* Only returns a party member that is fainted
Expand All @@ -96,6 +104,20 @@ public BattleCharacter getFaintedPartyMember(@NotNull String characterName) thro
return null;
}


/**
* Removes the given fainted party
* This should only be done to fainted party members who's hp is about to become above zero
* @param characterName the name of the party member that is fainted
*/
public void removeFaintedPartyMember(@NotNull String characterName){
BattleCharacter character = getFaintedPartyMember(characterName);
if(character == null){
return;
}
this.fainted.remove(character);
}

/**
* Returns the correct party member given a name
* @param characterName the name of the party member to return
Expand All @@ -105,7 +127,7 @@ public BattleCharacter getFaintedPartyMember(@NotNull String characterName) thro

public @NotNull BattleCharacter getPartyMember(String characterName) throws NoSuchElementException{
BattleCharacter member = getPartyAliveMember(characterName);
if( member != null){
if(member != null){
return member;
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/mg105/entities/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,22 @@ public void removeAll() {
/**
* Uses the item on the given character
*
* @param character The character to use the item on
* @param state The current state of the game
* @param itemName The name of the item to use
* @param characterName the character to use the item on
* @return true iff the item with a name of itemName was used
* @see Item
* @see Consumable
*/

public boolean useItem(@NotNull BattleCharacter character, @NotNull String itemName) {
public boolean useItem(@NotNull GameState state, @NotNull String itemName, @NotNull String characterName) {
for (int i = 0; i < numberOfItems(); i++) {
Item item = this.items.get(i);

if (item.getName().equals(itemName)) {

if (item instanceof Consumable consumableItem) {
consumableItem.consume(character);
consumableItem.consume(state, characterName);
this.items.remove(i);
return true;

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/mg105/entities/items/HealthPotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mg105.entities.BattleCharacter;
import com.mg105.entities.Consumable;
import com.mg105.entities.GameState;
import com.mg105.entities.Item;
import com.mg105.utils.ItemConstants;
import org.jetbrains.annotations.NotNull;
Expand All @@ -28,12 +29,16 @@ public static int getHealingPoints() {

/**
* Heals the battleCharacter provided
*
* @param character the battleCharacter to use the item on
* @see Consumable
* @param state the state of the game
* @param characterName the name of the character to use the item on
*/
@Override
public void consume(@NotNull BattleCharacter character) {
public void consume(@NotNull GameState state, @NotNull String characterName) {
BattleCharacter character = state.getPartyMember(characterName);
if(character.getHp() == 0){
state.removeFaintedPartyMember(characterName);
state.addPartyMemberToAlive(character);
}
character.modifyHealth(HEALING_POINTS);

}
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/mg105/entities/items/MegaPotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mg105.entities.BattleCharacter;
import com.mg105.entities.Consumable;
import com.mg105.entities.GameState;
import com.mg105.entities.Item;
import com.mg105.utils.ItemConstants;
import org.jetbrains.annotations.NotNull;
Expand All @@ -26,12 +27,24 @@ public static int getHealingPoints() {

/**
* Heals the battleCharacter provided
*
* @param character the battleCharacter to use the item on
* @see Consumable
* @param state the state of the game
* @param characterName the name of the character to use the item on
*/
@Override
public void consume(@NotNull BattleCharacter character) {
public void consume(@NotNull GameState state, @NotNull String characterName) {
BattleCharacter character = state.getFaintedPartyMember(characterName);
if(character != null){
state.removeFaintedPartyMember(characterName);
state.addPartyMemberToAlive(character);
character.modifyHealth(HEALING_POINTS);
return;
}

character = state.getPartyAliveMember(characterName);

if(character == null){
return;
}
character.modifyHealth(HEALING_POINTS);

}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/mg105/entities/items/UpgradeToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mg105.entities.BattleCharacter;
import com.mg105.entities.Consumable;
import com.mg105.entities.GameState;
import com.mg105.entities.Item;
import com.mg105.utils.ItemConstants;
import org.jetbrains.annotations.NotNull;
Expand All @@ -22,11 +23,12 @@ public UpgradeToken() {

/**
* Upgrades the stats of the given character
*
* @param character the BattleCharacter to use the item on
* @param state the state of the game
* @param characterName the name of the character to use the item on
*/
@Override
public void consume(@NotNull BattleCharacter character) {
public void consume(@NotNull GameState state, @NotNull String characterName) {
BattleCharacter character = state.getPartyMember(characterName);
character.modifyDamage(1);
character.modifyMaxHp(1);
character.modifySpeed(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mg105.user_interface.Alert;
package com.mg105.interface_adapters;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mg105.interface_adapters.inventory;

import com.mg105.user_interface.Alert.Alert;
import com.mg105.interface_adapters.Alert;

/**
* The interface defines all the functions that should be called to update the ui to add or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mg105.use_cases.inventory;

import com.mg105.entities.BattleCharacter;
import com.mg105.entities.GameState;
import com.mg105.use_cases.outputds.ItemDetails;
import com.mg105.utils.ItemConstants;
Expand Down Expand Up @@ -87,8 +86,7 @@ public void useItem(@NotNull String itemName, @NotNull String characterName) {
response.useItem(false, characterName, getItemDetails(itemName));
return;
}
BattleCharacter partyMember = state.getPartyMember(characterName);
boolean isUsed = state.getInventory().useItem(partyMember, itemName);
boolean isUsed = state.getInventory().useItem(state, itemName,characterName);

response.useItem(isUsed, characterName, getItemDetails(itemName));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mg105.user_interface.Alert;
package com.mg105.user_interface;

import javafx.geometry.Pos;
import javafx.scene.Scene;
Expand All @@ -22,8 +22,8 @@ public void display(String msg) {
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Alert");
window.setHeight(100);
window.setWidth(200);
window.setHeight(400);
window.setWidth(400);
window.setResizable(false);
Label label = new Label();
label.setText(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mg105.interface_adapters.inventory.InventoryController;
import com.mg105.interface_adapters.inventory.InventoryViewInterface;
import com.mg105.user_interface.Alert.AlertBox;
import com.mg105.utils.PartyConstants;
import javafx.scene.Scene;
import javafx.scene.control.Button;
Expand Down
22 changes: 18 additions & 4 deletions src/test/java/com/mg105/entities/InventoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.mg105.utils.ItemConstants;
import org.junit.jupiter.api.Test;

import java.awt.*;

import static org.junit.jupiter.api.Assertions.*;

class InventoryTest {
Expand All @@ -16,7 +18,7 @@ void numberOfItemsEmpty() {
Inventory inventory = new Inventory();
assertEquals(0, inventory.numberOfItems());
}

@Test
void inLimit() {
Inventory inventory = new Inventory();
Expand Down Expand Up @@ -346,7 +348,11 @@ void useItemNotInInventory() {
inventory.addItem(new HealthPotion());
inventory.addItem(new HealthPotion());

assertFalse(inventory.useItem(character, ItemConstants.UPGRADE_TOKEN_NAME));
BattleCharacter[] cs = {character};

GameState state = new GameState(inventory, cs, new WalkingCharacter(new Point()));

assertFalse(inventory.useItem(state, ItemConstants.UPGRADE_TOKEN_NAME, "John"));

}

Expand All @@ -361,7 +367,11 @@ void useItemSingle() {

inventory.addItem(new UpgradeToken());

inventory.useItem(character, ItemConstants.UPGRADE_TOKEN_NAME);
BattleCharacter[] cs = {character};

GameState state = new GameState(inventory, cs, new WalkingCharacter(new Point()));

inventory.useItem(state, ItemConstants.UPGRADE_TOKEN_NAME, "John");

assertEquals(0, inventory.numberOfItems());

Expand All @@ -386,7 +396,11 @@ void useItemHasUsableItems() {
inventory.addItem(new HealthPotion());
inventory.addItem(new HealthPotion());

inventory.useItem(character, ItemConstants.HEALTH_POTION_NAME);
BattleCharacter[] cs = {character};

GameState state = new GameState(inventory, cs, new WalkingCharacter(new Point()));

inventory.useItem(state, ItemConstants.HEALTH_POTION_NAME, "John");

assertEquals(1, inventory.numberOfItems());

Expand Down

0 comments on commit f4cc2a1

Please sign in to comment.