-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
275 additions
and
47 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/com/wenxin2/marioverse/entities/ai/goals/PiranhaPlantHideInBlockGoal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.wenxin2.marioverse.entities.ai.goals; | ||
|
||
import com.wenxin2.marioverse.entities.PiranhaPlantEntity; | ||
import com.wenxin2.marioverse.init.TagRegistry; | ||
import java.util.EnumSet; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.entity.ai.goal.Goal; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
|
||
public class PiranhaPlantHideInBlockGoal extends Goal { | ||
private final PiranhaPlantEntity entity; | ||
private final int hideDuration; // Ticks to remain hidden | ||
private int hideCooldown; | ||
private int hideTime; | ||
private boolean isHiding; | ||
private double targetY; | ||
|
||
public PiranhaPlantHideInBlockGoal(PiranhaPlantEntity entity, int hideDuration, int hideCooldown) { | ||
this.entity = entity; | ||
this.hideCooldown = hideCooldown; | ||
this.hideDuration = hideDuration; | ||
this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK)); | ||
this.isHiding = false; | ||
} | ||
|
||
@Override | ||
public boolean canUse() { | ||
if (hideCooldown > 0) { | ||
--hideCooldown; | ||
return false; | ||
} | ||
|
||
BlockPos posBelow = this.entity.blockPosition().below(); | ||
BlockState blockBelow = this.entity.level().getBlockState(posBelow); | ||
return blockBelow.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
hideTime = 0; | ||
hideCooldown = hideDuration; | ||
BlockPos posBelow = entity.blockPosition().below(); | ||
targetY = posBelow.getY(); | ||
isHiding = false; | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
double currentY = entity.getY(); | ||
double speed = 0.1; | ||
|
||
if (!isHiding) { | ||
// Move down to hide | ||
if (currentY > targetY + speed) { | ||
entity.setDeltaMovement(0, -speed, 0); | ||
entity.tryToHide(); | ||
} else { | ||
entity.setDeltaMovement(0, 0, 0); | ||
entity.setPos(entity.getX(), targetY, entity.getZ()); | ||
entity.tryToHide(); // Activate hiding | ||
isHiding = true; | ||
} | ||
} else { | ||
// Stay hidden for `hideDuration` ticks | ||
hideTime++; | ||
if (hideTime >= hideDuration) { | ||
// Start rising after hiding duration | ||
targetY = entity.getY() + 1.0; // Move up by 1 block | ||
if (currentY < targetY - speed) { | ||
entity.setDeltaMovement(0, speed, 0); | ||
} else { | ||
entity.setDeltaMovement(0, 0, 0); | ||
entity.setPos(entity.getX(), targetY, entity.getZ()); | ||
entity.stopHiding(); // Exit hiding state | ||
isHiding = false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean canContinueToUse() { | ||
return hideCooldown > 0 || isHiding; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.