git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8284 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
a5be89598f
commit
7c77fc404b
@ -1,47 +0,0 @@ |
|||||||
package jme3test.network.sync; |
|
||||||
|
|
||||||
import com.jme3.asset.AssetManager; |
|
||||||
import com.jme3.material.Material; |
|
||||||
import com.jme3.math.ColorRGBA; |
|
||||||
import com.jme3.math.Vector3f; |
|
||||||
import com.jme3.network.sync.Sync; |
|
||||||
import com.jme3.network.sync.SyncEntity; |
|
||||||
import com.jme3.scene.Geometry; |
|
||||||
import com.jme3.scene.shape.Box; |
|
||||||
|
|
||||||
public class BoxEntity extends Geometry implements SyncEntity { |
|
||||||
|
|
||||||
protected @Sync Vector3f pos; |
|
||||||
protected @Sync Vector3f vel; |
|
||||||
|
|
||||||
public BoxEntity(AssetManager assetManager, ColorRGBA color){ |
|
||||||
super("Box", new Box(1,1,1)); |
|
||||||
setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md")); |
|
||||||
getMaterial().setColor("Color", color); |
|
||||||
} |
|
||||||
|
|
||||||
public void setPosVel(Vector3f pos, Vector3f vel){ |
|
||||||
setLocalTranslation(pos); |
|
||||||
this.pos = pos; |
|
||||||
this.vel = vel; |
|
||||||
} |
|
||||||
|
|
||||||
public void onRemoteCreate() { |
|
||||||
} |
|
||||||
|
|
||||||
public void onRemoteUpdate(float latencyDelta) { |
|
||||||
} |
|
||||||
|
|
||||||
public void onRemoteDelete() { |
|
||||||
removeFromParent(); |
|
||||||
} |
|
||||||
|
|
||||||
public void onLocalUpdate() { |
|
||||||
} |
|
||||||
|
|
||||||
public void interpolate(float blendAmount) { |
|
||||||
} |
|
||||||
|
|
||||||
public void extrapolate(float tpf) { |
|
||||||
} |
|
||||||
} |
|
@ -1,46 +0,0 @@ |
|||||||
package jme3test.network.sync; |
|
||||||
|
|
||||||
import com.jme3.asset.AssetManager; |
|
||||||
import com.jme3.math.ColorRGBA; |
|
||||||
import com.jme3.math.Vector3f; |
|
||||||
|
|
||||||
public class ClientBoxEntity extends BoxEntity { |
|
||||||
|
|
||||||
private Vector3f prevPos = new Vector3f(); |
|
||||||
|
|
||||||
public ClientBoxEntity(AssetManager assetManager, ColorRGBA color){ |
|
||||||
super(assetManager, color); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void onRemoteCreate() { |
|
||||||
System.out.println("ClientBoxEntity created"); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void onRemoteDelete() { |
|
||||||
System.out.println("ClientBoxEntity deleted"); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void onRemoteUpdate(float latencyDelta) { |
|
||||||
prevPos.set(getLocalTranslation()); |
|
||||||
pos.addLocal(vel.mult(latencyDelta)); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void interpolate(float blendAmount) { |
|
||||||
if (pos != null){ |
|
||||||
getLocalTranslation().interpolate(prevPos, pos, blendAmount); |
|
||||||
setLocalTranslation(getLocalTranslation()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void extrapolate(float tpf) { |
|
||||||
if (pos != null){ |
|
||||||
pos.addLocal(vel.mult(tpf)); |
|
||||||
setLocalTranslation(pos); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,113 +0,0 @@ |
|||||||
package jme3test.network.sync; |
|
||||||
|
|
||||||
import com.jme3.app.SimpleApplication; |
|
||||||
import com.jme3.effect.shapes.EmitterSphereShape; |
|
||||||
import com.jme3.math.ColorRGBA; |
|
||||||
import com.jme3.math.Vector3f; |
|
||||||
import com.jme3.network.connection.Client; |
|
||||||
import com.jme3.network.connection.Server; |
|
||||||
import com.jme3.network.serializing.Serializer; |
|
||||||
import com.jme3.network.sync.ClientSyncService; |
|
||||||
import com.jme3.network.sync.EntityFactory; |
|
||||||
import com.jme3.network.sync.ServerSyncService; |
|
||||||
import com.jme3.network.sync.SyncEntity; |
|
||||||
import com.jme3.network.sync.SyncMessage; |
|
||||||
import java.io.IOException; |
|
||||||
|
|
||||||
public class TestSync extends SimpleApplication implements EntityFactory { |
|
||||||
|
|
||||||
// Client Variables
|
|
||||||
private Client client; |
|
||||||
private ClientSyncService clientSyncServ; |
|
||||||
|
|
||||||
// Server Variables
|
|
||||||
private Server server; |
|
||||||
private ServerSyncService serverSyncServ; |
|
||||||
private BoxEntity serverBox; |
|
||||||
|
|
||||||
private Vector3f targetPos = new Vector3f(); |
|
||||||
private float boxSpeed = 3f; |
|
||||||
private EmitterSphereShape randomPosSphere = new EmitterSphereShape(Vector3f.ZERO, 5); |
|
||||||
|
|
||||||
public static void main(String[] args){ |
|
||||||
TestSync app = new TestSync(); |
|
||||||
app.start(); |
|
||||||
} |
|
||||||
|
|
||||||
public void simpleInitApp(){ |
|
||||||
Serializer.registerClass(SyncMessage.class); |
|
||||||
|
|
||||||
// ----- Start Server -------
|
|
||||||
try { |
|
||||||
server = new Server(5110, 5110); |
|
||||||
server.start(); |
|
||||||
} catch (IOException ex) { |
|
||||||
ex.printStackTrace(); |
|
||||||
} |
|
||||||
|
|
||||||
// Create SyncService for server
|
|
||||||
serverSyncServ = server.getService(ServerSyncService.class); |
|
||||||
// Create server box entity (red)
|
|
||||||
serverBox = new BoxEntity(assetManager, ColorRGBA.Red); |
|
||||||
serverSyncServ.addNpc(serverBox); |
|
||||||
rootNode.attachChild(serverBox); |
|
||||||
|
|
||||||
// Enable 10% packet drop rate and 200 ms latency
|
|
||||||
serverSyncServ.setNetworkSimulationParams(0.1f, 200); |
|
||||||
|
|
||||||
|
|
||||||
// ------ Start Client -------
|
|
||||||
try { |
|
||||||
client = new Client("localhost", 5110, 5110); |
|
||||||
client.start(); |
|
||||||
} catch (IOException ex) { |
|
||||||
ex.printStackTrace(); |
|
||||||
} |
|
||||||
|
|
||||||
clientSyncServ = client.getService(ClientSyncService.class); |
|
||||||
clientSyncServ.setEntityFactory(this); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create a new entity (for client) |
|
||||||
* @param entityType |
|
||||||
* @return |
|
||||||
*/ |
|
||||||
public SyncEntity createEntity(Class<? extends SyncEntity> entityType) { |
|
||||||
BoxEntity clientBox = new ClientBoxEntity(assetManager, ColorRGBA.Green); |
|
||||||
rootNode.attachChild(clientBox); |
|
||||||
return clientBox; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void simpleUpdate(float tpf){ |
|
||||||
// --------- Update Client Sync ---------
|
|
||||||
clientSyncServ.update(tpf); |
|
||||||
|
|
||||||
// --------- Update Server Sync ---------
|
|
||||||
// if needed determine next box position
|
|
||||||
if (serverBox.getLocalTranslation().distance(targetPos) < 0.1f){ |
|
||||||
randomPosSphere.getRandomPoint(targetPos); |
|
||||||
}else{ |
|
||||||
Vector3f velocity = new Vector3f(targetPos); |
|
||||||
velocity.subtractLocal(serverBox.getLocalTranslation()); |
|
||||||
velocity.normalizeLocal().multLocal(boxSpeed); |
|
||||||
|
|
||||||
Vector3f newPos = serverBox.getLocalTranslation().clone(); |
|
||||||
newPos.addLocal(velocity.mult(tpf)); |
|
||||||
serverBox.setPosVel(newPos, velocity); |
|
||||||
} |
|
||||||
|
|
||||||
serverSyncServ.update(tpf); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void destroy(){ |
|
||||||
super.destroy(); |
|
||||||
try { |
|
||||||
client.disconnect(); |
|
||||||
} catch (IOException ex) { |
|
||||||
ex.printStackTrace(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue