Multiplayer: Smooth building + rotation / Disabled UDP / Cleanup

This commit is contained in:
Anuken
2019-08-19 22:16:43 -04:00
parent 8eb4d25638
commit a873560d4e
9 changed files with 145 additions and 235 deletions
@@ -1,14 +1,15 @@
package io.anuke.mindustry.net;
import io.anuke.arc.math.Mathf;
import io.anuke.arc.math.geom.Vector2;
import io.anuke.arc.util.Time;
import io.anuke.arc.math.*;
import io.anuke.arc.math.geom.*;
import io.anuke.arc.util.*;
public class Interpolator{
//used for movement
public Vector2 target = new Vector2();
public Vector2 last = new Vector2();
public float[] targets = {};
public float[] lasts = {};
public long lastUpdated, updateSpacing;
//current state
@@ -21,6 +22,12 @@ public class Interpolator{
lastUpdated = Time.millis();
targets = target1ds;
if(lasts.length != values.length){
lasts = new float[values.length];
}
for(int i = 0; i < values.length; i++){
lasts[i] = values[i];
}
last.set(cx, cy);
target.set(x, y);
}
@@ -46,8 +53,12 @@ public class Interpolator{
values = new float[targets.length];
}
if(lasts.length != targets.length){
lasts = new float[targets.length];
}
for(int i = 0; i < values.length; i++){
values[i] = Mathf.slerp(values[i], targets[i], alpha);
values[i] = Mathf.slerp(lasts[i], targets[i], alpha);
}
}else{
pos.set(target);
+48 -14
View File
@@ -162,8 +162,8 @@ public class Net{
/**
* Returns a list of all connections IDs.
*/
public static Array<NetConnection> getConnections(){
return (Array<NetConnection>)serverProvider.getConnections();
public static Iterable<NetConnection> getConnections(){
return (Iterable<NetConnection>)serverProvider.getConnections();
}
/**
@@ -326,7 +326,7 @@ public class Net{
public static void dispose(){
if(clientProvider != null) clientProvider.dispose();
if(serverProvider != null) serverProvider.dispose();
if(serverProvider != null) serverProvider.close();
clientProvider = null;
serverProvider = null;
server = false;
@@ -377,16 +377,53 @@ public class Net{
void host(int port) throws IOException;
/** Sends a large stream of data to a specific client. */
void sendStream(int id, Streamable stream);
default void sendStream(int id, Streamable stream){
NetConnection connection = getByID(id);
if(connection == null) return;
try{
int cid;
StreamBegin begin = new StreamBegin();
begin.total = stream.stream.available();
begin.type = Registrator.getID(stream.getClass());
connection.send(begin, SendMode.tcp);
cid = begin.id;
/** Send an object to everyone connected. */
void send(Object object, SendMode mode);
while(stream.stream.available() > 0){
byte[] bytes = new byte[Math.min(512, stream.stream.available())];
stream.stream.read(bytes);
/** Send an object to a specific client ID. */
void sendTo(int id, Object object, SendMode mode);
StreamChunk chunk = new StreamChunk();
chunk.id = cid;
chunk.data = bytes;
connection.send(chunk, SendMode.tcp);
}
}catch(IOException e){
throw new RuntimeException(e);
}
}
/** Send an object to everyone <i>except</i> a client ID. */
void sendExcept(int id, Object object, SendMode mode);
default void send(Object object, SendMode mode){
for(NetConnection con : getConnections()){
con.send(object, mode);
}
}
default void sendTo(int id, Object object, SendMode mode){
NetConnection conn = getByID(id);
if(conn == null){
Log.err("Failed to find connection with ID {0}.", id);
return;
}
conn.send(object, mode);
}
default void sendExcept(int id, Object object, SendMode mode){
for(NetConnection con : getConnections()){
if(con.id != id){
con.send(object, mode);
}
}
}
/** Close the server connection. */
void close();
@@ -395,12 +432,9 @@ public class Net{
byte[] compressSnapshot(byte[] input);
/** Return all connected users. */
Array<? extends NetConnection> getConnections();
Iterable<? extends NetConnection> getConnections();
/** Returns a connection by ID. */
NetConnection getByID(int id);
/** Close all connections. */
void dispose();
}
}
@@ -3,6 +3,8 @@ package io.anuke.mindustry.net;
import io.anuke.mindustry.net.Net.SendMode;
public abstract class NetConnection{
private static int lastID;
public final int id;
public final String address;
@@ -18,8 +20,9 @@ public abstract class NetConnection{
public boolean hasBegunConnecting = false;
public float viewWidth, viewHeight, viewX, viewY;
public NetConnection(int id, String address){
this.id = id;
/** Assigns this connection a unique ID. No two connections will ever have the same ID.*/
public NetConnection(String address){
this.id = lastID++;
this.address = address;
}