Skip to content

Commit

Permalink
fleshed out the health, hunger, and stamina systems; also added an apple
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisj42 committed Feb 25, 2018
1 parent 0b01e54 commit f1c438c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 14 deletions.
37 changes: 37 additions & 0 deletions core/src/miniventure/game/item/FoodItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package miniventure.game.item;

import miniventure.game.GameCore;
import miniventure.game.world.Level;
import miniventure.game.world.entity.TextParticle;
import miniventure.game.world.entity.mob.Player;
import miniventure.game.world.entity.mob.Player.Stat;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import org.jetbrains.annotations.NotNull;

public enum FoodItem {

Apple(2);

private final int healthGained;

FoodItem(int healthGained) {
this.healthGained = healthGained;
}

@NotNull
public Item get() {
return new Item(name(), GameCore.icons.size() > 0 ? GameCore.icons.get(name().toLowerCase()) : new TextureRegion()) {
@Override public Item copy() { return this; }

@Override public void interact(Player player) {
int gained = player.changeStat(Stat.Hunger, healthGained);
Level level = player.getLevel();
if(level != null)
level.addEntity(new TextParticle(gained+"", Color.CORAL), player.getCenter(), true);
}
};
}
}
1 change: 1 addition & 0 deletions core/src/miniventure/game/world/entity/mob/Mob.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public boolean attackedBy(WorldObject obj, @Nullable Item item, int damage) {
return true;
}

void regenHealth(int amount) { health = Math.min(maxHealth, health + amount); }

