added basic state structure
This commit is contained in:
parent
c3f05530da
commit
33c425c368
@ -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();
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Darth Affe <http://wyrez.org> 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.wyrez.shootingstars.states;
|
||||
|
||||
import com.jme3.app.state.AbstractAppState;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Darth Affe
|
||||
*/
|
||||
public class GameState extends AbstractAppState {
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Darth Affe <http://wyrez.org> 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.wyrez.shootingstars.states;
|
||||
|
||||
import com.jme3.app.state.AbstractAppState;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Darth Affe
|
||||
*/
|
||||
public class LoadingState extends AbstractAppState{
|
||||
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Darth Affe <http://wyrez.org> 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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) {
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Darth Affe <http://wyrez.org> 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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) {
|
||||
}
|
||||
}
|
||||
53
ShootingStars/src/org/wyrez/shootingstars/states/State.java
Normal file
53
ShootingStars/src/org/wyrez/shootingstars/states/State.java
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Darth Affe <http://wyrez.org> 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<? extends AppState> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public Class<? extends AppState> 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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Darth Affe <http://wyrez.org> 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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 extends AppState> 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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user