Skip to content

Commit 435d0ff

Browse files
committed
feat(nui): convert the talk screen to NUI
1 parent 945fb4f commit 435d0ff

File tree

11 files changed

+240
-148
lines changed

11 files changed

+240
-148
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
@@ -24,6 +24,7 @@
2424
import org.destinationsol.game.item.SolItem;
2525
import org.destinationsol.game.ship.SolShip;
2626
import org.destinationsol.ui.nui.screens.InventoryScreen;
27+
import org.destinationsol.ui.nui.screens.TalkScreen;
2728
import org.destinationsol.ui.nui.widgets.UIWarnButton;
2829
import org.terasology.nui.backends.libgdx.GDXInputUtil;
2930
import org.terasology.nui.widgets.UIButton;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.destinationsol.game.ship.hulls.Hull;
3030
import org.destinationsol.game.ship.hulls.HullConfig;
3131
import org.destinationsol.ui.nui.screens.InventoryScreen;
32+
import org.destinationsol.ui.nui.screens.TalkScreen;
3233
import org.destinationsol.ui.nui.widgets.KeyActivatedButton;
3334
import org.terasology.nui.backends.libgdx.GDXInputUtil;
3435
import org.terasology.nui.widgets.UIButton;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import org.destinationsol.SolApplication;
1919
import org.destinationsol.game.context.Context;
2020
import org.destinationsol.ui.SolLayouts;
21+
import org.destinationsol.ui.nui.NUIManager;
2122
import org.destinationsol.ui.nui.screens.InventoryScreen;
2223
import org.destinationsol.ui.nui.screens.MenuScreen;
24+
import org.destinationsol.ui.nui.screens.TalkScreen;
2325

2426
public class GameScreens {
2527
public final MainGameScreen mainGameScreen;
@@ -33,11 +35,13 @@ public class GameScreens {
3335
public GameScreens(SolApplication cmp, Context context) {
3436
SolLayouts layouts = cmp.getLayouts();
3537
RightPaneLayout rightPaneLayout = layouts.rightPaneLayout;
38+
NUIManager nuiManager = cmp.getNuiManager();
39+
3640
mainGameScreen = new MainGameScreen(rightPaneLayout, context);
3741
mapScreen = new MapScreen(rightPaneLayout, cmp.isMobile(), cmp.getOptions());
38-
menuScreen = (MenuScreen) cmp.getNuiManager().createScreen("engine:menuScreen");
39-
inventoryScreen = (org.destinationsol.ui.nui.screens.InventoryScreen) cmp.getNuiManager().createScreen("engine:inventoryScreen");
40-
talkScreen = new TalkScreen(layouts.menuLayout, cmp.getOptions());
42+
menuScreen = (MenuScreen) nuiManager.createScreen("engine:menuScreen");
43+
inventoryScreen = (InventoryScreen) nuiManager.createScreen("engine:inventoryScreen");
44+
talkScreen = (TalkScreen) nuiManager.createScreen("engine:talkScreen");
4145
waypointCreationScreen = new WaypointCreationScreen(layouts.menuLayout, cmp.getOptions(), mapScreen);
4246
consoleScreen = new ConsoleScreen(context);
4347
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.destinationsol.game.item.SolItem;
2424
import org.destinationsol.mercenary.MercenaryUtils;
2525
import org.destinationsol.ui.nui.screens.InventoryScreen;
26+
import org.destinationsol.ui.nui.screens.TalkScreen;
2627
import org.destinationsol.ui.nui.widgets.KeyActivatedButton;
2728
import org.terasology.nui.backends.libgdx.GDXInputUtil;
2829
import org.terasology.nui.widgets.UIButton;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.destinationsol.game.item.SolItem;
2424
import org.destinationsol.game.ship.SolShip;
2525
import org.destinationsol.ui.nui.screens.InventoryScreen;
26+
import org.destinationsol.ui.nui.screens.TalkScreen;
2627
import org.destinationsol.ui.nui.widgets.KeyActivatedButton;
2728
import org.terasology.nui.backends.libgdx.GDXInputUtil;
2829
import org.terasology.nui.widgets.UIButton;

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

Lines changed: 0 additions & 140 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
@@ -199,9 +199,9 @@ public void start() {
199199
}
200200

201201
if (mouseCtrl || mobile) {
202-
addStep("See what there is to buy", screens.talkScreen.buyControl, true);
202+
addStep("See what there is to buy", screens.talkScreen.getBuyButton(), true);
203203
} else {
204-
addStep("See what there is to buy\n(" + gameOptions.getKeyBuyMenuName() + " key)", screens.talkScreen.buyControl, true);
204+
addStep("See what there is to buy\n(" + gameOptions.getKeyBuyMenuName() + " key)", screens.talkScreen.getBuyButton(), true);
205205
}
206206

207207
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
@@ -24,7 +24,6 @@
2424
import org.destinationsol.game.SolGame;
2525
import org.destinationsol.game.SolObject;
2626
import org.destinationsol.game.screens.GameScreens;
27-
import org.destinationsol.game.screens.TalkScreen;
2827
import org.destinationsol.game.ship.SolShip;
2928
import org.destinationsol.ui.SolInputManager;
3029
import org.destinationsol.ui.nui.NUIScreenLayer;
@@ -279,9 +278,11 @@ private void onTalkButtonClicked(UIWidget widget) {
279278
GameScreens gameScreens = solApplication.getGame().getScreens();
280279

281280
solInputManager.setScreen(solApplication, gameScreens.mainGameScreen);
282-
if (!solInputManager.isScreenOn(gameScreens.talkScreen)) {
281+
if (!nuiManager.hasScreen(gameScreens.talkScreen)) {
283282
gameScreens.talkScreen.setTarget(talkTarget);
284-
solInputManager.addScreen(solApplication, gameScreens.talkScreen);
283+
nuiManager.pushScreen(gameScreens.talkScreen);
284+
} else {
285+
nuiManager.removeScreen(gameScreens.talkScreen);
285286
}
286287
}
287288

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

0 commit comments

Comments
 (0)