Water System - 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.
- thirsty
WaterDisplay.java
The icon of the water will be stored in a list-waterImage , The timing refers to the scoreing system developed by the group 9
public static ArrayList<Image> chickenImage = new ArrayList<>();
The listeners implemented here lookout for trigger events,Creates reusable drop of water ui styles and adds actors to the stage.
public void create() {
super.create();
addActors();
entity.getEvents().addListener("updateWater", 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(150f).padLeft(10f);
//Add water image
float clockSideLength = 30f;
waterImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/water1.png", Texture.class)));
waterImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/water1.png", Texture.class)));
waterImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/water1.png", Texture.class)));
waterImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/water1.png", Texture.class)));
add images into the screen
table.add(waterImage.get(0)).size(clockSideLength).pad(3);
table.add(waterImage.get(1)).size(clockSideLength).pad(3);
table.add(waterImage.get(2)).size(clockSideLength).pad(3);
table.add(waterImage.get(3)).size(clockSideLength).pad(3);
stage.addActor(table);
Reduce water when time increase, and lookout for trigger event
public void update() {
super.update();
int minutes = countTime.getMinutes();
int seconds = countTime.getSeconds();
int dis = (minutes*60+seconds)/2;
if(dis> countWaterSystem.getTimer()){
countWaterSystem.setDifference(1);
countWaterSystem.setTimer(dis);
}else {
countWaterSystem.setDifference(0);
}
//update the water regularly
entity.getEvents().trigger("updateWater", countWaterSystem.getDifference());
}
}
Updates the Chicken ui time by time increase.
if(waterImage.size() > 0){
table.reset();
table.top().left();
table.setFillParent(true);
table.padTop(150f).padLeft(10f);
waterImage.remove(waterImage.size() - 1);
for (Image ima: waterImage) {
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 < waterImage.size(); ++i){
waterImage.remove(i);
}
}
Add/remove a image, value =1: add a waterimage; value = -1 :remove a water image
public static void addOrRemoveImage(int value){
if(value == 1){
if(chickenImage.size() < 4){
chickenImage.add(new Image(ServiceLocator.getResourceService()
.getAsset("images/water1.png", Texture.class)));
table.add(chickenImage.get(images/water1.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 isThirst(){
return waterImage.size() <= 0;;
}
add a water icon on main screen when player pick up a target(item)
public void increaseWater(Entity target) {
if (target != null) {
if (WaterDisplay.waterImage.size() < 4) {
WaterDisplay.addOrRemoveImage(1);
}
if water icon count less than 0, then it will be into that codes to run. it reduce player health value.
public void updatePlayerHealth(int dis){
if(dis == 2){
if(waterImage.size() <= 0){
//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
);
}
}
}