Basic multiplayer placing/breaking/movement (broken)

This commit is contained in:
Anuken
2017-12-31 14:23:13 -05:00
parent eecd0f6d02
commit 36e357819b
29 changed files with 508 additions and 197 deletions

View File

@@ -1,18 +1,15 @@
package io.anuke.mindustry.net;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.ObjectMap;
import io.anuke.mindustry.net.Streamable.StreamBegin;
import io.anuke.mindustry.net.Streamable.StreamBuilder;
import io.anuke.mindustry.net.Streamable.StreamChunk;
import io.anuke.ucore.function.Consumer;
import java.io.IOException;
//TODO stub
public class Net{
private static boolean server;
@@ -22,6 +19,7 @@ public class Net{
private static ClientProvider clientProvider;
private static ServerProvider serverProvider;
private static int lastConnection = -1;
private static IntMap<StreamBuilder> streams = new IntMap<>();
/**Connect to an address.*/
@@ -65,6 +63,11 @@ public class Net{
serverProvider.sendTo(id, object, mode);
}
/**Send an object to everyone EXCEPT certain client. Server-side only*/
public static void sendExcept(int id, Object object, SendMode mode){
serverProvider.sendExcept(id, object, mode);
}
/**Send a stream to a specific client. Server-side only.*/
public static void sendStream(int id, Streamable stream){
serverProvider.sendStream(id, stream);
@@ -114,13 +117,19 @@ public class Net{
}
/**Call to handle a packet being recieved for the server.*/
public static void handleServerReceived(Object object){
public static void handleServerReceived(Object object, int connection){
if(serverListeners.get(object.getClass()) != null){
lastConnection = connection;
serverListeners.get(object.getClass()).accept(object);
}else{
Gdx.app.error("Mindustry::Net", "Unhandled packet type: '" + object.getClass() + "'!");
}
}
/**Returns the last connection that sent a packet to this server.*/
public static int getLastConnection(){
return lastConnection;
}
/**Whether the net is active, e.g. whether this is a multiplayer game.*/
public static boolean active(){

View File

@@ -1,7 +1,5 @@
package io.anuke.mindustry.net;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.entities.Player;
/**Class for storing all packets.*/
@@ -27,6 +25,45 @@ public class Packets {
}
public static class SyncPacket{
public int[] ids;
public float[][] data;
}
public static class BlockSyncPacket extends Streamable{
}
public static class StateSyncPacket {
public int[] items;
public float countdown;
public int enemies, wave;
}
public static class PositionPacket{
public float[] data;
}
public static class EffectPacket{
public int id;
public float x, y, rotation;
public int color;
}
public static class ShootPacket{
public byte weaponid;
public float x, y, rotation;
public int playerid;
}
public static class PlacePacket{
public int playerid;
public byte rotation;
public short x, y;
public int block;
}
public static class BreakPacket{
public int playerid;
public short x, y;
}
}

View File

@@ -3,13 +3,10 @@ package io.anuke.mindustry.net;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.net.Packets.EntityDataPacket;
import io.anuke.mindustry.net.Packets.SyncPacket;
import io.anuke.mindustry.net.Packets.WorldData;
import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.net.Streamable.StreamBegin;
import io.anuke.mindustry.net.Streamable.StreamChunk;
import io.anuke.mindustry.resource.Mech;
import io.anuke.mindustry.resource.Weapon;
import io.anuke.ucore.entities.Entity;
public class Registrator {
@@ -21,8 +18,18 @@ public class Registrator {
WorldData.class,
SyncPacket.class,
EntityDataPacket.class,
PositionPacket.class,
ShootPacket.class,
PlacePacket.class,
BreakPacket.class,
StateSyncPacket.class,
BlockSyncPacket.class,
Class.class,
byte[].class,
float[].class,
float[][].class,
int[].class,
Entity[].class,
Player[].class,
Array.class,

View File

@@ -1,4 +1,57 @@
package io.anuke.mindustry.net;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.entities.Player;
import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.util.Mathf;
public interface Syncable {
public Interpolator<?> getInterpolator();
public abstract class SyncType<T extends Entity>{
public abstract float[] write(T entity);
public abstract void read(T entity, float[] data);
public abstract void update(T entity, Interpolator interpolator);
public static final SyncType<Player> player = new SyncType<Player>() {
@Override
public float[] write(Player entity) {
return new float[]{entity.x, entity.y, entity.angle, entity.health};
}
@Override
public void read(Player entity, float[] data) {
entity.getInterpolator().target.set(data[0], data[1]);
entity.getInterpolator().targetrot = data[2];
entity.health = (int)data[3];
}
@Override
public void update(Player entity, Interpolator interpolator) {
Interpolator i = entity.getInterpolator();
if(i.target.dst(entity.x, entity.y) > 16){
entity.set(i.target.x, i.target.y);
}
entity.x = Mathf.lerpDelta(entity.x, i.target.x, 0.4f);
entity.y = Mathf.lerpDelta(entity.y, i.target.y, 0.4f);
entity.angle = Mathf.lerpAngDelta(entity.angle, i.targetrot, 0.6f);
}
};
}
public static class Interpolator<T extends Entity> {
public SyncType<T> type;
public Vector2 target = new Vector2();
public float targetrot;
public Interpolator(SyncType<T> type){
this.type = type;
}
public void update(T entity){
this.type.update(entity, this);
}
}
}