From 33c425c36818727898cf526753870d1bea78ff01 Mon Sep 17 00:00:00 2001 From: "Raybz@Raybz" Date: Thu, 16 May 2013 13:24:32 +0200 Subject: [PATCH] added basic state structure --- .../wyrez/shootingstars/ShootingStars.java | 31 ++++++++++ .../wyrez/shootingstars/states/GameState.java | 26 +++++++++ .../shootingstars/states/LoadingState.java | 27 +++++++++ .../wyrez/shootingstars/states/MenuState.java | 42 ++++++++++++++ .../shootingstars/states/StartState.java | 45 +++++++++++++++ .../org/wyrez/shootingstars/states/State.java | 53 +++++++++++++++++ .../shootingstars/states/StateManager.java | 57 +++++++++++++++++++ 7 files changed, 281 insertions(+) create mode 100644 ShootingStars/src/org/wyrez/shootingstars/states/GameState.java create mode 100644 ShootingStars/src/org/wyrez/shootingstars/states/LoadingState.java create mode 100644 ShootingStars/src/org/wyrez/shootingstars/states/MenuState.java create mode 100644 ShootingStars/src/org/wyrez/shootingstars/states/StartState.java create mode 100644 ShootingStars/src/org/wyrez/shootingstars/states/State.java create mode 100644 ShootingStars/src/org/wyrez/shootingstars/states/StateManager.java diff --git a/ShootingStars/src/org/wyrez/shootingstars/ShootingStars.java b/ShootingStars/src/org/wyrez/shootingstars/ShootingStars.java index 79697ad..1ba6ae5 100644 --- a/ShootingStars/src/org/wyrez/shootingstars/ShootingStars.java +++ b/ShootingStars/src/org/wyrez/shootingstars/ShootingStars.java @@ -16,7 +16,15 @@ */ package org.wyrez.shootingstars; +import com.jme3.app.DebugKeysAppState; import com.jme3.app.SimpleApplication; +import com.jme3.app.StatsAppState; +import com.jme3.app.state.AppStateManager; +import com.jme3.renderer.Camera; +import com.jme3.scene.Node; +import org.wyrez.persij.PersiJContainer; +import org.wyrez.shootingstars.states.State; +import org.wyrez.shootingstars.states.StateManager; /** * @@ -24,14 +32,37 @@ import com.jme3.app.SimpleApplication; */ public class ShootingStars extends SimpleApplication { + private PersiJContainer container; + private StateManager sManager; + + public ShootingStars() { + super(new StatsAppState(), new DebugKeysAppState()); + } + @Override public void simpleInitApp() { + registerTypes(); + + sManager = container.resolve(StateManager.class); + sManager.setState(State.START); } @Override public void simpleUpdate(float tpf) { } + public StateManager getSManager() { + return sManager; + } + + private void registerTypes() { + container = new PersiJContainer(); + container.registerSingleton(Node.class, rootNode); + container.registerSingleton(Camera.class, cam); + container.registerSingleton(AppStateManager.class, stateManager); + container.registerType(StateManager.class, true); + } + public static void main(String[] args) { ShootingStars app = new ShootingStars(); app.start(); diff --git a/ShootingStars/src/org/wyrez/shootingstars/states/GameState.java b/ShootingStars/src/org/wyrez/shootingstars/states/GameState.java new file mode 100644 index 0000000..ce2086f --- /dev/null +++ b/ShootingStars/src/org/wyrez/shootingstars/states/GameState.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2013 Darth Affe and contributors + * + * 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 . + */ +package org.wyrez.shootingstars.states; + +import com.jme3.app.state.AbstractAppState; + +/** + * + * @author Darth Affe + */ +public class GameState extends AbstractAppState { +} diff --git a/ShootingStars/src/org/wyrez/shootingstars/states/LoadingState.java b/ShootingStars/src/org/wyrez/shootingstars/states/LoadingState.java new file mode 100644 index 0000000..5ef5ada --- /dev/null +++ b/ShootingStars/src/org/wyrez/shootingstars/states/LoadingState.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 Darth Affe and contributors + * + * 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 . + */ +package org.wyrez.shootingstars.states; + +import com.jme3.app.state.AbstractAppState; + +/** + * + * @author Darth Affe + */ +public class LoadingState extends AbstractAppState{ + +} diff --git a/ShootingStars/src/org/wyrez/shootingstars/states/MenuState.java b/ShootingStars/src/org/wyrez/shootingstars/states/MenuState.java new file mode 100644 index 0000000..3be9d4a --- /dev/null +++ b/ShootingStars/src/org/wyrez/shootingstars/states/MenuState.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2013 Darth Affe and contributors + * + * 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 . + */ +package org.wyrez.shootingstars.states; + +import com.jme3.app.state.AbstractAppState; +import com.jme3.app.state.AppStateManager; + +/** + * + * @author Darth Affe + */ +public class MenuState extends AbstractAppState { + + public MenuState() { + } + + @Override + public void stateAttached(AppStateManager stateManager) { + } + + @Override + public void stateDetached(AppStateManager stateManager) { + } + + @Override + public void update(float tpf) { + } +} diff --git a/ShootingStars/src/org/wyrez/shootingstars/states/StartState.java b/ShootingStars/src/org/wyrez/shootingstars/states/StartState.java new file mode 100644 index 0000000..0a42d13 --- /dev/null +++ b/ShootingStars/src/org/wyrez/shootingstars/states/StartState.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2013 Darth Affe and contributors + * + * 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 . + */ +package org.wyrez.shootingstars.states; + +import com.jme3.app.state.AbstractAppState; +import com.jme3.app.state.AppStateManager; + +/** + * + * @author Darth Affe + */ +public class StartState extends AbstractAppState { + + private StateManager stateManager; + + public StartState(StateManager stateManager) { + this.stateManager = stateManager; + } + + @Override + public void stateAttached(AppStateManager stateManager) { + } + + @Override + public void stateDetached(AppStateManager stateManager) { + } + + @Override + public void update(float tpf) { + } +} diff --git a/ShootingStars/src/org/wyrez/shootingstars/states/State.java b/ShootingStars/src/org/wyrez/shootingstars/states/State.java new file mode 100644 index 0000000..48059d6 --- /dev/null +++ b/ShootingStars/src/org/wyrez/shootingstars/states/State.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2013 Darth Affe and contributors + * + * 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 . + */ +package org.wyrez.shootingstars.states; + +import com.jme3.app.state.AppState; + +/** + * + * @author Darth Affe + */ +public enum State { + + NONE(null), START(StartState.class), MENU(MenuState.class), + LOADING(LoadingState.class), GAME(GameState.class); + private static State currentState = State.NONE; + private static State previousState = State.NONE; + private final Class clazz; + + private State(Class clazz) { + this.clazz = clazz; + } + + public Class getClazz() { + return clazz; + } + + static void setState(State currentGameState) { + previousState = State.currentState; + State.currentState = currentGameState; + } + + public static State getState() { + return currentState; + } + + public static State getPreviousState() { + return previousState; + } +} diff --git a/ShootingStars/src/org/wyrez/shootingstars/states/StateManager.java b/ShootingStars/src/org/wyrez/shootingstars/states/StateManager.java new file mode 100644 index 0000000..f63299d --- /dev/null +++ b/ShootingStars/src/org/wyrez/shootingstars/states/StateManager.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013 Darth Affe and contributors + * + * 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 . + */ +package org.wyrez.shootingstars.states; + +import com.jme3.app.state.AppState; +import com.jme3.app.state.AppStateManager; +import org.wyrez.persij.PersiJContainer; + +/** + * + * @author Darth Affe + */ +public class StateManager { + + private PersiJContainer container; + private AppStateManager appStateManager; + + public StateManager(PersiJContainer container, AppStateManager appStateManager) { + this.container = container; + this.appStateManager = appStateManager; + } + + public void setState(State state) { + State.setState(state); + appStateManager.detach(getState(State.getPreviousState())); + appStateManager.attach(getState(State.getState())); + } + + public T getTypedState(State state) { + return (T) getState(state); + } + + public AppState getState(State state) { + Class clazz = state.getClazz(); + if (clazz == null) { + return null; + } + if (!container.isTypeRegistered(clazz)) { + container.registerType(clazz, true); + } + return (AppState) container.resolve(clazz); + } +}