Added visual interpolation for slow tickrates

This commit is contained in:
Anuken
2018-02-07 17:56:39 -05:00
parent 153905cf47
commit f4227b99ff
8 changed files with 72 additions and 11 deletions

View File

@@ -35,7 +35,6 @@ public class Player extends SyncEntity{
public Weapon weaponRight = Weapon.blaster;
public Mech mech = Mech.standard;
public float angle;
public float targetAngle = 0f;
public float stucktime = 0f;
public boolean dashing = false;
@@ -106,7 +105,7 @@ public class Player extends SyncEntity{
}
@Override
public void draw(){
public void drawSmooth(){
if(isAndroid && isLocal){
angle = Mathf.lerpAngDelta(angle, targetAngle, 0.2f);
}

View File

@@ -1,17 +1,27 @@
package io.anuke.mindustry.entities;
import com.badlogic.gdx.Gdx;
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 io.anuke.ucore.util.Mathf;
import java.nio.ByteBuffer;
import static io.anuke.mindustry.Vars.threads;
public abstract class SyncEntity extends DestructibleEntity{
private static ObjectIntMap<Class<? extends SyncEntity>> writeSizes = new ObjectIntMap<>();
protected transient Interpolator interpolator = new Interpolator();
//for interpolating at low tick speeds.
private transient Vector2 tpos = new Vector2(-999, -999);
private transient float tang = 0f;
public float angle;
static{
setWriteSize(Enemy.class, 4 + 4 + 2 + 2);
setWriteSize(Player.class, 4 + 4 + 4 + 2 + 1);
@@ -24,6 +34,40 @@ public abstract class SyncEntity extends DestructibleEntity{
public abstract void read(ByteBuffer data, long time);
public abstract void interpolate();
@Override
public final void draw(){
float x = this.x, y = this.y, angle = this.angle;
//interpolates data at low tick speeds.
if(isSmoothing()){
if(tpos.dst(x, y) > 100){
tpos.set(x, y);
}
tpos.x = Mathf.lerpDelta(tpos.x, x, 0.3f);
tpos.y = Mathf.lerpDelta(tpos.y, y, 0.3f);
tang = Mathf.lerpAngDelta(tang, angle, 0.3f);
this.x = tpos.x;
this.y = tpos.y;
this.angle = tang;
}
drawSmooth();
this.x = x;
this.y = y;
this.angle = angle;
}
private boolean isSmoothing(){
return threads.isEnabled() && threads.getFPS() <= Gdx.graphics.getFramesPerSecond() / 2f;
}
public Vector2 getDrawPosition(){
return isSmoothing() ? tpos : tpos.set(x, y);
}
public void drawSmooth(){}
public int getWriteSize(){
return getWriteSize(getClass());
}

View File

@@ -29,12 +29,12 @@ public class Enemy extends SyncEntity {
public Enemy spawner;
public int spawned;
public float angle;
public Vector2 velocity = new Vector2();
public Vector2 totalMove = new Vector2();
public Vector2 tpos = new Vector2(-999, -999);
public Entity target;
public float hitTime;
public int tier = 1;
public Vector2 totalMove = new Vector2();
public TextureRegion region;
public Translator tr = new Translator();
@@ -52,7 +52,7 @@ public class Enemy extends SyncEntity {
}
@Override
public void draw(){
public void drawSmooth(){
type.draw(this);
}