Skip to content

Commit 78a2d32

Browse files
committed
Added game session events and hooked the spinning SiO2 model to
enable/disable as the game session starts/ends.
1 parent 77d263f commit 78a2d32

File tree

5 files changed

+174
-15
lines changed

5 files changed

+174
-15
lines changed

sim-eth-basic/src/main/java/example/ConnectionState.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
import org.slf4j.*;
4343

44+
import com.google.common.base.Strings;
45+
4446
import com.jme3.app.Application;
4547
import com.jme3.app.state.AppState;
4648
import com.jme3.app.state.BaseAppState;
@@ -95,14 +97,26 @@ public void disconnect() {
9597
public boolean join( String userName ) {
9698
log.info("join(" + userName + ")");
9799

98-
userName = userName.trim();
99-
if( userName.length() == 0 ) {
100+
if( userName != null ) {
101+
userName = userName.trim();
102+
}
103+
104+
if( Strings.isNullOrEmpty(userName) ) {
100105
showError("Join Error", "Please specify a player name for use in game.", null, false);
101106
return false;
102107
}
103108

109+
// So here we'd login and then when we get a response from the
110+
// server that we are logged in then we'd launch the game state and
111+
// so on... for now we'll just do it directly.
112+
onLoggedOn();
113+
104114
return true;
105115
}
116+
117+
protected void onLoggedOn() {
118+
getStateManager().attach(new GameSessionState());
119+
}
106120

107121
@Override
108122
protected void initialize( Application app ) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* $Id$
3+
*
4+
* Copyright (c) 2016, Simsilica, LLC
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
*
19+
* 3. Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived
21+
* from this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34+
* OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*/
36+
37+
package example;
38+
39+
import com.simsilica.event.*;
40+
41+
/**
42+
* Events that are sent to the event bus for different client side player-related
43+
* events.
44+
*
45+
* @author Paul Speed
46+
*/
47+
public class GameSessionEvent {
48+
49+
/**
50+
* Singals that the local player has joined the game.
51+
*/
52+
public static EventType<GameSessionEvent> sessionStarted = EventType.create("SessionStarted", GameSessionEvent.class);
53+
54+
/**
55+
* Singals that the local player has left the game.
56+
*/
57+
public static EventType<GameSessionEvent> sessionEnded = EventType.create("SessionEnded", GameSessionEvent.class);
58+
59+
/**
60+
* Creates a game session event.
61+
*/
62+
public GameSessionEvent() {
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return getClass().getSimpleName() + "[]";
68+
}
69+
}
70+
71+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* $Id$
3+
*
4+
* Copyright (c) 2016, Simsilica, LLC
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
*
19+
* 3. Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived
21+
* from this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34+
* OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*/
36+
37+
package example;
38+
39+
import com.jme3.app.Application;
40+
41+
import com.simsilica.event.EventBus;
42+
import com.simsilica.state.CompositeAppState;
43+
44+
/**
45+
* The core state that manages the game session. This has several
46+
* child app states whose lifecycles are directly linked to this one.
47+
*
48+
* @author Paul Speed
49+
*/
50+
public class GameSessionState extends CompositeAppState {
51+
52+
public GameSessionState() {
53+
}
54+
55+
@Override
56+
protected void initialize( Application app ) {
57+
super.initialize(app);
58+
59+
EventBus.publish(GameSessionEvent.sessionStarted, new GameSessionEvent());
60+
61+
}
62+
63+
@Override
64+
protected void cleanup( Application app ) {
65+
super.cleanup(app);
66+
67+
EventBus.publish(GameSessionEvent.sessionEnded, new GameSessionEvent());
68+
}
69+
}

sim-eth-basic/src/main/java/example/LoginState.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ protected void join() {
6666
if( getState(ConnectionState.class).join(nameField.getText()) ) {
6767
getStateManager().detach(this);
6868
}
69-
}
70-
69+
}
70+
7171
protected void cancel() {
7272
getState(ConnectionState.class).disconnect();
7373
getStateManager().detach(this);

sim-eth-basic/src/main/java/example/Main.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.jme3.scene.shape.Box;
4747
import com.jme3.texture.Texture;
4848

49+
import com.simsilica.event.EventBus;
4950
import com.simsilica.lemur.GuiGlobals;
5051
import com.simsilica.lemur.OptionPanelState;
5152
import com.simsilica.lemur.anim.AnimationState;
@@ -94,19 +95,23 @@ public void simpleInitApp() {
9495
GuiGlobals globals = GuiGlobals.getInstance();
9596
BaseStyles.loadGlassStyle();
9697
globals.getStyles().setDefaultStyle("glass");
97-
98-
// Just load a sample object for the background for now
99-
/*logo = new Node("LogoHolder");
100-
Spatial molecule = assetManager.loadModel("Models/simsilica.j3o");
101-
molecule.center();
102-
logo.attachChild(molecule);
103-
logo.setLocalScale(0.5f);
104-
rootNode.attachChild(logo);
105-
98+
99+
// Since we've added the background spinning widget here, we'll
100+
// also register events to enable/disable it.
101+
EventBus.addListener(new GameListener(),
102+
GameSessionEvent.sessionStarted,
103+
GameSessionEvent.sessionEnded);
104+
106105
}
107106

108-
public void simpleUpdate( float tpf ) {
109-
logo.rotate(0, tpf, 0);*/
107+
private class GameListener {
108+
public void sessionStarted( GameSessionEvent event ) {
109+
stateManager.getState(SiliconDioxideState.class).setEnabled(false);
110+
}
111+
112+
public void sessionEnded( GameSessionEvent event ) {
113+
stateManager.getState(SiliconDioxideState.class).setEnabled(true);
114+
}
110115
}
111116
}
112117

0 commit comments

Comments
 (0)