public boolean maySpawn(TileType type) {
return type == TileType.GRASS || type == TileType.DIRT || type == TileType.SAND;
Expand Down
75 changes: 62 additions & 13 deletions core/src/miniventure/game/world/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import miniventure.game.item.InventoryScreen;
import miniventure.game.item.Item;
import miniventure.game.item.Recipes;
import miniventure.game.util.MyUtils;
import miniventure.game.world.Level;
import miniventure.game.world.WorldObject;
import miniventure.game.world.entity.ActionParticle.ActionType;
Expand Down Expand Up @@ -53,7 +54,7 @@ public enum Stat {

Stamina("bolt", 12, 100),

Hunger("burger", 10, 10),
Hunger("burger", 10, 20),

Armor("", 10, 10, 0);

Expand Down Expand Up @@ -98,8 +99,13 @@ public Player() {
public int getStat(@NotNull Stat stat) {
return stats.get(stat);
}
public void changeStat(@NotNull Stat stat, int amt) {
public int changeStat(@NotNull Stat stat, int amt) {
int prevVal = stats.get(stat);
stats.put(stat, Math.max(0, Math.min(stat.max, stats.get(stat) + amt)));
if(stat == Stat.Health && amt > 0)
regenHealth(amt);

return stats.get(stat) - prevVal;
}

public void checkInput(@NotNull Vector2 mouseInput) {
Expand All @@ -120,7 +126,9 @@ public void checkInput(@NotNull Vector2 mouseInput) {

move(movement.x, movement.y);

getStatEvo(StaminaSystem.class).curStaminaRate = movement.isZero() ? 1 : 0.75f;
getStatEvo(StaminaSystem.class).isMoving = !movement.isZero();
if(!movement.isZero())
getStatEvo(HungerSystem.class).addHunger(Gdx.graphics.getDeltaTime() * 0.35f);

if(!isKnockedBack()) {
if (GameCore.input.pressingKey(Input.Keys.C))
Expand Down Expand Up @@ -161,22 +169,22 @@ private void renderBar(Stat stat, float x, float y, SpriteBatch batch, int spaci

int iconWidth = stat.iconWidth + spacing;

if(!rightSide) x -= stat.iconCount * iconWidth;

// for each icon...
for(int i = 0; i < stat.iconCount; i++) {
// gets the amount this icon should be "filled" with the fullIcon
float iconFillAmount = Math.min(Math.max(0, stats.get(stat) - i * pointsPerIcon) / pointsPerIcon, 1);

// converts it to a pixel width
int fullWidth = (int) (iconFillAmount * fullIcon.getRegionWidth());
float fullX = rightSide ? x+i*iconWidth : x - i*iconWidth - fullWidth;
if(fullWidth > 0)
batch.draw(fullIcon.getTexture(), x+i*iconWidth, y, fullIcon.getRegionX(), fullIcon.getRegionY(), fullWidth, fullIcon.getRegionHeight());
batch.draw(fullIcon.getTexture(), fullX, y, fullIcon.getRegionX() + (rightSide?0:fullIcon.getRegionWidth()-fullWidth), fullIcon.getRegionY(), fullWidth, fullIcon.getRegionHeight());

// now draw the rest of the icon with the empty sprite.
int emptyWidth = emptyIcon.getRegionWidth()-fullWidth;
float emptyX = rightSide ? x+i*iconWidth+fullWidth : x - (i+1)*iconWidth;
if(emptyWidth > 0)
batch.draw(emptyIcon.getTexture(), x+i*iconWidth+fullWidth, y, emptyIcon.getRegionX() + fullWidth, emptyIcon.getRegionY(), emptyWidth, emptyIcon.getRegionHeight());
batch.draw(emptyIcon.getTexture(), emptyX, y, emptyIcon.getRegionX() + (rightSide?fullWidth:0), emptyIcon.getRegionY(), emptyWidth, emptyIcon.getRegionHeight());
}
}

Expand Down Expand Up @@ -287,15 +295,19 @@ private class StaminaSystem implements StatEvolver {

private static final float STAMINA_REGEN_RATE = 0.25f; // time taken to regen 1 stamina point.

private float curStaminaRate = 1;
private boolean isMoving = false;
private float regenTime;

public StaminaSystem() {}
StaminaSystem() {}

@Override
public void update(float delta) {
regenTime += delta;
float regenRate = STAMINA_REGEN_RATE * curStaminaRate;
float regenRate = STAMINA_REGEN_RATE;
if(isMoving) regenRate *= 0.75f;
if(getStat(Stat.Health) != Stat.Health.max)
regenRate *= 1 - (0.5f * getStat(Stat.Hunger) / Stat.Hunger.max); // slow the stamina gen based on how fast you're regen-ing health; if you have very little hunger, then you aren't regen-ing much, so your stamina isn't affected as much.

int staminaGained = MathUtils.floor(regenTime / regenRate);
if(staminaGained > 0) {
regenTime -= staminaGained * regenRate;
Expand All @@ -306,21 +318,58 @@ public void update(float delta) {

private class HealthSystem implements StatEvolver {

public HealthSystem() {}
private static final float REGEN_RATE = 2f; // whenever the regenTime reaches this value, a health point is added.
private float regenTime;

HealthSystem() {}

@Override
public void update(float delta) {

if(getStat(Stat.Health) != Stat.Health.max) {
float hungerRatio = getStat(Stat.Hunger)*1f / Stat.Hunger.max;
regenTime += delta * hungerRatio;
if(regenTime >= REGEN_RATE) {
int healthGained = MathUtils.floor(regenTime / REGEN_RATE);
changeStat(Stat.Health, healthGained);
regenTime -= healthGained * REGEN_RATE;
}
}
else regenTime = 0;
}
}

private class HungerSystem implements StatEvolver {
/*
Hunger... you get it:
- over time
- walking
- doing things (aka when stamina is low)
*/

private static final float HUNGER_RATE = 60f; // whenever the hunger count reaches this value, a hunger point is taken off.
private static final float MAX_STAMINA_MULTIPLIER = 8; // you will lose hunger this many times as fast if you have absolutely no stamina.

private float hunger = 0;

public HungerSystem() {}
HungerSystem() {}

public void addHunger(float amt) {
float hungerRatio = getStat(Stat.Hunger)*1f / Stat.Hunger.max;
// make it so a ratio of 1 means x2 addition, and a ratio of 0 makes it 0.5 addition
float amtMult = MyUtils.map(hungerRatio, 0, 1, 0.5f, 2);
hunger += amt * amtMult;
}

@Override
public void update(float delta) {
float staminaRatio = 1 + (1 - (getStat(Stat.Stamina)*1f / Stat.Stamina.max)) * MAX_STAMINA_MULTIPLIER;
addHunger(delta * staminaRatio);

if(hunger >= HUNGER_RATE) {
int hungerLost = MathUtils.floor(hunger / HUNGER_RATE);
changeStat(Stat.Hunger, -hungerLost);
hunger -= hungerLost * HUNGER_RATE;
}
}
}
}
3 changes: 2 additions & 1 deletion core/src/miniventure/game/world/tile/TileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.HashMap;

import miniventure.game.item.FoodItem;
import miniventure.game.item.ResourceItem;
import miniventure.game.item.TileItem;
import miniventure.game.item.ToolType;
Expand Down Expand Up @@ -93,7 +94,7 @@ public enum TileType {
TREE(
SolidProperty.SOLID,
new CoveredTileProperty(GRASS),
new DestructibleProperty(10, new PreferredTool(ToolType.Axe, 2), new ItemDrop(ResourceItem.Log.get())),
new DestructibleProperty(10, new PreferredTool(ToolType.Axe, 2), new ItemDrop(ResourceItem.Log.get()), new ItemDrop(FoodItem.Apple.get())),
new AnimationProperty(false, AnimationType.SINGLE_FRAME),
new ConnectionProperty(true)
);
Expand Down
Binary file added sprites/icon-sprites/apple.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f1c438c

Please sign in to comment.