|
| 1 | +/* |
| 2 | + * Copyright 2015 MovingBlocks |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.terasology.logic.nameTags; |
| 17 | + |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | +import org.terasology.entitySystem.entity.EntityRef; |
| 21 | +import org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent; |
| 22 | +import org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent; |
| 23 | +import org.terasology.entitySystem.event.ReceiveEvent; |
| 24 | +import org.terasology.entitySystem.systems.BaseComponentSystem; |
| 25 | +import org.terasology.entitySystem.systems.RegisterMode; |
| 26 | +import org.terasology.entitySystem.systems.RegisterSystem; |
| 27 | +import org.terasology.logic.characters.CharacterComponent; |
| 28 | +import org.terasology.logic.common.DisplayNameComponent; |
| 29 | +import org.terasology.logic.players.event.OnPlayerSpawnedEvent; |
| 30 | +import org.terasology.network.ClientComponent; |
| 31 | +import org.terasology.network.ClientInfoComponent; |
| 32 | +import org.terasology.network.ColorComponent; |
| 33 | +import org.terasology.network.NetworkSystem; |
| 34 | +import org.terasology.registry.In; |
| 35 | +import org.terasology.rendering.nui.Color; |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * Creates name tags for chacters controlled by players based on their name. |
| 40 | + * |
| 41 | + * Once there is the intention to implement multiple differnt name tag generation rules for players feel free to move |
| 42 | + * this system into a module so that it isn't always enabled. |
| 43 | + */ |
| 44 | +@RegisterSystem(RegisterMode.CLIENT) |
| 45 | +public class PlayerNameTagSystem extends BaseComponentSystem { |
| 46 | + private static final Logger logger = LoggerFactory.getLogger(NameTagClientSystem.class); |
| 47 | + |
| 48 | + @In |
| 49 | + private NetworkSystem networkSystem; |
| 50 | + |
| 51 | + /** |
| 52 | + * Listening for {@link OnPlayerSpawnedEvent} does not work, as it is an authority event that does not get |
| 53 | + * processed at clients. That is why we listen for the activation. |
| 54 | + */ |
| 55 | + @ReceiveEvent(components = CharacterComponent.class) |
| 56 | + public void onCharacterActivation(OnActivatedComponent event, EntityRef characterEntity, |
| 57 | + CharacterComponent characterComponent) { |
| 58 | + EntityRef ownerEntity = networkSystem.getOwnerEntity(characterEntity); |
| 59 | + if (ownerEntity == null) { |
| 60 | + return; // NPC |
| 61 | + } |
| 62 | + |
| 63 | + ClientComponent clientComponent = ownerEntity.getComponent(ClientComponent.class); |
| 64 | + if (clientComponent == null) { |
| 65 | + logger.warn("Can't create player based name tag for character as owner has no client component"); |
| 66 | + return; |
| 67 | + } |
| 68 | + if (clientComponent.local) { |
| 69 | + return; // the character belongs to the local player and does not need a name tag |
| 70 | + } |
| 71 | + |
| 72 | + EntityRef clientInfoEntity = clientComponent.clientInfo; |
| 73 | + |
| 74 | + DisplayNameComponent displayNameComponent = clientInfoEntity.getComponent(DisplayNameComponent.class); |
| 75 | + if (displayNameComponent == null) { |
| 76 | + logger.error("Can't create player based name tag for character as client info has no DisplayNameComponent"); |
| 77 | + return; |
| 78 | + } |
| 79 | + String name = displayNameComponent.name; |
| 80 | + |
| 81 | + float yOffset = characterComponent.nameTagOffset; |
| 82 | + |
| 83 | + Color color = Color.WHITE; |
| 84 | + ColorComponent colorComponent = clientInfoEntity.getComponent(ColorComponent.class); |
| 85 | + if (colorComponent != null) { |
| 86 | + color = colorComponent.color; |
| 87 | + } |
| 88 | + |
| 89 | + NameTagComponent nameTagComponent = characterEntity.getComponent(NameTagComponent.class); |
| 90 | + boolean newComponent = nameTagComponent == null; |
| 91 | + if (nameTagComponent == null) { |
| 92 | + nameTagComponent = new NameTagComponent(); |
| 93 | + } |
| 94 | + nameTagComponent.text = name; |
| 95 | + nameTagComponent.textColor = color; |
| 96 | + nameTagComponent.yOffset = yOffset; |
| 97 | + if (newComponent) { |
| 98 | + characterEntity.addComponent(nameTagComponent); |
| 99 | + } else { |
| 100 | + characterEntity.saveComponent(nameTagComponent); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * The player entity may currently become "local" afterh the player has been activated. |
| 107 | + * |
| 108 | + * To address this issue the name tag component will be removed again when a client turns out to be local |
| 109 | + * afterwards. |
| 110 | + */ |
| 111 | + @ReceiveEvent |
| 112 | + public void onClientComponentChange(OnChangedComponent event, EntityRef clientEntity, |
| 113 | + ClientComponent clientComponent) { |
| 114 | + if (clientComponent.local) { |
| 115 | + EntityRef character = clientComponent.character; |
| 116 | + if (character.exists() && character.hasComponent(NameTagComponent.class)) { |
| 117 | + character.removeComponent(NameTagComponent.class); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @ReceiveEvent |
| 123 | + public void onDisplayNameChange(OnChangedComponent event, EntityRef clientInfoEntity, |
| 124 | + DisplayNameComponent displayNameComponent) { |
| 125 | + ClientInfoComponent clientInfoComp = clientInfoEntity.getComponent(ClientInfoComponent.class); |
| 126 | + if (clientInfoComp == null) { |
| 127 | + return; // not a client info object |
| 128 | + } |
| 129 | + |
| 130 | + EntityRef clientEntity = clientInfoComp.client; |
| 131 | + if (!clientEntity.exists()) { |
| 132 | + return; // offline players aren't visible: nothing to do |
| 133 | + } |
| 134 | + |
| 135 | + ClientComponent clientComponent = clientEntity.getComponent(ClientComponent.class); |
| 136 | + if (clientComponent == null) { |
| 137 | + logger.warn("Can't update name tag as client entity lacks ClietnComponent"); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + EntityRef characterEntity = clientComponent.character; |
| 142 | + if (characterEntity == null || !characterEntity.exists()) { |
| 143 | + return; // player has no character, nothing to do |
| 144 | + } |
| 145 | + |
| 146 | + NameTagComponent nameTagComponent = characterEntity.getComponent(NameTagComponent.class); |
| 147 | + if (nameTagComponent == null) { |
| 148 | + return; // local players don't have a name tag |
| 149 | + } |
| 150 | + |
| 151 | + nameTagComponent.text = displayNameComponent.name; |
| 152 | + characterEntity.saveComponent(nameTagComponent); |
| 153 | + } |
| 154 | +} |
0 commit comments