Sprint2 Food and Water System Code - UQdeco2800/2021-ext-studio-2 GitHub Wiki
The amount of water and food icons under the health bar will decrease over time. The player needs to restore his state by picking up water and food which is resources that may appear on the map, otherwise he will be affected by the two negative buffs :thirst and hunger.
- hunger
FoodDisplay.java
The icon of the food will be stored in a list, The timing refers to the scoreing system developed by the group 9
public class FoodDisplay extends UIComponent {
static Table table;
private Label timeLabel;
private final CountFoodSystem countFoodSystem = new CountFoodSystem();
private final ScoringSystemV1 timeCount = new ScoringSystemV1();
public static ArrayList<Image> chickenImage = new ArrayList<>();
private int chickenCurrent = 4;
The listeners implemented here lookout for trigger events,Creates reusable food ui styles and adds actors to the stage.
@Override
public void create() {
super.create();
addActors();
entity.getEvents().addListener("updateChicken", this::updatePlayerTimerUI);
}
Creates actors, add images into list, and positions them on the stage using a table.
private void addActors() {
table = new Table();
table.top().left();
table.setFillParent(true);
table.padTop(100f).padLeft(10f);
float clockSideLength = 30f;
chickenImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/food1.png", Texture.class)));
chickenImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/food1.png", Texture.class)));
chickenImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/food1.png", Texture.class)));
chickenImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/food1.png", Texture.class)));
add images into the screen
table.add(chickenImage.get(0)).size(clockSideLength).pad(3);
table.add(chickenImage.get(1)).size(clockSideLength).pad(3);
table.add(chickenImage.get(2)).size(clockSideLength).pad(3);
table.add(chickenImage.get(3)).size(clockSideLength).pad(3);
stage.addActor(table);
Reduce food when time increase, and lookout for trigger event
public void update() {
super.update();
int minutes = timeCount.getMinutes();
int seconds = timeCount.getSeconds();
int dis = (minutes * 60 + seconds) / 3;
if(dis > countFoodSystem.getTimer()){
countFoodSystem.setDifference(1);
countFoodSystem.setTimer(dis);
}else {
countFoodSystem.setDifference(0);
}
//update the food regularly
entity.getEvents().trigger("updateChicken", countFoodSystem.getDifference());
}
Updates the Chicken ui time by time increase.
public void updatePlayerTimerUI(int dis) {
if(dis > 0){
if(chickenImage.size() > 0){
table.reset();
table.top().left();
table.setFillParent(true);
table.padTop(100f).padLeft(10f);
chickenImage.remove(chickenImage.size() - 1);
for (Image ima: chickenImage) {
table.add(ima).size(30f).pad(3);
}
}
}
}
remove chickenImage by useing a for loop
public void dispose() {
super.dispose();
timeLabel.remove();
for (int i = 0; i < chickenImage.size(); ++i){
chickenImage.remove(i);
}
}
Add/remove a image, value =1: add a food image; value = -1 :remove a food image
public static void addOrRemoveImage(int value){
if(value == 1){
if(chickenImage.size() < 4){
chickenImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/food1.png", Texture.class)));
table.add(chickenImage.get(chickenImage.size() - 1)).size(30f).pad(3);
}
}else if(value == -1){
if(chickenImage.size() > 0){
table.removeActor(chickenImage.get(chickenImage.size() - 1));
}
}
}
if there is no images in the list, it will return a true
public static boolean isHunger(){
return chickenImage.size() <= 0;
}
add a food image in the main screen when player pick up a item
public void increaseFood(Entity target) {
if (target != null) {
if (FoodDisplay.chickenImage.size() < 4) {
FoodDisplay.addOrRemoveImage(1);
}
}
}
if foof icon count less than 0, then it will be into that codes to run. it increase player health per second.
public void updatePlayerHealth(int dis){
int health = MainGameScreen.players.getComponent(CombatStatsComponent.class).getHealth();
if(dis == 2){
if(foodImage.size() <= 0 && health <100){
//if water icon count less than 0, then it will be into that codes to run. it reduce player health value.
//reduce player health value
MainGameScreen.players.getComponent(CombatStatsComponent.class).setHealth(
MainGameScreen.players.getComponent(CombatStatsComponent.class).getHealth() + 1
);
}
}
}