diff --git a/engine/src/main/java/org/destinationsol/game/screens/BuyItemsScreen.java b/engine/src/main/java/org/destinationsol/game/screens/BuyItemsScreen.java index ff03fddbb..8bf70702f 100644 --- a/engine/src/main/java/org/destinationsol/game/screens/BuyItemsScreen.java +++ b/engine/src/main/java/org/destinationsol/game/screens/BuyItemsScreen.java @@ -26,6 +26,7 @@ import org.destinationsol.game.ship.SolShip; import org.destinationsol.ui.SolInputManager; import org.destinationsol.ui.SolUiControl; +import org.destinationsol.ui.nui.screens.TalkScreen; public class BuyItemsScreen extends InventoryOperationsScreen { public final SolUiControl buyControl; diff --git a/engine/src/main/java/org/destinationsol/game/screens/ChangeShipScreen.java b/engine/src/main/java/org/destinationsol/game/screens/ChangeShipScreen.java index 811e9895d..4c9390376 100644 --- a/engine/src/main/java/org/destinationsol/game/screens/ChangeShipScreen.java +++ b/engine/src/main/java/org/destinationsol/game/screens/ChangeShipScreen.java @@ -31,6 +31,7 @@ import org.destinationsol.game.ship.hulls.HullConfig; import org.destinationsol.ui.SolInputManager; import org.destinationsol.ui.SolUiControl; +import org.destinationsol.ui.nui.screens.TalkScreen; public class ChangeShipScreen extends InventoryOperationsScreen { private final SolUiControl changeControl; diff --git a/engine/src/main/java/org/destinationsol/game/screens/GameScreens.java b/engine/src/main/java/org/destinationsol/game/screens/GameScreens.java index c7303c0ae..ec16472a8 100644 --- a/engine/src/main/java/org/destinationsol/game/screens/GameScreens.java +++ b/engine/src/main/java/org/destinationsol/game/screens/GameScreens.java @@ -20,6 +20,7 @@ import org.destinationsol.game.context.Context; import org.destinationsol.ui.SolLayouts; import org.destinationsol.ui.nui.screens.MenuScreen; +import org.destinationsol.ui.nui.screens.TalkScreen; import javax.inject.Inject; @@ -49,7 +50,7 @@ public GameScreens(SolApplication cmp, Context context) { mapScreen = new MapScreen(rightPaneLayout, cmp.isMobile(), cmp.getOptions()); menuScreen = (MenuScreen) cmp.getNuiManager().createScreen("engine:menuScreen"); inventoryScreen = new InventoryScreen(cmp.getOptions()); - talkScreen = new TalkScreen(layouts.menuLayout, cmp.getOptions()); + talkScreen = (TalkScreen) cmp.getNuiManager().createScreen("engine:talkScreen"); waypointCreationScreen = new WaypointCreationScreen(layouts.menuLayout, cmp.getOptions(), mapScreen); consoleScreen = new ConsoleScreen(context.get(Console.class)); } diff --git a/engine/src/main/java/org/destinationsol/game/screens/HireShipsScreen.java b/engine/src/main/java/org/destinationsol/game/screens/HireShipsScreen.java index 3e298bc43..1b3af3928 100644 --- a/engine/src/main/java/org/destinationsol/game/screens/HireShipsScreen.java +++ b/engine/src/main/java/org/destinationsol/game/screens/HireShipsScreen.java @@ -25,6 +25,7 @@ import org.destinationsol.mercenary.MercenaryUtils; import org.destinationsol.ui.SolInputManager; import org.destinationsol.ui.SolUiControl; +import org.destinationsol.ui.nui.screens.TalkScreen; public class HireShipsScreen extends InventoryOperationsScreen { private final SolUiControl hireControl; diff --git a/engine/src/main/java/org/destinationsol/game/screens/SellItems.java b/engine/src/main/java/org/destinationsol/game/screens/SellItems.java index 0d6488886..76c34d8a7 100644 --- a/engine/src/main/java/org/destinationsol/game/screens/SellItems.java +++ b/engine/src/main/java/org/destinationsol/game/screens/SellItems.java @@ -24,6 +24,7 @@ import org.destinationsol.game.ship.SolShip; import org.destinationsol.ui.SolInputManager; import org.destinationsol.ui.SolUiControl; +import org.destinationsol.ui.nui.screens.TalkScreen; public class SellItems extends InventoryOperationsScreen { private static float PERC = .8f; diff --git a/engine/src/main/java/org/destinationsol/game/screens/TalkScreen.java b/engine/src/main/java/org/destinationsol/game/screens/TalkScreen.java deleted file mode 100644 index f21d45205..000000000 --- a/engine/src/main/java/org/destinationsol/game/screens/TalkScreen.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2018 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.destinationsol.game.screens; - -import com.badlogic.gdx.math.Rectangle; -import org.destinationsol.GameOptions; -import org.destinationsol.SolApplication; -import org.destinationsol.common.SolColor; -import org.destinationsol.game.Hero; -import org.destinationsol.game.SolGame; -import org.destinationsol.game.ship.SolShip; -import org.destinationsol.game.ship.hulls.HullConfig; -import org.destinationsol.menu.MenuLayout; -import org.destinationsol.ui.SolInputManager; -import org.destinationsol.ui.SolUiBaseScreen; -import org.destinationsol.ui.SolUiControl; -import org.destinationsol.ui.UiDrawer; - -public class TalkScreen extends SolUiBaseScreen { - public static final float MAX_TALK_DIST = 1f; - - public final SolUiControl buyControl; - public final SolUiControl closeControl; - private final SolUiControl sellControl; - private final SolUiControl shipsControl; - private final SolUiControl hireControl; - - private final Rectangle background; - private SolShip target; - - TalkScreen(MenuLayout menuLayout, GameOptions gameOptions) { - sellControl = new SolUiControl(menuLayout.buttonRect(-1, 0), true, gameOptions.getKeySellMenu()); - sellControl.setDisplayName("Sell"); - controls.add(sellControl); - - buyControl = new SolUiControl(menuLayout.buttonRect(-1, 1), true, gameOptions.getKeyBuyMenu()); - buyControl.setDisplayName("Buy"); - controls.add(buyControl); - - shipsControl = new SolUiControl(menuLayout.buttonRect(-1, 2), true, gameOptions.getKeyChangeShipMenu()); - shipsControl.setDisplayName("Change Ship"); - controls.add(shipsControl); - - hireControl = new SolUiControl(menuLayout.buttonRect(-1, 3), true, gameOptions.getKeyHireShipMenu()); - hireControl.setDisplayName("Hire"); - controls.add(hireControl); - - closeControl = new SolUiControl(menuLayout.buttonRect(-1, 4), true, gameOptions.getKeyClose()); - closeControl.setDisplayName("Close"); - controls.add(closeControl); - - background = menuLayout.background(-1, 0, 5); - } - - @Override - public void updateCustom(SolApplication solApplication, SolInputManager.InputPointer[] inputPointers, boolean clickedOutside) { - if (clickedOutside) { - closeControl.maybeFlashPressed(solApplication.getOptions().getKeyClose()); - return; - } - SolGame game = solApplication.getGame(); - Hero hero = game.getHero(); - SolInputManager inputManager = solApplication.getInputManager(); - if (closeControl.isJustOff() || isTargetFar(hero)) { - inputManager.setScreen(solApplication, game.getScreens().oldMainGameScreen); - return; - } - - boolean station = target.getHull().config.getType() == HullConfig.Type.STATION; - shipsControl.setEnabled(station); - hireControl.setEnabled(station); - - InventoryScreen inventoryScreen = game.getScreens().inventoryScreen; - boolean sell = sellControl.isJustOff(); - boolean buy = buyControl.isJustOff(); - boolean sellShips = shipsControl.isJustOff(); - boolean hire = hireControl.isJustOff(); - if (sell || buy || sellShips || hire) { - inventoryScreen.setOperations(sell ? inventoryScreen.sellItems : buy ? inventoryScreen.buyItemsScreen : sellShips ? inventoryScreen.changeShipScreen : inventoryScreen.hireShipsScreen); - inputManager.setScreen(solApplication, game.getScreens().oldMainGameScreen); - inputManager.addScreen(solApplication, inventoryScreen); - } - } - - boolean isTargetFar(Hero hero) { - if (hero.isTranscendent() || target == null || target.getLife() <= 0) { - return true; - } - float dst = target.getPosition().dst(hero.getPosition()) - hero.getHull().config.getApproxRadius() - target.getHull().config.getApproxRadius(); - return MAX_TALK_DIST < dst; - } - - @Override - public void drawBackground(UiDrawer uiDrawer, SolApplication solApplication) { - uiDrawer.draw(background, SolColor.UI_BG); - } - - @Override - public boolean reactsToClickOutside() { - return true; - } - - @Override - public boolean isCursorOnBackground(SolInputManager.InputPointer inputPointer) { - return background.contains(inputPointer.x, inputPointer.y); - } - - public SolShip getTarget() { - return target; - } - - public void setTarget(SolShip target) { - this.target = target; - } -} diff --git a/engine/src/main/java/org/destinationsol/ui/TutorialManager.java b/engine/src/main/java/org/destinationsol/ui/TutorialManager.java index fdeb7e26d..69d733734 100644 --- a/engine/src/main/java/org/destinationsol/ui/TutorialManager.java +++ b/engine/src/main/java/org/destinationsol/ui/TutorialManager.java @@ -198,9 +198,9 @@ public void start() { } if (mouseCtrl || mobile) { - addStep("See what there is to buy", screens.talkScreen.buyControl, true); + addStep("See what there is to buy", screens.talkScreen.getBuyButton(), true); } else { - addStep("See what there is to buy\n(" + gameOptions.getKeyBuyMenuName() + " key)", screens.talkScreen.buyControl, true); + addStep("See what there is to buy\n(" + gameOptions.getKeyBuyMenuName() + " key)", screens.talkScreen.getBuyButton(), true); } if (mobile) { diff --git a/engine/src/main/java/org/destinationsol/ui/nui/screens/MainGameScreen.java b/engine/src/main/java/org/destinationsol/ui/nui/screens/MainGameScreen.java index 678ac4b89..fdb3a1282 100644 --- a/engine/src/main/java/org/destinationsol/ui/nui/screens/MainGameScreen.java +++ b/engine/src/main/java/org/destinationsol/ui/nui/screens/MainGameScreen.java @@ -37,7 +37,6 @@ import org.destinationsol.game.planet.Planet; import org.destinationsol.game.screens.BorderDrawer; import org.destinationsol.game.screens.GameScreens; -import org.destinationsol.game.screens.TalkScreen; import org.destinationsol.game.screens.ZoneNameAnnouncer; import org.destinationsol.game.ship.SolShip; import org.destinationsol.ui.SolInputManager; @@ -941,9 +940,11 @@ private void onTalkButtonClicked(UIWidget widget) { GameScreens gameScreens = game.getScreens(); solInputManager.setScreen(solApplication, gameScreens.oldMainGameScreen); - if (!solInputManager.isScreenOn(gameScreens.talkScreen)) { + if (!nuiManager.hasScreen(gameScreens.talkScreen)) { gameScreens.talkScreen.setTarget(talkTarget); - solInputManager.addScreen(solApplication, gameScreens.talkScreen); + nuiManager.pushScreen(gameScreens.talkScreen); + } else { + nuiManager.removeScreen(gameScreens.talkScreen); } } diff --git a/engine/src/main/java/org/destinationsol/ui/nui/screens/TalkScreen.java b/engine/src/main/java/org/destinationsol/ui/nui/screens/TalkScreen.java new file mode 100644 index 000000000..2c6f5ed89 --- /dev/null +++ b/engine/src/main/java/org/destinationsol/ui/nui/screens/TalkScreen.java @@ -0,0 +1,148 @@ +/* + * Copyright 2021 The Terasology Foundation + * + * 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.destinationsol.ui.nui.screens; + +import org.destinationsol.SolApplication; +import org.destinationsol.game.Hero; +import org.destinationsol.game.screens.InventoryScreen; +import org.destinationsol.game.ship.SolShip; +import org.destinationsol.game.ship.hulls.HullConfig; +import org.destinationsol.ui.nui.NUIScreenLayer; +import org.destinationsol.ui.nui.widgets.KeyActivatedButton; +import org.destinationsol.ui.nui.widgets.UIWarnButton; +import org.terasology.nui.backends.libgdx.GDXInputUtil; + +import javax.inject.Inject; + +/** + * The talk screen allows the player to perform actions at a station. + * At the moment, the player can buy and sell items from here, purchase new ships or hire mercenaries. + */ +public class TalkScreen extends NUIScreenLayer { + public static final float MAX_TALK_DIST = 1f; + private final SolApplication solApplication; + private UIWarnButton buyButton; + private KeyActivatedButton changeShipButton; + private KeyActivatedButton hireButton; + private SolShip target; + + @Inject + public TalkScreen(SolApplication solApplication) { + this.solApplication = solApplication; + } + + @Override + public void initialise() { + KeyActivatedButton sellButton = find("sellButton", KeyActivatedButton.class); + sellButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeySellMenu())); + sellButton.subscribe(button -> { + InventoryScreen inventoryScreen = solApplication.getGame().getScreens().inventoryScreen; + inventoryScreen.setOperations(inventoryScreen.sellItems); + nuiManager.removeScreen(this); + solApplication.getInputManager().addScreen(solApplication, inventoryScreen); + }); + + buyButton = find("buyButton", UIWarnButton.class); + buyButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyBuyMenu())); + buyButton.subscribe(button -> { + InventoryScreen inventoryScreen = solApplication.getGame().getScreens().inventoryScreen; + inventoryScreen.setOperations(inventoryScreen.buyItemsScreen); + nuiManager.removeScreen(this); + solApplication.getInputManager().addScreen(solApplication, inventoryScreen); + }); + + changeShipButton = find("changeShipButton", KeyActivatedButton.class); + changeShipButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyChangeShipMenu())); + changeShipButton.subscribe(button -> { + InventoryScreen inventoryScreen = solApplication.getGame().getScreens().inventoryScreen; + inventoryScreen.setOperations(inventoryScreen.changeShipScreen); + nuiManager.removeScreen(this); + solApplication.getInputManager().addScreen(solApplication, inventoryScreen); + }); + + hireButton = find("hireButton", KeyActivatedButton.class); + hireButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyHireShipMenu())); + hireButton.subscribe(button -> { + InventoryScreen inventoryScreen = solApplication.getGame().getScreens().inventoryScreen; + inventoryScreen.setOperations(inventoryScreen.hireShipsScreen); + nuiManager.removeScreen(this); + solApplication.getInputManager().addScreen(solApplication, inventoryScreen); + }); + + KeyActivatedButton closeButton = find("closeButton", KeyActivatedButton.class); + closeButton.setKey(GDXInputUtil.GDXToNuiKey(solApplication.getOptions().getKeyClose())); + closeButton.subscribe(button -> { + nuiManager.removeScreen(this); + }); + } + + @Override + public void onAdded() { + boolean isStation = target.getHull().config.getType() == HullConfig.Type.STATION; + changeShipButton.setEnabled(isStation); + hireButton.setEnabled(isStation); + } + + @Override + public void update(float delta) { + super.update(delta); + + if (isTargetFar(solApplication.getGame().getHero())) { + nuiManager.removeScreen(this); + } + } + + /** + * Returns the button pressed to open the buy items screen. + * + * This is exposed directly for use in the tutorial. + * @return the buy items button + */ + public UIWarnButton getBuyButton() { + return buyButton; + } + + /** + * Returns the current ship being talked to. + * @return the current ship being talked to + */ + public SolShip getTarget() { + return target; + } + + /** + * Assigns the ship to talk to + * @param target the ship to talk to + */ + public void setTarget(SolShip target) { + this.target = target; + } + + /** + * Returns true if the target is within communicating range. + * @return true, if the target is within communicating range, otherwise false + */ + public boolean isTargetFar(Hero hero) { + if (hero.isTranscendent() || target == null || target.getLife() <= 0) { + return true; + } + + float distance = target.getPosition().dst(hero.getPosition()) - hero.getHull().config.getApproxRadius() - target.getHull().config.getApproxRadius(); + + return (MAX_TALK_DIST < distance); + } +} diff --git a/engine/src/main/resources/org/destinationsol/assets/skins/talkScreen.skin b/engine/src/main/resources/org/destinationsol/assets/skins/talkScreen.skin new file mode 100644 index 000000000..087d482d2 --- /dev/null +++ b/engine/src/main/resources/org/destinationsol/assets/skins/talkScreen.skin @@ -0,0 +1,23 @@ +{ + "inherit": "engine:mainGameScreen", + "families": { + "talkScreenButton": { + "elements": { + "UIButton": { + "max-width": 320, + "max-height": 256 + } + }, + "font": "engine:main#1" + }, + "talkScreenMenuBox": { + "max-width": 320, + "max-height": 800, + "elements": { + "UIBox": { + "background": "engine:background" + } + } + } + } +} \ No newline at end of file diff --git a/engine/src/main/resources/org/destinationsol/assets/ui/talkScreen.ui b/engine/src/main/resources/org/destinationsol/assets/ui/talkScreen.ui new file mode 100644 index 000000000..6d7d8dcb5 --- /dev/null +++ b/engine/src/main/resources/org/destinationsol/assets/ui/talkScreen.ui @@ -0,0 +1,63 @@ +{ + "type": "TalkScreen", + "skin": "engine:talkScreen", + "contents": { + "type": "RelativeLayout", + "contents": [ + { + "type": "UIBackingBox", + "family": "talkScreenMenuBox", + "layoutInfo": { + "position-top": { + "target": "MIDDLE", + "offset": 32 + }, + "position-bottom": { + "offset": 32 + }, + "position-left": { + "offset": 64 + }, + "position-right": { + "offset": 64 + }, + "position-horizontal-center": {} + }, + "content": { + "type": "ColumnLayout", + "id": "menuButtons", + "family": "talkScreenButton", + "columns": 1, + "verticalSpacing": 10, + "contents": [ + { + "type": "KeyActivatedButton", + "id": "sellButton", + "text": "Sell" + }, + { + "type": "UIWarnButton", + "id": "buyButton", + "text": "Buy" + }, + { + "type": "KeyActivatedButton", + "id": "changeShipButton", + "text": "Change Ship" + }, + { + "type": "KeyActivatedButton", + "id": "hireButton", + "text": "Hire" + }, + { + "type": "KeyActivatedButton", + "id": "closeButton", + "text": "Close" + } + ] + } + } + ] + } +} \ No newline at end of file