Added network synchronization for position, velocity and angle

This commit is contained in:
Collin Smith 2019-12-15 01:25:26 -08:00
parent 15687b71c6
commit 8c38af053a
5 changed files with 114 additions and 6 deletions

View File

@ -8,8 +8,11 @@ public final class SyncData {
public static final byte CofComponents = 1;
public static final byte CofTransforms = 2;
public static final byte CofAlphas = 3;
public static final byte Position = 4;
public static final byte Velocity = 5;
public static final byte Angle = 6;
public static final String[] names = { "NONE", "CofComponents", "CofTransforms", "CofAlphas", };
public static final String[] names = { "NONE", "CofComponents", "CofTransforms", "CofAlphas", "Position", "Velocity", "Angle", };
public static String name(int e) { return names[e]; }
}

View File

@ -8,12 +8,16 @@ import com.artemis.annotations.All;
import com.artemis.annotations.Wire;
import com.artemis.systems.IntervalSystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.net.Socket;
import com.riiablo.Riiablo;
import com.riiablo.engine.server.component.Angle;
import com.riiablo.engine.server.component.CofAlphas;
import com.riiablo.engine.server.component.CofComponents;
import com.riiablo.engine.server.component.CofTransforms;
import com.riiablo.engine.server.component.Networked;
import com.riiablo.engine.server.component.Position;
import com.riiablo.engine.server.component.Velocity;
import com.riiablo.net.packet.d2gs.Connection;
import com.riiablo.net.packet.d2gs.D2GS;
import com.riiablo.net.packet.d2gs.D2GSData;
@ -34,6 +38,9 @@ public class ClientNetworkSyncronizer extends IntervalSystem {
protected ComponentMapper<CofComponents> mCofComponents;
protected ComponentMapper<CofTransforms> mCofTransforms;
protected ComponentMapper<CofAlphas> mCofAlphas;
protected ComponentMapper<Position> mPosition;
protected ComponentMapper<Velocity> mVelocity;
protected ComponentMapper<Angle> mAngle;
protected NetworkIdManager idManager;
@ -117,21 +124,30 @@ public class ClientNetworkSyncronizer extends IntervalSystem {
byte[] transform = mCofTransforms.get(entityId).transform;
float[] alpha = mCofAlphas.get(entityId).alpha;
Vector2 position = mPosition.get(entityId).position;
Vector2 velocity = mVelocity.get(entityId).velocity;
Vector2 angle = mAngle.get(entityId).target;
int cofComponents = com.riiablo.net.packet.d2gs.CofComponents.createComponentVector(builder, component);
int cofTransforms = com.riiablo.net.packet.d2gs.CofTransforms.createTransformVector(builder, transform);
int cofAlphas = com.riiablo.net.packet.d2gs.CofAlphas.createAlphaVector(builder, alpha);
byte[] dataTypes = new byte[3];
byte[] dataTypes = new byte[6];
dataTypes[0] = SyncData.CofComponents;
dataTypes[1] = SyncData.CofTransforms;
dataTypes[2] = SyncData.CofAlphas;
dataTypes[3] = SyncData.Position;
dataTypes[4] = SyncData.Velocity;
dataTypes[5] = SyncData.Angle;
int dataTypesOffset = Sync.createDataTypeVector(builder, dataTypes);
int[] data = new int[3];
int[] data = new int[6];
data[0] = com.riiablo.net.packet.d2gs.CofComponents.createCofComponents(builder, cofComponents);
data[1] = com.riiablo.net.packet.d2gs.CofTransforms.createCofTransforms(builder, cofTransforms);
data[2] = com.riiablo.net.packet.d2gs.CofAlphas.createCofAlphas(builder, cofAlphas);
data[3] = com.riiablo.net.packet.d2gs.Position.createPosition(builder, position.x, position.y);
data[4] = com.riiablo.net.packet.d2gs.Velocity.createVelocity(builder, velocity.x, velocity.y);
data[5] = com.riiablo.net.packet.d2gs.Angle.createAngle(builder, angle.x, angle.y);
int dataOffset = Sync.createDataVector(builder, data);
Sync.startSync(builder);

View File

@ -4,6 +4,9 @@ union SyncData {
CofComponents,
CofTransforms,
CofAlphas,
Position,
Velocity,
Angle,
}
table Sync {
@ -21,4 +24,19 @@ table CofTransforms {
table CofAlphas {
alpha:[float32];
}
table Position {
x:float32;
y:float32;
}
table Velocity {
x:float32;
y:float32;
}
table Angle {
x:float32;
y:float32;
}

View File

@ -6,6 +6,7 @@ import com.artemis.WorldConfigurationBuilder;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.net.Socket;
import com.badlogic.gdx.physics.box2d.Body;
import com.riiablo.CharData;
import com.riiablo.CharacterClass;
import com.riiablo.Riiablo;
@ -15,11 +16,15 @@ import com.riiablo.engine.client.ClientEntityFactory;
import com.riiablo.engine.client.ClientNetworkSyncronizer;
import com.riiablo.engine.client.NetworkIdManager;
import com.riiablo.engine.server.CofManager;
import com.riiablo.engine.server.component.Angle;
import com.riiablo.engine.server.component.Box2DBody;
import com.riiablo.engine.server.component.CofAlphas;
import com.riiablo.engine.server.component.CofComponents;
import com.riiablo.engine.server.component.CofTransforms;
import com.riiablo.engine.server.component.Networked;
import com.riiablo.engine.server.component.Player;
import com.riiablo.engine.server.component.Position;
import com.riiablo.engine.server.component.Velocity;
import com.riiablo.map.Map;
import com.riiablo.net.packet.d2gs.Connection;
import com.riiablo.net.packet.d2gs.D2GS;
@ -196,6 +201,32 @@ public class NetworkedGameScreen extends GameScreen {
}
break;
}
case SyncData.Position: {
Vector2 position = engine.getMapper(Position.class).get(entityId).position;
com.riiablo.net.packet.d2gs.Position data = (com.riiablo.net.packet.d2gs.Position) s.data(new com.riiablo.net.packet.d2gs.Position(), i);
position.x = data.x();
position.y = data.y();
Body body = engine.getMapper(Box2DBody.class).get(entityId).body;
body.setTransform(position, body.getAngle());
Gdx.app.log(TAG, " " + position);
break;
}
case SyncData.Velocity: {
Vector2 velocity = engine.getMapper(Velocity.class).get(entityId).velocity;
com.riiablo.net.packet.d2gs.Velocity data = (com.riiablo.net.packet.d2gs.Velocity) s.data(new com.riiablo.net.packet.d2gs.Velocity(), i);
velocity.x = data.x();
velocity.y = data.y();
Gdx.app.log(TAG, " " + velocity);
break;
}
case SyncData.Angle: {
Vector2 angle = engine.getMapper(Angle.class).get(entityId).target;
com.riiablo.net.packet.d2gs.Angle data = (com.riiablo.net.packet.d2gs.Angle) s.data(new com.riiablo.net.packet.d2gs.Angle(), i);
angle.x = data.x();
angle.y = data.y();
Gdx.app.log(TAG, " " + angle);
break;
}
default:
Gdx.app.error(TAG, "Unknown packet type: " + SyncData.name(s.dataType(i)));
}

View File

@ -7,12 +7,16 @@ import com.artemis.annotations.All;
import com.artemis.annotations.Wire;
import com.artemis.systems.IteratingSystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.IntIntMap;
import com.riiablo.engine.server.CofManager;
import com.riiablo.engine.server.component.Angle;
import com.riiablo.engine.server.component.CofAlphas;
import com.riiablo.engine.server.component.CofComponents;
import com.riiablo.engine.server.component.CofTransforms;
import com.riiablo.engine.server.component.Networked;
import com.riiablo.engine.server.component.Position;
import com.riiablo.engine.server.component.Velocity;
import com.riiablo.net.packet.d2gs.D2GSData;
import com.riiablo.net.packet.d2gs.Sync;
import com.riiablo.net.packet.d2gs.SyncData;
@ -29,6 +33,9 @@ public class NetworkSynchronizer extends IteratingSystem {
protected ComponentMapper<CofComponents> mCofComponents;
protected ComponentMapper<CofTransforms> mCofTransforms;
protected ComponentMapper<CofAlphas> mCofAlphas;
protected ComponentMapper<Position> mPosition;
protected ComponentMapper<Velocity> mVelocity;
protected ComponentMapper<Angle> mAngle;
protected CofManager cofs;
@ -53,7 +60,6 @@ public class NetworkSynchronizer extends IteratingSystem {
int[] component2 = mCofComponents.get(entityId).component;
byte[] component = new byte[16];
for (int i = 0; i < 16; i++) component[i] = (byte) component2[i];
Gdx.app.log(TAG, " " + DebugUtils.toByteArray(component));
int componentOffset = com.riiablo.net.packet.d2gs.CofComponents.createComponentVector(builder, component);
int cofComponents = com.riiablo.net.packet.d2gs.CofComponents.createCofComponents(builder, componentOffset);
@ -66,16 +72,26 @@ public class NetworkSynchronizer extends IteratingSystem {
int alphaOffset = com.riiablo.net.packet.d2gs.CofAlphas.createAlphaVector(builder, alpha);
int cofAlphas = com.riiablo.net.packet.d2gs.CofAlphas.createCofAlphas(builder, alphaOffset);
byte[] dataTypes = new byte[3];
Vector2 position = mPosition.get(entityId).position;
Vector2 velocity = mVelocity.get(entityId).velocity;
Vector2 angle = mAngle.get(entityId).target;
byte[] dataTypes = new byte[6];
dataTypes[0] = SyncData.CofComponents;
dataTypes[1] = SyncData.CofTransforms;
dataTypes[2] = SyncData.CofAlphas;
dataTypes[3] = SyncData.Position;
dataTypes[4] = SyncData.Velocity;
dataTypes[5] = SyncData.Angle;
int dataTypesOffset = Sync.createDataTypeVector(builder, dataTypes);
int[] data = new int[3];
int[] data = new int[6];
data[0] = cofComponents;
data[1] = cofTransforms;
data[2] = cofAlphas;
data[3] = com.riiablo.net.packet.d2gs.Position.createPosition(builder, position.x, position.y);
data[4] = com.riiablo.net.packet.d2gs.Velocity.createVelocity(builder, velocity.x, velocity.y);
data[5] = com.riiablo.net.packet.d2gs.Angle.createAngle(builder, angle.x, angle.y);
int dataOffset = Sync.createDataVector(builder, data);
Sync.startSync(builder);
@ -119,6 +135,30 @@ public class NetworkSynchronizer extends IteratingSystem {
Gdx.app.log(TAG, " " + Arrays.toString(alpha));
break;
}
case SyncData.Position: {
Vector2 position = mPosition.get(entityId).position;
com.riiablo.net.packet.d2gs.Position data = (com.riiablo.net.packet.d2gs.Position) sync.data(new com.riiablo.net.packet.d2gs.Position(), i);
position.x = data.x();
position.y = data.y();
Gdx.app.log(TAG, " " + position);
break;
}
case SyncData.Velocity: {
Vector2 velocity = mVelocity.get(entityId).velocity;
com.riiablo.net.packet.d2gs.Velocity data = (com.riiablo.net.packet.d2gs.Velocity) sync.data(new com.riiablo.net.packet.d2gs.Velocity(), i);
velocity.x = data.x();
velocity.y = data.y();
Gdx.app.log(TAG, " " + velocity);
break;
}
case SyncData.Angle: {
Vector2 angle = mAngle.get(entityId).target;
com.riiablo.net.packet.d2gs.Angle data = (com.riiablo.net.packet.d2gs.Angle) sync.data(new com.riiablo.net.packet.d2gs.Angle(), i);
angle.x = data.x();
angle.y = data.y();
Gdx.app.log(TAG, " " + angle);
break;
}
default:
Gdx.app.error(TAG, "Unknown packet type: " + SyncData.name(sync.dataType(i)));
}