Skip to content

Commit

Permalink
AI: Zombies are looking for and breaking torches
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Oct 9, 2024
1 parent cf5e6a6 commit 71a495f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.imesense.dynamicspawncontrol.ai.zombie;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;

import java.util.ArrayList;
import java.util.List;

/**
*
*/
public class BreakTorchTask extends EntityAIBase
{
private final EntityZombie zombie;
private BlockPos targetTorchPos;

public BreakTorchTask(EntityZombie zombie) {
this.zombie = zombie;
this.setMutexBits(3); // Устанавливаем флаги выполнения задачи (например, нельзя выполнять одновременно с атакой)
}

@Override
public boolean shouldExecute() {
// Ищем ближайший факел в радиусе 10 блоков
List<BlockPos> nearbyTorches = findNearbyTorches();
if (!nearbyTorches.isEmpty()) {
targetTorchPos = nearbyTorches.get(0);
return true;
}
return false;
}

@Override
public void startExecuting() {
if (targetTorchPos != null) {
// Зомби начинает движение к факелу
this.zombie.getNavigator().tryMoveToXYZ(targetTorchPos.getX(), targetTorchPos.getY(), targetTorchPos.getZ(), 1.0);
}
}

@Override
public boolean shouldContinueExecuting() {
// Продолжает выполнять, пока не достигнет факела
return !zombie.getNavigator().noPath() && targetTorchPos != null;
}

@Override
public void updateTask() {
// Проверяем, что зомби рядом с факелом, и если да, ломаем его
if (targetTorchPos != null && zombie.getDistanceSqToCenter(targetTorchPos) < 2.0) {
IBlockState blockState = zombie.world.getBlockState(targetTorchPos);
if (blockState.getBlock() == Blocks.TORCH) {
zombie.world.destroyBlock(targetTorchPos, false); // Ломаем факел
targetTorchPos = null; // Сбрасываем цель
}
}
}

private List<BlockPos> findNearbyTorches() {
// Поиск факелов в радиусе 10 блоков
BlockPos zombiePos = new BlockPos(zombie);
List<BlockPos> torches = new ArrayList<>();
for (BlockPos pos : BlockPos.getAllInBox(zombiePos.add(-10, -10, -10), zombiePos.add(10, 10, 10))) {
if (zombie.world.getBlockState(pos).getBlock() == Blocks.TORCH) {
torches.add(pos);
}
}
return torches;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.imesense.dynamicspawncontrol.debug.events;

import net.minecraft.entity.monster.EntityZombie;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.imesense.dynamicspawncontrol.ai.zombie.BreakTorchTask;
import org.imesense.dynamicspawncontrol.debug.CodeGenericUtils;
import org.imesense.dynamicspawncontrol.technical.customlibrary.Log;

Expand Down Expand Up @@ -30,4 +34,12 @@ public OnEventDummy()

instanceExists = true;
}

@SubscribeEvent
public static void onZombieSpawn(EntityJoinWorldEvent event) {
if (event.getEntity() instanceof EntityZombie) {
EntityZombie zombie = (EntityZombie) event.getEntity();
zombie.tasks.addTask(1, new BreakTorchTask(zombie)); // Добавляем новую задачу для зомби
}
}
}

0 comments on commit 71a495f

Please sign in to comment.