Skip to content

Commit

Permalink
fixed bug in getJumpPos where yLen was NaN because a negative number …
Browse files Browse the repository at this point in the history
…was being put into the sqrt function. jump is now fully implemented
  • Loading branch information
gregchan550 committed Sep 19, 2023
1 parent 83a35ca commit 4d2ac04
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public void create() {
private void registerAnimationListener(String animationName) {
entity.getEvents().addListener(animationName, () -> animator.startAnimation(animationName));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class DemonBossTask extends DefaultTask implements PriorityTask {
private static final float TIME_INTERVAL = 10f; // 10 seconds
private static final int BURN_BALLS = 5;
private static final int X_LENGTH = 20; // for projectile destination calculations
private static final int JUMP_DISTANCE = 4;
private static final float JUMP_DISTANCE = 4.0f;

// Private variables
private static final Logger logger = LoggerFactory.getLogger(DemonBossTask.class);
Expand Down Expand Up @@ -82,7 +82,6 @@ public void run() {
@Override
public void update() {
animate();
System.out.println(state);
currentPos = demon.getPosition();

switch (state) {
Expand Down Expand Up @@ -173,26 +172,27 @@ private Vector2 getJumpPos() {

// generate random jump pos
float randomX = MathUtils.random(jumpMinX, jumpMaxX);
float yLen = (float) Math.sqrt(Math.pow(JUMP_DISTANCE, 2) - Math.pow(randomX, 2));
float yDown = demon.getPosition().y - yLen;
float yUp = demon.getPosition().y + yLen;
float yValue = 0;
float xValue = currentPos.x - randomX;
float xLen = (float) Math.sqrt((double) xValue * xValue);
float yLen = (float) Math.sqrt(JUMP_DISTANCE * JUMP_DISTANCE - xLen * xLen);
float yDown = currentPos.y - yLen;
float yUp = currentPos.y + yLen;
float yValue = 0f;

// check y bounds
if (yUp > 7) { yValue = yDown; }
if (yDown < 1) { yValue = yUp; }

// randomise y value selection
if (yValue == 0) {
if (yValue == 0f) {
int randomNumber = (int) (Math.random() * 100);
if (randomNumber % 2 == 0) { yValue = yUp; } else { yValue = yDown; }
}
return jumpPos = new Vector2(randomX, yValue);
jumpPos = new Vector2(randomX, yValue);
return jumpPos;
}

private boolean isAtTarget() {
System.out.println(currentPos);
System.out.println(jumpPos);
return currentPos.dst(jumpPos) <= STOP_DISTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public static Entity createDemonBoss() {
animator.addAnimation("take_hit", 0.2f, Animation.PlayMode.LOOP);
animator.addAnimation("transform", 0.2f, Animation.PlayMode.LOOP);


// AI task addition
AITaskComponent aiTaskComponent = new AITaskComponent()
.addTask(new DemonBossTask());
Expand Down

0 comments on commit 4d2ac04

Please sign in to comment.