Implemented new syncing system

This commit is contained in:
Anuken
2018-01-14 20:27:10 -05:00
parent e545b7cca7
commit cc45e5afca
16 changed files with 237 additions and 191 deletions

View File

@@ -2,20 +2,20 @@ package io.anuke.mindustry.entities;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.net.Syncable;
import io.anuke.mindustry.resource.Mech;
import io.anuke.mindustry.resource.Weapon;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Blocks;
import io.anuke.ucore.core.*;
import io.anuke.ucore.entities.DestructibleEntity;
import io.anuke.ucore.entities.SolidEntity;
import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Mathf;
import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.*;
public class Player extends DestructibleEntity implements Syncable{
public class Player extends SyncEntity{
static final float speed = 1.1f;
static final float dashSpeed = 1.8f;
@@ -33,7 +33,6 @@ public class Player extends DestructibleEntity implements Syncable{
public transient int clientid;
public transient boolean isLocal = false;
public transient Interpolator<Player> inter = new Interpolator<>(SyncType.player);
public Player(){
hitbox.setSize(5);
@@ -43,11 +42,6 @@ public class Player extends DestructibleEntity implements Syncable{
heal();
}
@Override
public Interpolator<Player> getInterpolator() {
return inter;
}
@Override
public void damage(int amount){
if(!Vars.debug && !isAndroid)
@@ -117,7 +111,7 @@ public class Player extends DestructibleEntity implements Syncable{
@Override
public void update(){
if(!isLocal || isAndroid || Vars.ui.chatfrag.chatOpen()){
if(!isDead() && !isLocal) inter.update(this);
if(!isDead() && !isLocal) interpolate();
return;
}
@@ -186,4 +180,49 @@ public class Player extends DestructibleEntity implements Syncable{
public String toString() {
return "Player{" + id + ", android=" + isAndroid + ", local=" + isLocal + ", " + x + ", " + y + "}\n";
}
@Override
public void write(ByteBuffer data) {
data.putFloat(x);
data.putFloat(y);
data.putFloat(angle);
data.putShort((short)health);
data.put((byte)(dashing ? 1 : 0));
}
@Override
public void read(ByteBuffer data) {
float x = data.getFloat();
float y = data.getFloat();
float angle = data.getFloat();
short health = data.getShort();
byte dashing = data.get();
interpolator.target.set(x, y);
interpolator.targetrot = angle;
this.health = health;
this.dashing = dashing == 1;
}
@Override
public void interpolate() {
Interpolator i = interpolator;
if(i.target.dst(x, y) > 16 && !isAndroid){
set(i.target.x, i.target.y);
}
if(isAndroid && i.target.dst(x, y) > 2f && Timers.get(this, "dashfx", 2)){
Angles.translation(angle + 180, 3f);
Effects.effect(Fx.dashsmoke, x + Angles.x(), y + Angles.y());
}
if(dashing && Timers.get(this, "dashfx", 3)){
Angles.translation(angle + 180, 3f);
Effects.effect(Fx.dashsmoke, x + Angles.x(), y + Angles.y());
}
x = Mathf.lerpDelta(x, i.target.x, 0.4f);
y = Mathf.lerpDelta(y, i.target.y, 0.4f);
angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f);
}
}

View File

@@ -0,0 +1,46 @@
package io.anuke.mindustry.entities;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ObjectIntMap;
import io.anuke.mindustry.entities.enemies.Enemy;
import io.anuke.ucore.entities.DestructibleEntity;
import java.nio.ByteBuffer;
public abstract class SyncEntity extends DestructibleEntity{
private static ObjectIntMap<Class<? extends SyncEntity>> writeSizes = new ObjectIntMap<>();
public transient Interpolator interpolator = new Interpolator();
static{
setWriteSize(Enemy.class, 4 + 4 + 2 + 2);
setWriteSize(Player.class, 4 + 4 + 4 + 2 + 1);
}
public abstract void write(ByteBuffer data);
public abstract void read(ByteBuffer data);
public abstract void interpolate();
public int getWriteSize(){
return getWriteSize(getClass());
}
public static int getWriteSize(Class<? extends SyncEntity> type){
int i = writeSizes.get(type, -1);
if(i == -1) throw new RuntimeException("Write size for class \"" + type + "\" is not defined!");
return i;
}
protected static void setWriteSize(Class<? extends SyncEntity> type, int size){
writeSizes.put(type, size);
}
public class Interpolator {
public Vector2 target = new Vector2();
public Vector2 delta = new Vector2();
public Vector2 last = new Vector2();
public float targetrot;
}
}

View File

@@ -4,17 +4,17 @@ import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.Bullet;
import io.anuke.mindustry.entities.BulletType;
import io.anuke.mindustry.entities.SyncEntity;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Syncable;
import io.anuke.ucore.entities.DestructibleEntity;
import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.entities.SolidEntity;
import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Timer;
public class Enemy extends DestructibleEntity implements Syncable{
protected Interpolator<Enemy> inter = new Interpolator<>(SyncType.enemy);
import java.nio.ByteBuffer;
public class Enemy extends SyncEntity {
public final EnemyType type;
public Timer timer = new Timer(5);
@@ -92,8 +92,36 @@ public class Enemy extends DestructibleEntity implements Syncable{
}
@Override
public Interpolator<Enemy> getInterpolator() {
return inter;
public void write(ByteBuffer data) {
data.putFloat(x);
data.putFloat(y);
data.putShort((short)(angle*2));
data.putShort((short)health);
}
@Override
public void read(ByteBuffer data) {
float x = data.getFloat();
float y = data.getFloat();
short angle = data.getShort();
short health = data.getShort();
interpolator.target.set(x, y);
interpolator.targetrot = angle/2f;
this.health = health;
}
@Override
public void interpolate() {
Interpolator i = interpolator;
if(i.target.dst(x, y) > 16){
set(i.target.x, i.target.y);
}
x = Mathf.lerpDelta(x, i.target.x, 0.4f);
y = Mathf.lerpDelta(y, i.target.y, 0.4f);
angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f);
}
public void shoot(BulletType bullet){

View File

@@ -138,7 +138,7 @@ public class EnemyType {
float range = this.range + enemy.tier * 5;
if(Net.client() && Net.active()){
enemy.inter.update(enemy); //TODO? better structure for interpolation
enemy.interpolate(); //TODO? better structure for interpolation
return;
}