Skip to content

Commit 7ec98eb

Browse files
authored
Merge pull request #667 from BenjaminAmos/nui-screen-conversion/talk-screen
Convert the talk screen to NUI
2 parents 14c38a5 + 3904ff5 commit 7ec98eb

File tree

11 files changed

+246
-134
lines changed

11 files changed

+246
-134
lines changed

engine/src/main/java/org/destinationsol/game/screens/BuyItemsScreen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.destinationsol.game.ship.SolShip;
2727
import org.destinationsol.ui.SolInputManager;
2828
import org.destinationsol.ui.SolUiControl;
29+
import org.destinationsol.ui.nui.screens.TalkScreen;
2930

3031
public class BuyItemsScreen extends InventoryOperationsScreen {
3132
public final SolUiControl buyControl;

engine/src/main/java/org/destinationsol/game/screens/ChangeShipScreen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.destinationsol.game.ship.hulls.HullConfig;
3232
import org.destinationsol.ui.SolInputManager;
3333
import org.destinationsol.ui.SolUiControl;
34+
import org.destinationsol.ui.nui.screens.TalkScreen;
3435

3536
public class ChangeShipScreen extends InventoryOperationsScreen {
3637
private final SolUiControl changeControl;

engine/src/main/java/org/destinationsol/game/screens/GameScreens.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.destinationsol.game.context.Context;
2121
import org.destinationsol.ui.SolLayouts;
2222
import org.destinationsol.ui.nui.screens.MenuScreen;
23+
import org.destinationsol.ui.nui.screens.TalkScreen;
2324

2425
import javax.inject.Inject;
2526

@@ -49,7 +50,7 @@ public GameScreens(SolApplication cmp, Context context) {
4950
mapScreen = new MapScreen(rightPaneLayout, cmp.isMobile(), cmp.getOptions());
5051
menuScreen = (MenuScreen) cmp.getNuiManager().createScreen("engine:menuScreen");
5152
inventoryScreen = new InventoryScreen(cmp.getOptions());
52-
talkScreen = new TalkScreen(layouts.menuLayout, cmp.getOptions());
53+
talkScreen = (TalkScreen) cmp.getNuiManager().createScreen("engine:talkScreen");
5354
waypointCreationScreen = new WaypointCreationScreen(layouts.menuLayout, cmp.getOptions(), mapScreen);
5455
consoleScreen = new ConsoleScreen(context.get(Console.class));
5556
}

engine/src/main/java/org/destinationsol/game/screens/HireShipsScreen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.destinationsol.mercenary.MercenaryUtils;
2626
import org.destinationsol.ui.SolInputManager;
2727
import org.destinationsol.ui.SolUiControl;
28+
import org.destinationsol.ui.nui.screens.TalkScreen;
2829

2930
public class HireShipsScreen extends InventoryOperationsScreen {
3031
private final SolUiControl hireControl;

engine/src/main/java/org/destinationsol/game/screens/SellItems.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.destinationsol.game.ship.SolShip;
2525
import org.destinationsol.ui.SolInputManager;
2626
import org.destinationsol.ui.SolUiControl;
27+
import org.destinationsol.ui.nui.screens.TalkScreen;
2728

2829
public class SellItems extends InventoryOperationsScreen {
2930
private static float PERC = .8f;

engine/src/main/java/org/destinationsol/game/screens/TalkScreen.java

Lines changed: 0 additions & 128 deletions
This file was deleted.

engine/src/main/java/org/destinationsol/ui/TutorialManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ public void start() {
198198
}
199199

200200
if (mouseCtrl || mobile) {
201-
addStep("See what there is to buy", screens.talkScreen.buyControl, true);
201+
addStep("See what there is to buy", screens.talkScreen.getBuyButton(), true);
202202
} else {
203-
addStep("See what there is to buy\n(" + gameOptions.getKeyBuyMenuName() + " key)", screens.talkScreen.buyControl, true);
203+
addStep("See what there is to buy\n(" + gameOptions.getKeyBuyMenuName() + " key)", screens.talkScreen.getBuyButton(), true);
204204
}
205205

206206
if (mobile) {

engine/src/main/java/org/destinationsol/ui/nui/screens/MainGameScreen.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.destinationsol.game.planet.Planet;
3838
import org.destinationsol.game.screens.BorderDrawer;
3939
import org.destinationsol.game.screens.GameScreens;
40-
import org.destinationsol.game.screens.TalkScreen;
4140
import org.destinationsol.game.screens.ZoneNameAnnouncer;
4241
import org.destinationsol.game.ship.SolShip;
4342
import org.destinationsol.ui.SolInputManager;
@@ -941,9 +940,11 @@ private void onTalkButtonClicked(UIWidget widget) {
941940
GameScreens gameScreens = game.getScreens();
942941

943942
solInputManager.setScreen(solApplication, gameScreens.oldMainGameScreen);
944-
if (!solInputManager.isScreenOn(gameScreens.talkScreen)) {
943+
if (!nuiManager.hasScreen(gameScreens.talkScreen)) {
945944
gameScreens.talkScreen.setTarget(talkTarget);
946-
solInputManager.addScreen(solApplication, gameScreens.talkScreen);
945+
nuiManager.pushScreen(gameScreens.talkScreen);
946+
} else {
947+
nuiManager.removeScreen(gameScreens.talkScreen);
947948
}
948949
}
949950

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"inherit": "engine:mainGameScreen",
3+
"families": {
4+
"talkScreenButton": {
5+
"elements": {
6+
"UIButton": {
7+
"max-width": 320,
8+
"max-height": 256
9+
}
10+
},
11+
"font": "engine:main#1"
12+
},
13+
"talkScreenMenuBox": {
14+
"max-width": 320,
15+
"max-height": 800,
16+
"elements": {
17+
"UIBox": {
18+
"background": "engine:background"
19+
}
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)