Java tutorial
/* * CrimsonGlow is an adult computer roleplaying game with spanking content. * Copyright (C) 2016 Andrew Russell * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package com.spankingrpgs.util; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.spankingrpgs.scarletmoon.combat.CrimsonCharacterFactory; import com.spankingrpgs.scarletmoon.items.CrimsonItemFactory; import com.spankingrpgs.scarletmoon.loader.CharacterLoader; import com.spankingrpgs.scarletmoon.loader.EventLoader; import com.spankingrpgs.scarletmoon.loader.ItemLoader; import com.spankingrpgs.scarletmoon.skills.*; import com.spankingrpgs.scarletmoon.story.CrimsonStateModificationCommands; import com.spankingrpgs.model.GameState; import com.spankingrpgs.model.characters.CharacterFactory; import com.spankingrpgs.model.items.ItemFactory; import com.spankingrpgs.model.loader.Loader; import com.spankingrpgs.model.music.MusicMap; import com.spankingrpgs.model.skills.Skill; import com.spankingrpgs.model.story.DynamicTextFunctions; import com.spankingrpgs.model.story.EventPredicates; import com.spankingrpgs.model.story.StateModificationCommands; import com.spankingrpgs.model.story.TextParser; import javafx.scene.media.Media; import java.io.File; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.StreamSupport; /** * Provides a collection of utility methods for loading game state. */ public class LoadStateUtils { private static final Logger LOG = Logger.getLogger(LoadStateUtils.class.getName()); private static boolean loadMusic = new File("data/music").exists(); public static void loadCrossGameData() { loadCrossGameData(System.getProperty("user.dir")); } public static void loadCrossGameData(String gameRoot) { CharacterFactory characterFactory = new CrimsonCharacterFactory(); CharacterLoader jsonCharacterLoader = new CharacterLoader(characterFactory); CharacterLoader yamlCharacterLoader = new CharacterLoader(characterFactory, new ObjectMapper(new YAMLFactory())); EventLoader jsonEventLoader = new EventLoader(gameRoot); EventLoader yamlEventLoader = new EventLoader(new ObjectMapper(new YAMLFactory()), gameRoot); ItemFactory itemFactory = new CrimsonItemFactory(); ItemLoader jsonItemLoader = new ItemLoader(itemFactory); ItemLoader yamlItemLoader = new ItemLoader(itemFactory, new ObjectMapper(new YAMLFactory())); try { DynamicTextFunctions.registerFunctions(); StateModificationCommands.registerCommands(); CrimsonStateModificationCommands.registerCommands(); EventPredicates.registerPredicates(); LoadStateUtils.loadSkills(); LoadStateUtils.loadJsonData(jsonItemLoader, Paths.get(gameRoot, "data/items")); LoadStateUtils.loadYamlData(yamlItemLoader, Paths.get(gameRoot, "data/items")); LoadStateUtils.loadJsonData(jsonCharacterLoader, Paths.get(gameRoot, "data/characters")); LoadStateUtils.loadYamlData(yamlCharacterLoader, Paths.get(gameRoot, "data/characters")); LoadStateUtils.loadJsonData(jsonEventLoader, Paths.get(gameRoot, "data/events")); LoadStateUtils.loadYamlData(yamlEventLoader, Paths.get(gameRoot, "data/events")); LoadStateUtils.loadBattleMusic(gameRoot); } catch (Exception e) { LOG.log(Level.SEVERE, "Error encountered while loading.", e); System.exit(1); } } private static void loadBattleMusic(String gameRoot) { if (!loadMusic) { return; } Media battleMusic = new Media( new File(Paths.get(gameRoot, "data/music/bloodlust.mp3").toString()).toURI().toString()); Media bossMusic = new Media( new File(Paths.get(gameRoot, "data/music/megaboss.mp3").toString()).toURI().toString()); MusicMap.INSTANCE.put("bloodlust", battleMusic); MusicMap.INSTANCE.put("megaboss", bossMusic); StateModificationCommands.battleMusic = "bloodlust"; StateModificationCommands.bossMusic = "megaboss"; } /** * Loads all the skills into the game state. */ public static void loadSkills() { GameState cleanState = GameState.getCleanInstance(); List<Skill> skills = Arrays.asList(new ScarletSlap(), new ScarletFlare(), new ScarletEyes(), new ScarletCord(), new ScarletBoots(), new ScarletBeam(), new ScarletArmor(), new Buzzsaw(), new Chainwhip(), new SoaringLeap()); skills.forEach(skill -> cleanState.addSkill(skill.getName(), skill)); } /** * Given a Loader, and a path to data files, loads the data from the files into the game using the loader. * * @param loader The loader to use to load data into the game * @param dataPath The path containing the files containing the data to load * @param fileGlob A glob that describes the kinds of files to load * @param newLineMarker The String to use to represent new lines */ public static void loadData(Loader loader, Path dataPath, String fileGlob, String newLineMarker) { LOG.info(String.format("Loading %s", dataPath)); try { PathMatcher jsonMatcher = FileSystems.getDefault().getPathMatcher(fileGlob); Collection<String> data = StreamSupport.stream(Files.newDirectoryStream(dataPath).spliterator(), false) .peek(path -> LOG.fine(String.format("Loading data from %s", path))) .filter(jsonMatcher::matches).map((Path path) -> { try { return String.join(newLineMarker, Files.readAllLines(path)); } catch (IOException e) { String msg = String.format("Problem reading file: %s", path); LOG.log(Level.SEVERE, String.format("%s with exception:%s", msg, e), e); throw new RuntimeException(msg); } }).collect(Collectors.toList()); loader.load(data, GameState.getCleanInstance()); } catch (IOException exception) { String msg = String.format("Problem reading files in: %s", dataPath); LOG.log(Level.SEVERE, String.format("%s with exception: %s", msg, exception), exception); throw new RuntimeException(msg); } } /** * Given a Loader, and a path to JSON files, loads the data from the JSON files into the game using the loader. * * @param loader The loader to use to load data into the game * @param dataPath The path containing the files containing the data to load */ public static void loadJsonData(Loader loader, Path dataPath) { loadData(loader, dataPath, "glob:**/*.json", TextParser.NEW_LINE_MARKER); } /** * Given a Loader, and a path to YAML files, loads the data from the JSON files into the game using the loader. * * @param loader The loader to use to load data into the game * @param dataPath The path containing the files containing the data to load */ public static void loadYamlData(Loader loader, Path dataPath) { loadData(loader, dataPath, "glob:**/*.yaml", "\n"); } }