1
0
mirror of https://github.com/Anuken/Mindustry.git synced 2024-09-22 22:07:31 +03:00

Completely implemented new interpolation

This commit is contained in:
Anuken 2018-02-03 20:43:48 -05:00
parent 23c1edc3fa
commit da22f8fbce
8 changed files with 39 additions and 35 deletions

View File

@ -22,7 +22,7 @@ allprojects {
appName = "Mindustry"
gdxVersion = '1.9.8'
aiVersion = '1.8.1'
uCoreVersion = 'f8a3b56'
uCoreVersion = 'f40fdf3'
}
repositories {

View File

@ -112,6 +112,7 @@ public class NetClient extends Module {
int enemies = 0;
ByteBuffer data = ByteBuffer.wrap(packet.data);
long time = data.getLong();
byte groupid = data.get();
@ -134,7 +135,7 @@ public class NetClient extends Module {
}
data.position(data.position() + SyncEntity.getWriteSize((Class<? extends SyncEntity>) group.getType()));
} else {
entity.read(data);
entity.read(data, time);
}
}
@ -324,8 +325,9 @@ public class NetClient extends Module {
void sync(){
if(Timers.get("syncPlayer", playerSyncTime)){
byte[] bytes = new byte[player.getWriteSize()];
byte[] bytes = new byte[player.getWriteSize() + 8];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.putLong(TimeUtils.millis());
player.write(buffer);
PositionPacket packet = new PositionPacket();

View File

@ -123,8 +123,11 @@ public class NetServer extends Module{
});
Net.handleServer(PositionPacket.class, (id, packet) -> {
ByteBuffer buffer = ByteBuffer.wrap(packet.data);
long time = buffer.getLong();
Player player = connections.get(id);
player.read(ByteBuffer.wrap(packet.data));
player.read(buffer, time);
});
Net.handleServer(ShootPacket.class, (id, packet) -> {
@ -238,9 +241,10 @@ public class NetServer extends Module{
//calculate amount of entities to go into this packet
int csize = Math.min(amount-i, maxsize);
//create a byte array to write to
byte[] bytes = new byte[csize*writesize + 1];
byte[] bytes = new byte[csize*writesize + 1 + 8];
//wrap it for easy writing
current = ByteBuffer.wrap(bytes);
current.putLong(TimeUtils.millis());
//write the group ID so the client knows which group this is
current.put((byte)group.getID());
}

View File

@ -1,7 +1,6 @@
package io.anuke.mindustry.entities;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.graphics.Shaders;
@ -261,22 +260,20 @@ public class Player extends SyncEntity{
data.putFloat(angle);
data.putShort((short)health);
data.put((byte)(dashing ? 1 : 0));
data.putLong(TimeUtils.millis());
}
@Override
public void read(ByteBuffer data) {
public void read(ByteBuffer data, long time) {
float x = data.getFloat();
float y = data.getFloat();
float angle = data.getFloat();
short health = data.getShort();
byte dashing = data.get();
long time = data.getLong();
interpolator.targetrot = angle;
this.health = health;
this.dashing = dashing == 1;
interpolator.targetrot = angle;
interpolator.time = 0f;
interpolator.last.set(this.x, this.y);
interpolator.target.set(x, y);
@ -289,9 +286,12 @@ public class Player extends SyncEntity{
i.time += 1f / i.spacing * Timers.delta();
lerp2(Tmp.v2.set(i.last), i.target, i.time);
Mathf.lerp2(Tmp.v2.set(i.last), i.target, i.time);
x = Tmp.v2.x;
y = Tmp.v2.y;
angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f);
if(isAndroid && i.target.dst(i.last) > 2f && Timers.get(this, "dashfx", 2)){
Angles.translation(angle + 180, 3f);
@ -302,17 +302,6 @@ public class Player extends SyncEntity{
Angles.translation(angle + 180, 3f);
Effects.effect(Fx.dashsmoke, x + Angles.x(), y + Angles.y());
}
x = Tmp.v2.x;
y = Tmp.v2.y;
angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f);
}
private Vector2 lerp2 (Vector2 v, Vector2 target, float alpha) {
v.x = (v.x) + ((target.x - v.x) * alpha);
v.y = (v.y) + ((target.y - v.y) * alpha);
return v;
}
public Color getColor(){

View File

@ -14,14 +14,14 @@ public abstract class SyncEntity extends DestructibleEntity{
static{
setWriteSize(Enemy.class, 4 + 4 + 2 + 2);
setWriteSize(Player.class, 4 + 4 + 4 + 2 + 1 + 8);
setWriteSize(Player.class, 4 + 4 + 4 + 2 + 1);
}
public abstract void writeSpawn(ByteBuffer data);
public abstract void readSpawn(ByteBuffer data);
public abstract void write(ByteBuffer data);
public abstract void read(ByteBuffer data);
public abstract void read(ByteBuffer data, long time);
public abstract void interpolate();
public int getWriteSize(){

View File

@ -1,16 +1,19 @@
package io.anuke.mindustry.entities.enemies;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.TimeUtils;
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.NetEvents;
import io.anuke.ucore.core.Timers;
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;
import io.anuke.ucore.util.Tmp;
import java.nio.ByteBuffer;
@ -126,27 +129,33 @@ public class Enemy extends SyncEntity {
}
@Override
public void read(ByteBuffer data) {
public void read(ByteBuffer data, long time) {
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;
interpolator.targetrot = angle / 2f;
interpolator.time = 0f;
interpolator.last.set(this.x, this.y);
interpolator.target.set(x, y);
interpolator.spacing = Math.max(((TimeUtils.timeSinceMillis(time) / 1000f) * 60f), 1f);
}
@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);
i.time += 1f / i.spacing * Timers.delta();
Mathf.lerp2(Tmp.v2.set(i.last), i.target, i.time);
x = Tmp.v2.x;
y = Tmp.v2.y;
angle = Mathf.lerpAngDelta(angle, i.targetrot, 0.6f);
}

View File

@ -142,7 +142,7 @@ public class Packets {
@Override
public void read(ByteBuffer buffer) {
data = new byte[SyncEntity.getWriteSize(Player.class)];
data = new byte[SyncEntity.getWriteSize(Player.class) + 8];
buffer.get(data);
}
}

View File

@ -15,11 +15,11 @@ import static io.anuke.mindustry.Vars.headless;
import static io.anuke.mindustry.Vars.playerGroup;
public class KryoRegistrator {
public static boolean fakeLag = true;
public static boolean fakeLag = false;
public static final int fakeLagAmount = 500;
static{
Log.set(Log.LEVEL_ERROR);
Log.set(Log.LEVEL_WARN);
Log.setLogger(new Logger(){
public void log (int level, String category, String message, Throwable ex) {