Skip to content

Commit

Permalink
Merge pull request #63 from hdescottes/Implement_equipment_set
Browse files Browse the repository at this point in the history
feat: Add set item
  • Loading branch information
hdescottes authored Jun 26, 2024
2 parents 6a5a12c + 5f3de49 commit 9a9b64b
Show file tree
Hide file tree
Showing 31 changed files with 535 additions and 53 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ graph TD;
- [ ] implement bonus gained by class
- [ ] implement magic, resist, ... stats

- [ ] armor sets

- [ ] crafting feature

- [ ] credits
Expand All @@ -94,6 +92,7 @@ graph TD;
-------
## Bugs

- Objects in inventory are stacking even if there are not the same
- FadeIn effect not working when transition [menu → game]
- Drag and drop selection issue on item's render (items are far from mouse when picking them)
- Sometimes current hp/mp on status HUD add a blank space before the max player's hp/mp
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/gdx/game/battle/BattleHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import com.gdx.game.entities.player.characterClass.ClassObserver;
import com.gdx.game.entities.player.characterClass.tree.Node;
import com.gdx.game.entities.player.characterClass.tree.Tree;
import com.gdx.game.inventory.InventoryItem;
import com.gdx.game.inventory.InventoryItemLocation;
import com.gdx.game.inventory.item.InventoryItem;
import com.gdx.game.inventory.item.InventoryItemLocation;
import com.gdx.game.inventory.InventoryObserver;
import com.gdx.game.manager.ResourceManager;
import com.gdx.game.map.MapManager;
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/java/com/gdx/game/battle/BattleInventoryUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.utils.Array;
import com.gdx.game.entities.Entity;
import com.gdx.game.inventory.InventoryItem;
import com.gdx.game.inventory.InventoryItemFactory;
import com.gdx.game.inventory.InventoryItemLocation;
import com.gdx.game.inventory.item.InventoryItem;
import com.gdx.game.inventory.item.InventoryItemFactory;
import com.gdx.game.inventory.item.InventoryItemLocation;
import com.gdx.game.inventory.InventoryObserver;
import com.gdx.game.inventory.InventorySubject;
import com.gdx.game.inventory.slot.InventorySlot;
Expand Down
29 changes: 29 additions & 0 deletions core/src/main/java/com/gdx/game/battle/BattleState.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import static com.gdx.game.common.UtilityClass.calculateBonus;

