Renderer/src/main/java/electrosphere/entity/state/attack/ClientAttackTree.java
austin 8484fad7da
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit
animation flickering bugfixes
2024-07-26 20:59:59 -04:00

587 lines
21 KiB
Java

package electrosphere.entity.state.attack;
import electrosphere.net.synchronization.BehaviorTreeIdEnums;
import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.btree.BehaviorTree;
import electrosphere.entity.state.AnimationPriorities;
import electrosphere.entity.state.client.firstPerson.FirstPersonTree;
import electrosphere.entity.state.collidable.Impulse;
import electrosphere.entity.state.equip.ClientEquipState;
import electrosphere.entity.state.hitbox.HitboxCollectionState;
import electrosphere.entity.state.rotator.RotatorTree;
import electrosphere.entity.types.attach.AttachUtils;
import electrosphere.entity.types.collision.CollisionObjUtils;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.game.data.creature.type.attack.AttackMove;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.net.synchronization.annotation.SyncedField;
import electrosphere.net.synchronization.annotation.SynchronizableEnum;
import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree;
import electrosphere.renderer.actor.Actor;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.joml.Vector3d;
@SynchronizedBehaviorTree(name = "clientAttackTree", isServer = false, correspondingTree="serverAttackTree")
/**
* Client basic attack tree
*/
public class ClientAttackTree implements BehaviorTree {
@SynchronizableEnum
/**
* States available to the attack tree
*/
public static enum AttackTreeState {
WINDUP,
HOLD,
ATTACK,
COOLDOWN,
IDLE,
}
@SynchronizableEnum
/**
* The state of drifting forward during the attack
*/
public static enum AttackTreeDriftState {
DRIFT,
NO_DRIFT,
}
//the current state of the tree
@SyncedField
AttackTreeState state;
//the current state of drifting caused by the tree
@SyncedField
AttackTreeDriftState driftState;
//the parent entity of this attack tree
Entity parent;
//the queue of network messages to process
CopyOnWriteArrayList<EntityMessage> networkMessageQueue = new CopyOnWriteArrayList<EntityMessage>();
//the last time this tree was updated by server
long lastUpdateTime = 0;
//the current frame of the current animation/move
float frameCurrent;
//the name of the current animation
String animationName = "SwingWeapon";
//the max frame
int maxFrame = 60;
List<AttackMove> currentMoveset = null;
AttackMove currentMove = null;
@SyncedField
String currentMoveId = null;
Entity currentWeapon = null;
boolean currentMoveHasWindup;
boolean currentMoveCanHold;
boolean stillHold = true;
boolean firesProjectile = false;
String projectileToFire = null;
String attackingPoint = null;
public ClientAttackTree(Entity e){
setState(AttackTreeState.IDLE);
setDriftState(AttackTreeDriftState.NO_DRIFT);
parent = e;
}
/**
* <p> Automatically generated </p>
* <p>
* Gets state.
* </p>
*/
public AttackTreeState getState(){
return state;
}
/**
* Starts an attack
*/
public void start(){
currentMoveCanHold = false;
currentMoveHasWindup = false;
stillHold = true;
firesProjectile = false;
projectileToFire = null;
currentWeapon = null;
attackingPoint = null;
//figure out attack type we should be doing
String attackType = getAttackType();
//if we can attack, setup doing so
if(canAttack(attackType)){
setAttackMoveTypeActive(attackType);
currentMoveset = getMoveset(attackType);
if(currentMoveset != null){
Globals.clientConnection.queueOutgoingMessage(EntityMessage.constructstartAttackMessage());
}
}
}
public void release(){
stillHold = false;
}
public void interrupt(){
setState(AttackTreeState.IDLE);
}
public void slowdown(){
setState(AttackTreeState.COOLDOWN);
}
@Override
public void simulate(float deltaTime){
frameCurrent = frameCurrent + (float)Globals.timekeeper.getDeltaFrames();
Actor entityActor = EntityUtils.getActor(parent);
Vector3d movementVector = CreatureUtils.getFacingVector(parent);
//
//synchronize move from server
if(this.currentMoveset == null){
this.currentMoveset = getMoveset(getAttackType());
}
if(
this.currentMoveset != null &&
(this.currentMove == null && this.currentMoveId != null)
||
(this.currentMove != null && this.currentMove.getAttackMoveId() != this.currentMoveId)
){
for(AttackMove move : currentMoveset){
if(move.getAttackMoveId().equals(currentMoveId)){
currentMove = move;
}
}
}
//
//parse attached network messages
for(EntityMessage message : networkMessageQueue){
networkMessageQueue.remove(message);
// System.out.println("MOVE to " + message.getX() + " " + message.getY() + " " + message.getZ());
long updateTime = message.gettime();
switch(message.getMessageSubtype()){
case ATTACKUPDATE:
if(updateTime > lastUpdateTime){
lastUpdateTime = updateTime;
switch(message.gettreeState()){
case 0:
setState(AttackTreeState.WINDUP);
frameCurrent = 0;
// System.out.println("Set state STARTUP");
break;
case 1:
frameCurrent = currentMove.getWindupFrames()+1;
setState(AttackTreeState.ATTACK);
// System.out.println("Set state MOVE");
break;
case 2:
frameCurrent = currentMove.getWindupFrames()+currentMove.getAttackFrames()+1;
setState(AttackTreeState.COOLDOWN);
// System.out.println("Set state SLOWDOWN");
break;
case 3:
frameCurrent = 60;
setState(AttackTreeState.IDLE);
// System.out.println("Set state IDLE");
break;
}
}
EntityUtils.getPosition(parent).set(message.getpositionX(),message.getpositionY(),message.getpositionZ());
CreatureUtils.setFacingVector(parent, new Vector3d(message.getrotationX(),message.getrotationY(),message.getrotationZ()));
break;
default:
//silently ignore
break;
}
}
//handle the drifting if we're supposed to currently
switch(driftState){
case DRIFT:
if(currentMove != null){
//calculate the vector of movement
CollisionObjUtils.getCollidable(parent).addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), currentMove.getDriftGoal() * Globals.timekeeper.getSimFrameTime(), "movement"));
if(frameCurrent > currentMove.getDriftFrameEnd()){
setDriftState(AttackTreeDriftState.NO_DRIFT);
}
}
break;
case NO_DRIFT:
if(currentMove != null){
if(frameCurrent > currentMove.getDriftFrameStart() && frameCurrent < currentMove.getDriftFrameEnd()){
setDriftState(AttackTreeDriftState.DRIFT);
}
}
break;
}
//state machine
switch(state){
case WINDUP: {
if(parent.containsKey(EntityDataStrings.CLIENT_ROTATOR_TREE)){
RotatorTree.getClientRotatorTree(parent).setActive(true);
}
if(currentMove != null){
if(entityActor != null){
if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(currentMove.getWindupAnimationName())){
entityActor.playAnimation(currentMove.getWindupAnimationName(),1);
entityActor.incrementAnimationTime(0.0001);
}
FirstPersonTree.conditionallyPlayAnimation(Globals.firstPersonEntity, currentMove.getAnimationFirstPersonWindup().getName(), AnimationPriorities.ATTACK);
}
}
} break;
case HOLD: {
if(entityActor != null){
if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(currentMove.getHoldAnimationName())){
entityActor.playAnimation(currentMove.getHoldAnimationName(),1);
entityActor.incrementAnimationTime(0.0001);
}
FirstPersonTree.conditionallyPlayAnimation(Globals.firstPersonEntity, currentMove.getAnimationFirstPersonHold().getName(), AnimationPriorities.ATTACK);
}
} break;
case ATTACK: {
if(entityActor != null && currentMove != null){
if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(currentMove.getAttackAnimationName())){
entityActor.playAnimation(currentMove.getAttackAnimationName(),1);
entityActor.incrementAnimationTime(0.0001);
}
FirstPersonTree.conditionallyPlayAnimation(Globals.firstPersonEntity, currentMove.getAnimationFirstPersonAttack().getName(), AnimationPriorities.ATTACK);
}
//activate hitboxes
List<Entity> attachedEntities = AttachUtils.getChildrenList(parent);
if(attachedEntities != null){
for(Entity currentAttached : attachedEntities){
if(HitboxCollectionState.hasHitboxState(currentAttached)){
HitboxCollectionState currentState = HitboxCollectionState.getHitboxState(currentAttached);
currentState.setActive(true);
}
}
}
} break;
case COOLDOWN: {
//deactive hitboxes
List<Entity> attachedEntities = AttachUtils.getChildrenList(parent);
if(attachedEntities != null){
for(Entity currentAttached : attachedEntities){
if(HitboxCollectionState.hasHitboxState(currentAttached)){
HitboxCollectionState currentState = HitboxCollectionState.getHitboxState(currentAttached);
currentState.setActive(false);
}
}
}
if(currentMove != null && frameCurrent > currentMove.getWindupFrames() + currentMove.getAttackFrames() + currentMove.getCooldownFrames()){
frameCurrent = 0;
if(parent.containsKey(EntityDataStrings.CLIENT_ROTATOR_TREE)){
RotatorTree.getClientRotatorTree(parent).setActive(false);
}
}
} break;
case IDLE: {
currentMove = null;
currentMoveset = null;
} break;
}
}
public void addNetworkMessage(EntityMessage networkMessage) {
networkMessageQueue.add(networkMessage);
}
/**
* Gets the current attack type
* @return The current attack type
*/
String getAttackType(){
String rVal = null;
if(ClientEquipState.hasEquipState(parent)){
ClientEquipState equipState = ClientEquipState.getEquipState(parent);
for(String point : equipState.getEquippedPoints()){
Entity item = equipState.getEquippedItemAtPoint(point);
if(ItemUtils.isWeapon(item)){
attackingPoint = point;
currentWeapon = item;
switch(ItemUtils.getWeaponClass(item)){
case "sword1h":
rVal = EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND;
break;
case "sword2h":
rVal = EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_TWO_HAND;
break;
case "bow2h":
rVal = EntityDataStrings.ATTACK_MOVE_TYPE_BOW_TWO_HAND;
break;
}
}
}
}
return rVal;
}
boolean canAttack(String attackType){
boolean rVal = true;
if(attackType == null){
return false;
} else if(state != AttackTreeState.IDLE){
//checks if we have a next move and if we're in the specified range of frames when we're allowed to chain into it
if(
currentMove != null &&
currentMove.getNextMoveId() != null &&
!currentMove.getNextMoveId().equals("") &&
frameCurrent >= currentMove.getMoveChainWindowStart() && frameCurrent <= currentMove.getMoveChainWindowEnd()
){
rVal = true;
}
} else {
if(ClientEquipState.hasEquipState(parent)){
// ClientEquipState equipState = ClientEquipState.getEquipState(parent);
// if(equipState.hasEquipPrimary()){
// switch(attackType){
// case EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND:
// break;
// default:
// rVal = false;
// break;
// }
// } else {
// switch(attackType){
// case EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND:
// rVal = false;
// break;
// default:
// rVal = false;
// break;
// }
// }
}
}
return rVal;
}
/**
* Gets the object for next move in the current attack chain
* @param moveset The moveset to search
* @param nextMoveId The id of the next move
* @return The object that corresponds to the id if it exists, otherwise false
*/
AttackMove getNextMove(List<AttackMove> moveset, String nextMoveId){
AttackMove rVal = null;
for(AttackMove move : moveset){
if(move.getAttackMoveId().equals(nextMoveId)){
rVal = move;
break;
}
}
return rVal;
}
/**
* Sets the current attack type of the entity
* @param attackType the current attack type
*/
public void setAttackMoveTypeActive(String attackType){
parent.putData(EntityDataStrings.ATTACK_MOVE_TYPE_ACTIVE, attackType);
}
/**
* Gets the current moveset
* @param attackType the attack type
* @return The moveset if it exists
*/
public List<AttackMove> getMoveset(String attackType){
return (List<AttackMove>)parent.getData(attackType);
}
/**
* <p> Automatically generated </p>
* <p>
* Sets state and handles the synchronization logic for it.
* </p>
* @param state The value to set state to.
*/
public void setState(AttackTreeState state){
this.state = state;
}
/**
* <p> Automatically generated </p>
* <p>
* Gets driftState.
* </p>
*/
public AttackTreeDriftState getDriftState(){
return driftState;
}
/**
* <p> Automatically generated </p>
* <p>
* Sets driftState and handles the synchronization logic for it.
* </p>
* @param driftState The value to set driftState to.
*/
public void setDriftState(AttackTreeDriftState driftState){
this.driftState = driftState;
}
/**
* <p> (initially) Automatically generated </p>
* <p> More parameters can be safely added to this method</p>
* <p>
* Attaches this tree to the entity.
* </p>
* @param entity The entity to attach to
* @param tree The behavior tree to attach
*/
public static ClientAttackTree attachTree(Entity parent){
ClientAttackTree rVal = new ClientAttackTree(parent);
//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
parent.putData(EntityDataStrings.TREE_CLIENTATTACKTREE, rVal);
Globals.clientScene.registerBehaviorTree(rVal);
Globals.entityValueTrackingService.attachTreeToEntity(parent, BehaviorTreeIdEnums.BTREE_CLIENTATTACKTREE_ID);
return rVal;
}
/**
* <p> Automatically generated </p>
* <p>
* Detatches this tree from the entity.
* </p>
* @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_CLIENTATTACKTREE_ID);
}
/**
* <p>
* Gets the ClientAttackTree of the entity
* </p>
* @param entity the entity
* @return The ClientAttackTree
*/
public static ClientAttackTree getClientAttackTree(Entity entity){
return (ClientAttackTree)entity.getData(EntityDataStrings.TREE_CLIENTATTACKTREE);
}
/**
* <p> Automatically generated </p>
* <p>
* Converts this enum type to an equivalent short value
* </p>
* @param enumVal The enum value
* @return The short value
*/
public static short getAttackTreeStateEnumAsShort(AttackTreeState enumVal){
switch(enumVal){
case WINDUP:
return 0;
case HOLD:
return 1;
case ATTACK:
return 2;
case COOLDOWN:
return 3;
case IDLE:
return 4;
default:
return 0;
}
}
/**
* <p> Automatically generated </p>
* <p>
* Converts a short to the equivalent enum value
* </p>
* @param shortVal The short value
* @return The enum value
*/
public static AttackTreeState getAttackTreeStateShortAsEnum(short shortVal){
switch(shortVal){
case 0:
return AttackTreeState.WINDUP;
case 1:
return AttackTreeState.HOLD;
case 2:
return AttackTreeState.ATTACK;
case 3:
return AttackTreeState.COOLDOWN;
case 4:
return AttackTreeState.IDLE;
default:
return AttackTreeState.WINDUP;
}
}
/**
* <p> Automatically generated </p>
* <p>
* Converts this enum type to an equivalent short value
* </p>
* @param enumVal The enum value
* @return The short value
*/
public static short getAttackTreeDriftStateEnumAsShort(AttackTreeDriftState enumVal){
switch(enumVal){
case DRIFT:
return 0;
case NO_DRIFT:
return 1;
default:
return 0;
}
}
/**
* <p> Automatically generated </p>
* <p>
* Converts a short to the equivalent enum value
* </p>
* @param shortVal The short value
* @return The enum value
*/
public static AttackTreeDriftState getAttackTreeDriftStateShortAsEnum(short shortVal){
switch(shortVal){
case 0:
return AttackTreeDriftState.DRIFT;
case 1:
return AttackTreeDriftState.NO_DRIFT;
default:
return AttackTreeDriftState.DRIFT;
}
}
/**
* <p> Automatically generated </p>
* <p>
* Gets currentMoveId.
* </p>
*/
public String getCurrentMoveId(){
return currentMoveId;
}
/**
* <p> Automatically generated </p>
* <p>
* Sets currentMoveId and handles the synchronization logic for it.
* </p>
* @param currentMoveId The value to set currentMoveId to.
*/
public void setCurrentMoveId(String currentMoveId){
this.currentMoveId = currentMoveId;
}
}