Various sync fixes

This commit is contained in:
Anuken
2020-05-24 17:01:17 -04:00
parent d202c6ebdd
commit 3051598b92
14 changed files with 71 additions and 79 deletions

View File

@@ -23,6 +23,15 @@ public class Annotations{
public @interface SyncField{
/** If true, the field will be linearly interpolated. If false, it will be interpolated as an angle. */
boolean value();
/** If true, the field is clamped to 0-1. */
boolean clamped() default false;
}
/** Indicates that a field will not be read from the server when syncing. */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.SOURCE)
public @interface SyncLocal{
}
/** Indicates that a component field is imported from other components. This means it doesn't actually exist. */

View File

@@ -61,7 +61,9 @@ public class EntityIO{
//add new revision if it doesn't match or there are no revisions
if(revisions.isEmpty() || !revisions.peek().equal(fields)){
revisions.add(new Revision(nextRevision, fields.map(f -> new RevisionField(f.name, f.type.toString(), f.type.isPrimitive() ? BaseProcessor.typeSize(f.type.toString()) : -1))));
revisions.add(new Revision(nextRevision,
fields.map(f -> new RevisionField(f.name, f.type.toString(),
f.type.isPrimitive() ? BaseProcessor.typeSize(f.type.toString()) : -1))));
//write revision
directory.child(nextRevision + ".json").writeString(json.toJson(revisions.peek()));
}
@@ -108,7 +110,7 @@ public class EntityIO{
}
}
void writeSync(MethodSpec.Builder method, boolean write, Array<Svar> syncFields) throws Exception{
void writeSync(MethodSpec.Builder method, boolean write, Array<Svar> syncFields, Array<Svar> allFields) throws Exception{
this.method = method;
this.write = write;
@@ -123,15 +125,22 @@ public class EntityIO{
//base read code
st("if(lastUpdated != 0) updateSpacing = $T.timeSinceMillis(lastUpdated)", Time.class);
st("lastUpdated = $T.millis()", Time.class);
st("boolean islocal = isLocal()");
//add code for reading revision
for(RevisionField field : rev.fields){
Svar sf = syncFields.find(s -> s.name().equals(field.name));
if(sf != null){
Svar var = allFields.find(s -> s.name().equals(field.name));
boolean sf = var.has(SyncField.class), sl = var.has(SyncLocal.class);
if(sl) cont("if(!islocal)");
if(sf){
st(field.name + lastSuf + " = this." + field.name);
}
io(field.type, "this." + (sf != null ? field.name + targetSuf : field.name) + " = ");
io(field.type, "this." + (sf ? field.name + targetSuf : field.name) + " = ");
if(sl) econt();
}
st("afterSync()");
@@ -173,7 +182,7 @@ public class EntityIO{
//write interpolated data, using slerp / lerp
for(Svar field : fields){
String name = field.name(), targetName = name + targetSuf, lastName = name + lastSuf;
st("$L = $T.$L($L, $L, alpha)", name, Mathf.class, field.annotation(SyncField.class).value() ? "lerp" : "slerp", lastName, targetName);
st("$L = $L($T.$L($L, $L, alpha))", name, field.annotation(SyncField.class).clamped() ? "arc.math.Mathf.clamp" : "", Mathf.class, field.annotation(SyncField.class).value() ? "lerp" : "slerp", lastName, targetName);
}
ncont("else"); //no meaningful data has arrived yet

View File

@@ -220,6 +220,7 @@ public class EntityProcess extends BaseProcessor{
//all SyncField fields
Array<Svar> syncedFields = new Array<>();
Array<Svar> allFields = new Array<>();
//add all components
for(Stype comp : components){
@@ -253,6 +254,8 @@ public class EntityProcess extends BaseProcessor{
builder.addField(fbuilder.build());
specVariables.put(builder.fieldSpecs.get(builder.fieldSpecs.size() - 1), f);
allFields.add(f);
//add extra sync fields
if(f.has(SyncField.class)){
if(!f.tname().toString().equals("float")) err("All SyncFields must be of type float", f);
@@ -357,7 +360,7 @@ public class EntityProcess extends BaseProcessor{
//SPECIAL CASE: sync I/O code
if((first.name().equals("readSync") || first.name().equals("writeSync"))){
io.writeSync(mbuilder, first.name().equals("writeSync"), syncedFields);
io.writeSync(mbuilder, first.name().equals("writeSync"), syncedFields, allFields);
}
//SPECIAL CASE: sync I/O code for writing to/from a manual buffer