Skip to content

Commit

Permalink
disable buttons which are not ready yet, and add default generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ar0ne committed Jun 27, 2018
1 parent 3be9575 commit 64fe6dd
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.terasology.rendering.nui.databinding.ReadOnlyBinding;
import org.terasology.rendering.nui.events.NUIKeyEvent;
import org.terasology.rendering.nui.itemRendering.StringTextRenderer;
import org.terasology.rendering.nui.layers.mainMenu.savedGames.GameInfo;
import org.terasology.rendering.nui.layers.mainMenu.savedGames.GameProvider;
import org.terasology.rendering.nui.layers.mainMenu.selectModulesScreen.SelectModulesScreen;
import org.terasology.rendering.nui.widgets.UIButton;
Expand All @@ -71,7 +70,6 @@ public class CreateGameScreen extends CoreScreenLayer {

public static final ResourceUrn ASSET_URI = new ResourceUrn("engine:createGameScreen");

private static final String DEFAULT_GAME_NAME_PREFIX = "Game ";
private static final Logger logger = LoggerFactory.getLogger(CreateGameScreen.class);

private static final String DEFAULT_GAME_TEMPLATE_NAME = "JoshariasSurvival";
Expand Down Expand Up @@ -345,19 +343,7 @@ public void onOpened() {

private void setGameName(UIText gameName) {
if (gameName != null) {
int gameNum = 1;
for (GameInfo info : GameProvider.getSavedGames()) {
if (info.getManifest().getTitle().startsWith(DEFAULT_GAME_NAME_PREFIX)) {
String remainder = info.getManifest().getTitle().substring(DEFAULT_GAME_NAME_PREFIX.length());
try {
gameNum = Math.max(gameNum, Integer.parseInt(remainder) + 1);
} catch (NumberFormatException e) {
logger.trace("Could not parse {} as integer (not an error)", remainder, e);
}
}
}

gameName.setText(DEFAULT_GAME_NAME_PREFIX + gameNum);
gameName.setText(GameProvider.getNextGameName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
import org.terasology.config.ModuleConfig;
import org.terasology.engine.GameEngine;
import org.terasology.engine.SimpleUri;
import org.terasology.engine.TerasologyConstants;
import org.terasology.engine.modes.StateLoading;
import org.terasology.engine.module.ModuleManager;
import org.terasology.engine.module.StandardModuleExtension;
import org.terasology.game.GameManifest;
import org.terasology.i18n.TranslationSystem;
import org.terasology.module.DependencyResolver;
import org.terasology.module.Module;
import org.terasology.module.ResolutionResult;
import org.terasology.naming.Name;
import org.terasology.network.NetworkMode;
import org.terasology.registry.In;
Expand All @@ -43,30 +41,23 @@
import org.terasology.rendering.nui.databinding.Binding;
import org.terasology.rendering.nui.databinding.ReadOnlyBinding;
import org.terasology.rendering.nui.itemRendering.StringTextRenderer;
import org.terasology.rendering.nui.layers.mainMenu.savedGames.GameInfo;
import org.terasology.rendering.nui.layers.mainMenu.savedGames.GameProvider;
import org.terasology.rendering.nui.layers.mainMenu.selectModulesScreen.AdvancedGameSetupScreen;
import org.terasology.rendering.nui.widgets.UIDropdown;
import org.terasology.rendering.nui.widgets.UIDropdownScrollable;
import org.terasology.rendering.nui.widgets.UILabel;
import org.terasology.rendering.nui.widgets.UIText;
import org.terasology.utilities.random.FastRandom;
import org.terasology.world.generator.internal.WorldGeneratorInfo;
import org.terasology.world.generator.internal.WorldGeneratorManager;
import org.terasology.world.internal.WorldInfo;
import org.terasology.world.time.WorldTime;

import java.util.Collections;
import java.util.List;

public class NewGameScreen extends CoreScreenLayer {

public static final ResourceUrn ASSET_URI = new ResourceUrn("engine:newGameScreen");
private static final String DEFAULT_GAME_NAME_PREFIX = "Game ";
private static final Logger logger = LoggerFactory.getLogger(CreateGameScreen.class);
private static final String DEFAULT_GAME_TEMPLATE_NAME = "JoshariasSurvival";
private static final String DEFAULT_WORLD_GENERATOR = "Core:FacetedPerlin";
private boolean loadingAsServer;

@In
private ModuleManager moduleManager;
Expand Down Expand Up @@ -158,34 +149,16 @@ public String get() {
});

WidgetUtil.trySubscribe(this, "play", button -> {
if(gameName.getText().isEmpty()) {
if (gameName.getText().isEmpty()) {
getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error", "Game name cannot be empty");
} else {
GameManifest gameManifest = new GameManifest();

gameManifest.setTitle(gameName.getText());
String tempSeed = new FastRandom().nextString(32);
gameManifest.setSeed(tempSeed);
DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(config.getDefaultModSelection().listModules());
if (!result.isSuccess()) {
MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
if (errorMessagePopup != null) {
errorMessagePopup.setMessage("Invalid Module Selection", "Please review your module seleciton and try again");
}
return;
}
for (Module module : result.getModules()) {
gameManifest.addModule(module.getId(), module.getVersion());
GameManifest gameManifest = NewGameSetupHelper.buildNewGameSetup(universeWrapper, moduleManager, config);
if (gameManifest != null) {
gameEngine.changeState(new StateLoading(gameManifest, (isLoadingAsServer()) ? NetworkMode.DEDICATED_SERVER : NetworkMode.NONE));
} else {
MessagePopup errorPopup = getManager().createScreen(MessagePopup.ASSET_URI, MessagePopup.class);
errorPopup.setMessage("Error", "Can't create new game!");
}

SimpleUri uri = config.getWorldGeneration().getDefaultGenerator();
// This is multiplied by the number of seconds in a day (86400000) to determine the exact millisecond at which the game will start.
final float timeOffset = 0.50f;
WorldInfo worldInfo = new WorldInfo(TerasologyConstants.MAIN_WORLD, tempSeed,
(long) (WorldTime.DAY_LENGTH * timeOffset), uri);
gameManifest.addWorld(worldInfo);
gameEngine.changeState(new StateLoading(gameManifest, (isLoadingAsServer()) ? NetworkMode.DEDICATED_SERVER : NetworkMode.NONE));
}
});

Expand All @@ -200,19 +173,7 @@ public String get() {
*/
private void setGameName(UIText gameName) {
if (gameName != null) {
int gameNumber = 1;
for (GameInfo info : GameProvider.getSavedGames()) {
if (info.getManifest().getTitle().startsWith(DEFAULT_GAME_NAME_PREFIX)) {
String remainder = info.getManifest().getTitle().substring(DEFAULT_GAME_NAME_PREFIX.length());
try {
gameNumber = Math.max(gameNumber, Integer.parseInt(remainder) + 1);
} catch (NumberFormatException e) {
logger.trace("Could not parse {} as integer (not an error)", remainder, e);
}
}
}

gameName.setText(DEFAULT_GAME_NAME_PREFIX + gameNumber);
gameName.setText(GameProvider.getNextGameName());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.terasology.rendering.nui.layers.mainMenu;

import org.codehaus.plexus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.config.Config;
import org.terasology.engine.SimpleUri;
import org.terasology.engine.TerasologyConstants;
import org.terasology.engine.module.ModuleManager;
import org.terasology.game.GameManifest;
import org.terasology.module.DependencyResolver;
import org.terasology.module.Module;
import org.terasology.module.ResolutionResult;
import org.terasology.rendering.nui.layers.mainMenu.savedGames.GameProvider;
import org.terasology.utilities.random.FastRandom;
import org.terasology.world.internal.WorldInfo;
import org.terasology.world.time.WorldTime;


/**
* @TODO: rename?, add more methods for different screens and cases?
*/
public class NewGameSetupHelper {

private static final Logger logger = LoggerFactory.getLogger(NewGameSetupHelper.class);

private NewGameSetupHelper() {
}

public static GameManifest buildNewGameSetup(final UniverseWrapper universeWrapper, final ModuleManager moduleManager, final Config config) {
GameManifest gameManifest = new GameManifest();
if (StringUtils.isNotBlank(universeWrapper.getGameName())) {
gameManifest.setTitle(universeWrapper.getGameName());
} else {
gameManifest.setTitle(GameProvider.getNextGameName());
}

String seed;
if (StringUtils.isNotBlank(universeWrapper.getSeed())) {
seed = universeWrapper.getSeed();
} else {
seed = new FastRandom().nextString(32);
}
gameManifest.setSeed(seed);

DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(config.getDefaultModSelection().listModules());
if (!result.isSuccess()) {
logger.error("Can't resolve dependencies");
return null;
}
for (Module module : result.getModules()) {
gameManifest.addModule(module.getId(), module.getVersion());
}

SimpleUri uri = config.getWorldGeneration().getDefaultGenerator();
// This is multiplied by the number of seconds in a day (86400000) to determine the exact millisecond at which the game will start.
final float timeOffset = 0.50f;
WorldInfo worldInfo = new WorldInfo(TerasologyConstants.MAIN_WORLD, seed,
(long) (WorldTime.DAY_LENGTH * timeOffset), uri);


gameManifest.addWorld(worldInfo);
return gameManifest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
public final class GameProvider {

private static final Logger logger = LoggerFactory.getLogger(GameProvider.class);
private static final String DEFAULT_GAME_NAME_PREFIX = "Game ";

private GameProvider() {
}
Expand Down Expand Up @@ -100,4 +101,22 @@ private static BufferedImage getSavedGamePreviewImage(Path path) {
return null;
}

/**
* Generates the game name based on the game number of the last saved game
*/
public static String getNextGameName() {
int gameNumber = 1;
for (GameInfo info : GameProvider.getSavedGames()) {
if (info.getManifest().getTitle().startsWith(DEFAULT_GAME_NAME_PREFIX)) {
String remainder = info.getManifest().getTitle().substring(DEFAULT_GAME_NAME_PREFIX.length());
try {
gameNumber = Math.max(gameNumber, Integer.parseInt(remainder) + 1);
} catch (NumberFormatException e) {
logger.trace("Could not parse {} as integer (not an error)", remainder, e);
}
}
}
return DEFAULT_GAME_NAME_PREFIX + gameNumber;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.codehaus.plexus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.assets.ResourceUrn;
import org.terasology.config.Config;
import org.terasology.config.ModuleConfig;
import org.terasology.config.SelectModulesConfig;
import org.terasology.engine.GameEngine;
import org.terasology.engine.SimpleUri;
import org.terasology.engine.TerasologyConstants;
import org.terasology.engine.modes.StateLoading;
import org.terasology.engine.module.DependencyResolutionFailedException;
import org.terasology.engine.module.ModuleInstaller;
import org.terasology.engine.module.ModuleManager;
import org.terasology.engine.module.StandardModuleExtension;
import org.terasology.game.GameManifest;
import org.terasology.i18n.TranslationSystem;
import org.terasology.math.geom.Vector2i;
import org.terasology.module.DependencyInfo;
Expand All @@ -38,6 +42,7 @@
import org.terasology.module.ModuleMetadata;
import org.terasology.module.ResolutionResult;
import org.terasology.naming.Name;
import org.terasology.network.NetworkMode;
import org.terasology.registry.In;
import org.terasology.rendering.nui.Canvas;
import org.terasology.rendering.nui.CoreScreenLayer;
Expand All @@ -48,6 +53,7 @@
import org.terasology.rendering.nui.itemRendering.AbstractItemRenderer;
import org.terasology.rendering.nui.layers.mainMenu.ConfirmPopup;
import org.terasology.rendering.nui.layers.mainMenu.MessagePopup;
import org.terasology.rendering.nui.layers.mainMenu.NewGameSetupHelper;
import org.terasology.rendering.nui.layers.mainMenu.UniverseSetupScreen;
import org.terasology.rendering.nui.layers.mainMenu.UniverseWrapper;
import org.terasology.rendering.nui.layers.mainMenu.WaitPopup;
Expand Down Expand Up @@ -96,6 +102,8 @@ public class AdvancedGameSetupScreen extends CoreScreenLayer {
private WorldGeneratorManager worldGenManager;
@In
private TranslationSystem translationSystem;
@In
private GameEngine gameEngine;

private Map<Name, ModuleSelectionInfo> modulesLookup;
private List<ModuleSelectionInfo> sortedModules;
Expand Down Expand Up @@ -431,6 +439,19 @@ public String get() {
triggerForwardAnimation(universeSetupScreen);
});

WidgetUtil.trySubscribe(this, "play", button -> {
if (StringUtils.isBlank(seed.getText())) {
getManager().createScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error", "Game seed cannot be empty!");
} else {
GameManifest gameManifest = NewGameSetupHelper.buildNewGameSetup(universeWrapper, moduleManager, config);
if (gameManifest != null) {
gameEngine.changeState(new StateLoading(gameManifest, (universeWrapper.getLoadingAsServer()) ? NetworkMode.DEDICATED_SERVER : NetworkMode.NONE));
} else {
getManager().createScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error", "Can't create new game!");
}
}
});

WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
{
"type": "UIButton",
"text": "${engine:menu#start-game}",
"id": "play"
"id": "play",
"enabled": false
}
],
"layoutInfo": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@
{
"type": "UIButton",
"text": "${engine:menu#start-game}",
"id": "play"
"id": "play",
"enabled": false
}
],
"layoutInfo": {
Expand Down

0 comments on commit 64fe6dd

Please sign in to comment.