public class BattleState extends BattleSubject {
private static final Logger LOGGER = LoggerFactory.getLogger(BattleState.class);

Expand All @@ -35,6 +41,8 @@ public BattleState() {

currentPlayerAP = ProfileManager.getInstance().getProperty("currentPlayerAP", Integer.class);
currentPlayerDP = ProfileManager.getInstance().getProperty("currentPlayerDP", Integer.class);

updateStatWithBonus("bonusSet");
}

public void resetDefaults() {
Expand Down Expand Up @@ -82,6 +90,27 @@ public void determineTurn() {
Timer.schedule(determineTurn, 1);
}

private void updateStatWithBonus(String bonusAttribute) {
HashMap<String, Integer> bonusMap = calculateBonus(bonusAttribute);
Map<String, String> mapping = Map.of(
EntityConfig.EntityProperties.ENTITY_PHYSICAL_ATTACK_POINTS.name(), "currentPlayerAP",
EntityConfig.EntityProperties.ENTITY_PHYSICAL_DEFENSE_POINTS.name(), "currentPlayerDP"
);

mapping.forEach((key, value) -> {
if (bonusMap.get(key) != null) {
try {
Field valueField = this.getClass().getDeclaredField(value);
int currentPlayerValue = valueField.getInt(this);
int newPlayerValue = currentPlayerValue + bonusMap.get(key);
valueField.setInt(this, newPlayerValue);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("A value in the mapping map does not exist", e);
}
}
});
}

public void playerAttacks() {
if (currentOpponent == null) {
return;
Expand Down
40 changes: 40 additions & 0 deletions core/src/main/java/com/gdx/game/common/UtilityClass.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package com.gdx.game.common;

import com.badlogic.gdx.utils.Array;
import com.gdx.game.entities.EntityBonus;
import com.gdx.game.entities.EntityConfig;
import com.gdx.game.profile.ProfileManager;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import static java.lang.Float.parseFloat;

public class UtilityClass {

public static <T, E> HashMap<E, T> mapInverter(HashMap<T, E> hashMap){
Expand All @@ -27,4 +35,36 @@ public static <T, E> Map<E, T> mapInverter(Map<T, E> hashMap){
return newMap;
}

public static HashMap<String, Integer> calculateBonus(String bonusProperty) {
final Hashtable<String, String> attributeTable = new Hashtable<>() {{
put(EntityConfig.EntityProperties.ENTITY_PHYSICAL_ATTACK_POINTS.name(), "currentPlayerAP");
put(EntityConfig.EntityProperties.ENTITY_PHYSICAL_DEFENSE_POINTS.name(), "currentPlayerDP");
}};

HashMap<String, Integer> bonusStatMap = new HashMap<>();
Array<EntityBonus> bonusArray = ProfileManager.getInstance().getProperty(bonusProperty, Array.class);

if (bonusArray == null || bonusArray.isEmpty()) {
return bonusStatMap;
}

HashMap<String, String> bonusEntityValues = new HashMap<>();
for (EntityBonus entityBonus : bonusArray) {
bonusEntityValues.put(entityBonus.getEntityProperty(), entityBonus.getValue());
}

for (String key : attributeTable.keySet()) {
float bonusValue = parseFloat(bonusEntityValues.get(key));
if (bonusValue > 1) {
bonusStatMap.put(key, (int) bonusValue);
} else {
int playerStat = ProfileManager.getInstance().getProperty(attributeTable.get(key), Integer.class);
int bonusStat = (int) Math.floor(playerStat * bonusValue);
bonusStatMap.put(key, bonusStat);
}
}

return bonusStatMap;
}

}
31 changes: 31 additions & 0 deletions core/src/main/java/com/gdx/game/entities/EntityBonus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.gdx.game.entities;

public class EntityBonus {

private String entityProperty;
private String value;

public EntityBonus() {
}

public EntityBonus(String entityProperty, String value) {
this.entityProperty = entityProperty;
this.value = value;
}

public String getEntityProperty() {
return entityProperty;
}

public void setEntityProperty(String entityProperty) {
this.entityProperty = entityProperty;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/com/gdx/game/entities/EntityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import com.gdx.game.inventory.InventoryItem;
import com.gdx.game.inventory.item.InventoryItem;

public class EntityConfig {
private Array<AnimationConfig> animationConfig;
Expand Down
22 changes: 14 additions & 8 deletions core/src/main/java/com/gdx/game/entities/player/PlayerHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
import com.gdx.game.entities.player.characterClass.ClassObserver;
import com.gdx.game.entities.player.characterClass.tree.Node;
import com.gdx.game.entities.player.characterClass.tree.Tree;
import com.gdx.game.inventory.InventoryItem;
import com.gdx.game.inventory.InventoryItemLocation;
import com.gdx.game.inventory.InventoryObserver;
import com.gdx.game.inventory.InventoryUI;
import com.gdx.game.inventory.item.InventoryItem;
import com.gdx.game.inventory.item.InventoryItemLocation;
import com.gdx.game.inventory.slot.InventorySlot;
import com.gdx.game.inventory.store.StoreInventoryObserver;
import com.gdx.game.inventory.store.StoreInventoryUI;
import com.gdx.game.manager.ResourceManager;
Expand All @@ -48,6 +49,9 @@
import com.gdx.game.status.StatusObserver;
import com.gdx.game.status.StatusUI;

import static com.gdx.game.inventory.InventoryUI.getInventory;
import static com.gdx.game.inventory.InventoryUI.setBonusFromSet;

public class PlayerHUD implements Screen, AudioSubject, ProfileObserver, ClassObserver, ComponentObserver, ConversationGraphObserver, BattleObserver, StoreInventoryObserver, InventoryObserver, StatusObserver {

private Stage stage;
Expand Down Expand Up @@ -282,7 +286,7 @@ public void onNotify(ProfileManager profileManager, ProfileEvent event) {
itemLocations.add(new InventoryItemLocation(i, items.get(i).toString(), 1, InventoryUI.PLAYER_INVENTORY));
}
InventoryUI.populateInventory(inventoryUI.getInventorySlotTable(), itemLocations, inventoryUI.getDragAndDrop(), InventoryUI.PLAYER_INVENTORY, false);
profileManager.setProperty("playerInventory", InventoryUI.getInventory(inventoryUI.getInventorySlotTable()));
profileManager.setProperty("playerInventory", getInventory(inventoryUI.getInventorySlotTable()));

//start the player with some money
statusUI.setGoldValue(20);
Expand All @@ -297,6 +301,9 @@ public void onNotify(ProfileManager profileManager, ProfileEvent event) {
inventoryUI.resetEquipSlots();
if (equipInventory != null && equipInventory.size > 0) {
InventoryUI.populateInventory(inventoryUI.getEquipSlotTable(), equipInventory, inventoryUI.getDragAndDrop(), InventoryUI.PLAYER_INVENTORY, false);
if (inventoryUI.isSetEquipped(equipInventory)) {
setBonusFromSet(((InventorySlot) inventoryUI.getEquipSlotTable().getCells().get(1).getActor()).getTopInventoryItem());
}
}

Array<QuestGraph> quests = profileManager.getProperty("playerQuests", Array.class);
Expand Down Expand Up @@ -329,8 +336,8 @@ public void onNotify(ProfileManager profileManager, ProfileEvent event) {
}
case SAVING_PROFILE -> {
profileManager.setProperty("playerQuests", questUI.getQuests());
profileManager.setProperty("playerInventory", InventoryUI.getInventory(inventoryUI.getInventorySlotTable()));
profileManager.setProperty("playerEquipInventory", InventoryUI.getInventory(inventoryUI.getEquipSlotTable()));
profileManager.setProperty("playerInventory", getInventory(inventoryUI.getInventorySlotTable()));
profileManager.setProperty("playerEquipInventory", getInventory(inventoryUI.getEquipSlotTable()));
if (mapManager.getPlayer() != null) {
profileManager.setProperty("playerCharacter", EntityFactory.EntityType.valueOf(mapManager.getPlayer().getEntityConfig().getEntityID()));
}
Expand All @@ -344,8 +351,6 @@ public void onNotify(ProfileManager profileManager, ProfileEvent event) {
profileManager.setProperty("currentPlayerHPMax", statusUI.getHPValueMax());
profileManager.setProperty("currentPlayerMP", statusUI.getMPValue());
profileManager.setProperty("currentPlayerMPMax", statusUI.getMPValueMax());
profileManager.setProperty("currentPlayerAP", inventoryUI.getAPVal());
profileManager.setProperty("currentPlayerDP", inventoryUI.getDPVal());
profileManager.setProperty("currentPlayerSPDP", inventoryUI.getSPDPVal());
}
case CLEAR_CURRENT_PROFILE -> {
Expand All @@ -364,6 +369,7 @@ public void onNotify(ProfileManager profileManager, ProfileEvent event) {
profileManager.setProperty("currentPlayerDP", 0);
profileManager.setProperty("currentPlayerSPDP", 0);
profileManager.setProperty("currentTime", 0);
profileManager.setProperty("bonusSet", null);
}
default -> {
}
Expand Down Expand Up @@ -421,7 +427,7 @@ public void onNotify(ConversationGraph graph, ConversationCommandEvent event) {
break;
}

Array<InventoryItemLocation> inventory = InventoryUI.getInventory(inventoryUI.getInventorySlotTable());
Array<InventoryItemLocation> inventory = getInventory(inventoryUI.getInventorySlotTable());
storeInventoryUI.loadPlayerInventory(inventory);

Array<InventoryItem.ItemTypeID> items = selectedEntity.getEntityConfig().getInventory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Json;
import com.gdx.game.entities.EntityConfig;
import com.gdx.game.profile.ProfileManager;

import java.util.*;
Expand Down Expand Up @@ -112,10 +113,10 @@ public Node checkForClassUpgrade(String currentClassId, int currentPlayerCharact
List<Node> nodes = findToWhichClassesUpgrade(currentClassId);

if (nodes != null && nodes.size() == 2) {
int AP0 = Integer.parseInt(nodes.get(0).getRequirements().get("ENTITY_PHYSICAL_ATTACK_POINTS"));
int DP0 = Integer.parseInt(nodes.get(0).getRequirements().get("ENTITY_PHYSICAL_DEFENSE_POINTS"));
int AP1 = Integer.parseInt(nodes.get(1).getRequirements().get("ENTITY_PHYSICAL_ATTACK_POINTS"));
int DP1 = Integer.parseInt(nodes.get(1).getRequirements().get("ENTITY_PHYSICAL_DEFENSE_POINTS"));
int AP0 = Integer.parseInt(nodes.get(0).getRequirements().get(EntityConfig.EntityProperties.ENTITY_PHYSICAL_ATTACK_POINTS.name()));
int DP0 = Integer.parseInt(nodes.get(0).getRequirements().get(EntityConfig.EntityProperties.ENTITY_PHYSICAL_DEFENSE_POINTS.name()));
int AP1 = Integer.parseInt(nodes.get(1).getRequirements().get(EntityConfig.EntityProperties.ENTITY_PHYSICAL_ATTACK_POINTS.name()));
int DP1 = Integer.parseInt(nodes.get(1).getRequirements().get(EntityConfig.EntityProperties.ENTITY_PHYSICAL_DEFENSE_POINTS.name()));

if (currentPlayerCharacterAP >= AP0 && currentPlayerCharacterDP >= DP0) {
return nodes.get(0);
Expand Down
Loading

0 comments on commit 9a9b64b

Please sign in to comment.