Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Health HUD reskinning based on team #79

Merged
merged 4 commits into from Jun 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,6 +23,7 @@ public final class LASUtils {
public static final String RED_FLAG_URI = "lightAndShadowResources:redFlag";
public static final String RED_TEAM = "red";
public static final String BLACK_TEAM = "black";
public static final String WHITE_TEAM = "white";
/**
* Position of Red base
*/
Expand Down Expand Up @@ -99,4 +100,30 @@ public static Vector3f getTeleportDestination(String team) {
}
return null;
}

public static String getHealthIcon (String team) {
if (team.equals(RED_TEAM)) {
return RED_HEALTH_ICON;
}
if (team.equals(BLACK_TEAM)) {
return BLACK_HEALTH_ICON;
}
if (team.equals(WHITE_TEAM)) {
return WHITE_HEALTH_ICON;
}
return null;
}

public static String getHealthSkin (String team) {
if (team.equals(RED_TEAM)) {
return RED_HEALTH_SKIN;
}
if (team.equals(BLACK_TEAM)) {
return BLACK_HEALTH_SKIN;
}
if (team.equals(WHITE_TEAM)) {
return WHITE_HEALTH_SKIN;
}
return null;
}
}
Expand Up @@ -24,16 +24,42 @@
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.ligthandshadow.componentsystem.LASUtils;
import org.terasology.ligthandshadow.componentsystem.events.AddPlayerSkinToPlayerEvent;
import org.terasology.ligthandshadow.componentsystem.events.SetPlayerHealthHUDEvent;
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.registry.In;
import org.terasology.rendering.nui.NUIManager;
import org.terasology.rendering.nui.layers.hud.HealthHud;
import org.terasology.rendering.nui.widgets.UIIconBar;
import org.terasology.utilities.Assets;

/**
* Handles changing players' health HUD and skin based on their teams.
*/
@RegisterSystem(RegisterMode.CLIENT)
public class ClientSkinSystem extends BaseComponentSystem {
@In
private EntityManager entityManager;
@In
private NUIManager nuiManager;
@In
private LocalPlayer localPlayer;

private EntityBuilder builder;

@Override
public void initialise() {
HealthHud healthHud = nuiManager.getHUD().getHUDElement("core:healthHud", HealthHud.class);
healthHud.find("healthBar", UIIconBar.class).setIcon(Assets.getTextureRegion(LASUtils.getHealthIcon(LASUtils.WHITE_TEAM)).get());
healthHud.setSkin(Assets.getSkin(LASUtils.getHealthSkin(LASUtils.WHITE_TEAM)).get());
}

/**
* Changes player skin on receiving the corresponding event.
*
* @param event
* @param entity
*/
@ReceiveEvent
public void onAddPlayerSkinToPlayer(AddPlayerSkinToPlayerEvent event, EntityRef entity) {
EntityRef player = event.player;
Expand All @@ -49,4 +75,22 @@ public void onAddPlayerSkinToPlayer(AddPlayerSkinToPlayerEvent event, EntityRef
builder.build();
}
}

/**
* Changes player health hud on receiving the corresponding event.
*
* @param event
* @param entity
*/
@ReceiveEvent
public void onSetPlayerHealthHUDEvent(SetPlayerHealthHUDEvent event, EntityRef entity) {
EntityRef player = event.player;
String team = event.team;

if (player.getId() == localPlayer.getCharacterEntity().getId()) {
HealthHud healthHud = nuiManager.getHUD().getHUDElement("core:healthHud", HealthHud.class);
healthHud.find("healthBar", UIIconBar.class).setIcon(Assets.getTextureRegion(LASUtils.getHealthIcon(team)).get());
healthHud.setSkin(Assets.getSkin(LASUtils.getHealthSkin(team)).get());
}
}
}
Expand Up @@ -28,12 +28,19 @@
import org.terasology.ligthandshadow.componentsystem.components.LASTeamComponent;
import org.terasology.ligthandshadow.componentsystem.components.SetTeamOnActivateComponent;
import org.terasology.ligthandshadow.componentsystem.events.AddPlayerSkinToPlayerEvent;
import org.terasology.ligthandshadow.componentsystem.events.SetPlayerHealthHUDEvent;
import org.terasology.logic.characters.CharacterTeleportEvent;
import org.terasology.logic.common.ActivateEvent;
import org.terasology.logic.inventory.InventoryManager;
import org.terasology.network.ClientComponent;
import org.terasology.registry.In;

/**
* Teleports players to play arena once they chose their team.
* It also sends events to change players skins and hud based on team they have chosen.
*
* @see ClientSkinSystem
*/
@RegisterSystem(RegisterMode.AUTHORITY)
public class TeleporterSystem extends BaseComponentSystem {
private static final Logger logger = LoggerFactory.getLogger(TeleporterSystem.class);
Expand All @@ -43,8 +50,13 @@ public class TeleporterSystem extends BaseComponentSystem {
@In
EntityManager entityManager;

/* Depending on which teleporter the player chooses, they are set to that team
* and teleported to that base */
/**
* Depending on which teleporter the player chooses, they are set to that team
* and teleported to that base
*
* @param event
* @param entity
*/
@ReceiveEvent(components = {SetTeamOnActivateComponent.class})
public void onActivate(ActivateEvent event, EntityRef entity) {
EntityRef player = event.getInstigator();
Expand All @@ -64,12 +76,17 @@ private void handlePlayerTeleport(EntityRef player, String team) {
player.send(new CharacterTeleportEvent(LASUtils.getTeleportDestination(team)));
inventoryManager.giveItem(player, EntityRef.NULL, entityManager.create(LASUtils.MAGIC_STAFF_URI));
setPlayerSkin(player, team);
setPlayerHud(player, team);
}

private void setPlayerSkin(EntityRef player, String team) {
sendEventToClients(new AddPlayerSkinToPlayerEvent(player, team));
}

private void setPlayerHud(EntityRef player, String team) {
sendEventToClients(new SetPlayerHealthHUDEvent(player, team));
}

private void sendEventToClients(Event event) {
if (entityManager.getCountOfEntitiesWith(ClientComponent.class) != 0) {
Iterable<EntityRef> clients = entityManager.getEntitiesWith(ClientComponent.class);
Expand Down
@@ -0,0 +1,37 @@
/*
* Copyright 2019 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terasology.ligthandshadow.componentsystem.events;

import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.Event;
import org.terasology.network.BroadcastEvent;

/**
* Event to change player health HUD.
*/
@BroadcastEvent
public class SetPlayerHealthHUDEvent implements Event {
public EntityRef player;
public String team;

public SetPlayerHealthHUDEvent() {
}

public SetPlayerHealthHUDEvent(EntityRef player, String team) {
this.player = player;
this.team = team;
}
}