Skip to content

Commit 484e911

Browse files
committed
Reworked the move() parameters a little to account for three
separate thrust values. Fixed to make Quaternion serializable. Updated the PlayerMovementState to send values to the server.
1 parent 0912249 commit 484e911

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

sim-eth-basic/src/main/java/example/net/GameSession.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
package example.net;
3838

3939
import com.jme3.math.Quaternion;
40+
import com.jme3.math.Vector3f;
4041
import com.jme3.network.service.rmi.Asynchronous;
4142

4243

@@ -54,5 +55,5 @@ public interface GameSession {
5455
* state is continuous, it doesn't need to be reliable.
5556
*/
5657
@Asynchronous(reliable=false)
57-
public void move( Quaternion rotation, float speed );
58+
public void move( Quaternion rotation, Vector3f thrust );
5859
}

sim-eth-basic/src/main/java/example/net/client/GameSessionClientService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.slf4j.*;
4343

4444
import com.jme3.math.Quaternion;
45+
import com.jme3.math.Vector3f;
4546
import com.jme3.network.service.AbstractClientService;
4647
import com.jme3.network.service.ClientServiceManager;
4748
import com.jme3.network.service.rmi.RmiClientService;
@@ -70,9 +71,9 @@ public GameSessionClientService() {
7071
}
7172

7273
@Override
73-
public void move( Quaternion dir, float speed ) {
74-
log.info("move(" + dir + ", " + speed + ")");
75-
delegate.move(dir, speed);
74+
public void move( Quaternion dir, Vector3f thrust ) {
75+
log.info("move(" + dir + ", " + thrust + ")");
76+
getDelegate().move(dir, thrust);
7677
}
7778

7879
private GameSession getDelegate() {

sim-eth-basic/src/main/java/example/net/server/GameSessionHostedService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@
4242
import java.util.concurrent.CopyOnWriteArrayList;
4343

4444
import com.jme3.math.Quaternion;
45+
import com.jme3.math.Vector3f;
4546
import com.jme3.network.HostedConnection;
4647
import com.jme3.network.MessageConnection;
4748
import com.jme3.network.Server;
49+
import com.jme3.network.serializing.Serializer;
50+
import com.jme3.network.serializing.serializers.FieldSerializer;
4851
import com.jme3.network.service.AbstractHostedConnectionService;
4952
import com.jme3.network.service.HostedServiceManager;
5053
import com.jme3.network.service.rmi.RmiHostedService;
@@ -78,6 +81,9 @@ public GameSessionHostedService() {
7881
// We do not autohost because we want to host only when the
7982
// player is actually logged on.
8083
setAutoHost(false);
84+
85+
// Make sure that quaternions are registered with the serializer
86+
Serializer.registerClass(Quaternion.class, new FieldSerializer());
8187
}
8288

8389

@@ -192,8 +198,8 @@ protected GameSessionListener getCallback() {
192198
}
193199

194200
@Override
195-
public void move( Quaternion rotation, float speed ) {
196-
log.info("move(" + rotation + ", " + speed + ")");
201+
public void move( Quaternion rotation, Vector3f thrust ) {
202+
log.info("move(" + rotation + ", " + thrust + ")");
197203

198204
// Need to forward this to the game world
199205
}

sim-eth-basic/src/main/java/example/view/PlayerMovementState.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
package example.view;
3838

39+
import org.slf4j.*;
40+
3941
import com.jme3.app.Application;
4042
import com.jme3.app.state.BaseAppState;
4143
import com.jme3.math.FastMath;
@@ -50,6 +52,10 @@
5052
import com.simsilica.lemur.input.InputState;
5153
import com.simsilica.lemur.input.StateFunctionListener;
5254

55+
import example.ConnectionState;
56+
import example.net.GameSession;
57+
import example.net.client.GameSessionClientService;
58+
5359
/**
5460
*
5561
*
@@ -58,6 +64,8 @@
5864
public class PlayerMovementState extends BaseAppState
5965
implements AnalogFunctionListener, StateFunctionListener {
6066

67+
static Logger log = LoggerFactory.getLogger(PlayerMovementState.class);
68+
6169
private InputMapper inputMapper;
6270
private Camera camera;
6371
private double turnSpeed = 2.5; // one half complete revolution in 2.5 seconds
@@ -70,6 +78,10 @@ public class PlayerMovementState extends BaseAppState
7078
private double side;
7179
private double elevation;
7280
private double speed = 3.0;
81+
82+
private Vector3f thrust = new Vector3f(); // not a direction, just 3 values
83+
84+
private GameSession session;
7385

7486
public PlayerMovementState() {
7587
}
@@ -125,6 +137,12 @@ protected void initialize( Application app ) {
125137
// of alternate ways this could have been done.
126138
inputMapper.addStateListener(this,
127139
PlayerMovementFunctions.F_BOOST);
140+
141+
// Grab the game session
142+
session = getState(ConnectionState.class).getService(GameSessionClientService.class);
143+
if( session == null ) {
144+
throw new RuntimeException("PlayerMovementState requires an active game session.");
145+
}
128146
}
129147

130148
@Override
@@ -159,15 +177,32 @@ protected void onDisable() {
159177
GuiGlobals.getInstance().setCursorEventsEnabled(true);
160178
}
161179

180+
private long nextSendTime = 0;
181+
private long sendFrequency = 1000000000L / 20; // 20 times a second, every 50 ms
182+
162183
@Override
163184
public void update( float tpf ) {
185+
186+
long time = System.nanoTime();
187+
if( time > nextSendTime ) {
188+
nextSendTime = time + sendFrequency;
189+
190+
Quaternion rot = camera.getRotation();
191+
192+
thrust.x = (float)(forward * speed);
193+
thrust.y = (float)(elevation * speed);
194+
thrust.z = (float)(side * speed);
195+
196+
session.move(rot, thrust);
197+
}
198+
164199

165200
// 'integrate' camera position based on the current move, strafe,
166201
// and elevation speeds.
167202
if( forward != 0 || side != 0 || elevation != 0 ) {
168-
Vector3f loc = camera.getLocation();
169-
203+
Vector3f loc = camera.getLocation();
170204
Quaternion rot = camera.getRotation();
205+
171206
Vector3f move = rot.mult(Vector3f.UNIT_Z).multLocal((float)(forward * speed * tpf));
172207
Vector3f strafe = rot.mult(Vector3f.UNIT_X).multLocal((float)(side * speed * tpf));
173208

0 commit comments

Comments
 (0)