package electrosphere.entity.state.block; import electrosphere.net.synchronization.FieldIdEnums; import electrosphere.engine.Globals; import electrosphere.entity.Entity; import electrosphere.entity.EntityDataStrings; import electrosphere.entity.btree.BehaviorTree; import electrosphere.entity.btree.StateTransitionUtil; import electrosphere.entity.btree.StateTransitionUtil.StateTransitionUtilItem; import electrosphere.net.synchronization.BehaviorTreeIdEnums; import electrosphere.server.datacell.utils.ServerBehaviorTreeUtils; import electrosphere.net.parser.net.message.SynchronizationMessage; import electrosphere.server.datacell.utils.DataCellSearchUtils; import electrosphere.entity.state.block.ClientBlockTree.BlockState; import electrosphere.game.data.creature.type.block.BlockSystem; import electrosphere.game.data.creature.type.block.BlockVariant; import electrosphere.net.synchronization.annotation.SyncedField; import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; @SynchronizedBehaviorTree(name = "serverBlockTree", isServer = true, correspondingTree="clientBlockTree") /** * Server block tree */ public class ServerBlockTree implements BehaviorTree { @SyncedField BlockState state = BlockState.NOT_BLOCKING; //the current state of the tree //the parent entity to this tree Entity parent; @SyncedField String currentBlockVariant = null; //The current block variant (depends on equipped items) //The data for block animations BlockSystem blockSystem; //The state transition util StateTransitionUtil stateTransitionUtil; /** * Constructor */ private ServerBlockTree(Entity parent, Object ... params){ this.parent = parent; this.blockSystem = (BlockSystem)params[0]; this.stateTransitionUtil = StateTransitionUtil.create(parent, true, new StateTransitionUtilItem[]{ StateTransitionUtilItem.create( BlockState.WIND_UP, () -> {return this.blockSystem.getBlockVariant(this.currentBlockVariant).getWindUpAnimation();}, null, () -> {this.setState(BlockState.BLOCKING);} ), StateTransitionUtilItem.create( BlockState.BLOCKING, () -> {return this.blockSystem.getBlockVariant(this.currentBlockVariant).getMainAnimation();}, null, null ), StateTransitionUtilItem.create( BlockState.COOLDOWN, () -> { BlockVariant variant = this.blockSystem.getBlockVariant(this.currentBlockVariant); if(variant != null){ return variant.getCooldownAnimation(); } return null; }, null, () -> {this.setState(BlockState.NOT_BLOCKING);} ), }); } /** * Starts the block tree */ public void start(){ if(this.currentBlockVariant != null && this.blockSystem.getBlockVariant(this.currentBlockVariant) != null){ this.stateTransitionUtil.reset(); setState(BlockState.WIND_UP); } } /** * Stops the block tree */ public void stop(){ this.stateTransitionUtil.reset(); setState(BlockState.COOLDOWN); } @Override public void simulate(float deltaTime) { switch(state){ case WIND_UP: { this.stateTransitionUtil.simulate(BlockState.WIND_UP); } break; case BLOCKING: { this.stateTransitionUtil.simulate(BlockState.BLOCKING); } break; case COOLDOWN: { this.stateTransitionUtil.simulate(BlockState.COOLDOWN); } break; case NOT_BLOCKING: { } break; } } /** * Gets whether the tree is blocking or not * @return true if blocking, false otherwise */ public boolean isBlocking(){ return this.state == BlockState.BLOCKING; } /** * Gets the block system data for this tree * @return the data if it exists, otherwise null */ public BlockSystem getBlockSystem(){ return this.blockSystem; } /** *
Automatically generated
** Gets state. *
*/ public BlockState getState(){ return state; } /** *Automatically generated
** Sets state and handles the synchronization logic for it. *
* @param state The value to set state to. */ public void setState(BlockState state){ this.state = state; int value = ClientBlockTree.getBlockStateEnumAsShort(state); DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStateMessage(parent.getId(), BehaviorTreeIdEnums.BTREE_SERVERBLOCKTREE_ID, FieldIdEnums.TREE_SERVERBLOCKTREE_SYNCEDFIELD_STATE_ID, value)); } /** *(initially) Automatically generated
*More parameters can be safely added to this method
** Attaches this tree to the entity. *
* @param entity The entity to attach to * @param tree The behavior tree to attach * @param params Optional parameters that will be provided to the constructor */ public static ServerBlockTree attachTree(Entity parent, Object ... params){ ServerBlockTree rVal = new ServerBlockTree(parent,params); //put manual code here (setting params, etc) //!!WARNING!! from here below should not be touched //This was generated automatically to properly alert various systems that the btree exists and should be tracked ServerBehaviorTreeUtils.attachBTreeToEntity(parent, rVal); parent.putData(EntityDataStrings.TREE_SERVERBLOCKTREE, rVal); Globals.entityValueTrackingService.attachTreeToEntity(parent, BehaviorTreeIdEnums.BTREE_SERVERBLOCKTREE_ID); return rVal; } /** *Automatically generated
** Detatches this tree from the entity. *
* @param entity The entity to detach to * @param tree The behavior tree to detach */ public static void detachTree(Entity entity, BehaviorTree tree){ Globals.entityValueTrackingService.detatchTreeFromEntity(entity, BehaviorTreeIdEnums.BTREE_SERVERBLOCKTREE_ID); } /** ** Gets the ServerBlockTree of the entity *
* @param entity the entity * @return The ServerBlockTree */ public static ServerBlockTree getServerBlockTree(Entity entity){ return (ServerBlockTree)entity.getData(EntityDataStrings.TREE_SERVERBLOCKTREE); } /** *Automatically generated
** Gets currentBlockVariant. *
*/ public String getCurrentBlockVariant(){ return currentBlockVariant; } /** *Automatically generated
** Sets currentBlockVariant and handles the synchronization logic for it. *
* @param currentBlockVariant The value to set currentBlockVariant to. */ public void setCurrentBlockVariant(String currentBlockVariant){ this.currentBlockVariant = currentBlockVariant; DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientStringStateMessage(parent.getId(), BehaviorTreeIdEnums.BTREE_SERVERBLOCKTREE_ID, FieldIdEnums.TREE_SERVERBLOCKTREE_SYNCEDFIELD_CURRENTBLOCKVARIANT_ID, currentBlockVariant)); } }