Renderer/src/main/java/electrosphere/entity/state/movement/JumpTree.java
2024-02-25 11:49:17 -05:00

103 lines
3.1 KiB
Java

package electrosphere.entity.state.movement;
import org.joml.Vector3d;
import electrosphere.collision.collidable.Collidable;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.BehaviorTree;
import electrosphere.entity.state.collidable.Impulse;
import electrosphere.entity.state.gravity.GravityUtils;
import electrosphere.entity.types.collision.CollisionObjUtils;
import electrosphere.renderer.actor.Actor;
public class JumpTree implements BehaviorTree {
public static enum JumpState {
INACTIVE,
ACTIVE,
AWAITING_LAND,
}
JumpState state = JumpState.INACTIVE;
String animationJump = "Armature|Jump";
Entity parent;
int jumpFrames = 0;
int currentFrame = 0;
float jumpForce = 10.0f;
float currentJumpForce = jumpForce;
static final float jumpFalloff = 0.99f;
public JumpTree(Entity parent, int jumpFrames, float jumpForce){
this.parent = parent;
this.jumpFrames = jumpFrames;
this.jumpForce = jumpForce;
}
public void start(){
if(state == JumpState.INACTIVE){
state = JumpState.ACTIVE;
currentFrame = 0;
currentJumpForce = jumpForce;
GravityUtils.clientAttemptActivateGravity(parent);
}
}
@Override
public void simulate(float deltaTime) {
Actor entityActor = EntityUtils.getActor(parent);
switch(state){
case ACTIVE:
if(entityActor != null){
String animationToPlay = determineCorrectAnimation();
if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay)){
entityActor.playAnimation(animationToPlay,1);
entityActor.incrementAnimationTime(0.0001);
}
}
currentFrame++;
currentJumpForce = currentJumpForce * jumpFalloff;
//push parent up
CollisionObjUtils.getCollidable(parent).addImpulse(new Impulse(new Vector3d(0,1,0), new Vector3d(0,0,0), new Vector3d(EntityUtils.getPosition(parent)), currentJumpForce, Collidable.TYPE_FORCE));
//potentially disable
if(currentFrame >= jumpFrames){
state = JumpState.AWAITING_LAND;
GravityUtils.clientAttemptActivateGravity(parent);
}
break;
case INACTIVE:
break;
case AWAITING_LAND:
break;
}
}
public static JumpTree getClientJumpTree(Entity parent){
return (JumpTree)parent.getData(EntityDataStrings.CLIENT_JUMP_TREE);
}
public void land(){
if(state != JumpState.INACTIVE && currentFrame > 2){
state = JumpState.INACTIVE;
}
}
public boolean isJumping(){
return state == JumpState.ACTIVE;
}
String determineCorrectAnimation(){
return animationJump;
}
public void setAnimationJump(String animationName){
animationJump = animationName;
}
}