diff --git a/src/main/java/org/mafagafogigante/dungeon/entity/creatures/Hero.java b/src/main/java/org/mafagafogigante/dungeon/entity/creatures/Hero.java index 0c2eae45..cd9e370a 100644 --- a/src/main/java/org/mafagafogigante/dungeon/entity/creatures/Hero.java +++ b/src/main/java/org/mafagafogigante/dungeon/entity/creatures/Hero.java @@ -438,6 +438,17 @@ private void printItems() { Writer.getDefaultWriter().write(text); } + private double adjustHealingForFreshness(Item item, double healing) { + if (!item.hasTag(Item.Tag.DECOMPOSES)) { + return healing; + } + long age = item.getAge(); + long decompositionPeriod = item.getDecompositionPeriod(); + double x = age / (double) decompositionPeriod; + double f = Math.min(1.0, 4 * Math.pow(1 - x, 2)); + return f * healing; + } + /** * Attempts to eat an item. */ @@ -449,12 +460,11 @@ public void eatItem(String[] arguments) { if (getInventory().hasItem(selectedItem)) { FoodComponent food = selectedItem.getFoodComponent(); double remainingBites = selectedItem.getIntegrity().getCurrent() / (double) food.getIntegrityDecrementOnEat(); - int healthChange; + double healthChange; if (remainingBites >= 1.0) { healthChange = food.getNutrition(); } else { - // The absolute value of the healthChange will never be equal to nutrition, only smaller. - healthChange = (int) (food.getNutrition() * remainingBites); + healthChange = food.getNutrition() * remainingBites; } selectedItem.decrementIntegrityByEat(); if (selectedItem.isBroken() && !selectedItem.hasTag(Item.Tag.REPAIRABLE)) { @@ -462,7 +472,8 @@ public void eatItem(String[] arguments) { } else { Writer.getDefaultWriter().write("You ate a bit of " + selectedItem.getName() + "."); } - addHealth(healthChange); + int healingAmount = (int) adjustHealingForFreshness(selectedItem, healthChange); + addHealth(healingAmount); if (healthChange > 0) { WELL_FED_EFFECT.affect(this); } diff --git a/src/main/java/org/mafagafogigante/dungeon/entity/items/Item.java b/src/main/java/org/mafagafogigante/dungeon/entity/items/Item.java index 6ddac414..561a0148 100644 --- a/src/main/java/org/mafagafogigante/dungeon/entity/items/Item.java +++ b/src/main/java/org/mafagafogigante/dungeon/entity/items/Item.java @@ -91,7 +91,7 @@ public Weight getWeight() { * * @return a long representing an amount of seconds */ - long getAge() { + public long getAge() { Duration existence = new Duration(dateOfCreation, Game.getGameState().getWorld().getWorldDate()); return existence.getSeconds(); } @@ -180,7 +180,7 @@ private String getIntegrityString() { return IntegrityState.getIntegrityState(getIntegrity().getCurrent(), getIntegrity().getMaximum()).toString(); } - long getDecompositionPeriod() { + public long getDecompositionPeriod() { return decompositionPeriod; }