added Youtube download, Video conversion and caching, tweaked some settings
This commit is contained in:
parent
f9e5988dca
commit
031c350dd0
@ -30,9 +30,9 @@ import com.jme3.renderer.ViewPort;
|
||||
import com.jme3.scene.Node;
|
||||
import org.wyrez.persij.PersiJContainer;
|
||||
import org.wyrez.shootingstars.data.AudioDataManager;
|
||||
import org.wyrez.shootingstars.data.YTDownloader;
|
||||
import org.wyrez.shootingstars.factories.Materials;
|
||||
import org.wyrez.shootingstars.game.GameSettings;
|
||||
import org.wyrez.shootingstars.states.GameState;
|
||||
import org.wyrez.shootingstars.states.State;
|
||||
import org.wyrez.shootingstars.states.StateManager;
|
||||
import org.wyrez.shootingstars.states.util.OptionSettings;
|
||||
@ -48,9 +48,9 @@ public class ShootingStars extends SimpleApplication {
|
||||
private StateManager sManager;
|
||||
private OptionSettings optionSettings;
|
||||
|
||||
public ShootingStars() {
|
||||
public ShootingStars(OptionSettings optionSettings) {
|
||||
super(new StatsAppState(), new DebugKeysAppState());
|
||||
optionSettings = new OptionSettings();
|
||||
this.optionSettings = optionSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -59,13 +59,9 @@ public class ShootingStars extends SimpleApplication {
|
||||
registerTypes();
|
||||
initGUI();
|
||||
initKeys();
|
||||
initSettings();
|
||||
|
||||
Materials.initialize(assetManager);
|
||||
cam.setFrustumFar(2500f);
|
||||
//TODO debug
|
||||
//cam.setLocation(new Vector3f(0f, 2500f, 0f));
|
||||
|
||||
sManager = container.resolve(StateManager.class);
|
||||
sManager.setState(State.START);
|
||||
}
|
||||
@ -89,30 +85,21 @@ public class ShootingStars extends SimpleApplication {
|
||||
container.registerType(StateManager.class, true);
|
||||
container.registerType(Screen.class, true);
|
||||
container.registerType(AudioDataManager.class, true);
|
||||
container.registerType(YTDownloader.class);
|
||||
}
|
||||
|
||||
private void initKeys() {
|
||||
inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
|
||||
inputManager.deleteMapping(INPUT_MAPPING_EXIT);
|
||||
inputManager.addMapping("ESC", new KeyTrigger(KeyInput.KEY_ESCAPE));
|
||||
|
||||
}
|
||||
|
||||
private void initSettings() {
|
||||
if (optionSettings.saveExists()) {
|
||||
optionSettings.init(false);
|
||||
optionSettings.load();
|
||||
} else {
|
||||
optionSettings.init(true);
|
||||
optionSettings.save();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ShootingStars app = new ShootingStars();
|
||||
|
||||
//app.setSettings(optionSettings);
|
||||
|
||||
OptionSettings optionSettings = new OptionSettings();
|
||||
optionSettings.init();
|
||||
ShootingStars app = new ShootingStars(optionSettings);
|
||||
app.setShowSettings(false);
|
||||
app.setSettings(optionSettings.getAppSettings());
|
||||
app.start();
|
||||
}
|
||||
}
|
||||
|
||||
@ -171,28 +171,28 @@ public class AudioDataManager {
|
||||
return bpm;
|
||||
}
|
||||
|
||||
private int calcIndex() {
|
||||
private int calcIndex(int length) {
|
||||
int index = (int) (currentTime * indexFactor);
|
||||
if (index >= speedData.length) {
|
||||
index = speedData.length - 1;
|
||||
if (index >= length) {
|
||||
index = length - 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public boolean isFlash() {
|
||||
return flashData[calcIndex()] > peakAverageFlash * 0.5f;
|
||||
return flashData[calcIndex(flashData.length)] > peakAverageFlash * 0.5f;
|
||||
}
|
||||
|
||||
public float getFlashData() {
|
||||
return flashData[calcIndex()];
|
||||
return flashData[calcIndex(flashData.length)];
|
||||
}
|
||||
|
||||
public boolean isGroundMove() {
|
||||
return groundMoveData[calcIndex()] > 0f;
|
||||
return groundMoveData[calcIndex(groundMoveData.length)] > 0f;
|
||||
}
|
||||
|
||||
public float getGroundMoveData() {
|
||||
return groundMoveData[calcIndex()];
|
||||
return groundMoveData[calcIndex(flashData.length)];
|
||||
}
|
||||
|
||||
public float getMovementData() {
|
||||
@ -202,7 +202,7 @@ public class AudioDataManager {
|
||||
}
|
||||
|
||||
public float getSpeedData() {
|
||||
return speedData[calcIndex()];
|
||||
return speedData[calcIndex(flashData.length)];
|
||||
}
|
||||
|
||||
public float getMidData() {
|
||||
|
||||
@ -25,6 +25,7 @@ import java.io.File;
|
||||
public class PathHelper {
|
||||
|
||||
private static final String CACHE_FOLDER = "cache/";
|
||||
public static final String CONFIG = "shootingstars.conf";
|
||||
|
||||
public static String getCacheFolder() {
|
||||
File file = new File(CACHE_FOLDER);
|
||||
|
||||
@ -89,10 +89,6 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public void setSettings(GameSettings settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public void loadGround() {
|
||||
ground = new Ground(audioDataManager);
|
||||
FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
|
||||
@ -122,17 +118,20 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
|
||||
player.addControl(new PlayerCamControl(camera));
|
||||
}
|
||||
|
||||
private void setRunning(boolean running) {
|
||||
isRunning = running;
|
||||
player.setUserData(UserDataKeys.RUNNING, running);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
gui.setStart();
|
||||
inputManager.setCursorVisible(false);
|
||||
player.setUserData(UserDataKeys.RUNNING, true);
|
||||
mediaPlayer.play();
|
||||
isRunning = true;
|
||||
setRunning(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateAttached(AppStateManager stateManager) {
|
||||
// stateManager.attach(new FlyCamAppState()); //TODO debug
|
||||
rootNode.attachChild(cinema);
|
||||
rootNode.attachChild(ground);
|
||||
rootNode.attachChild(player);
|
||||
@ -142,7 +141,6 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
|
||||
|
||||
@Override
|
||||
public void stateDetached(AppStateManager stateManager) {
|
||||
// stateManager.detach(new FlyCamAppState()); //TODO debug
|
||||
gui.detach();
|
||||
rootNode.detachChild(player);
|
||||
rootNode.detachChild(ground);
|
||||
@ -167,14 +165,11 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
|
||||
@Override
|
||||
public void cleanup() {
|
||||
super.cleanup();
|
||||
setRunning(false);
|
||||
mediaPlayer.release();
|
||||
mediaPlayerFactory.release();
|
||||
}
|
||||
|
||||
public GameSettings getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
public DirectMediaPlayer getMediaPlayer() {
|
||||
return mediaPlayer;
|
||||
}
|
||||
@ -185,18 +180,21 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
|
||||
|
||||
public void onAction(String name, boolean isPressed, float tpf) {
|
||||
if (name.equals("ESC") && !isPressed) {
|
||||
setRunning(false);
|
||||
mediaPlayer.pause();
|
||||
inputManager.setCursorVisible(true);
|
||||
gui.showMenu();
|
||||
//cinema.pause();
|
||||
}
|
||||
}
|
||||
|
||||
public void resume() {
|
||||
gui.resumeGame();
|
||||
//cinema.play();
|
||||
inputManager.setCursorVisible(false);
|
||||
mediaPlayer.play();
|
||||
setRunning(true);
|
||||
}
|
||||
|
||||
public void cancelTrack() {
|
||||
//cinema.stop();
|
||||
stateManager.setState(State.MENU);
|
||||
}
|
||||
}
|
||||
|
||||
@ -118,7 +118,7 @@ public class LoadingState extends AbstractAppState {
|
||||
|
||||
private boolean initAudioAnalysis() {
|
||||
GameState gs = stateManager.getState(State.GAME);
|
||||
return audioDataManager.initialize(gs.getSettings(), gs.getMediaPlayer());
|
||||
return audioDataManager.initialize(settings, gs.getMediaPlayer());
|
||||
}
|
||||
|
||||
private void convertAudio() throws Exception {
|
||||
|
||||
@ -18,6 +18,9 @@ package org.wyrez.shootingstars.states;
|
||||
|
||||
import com.jme3.app.state.AbstractAppState;
|
||||
import com.jme3.app.state.AppStateManager;
|
||||
import java.io.File;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.wyrez.shootingstars.data.YTDownloader;
|
||||
import org.wyrez.shootingstars.game.GameSettings;
|
||||
import org.wyrez.shootingstars.gui.YTDownloadGUI;
|
||||
@ -31,6 +34,8 @@ import tonegod.gui.core.Screen;
|
||||
*/
|
||||
public class YTDownloadState extends AbstractAppState implements YTDownloadListener {
|
||||
|
||||
private static final Pattern YT_LINK_PATTERN = Pattern.compile("\\?v=[^\\&]*\\&??");
|
||||
/**/
|
||||
private StateManager stateManager;
|
||||
private GameSettings settings;
|
||||
private YTDownloadGUI gui;
|
||||
@ -55,8 +60,38 @@ public class YTDownloadState extends AbstractAppState implements YTDownloadListe
|
||||
}
|
||||
|
||||
public void downloadYTVideo(String url) {
|
||||
downloader.download(url, PathHelper.getCacheFolder());
|
||||
settings.setVideoFile(downloader.getLastTarget().getPath());
|
||||
stateManager.setState(State.FILEMETAINFO);
|
||||
try {
|
||||
Matcher matcher = YT_LINK_PATTERN.matcher(url);
|
||||
String identifier = "";
|
||||
if (matcher.find()) {
|
||||
identifier = matcher.group().replace("?v=", "");
|
||||
} else {
|
||||
//TODO add error message
|
||||
stateManager.setState(State.MENU);
|
||||
}
|
||||
String path;
|
||||
if ((path = getVideoCache(identifier)) == null) {
|
||||
downloader.download(url, PathHelper.getCacheFolder());
|
||||
path = downloader.getLastTarget().getPath();
|
||||
new File(path).renameTo(new File(PathHelper.getCacheFolder()
|
||||
+ identifier + path.substring(path.lastIndexOf("."), path.length())));
|
||||
System.out.println("rename " + path + " --> " + (PathHelper.getCacheFolder()
|
||||
+ identifier + path.substring(path.lastIndexOf("."), path.length())));
|
||||
}
|
||||
settings.setVideoFile(path);
|
||||
stateManager.setState(State.FILEMETAINFO);
|
||||
} catch (Exception ex) {
|
||||
stateManager.setState(State.MENU);
|
||||
}
|
||||
}
|
||||
|
||||
private String getVideoCache(String identifier) {
|
||||
File[] cachedFiles = new File(PathHelper.getCacheFolder()).listFiles();
|
||||
for (File file : cachedFiles) {
|
||||
if (file.getName().substring(0, file.getName().lastIndexOf(".")).equals(identifier)) {
|
||||
return file.getPath();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,14 +23,25 @@ import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Set;
|
||||
import org.wyrez.shootingstars.helper.PathHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Snowsun
|
||||
*/
|
||||
public class OptionSettings {
|
||||
|
||||
private AppSettings settings;
|
||||
private static final String FILENAME = "shootingstars.conf";
|
||||
|
||||
public void init() {
|
||||
if (saveExists()) {
|
||||
init(false);
|
||||
load();
|
||||
} else {
|
||||
init(true);
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
public void init(boolean loadDefault) {
|
||||
settings = new AppSettings(loadDefault);
|
||||
@ -42,7 +53,7 @@ public class OptionSettings {
|
||||
settings.putAll(settings);
|
||||
}
|
||||
|
||||
private void create(boolean loadDefault) {
|
||||
private void create(boolean loadDefault) {
|
||||
if (loadDefault) {
|
||||
settings.setResolution(1024, 600);
|
||||
settings.setTitle("Shooting Stars");
|
||||
@ -174,7 +185,7 @@ public class OptionSettings {
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
OutputStream os = new FileOutputStream(FILENAME);
|
||||
OutputStream os = new FileOutputStream(PathHelper.CONFIG);
|
||||
settings.save(os);
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Can't save settings");
|
||||
@ -183,7 +194,7 @@ public class OptionSettings {
|
||||
|
||||
public void load() {
|
||||
try {
|
||||
InputStream is = new FileInputStream(FILENAME);
|
||||
InputStream is = new FileInputStream(PathHelper.CONFIG);
|
||||
settings.load(is);
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Can't load settings");
|
||||
@ -191,10 +202,10 @@ public class OptionSettings {
|
||||
}
|
||||
|
||||
public boolean saveExists() {
|
||||
return new File(FILENAME).exists();
|
||||
return new File(PathHelper.CONFIG).exists();
|
||||
}
|
||||
|
||||
public void put(String key, Object value){
|
||||
public void put(String key, Object value) {
|
||||
settings.put(key, value);
|
||||
}
|
||||
|
||||
@ -206,7 +217,7 @@ public class OptionSettings {
|
||||
return settings.get(key);
|
||||
}
|
||||
|
||||
public AppSettings getAppSettings(){
|
||||
public AppSettings getAppSettings() {
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user