|
| 1 | +/* |
| 2 | + * Copyright 2021 The Terasology Foundation |
| 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 | + |
| 17 | +package org.destinationsol.ui.nui.screens; |
| 18 | + |
| 19 | +import org.destinationsol.SolApplication; |
| 20 | +import org.destinationsol.game.Hero; |
| 21 | +import org.destinationsol.game.screens.InventoryScreen; |
| 22 | +import org.destinationsol.game.ship.SolShip; |
| 23 | +import org.destinationsol.game.ship.hulls.HullConfig; |
| 24 | +import org.destinationsol.ui.nui.NUIScreenLayer; |
| 25 | +import org.destinationsol.ui.nui.widgets.KeyActivatedButton; |
| 26 | +import org.destinationsol.ui.nui.widgets.UIWarnButton; |
| 27 | +import org.terasology.nui.backends.libgdx.GDXInputUtil; |
| 28 | + |
| 29 | +import javax.inject.Inject; |
| 30 | + |
| 31 | +/** |
| 32 | + * The talk screen allows the player to perform actions at a station. |
| 33 | + * At the moment, the player can buy and sell items from here, purchase new ships or hire mercenaries. |
| 34 | + */ |
| 35 | +public class TalkScreen extends NUIScreenLayer { |
| 36 | + public static final float MAX_TALK_DIST = 1f; |
| 37 | + private final SolApplication solApplication; |
| 38 | + private UIWarnButton buyButton; |
| 39 | + private KeyActivatedButton changeShipButton; |
| 40 | + private KeyActivatedButton hireButton; |
| 41 | + private SolShip target; |
| 42 | + |
| 43 | + @Inject |
| 44 | + public TalkScreen(SolApplication solApplication) { |
| 45 | + this.solApplication = solApplication; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void initialise() { |
| 50 | + KeyActivatedButton sellButton = find("sellButton", KeyActivatedButton.class); |
| 51 | + sellButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeySellMenu())); |
| 52 | + sellButton.subscribe(button -> { |
| 53 | + InventoryScreen inventoryScreen = solApplication.getGame().getScreens().inventoryScreen; |
| 54 | + inventoryScreen.setOperations(inventoryScreen.sellItems); |
| 55 | + nuiManager.removeScreen(this); |
| 56 | + solApplication.getInputManager().addScreen(solApplication, inventoryScreen); |
| 57 | + }); |
| 58 | + |
| 59 | + buyButton = find("buyButton", UIWarnButton.class); |
| 60 | + buyButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyBuyMenu())); |
| 61 | + buyButton.subscribe(button -> { |
| 62 | + InventoryScreen inventoryScreen = solApplication.getGame().getScreens().inventoryScreen; |
| 63 | + inventoryScreen.setOperations(inventoryScreen.buyItemsScreen); |
| 64 | + nuiManager.removeScreen(this); |
| 65 | + solApplication.getInputManager().addScreen(solApplication, inventoryScreen); |
| 66 | + }); |
| 67 | + |
| 68 | + changeShipButton = find("changeShipButton", KeyActivatedButton.class); |
| 69 | + changeShipButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyChangeShipMenu())); |
| 70 | + changeShipButton.subscribe(button -> { |
| 71 | + InventoryScreen inventoryScreen = solApplication.getGame().getScreens().inventoryScreen; |
| 72 | + inventoryScreen.setOperations(inventoryScreen.changeShipScreen); |
| 73 | + nuiManager.removeScreen(this); |
| 74 | + solApplication.getInputManager().addScreen(solApplication, inventoryScreen); |
| 75 | + }); |
| 76 | + |
| 77 | + hireButton = find("hireButton", KeyActivatedButton.class); |
| 78 | + hireButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyHireShipMenu())); |
| 79 | + hireButton.subscribe(button -> { |
| 80 | + InventoryScreen inventoryScreen = solApplication.getGame().getScreens().inventoryScreen; |
| 81 | + inventoryScreen.setOperations(inventoryScreen.hireShipsScreen); |
| 82 | + nuiManager.removeScreen(this); |
| 83 | + solApplication.getInputManager().addScreen(solApplication, inventoryScreen); |
| 84 | + }); |
| 85 | + |
| 86 | + KeyActivatedButton closeButton = find("closeButton", KeyActivatedButton.class); |
| 87 | + closeButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyClose())); |
| 88 | + closeButton.subscribe(button -> { |
| 89 | + nuiManager.removeScreen(this); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onAdded() { |
| 95 | + boolean isStation = target.getHull().config.getType() == HullConfig.Type.STATION; |
| 96 | + changeShipButton.setEnabled(isStation); |
| 97 | + hireButton.setEnabled(isStation); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void update(float delta) { |
| 102 | + super.update(delta); |
| 103 | + |
| 104 | + if (isTargetFar(solApplication.getGame().getHero())) { |
| 105 | + nuiManager.removeScreen(this); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Returns the button pressed to open the buy items screen. |
| 111 | + * |
| 112 | + * This is exposed directly for use in the tutorial. |
| 113 | + * @return the buy items button |
| 114 | + */ |
| 115 | + public UIWarnButton getBuyButton() { |
| 116 | + return buyButton; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the current ship being talked to. |
| 121 | + * @return the current ship being talked to |
| 122 | + */ |
| 123 | + public SolShip getTarget() { |
| 124 | + return target; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Assigns the ship to talk to |
| 129 | + * @param target the ship to talk to |
| 130 | + */ |
| 131 | + public void setTarget(SolShip target) { |
| 132 | + this.target = target; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns true if the target is within communicating range. |
| 137 | + * @return true, if the target is within communicating range, otherwise false |
| 138 | + */ |
| 139 | + public boolean isTargetFar(Hero hero) { |
| 140 | + if (hero.isTranscendent() || target == null || target.getLife() <= 0) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + float distance = target.getPosition().dst(hero.getPosition()) - hero.getHull().config.getApproxRadius() - target.getHull().config.getApproxRadius(); |
| 145 | + |
| 146 | + return (MAX_TALK_DIST < distance); |
| 147 | + } |
| 148 | +} |
0 commit comments