Synchronization codegen work

This commit is contained in:
austin 2023-06-25 14:13:36 -04:00
parent 058c6ac3d9
commit 7d4b3b1a97
7 changed files with 71 additions and 43 deletions

View File

@ -0,0 +1,21 @@
package electrosphere.entity.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
/**
* A field in a synchronized behavior tree that is synchronized between the server and the client.
*/
public @interface SyncedField {
//True if the field is an enum
boolean isEnum() default false;
//if the field is an enum, this value is MANDETORY. Determines what id will be set to when serialized/deserialized
int enumId() default 0;
}

View File

@ -0,0 +1,22 @@
package electrosphere.entity.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
/**
* A behavior tree that will have modifications by code generation.
* It is synchronized between the server and client with auto-generated netcode.
*/
public @interface SynchronizedBehaviorTree {
//The name of the behavior tree
public String name() default "";
//True if this is a server-side behavior tree
public boolean isServer() default false;
}

View File

@ -1,7 +1,14 @@
package electrosphere.entity.state;
/**
* A behavior tree
*/
public interface BehaviorTree {
/**
* Simulates the behavior tree
* @param deltaTime The time since the last call to the simulate function
*/
public void simulate(float deltaTime);
}

View File

@ -1,14 +0,0 @@
package electrosphere.entity.state;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface BehaviorTreeAnnotation {
public String name() default "";
}

View File

@ -1,14 +0,0 @@
package electrosphere.entity.state;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SyncedField {
boolean isEnum() default false;
}

View File

@ -1,8 +1,6 @@
package electrosphere.entity.state.idle;
import electrosphere.entity.state.BehaviorTree;
import electrosphere.entity.state.BehaviorTreeAnnotation;
import electrosphere.entity.state.SyncedField;
import electrosphere.entity.state.attack.AttackTree;
import electrosphere.entity.state.attack.AttackTree.AttackTreeState;
import electrosphere.entity.state.movement.AirplaneMovementTree;
@ -11,24 +9,28 @@ import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.annotation.SyncedField;
import electrosphere.entity.annotation.SynchronizedBehaviorTree;
import electrosphere.entity.state.movement.GroundMovementTree.MovementTreeState;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.renderer.actor.Actor;
import electrosphere.renderer.anim.Animation;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.joml.Vector3d;
@BehaviorTreeAnnotation(name="idle")
public class IdleTree {
@SynchronizedBehaviorTree(name = "idle", isServer = false)
public class IdleTree implements BehaviorTree {
public static enum IdleTreeState {
IDLE,
NOT_IDLE,
}
@SyncedField(isEnum = true)
@SyncedField(isEnum = true, enumId = 0)
IdleTreeState state;
Entity parent;
@ -45,8 +47,8 @@ public class IdleTree {
}
public IdleTreeState getState(){
return state;
}
return state;
}
public void start(){
//TODO: check if can start moving
@ -62,6 +64,7 @@ public class IdleTree {
state = IdleTreeState.NOT_IDLE;
}
@Override
public void simulate(float deltaTime){
Actor entityActor = EntityUtils.getActor(parent);
@ -115,7 +118,7 @@ public class IdleTree {
if(entityActor != null){
if(
(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(Animation.ANIMATION_IDLE_1)) &&
(Globals.assetManager.fetchModel(entityActor.getModelPath()) != null && Globals.assetManager.fetchModel(entityActor.getModelPath()).getAnimation(Animation.ANIMATION_IDLE_1) != null)
(Globals.assetManager.fetchModel(entityActor.getModelPath()) != null && Globals.assetManager.fetchModel(entityActor.getModelPath()).getAnimation(Animation.ANIMATION_IDLE_1) != null)
){
entityActor.playAnimation(Animation.ANIMATION_IDLE_1,3);
@ -172,4 +175,4 @@ public class IdleTree {
networkMessageQueue.add(networkMessage);
}
}
}

View File

@ -1,8 +1,6 @@
package electrosphere.entity.state.idle;
import electrosphere.entity.state.BehaviorTree;
import electrosphere.entity.state.BehaviorTreeAnnotation;
import electrosphere.entity.state.SyncedField;
import electrosphere.entity.state.attack.ServerAttackTree;
import electrosphere.entity.state.attack.ServerAttackTree.AttackTreeState;
import electrosphere.entity.state.movement.AirplaneMovementTree;
@ -11,16 +9,21 @@ import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.engine.Main;
import electrosphere.entity.annotation.SyncedField;
import electrosphere.entity.annotation.SynchronizedBehaviorTree;
import electrosphere.entity.state.movement.ServerGroundMovementTree.MovementTreeState;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.renderer.actor.Actor;
import electrosphere.renderer.anim.Animation;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.joml.Vector3d;
@BehaviorTreeAnnotation(name="idle")
@SynchronizedBehaviorTree(name = "serverIdle", isServer = true)
public class ServerIdleTree {
public static enum IdleTreeState {
@ -28,7 +31,7 @@ public class ServerIdleTree {
NOT_IDLE,
}
@SyncedField(isEnum = true)
@SyncedField(isEnum = true, enumId = 0)
IdleTreeState state;
Entity parent;
@ -115,7 +118,7 @@ public class ServerIdleTree {
if(entityActor != null){
if(
(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(Animation.ANIMATION_IDLE_1)) &&
(Globals.assetManager.fetchModel(entityActor.getModelPath()) != null && Globals.assetManager.fetchModel(entityActor.getModelPath()).getAnimation(Animation.ANIMATION_IDLE_1) != null)
(Globals.assetManager.fetchModel(entityActor.getModelPath()) != null && Globals.assetManager.fetchModel(entityActor.getModelPath()).getAnimation(Animation.ANIMATION_IDLE_1) != null)
){
entityActor.playAnimation(Animation.ANIMATION_IDLE_1,3);
@ -172,4 +175,4 @@ public class ServerIdleTree {
networkMessageQueue.add(networkMessage);
}
}
}