Skip to content

Commit

Permalink
大部分完成,剩余细节需耐心完善
Browse files Browse the repository at this point in the history
  • Loading branch information
byxiaobai committed Aug 19, 2020
1 parent 403fd2d commit 28dda12
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void init() {
this.addExecutor(1,(PlayerCommandExecutor)(sender,args)->{
int id=Integer.parseInt(args[0]);
AIZombie aiZombie=ZombieManager.INSTANCE.getZombieByEntityID(id);
aiZombie.getBukkitEntity().setVelocity(new Vector(0.1,0,0.1));
aiZombie.getZombieNavigation().gotoPathPoint(new FocessPathPoint(0,sender.getLocation()));
sender.sendMessage("id:"+aiZombie.getEntityID());
},"move");//移动僵尸 bai move ID
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/focess/betterai/listener/EntityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
import com.focess.pathfinder.core.entity.NMSFocessEntity;
import com.focess.pathfinder.entity.EntityManager;
import com.focess.pathfinder.goal.FocessGoalItem;
import com.focess.pathfinder.goal.GoalItem;
import com.focess.pathfinder.goal.GoalSelector;
import com.focess.pathfinder.goal.WrappedGoal;
import com.focess.pathfinder.goals.Goals;
import com.focess.pathfinder.wrapped.WrappedEntityCreature;
import com.focess.pathfinder.wrapped.WrappedEntityInsentient;

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

import org.bukkit.entity.Creature;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
Expand All @@ -29,6 +35,14 @@ public void onEntitySpawn(CreatureSpawnEvent event) {
event.getEntity().setCanPickupItems(true);
NMSFocessEntity entity = EntityManager.getFocessEntity(event.getEntity());
GoalSelector goalSelector = entity.getGoalSelector();
Set<GoalItem> items= goalSelector.getGoalItems();
List<GoalItem> goals=new ArrayList<>();
for(GoalItem item:items) {
goals.add(item);
}
for(GoalItem goal:goals) {
goalSelector.removeGoal(goal);
}
// for (WrappedGoal wrappedGoal:goalSelector.getGoals())
// if (wrappedGoal.getPriority() == 2 && wrappedGoal.getGoalItems().contains(Goals.TARGET.NEAREST_ATTACKABLE_TARGET))
// goalSelector.removeExactGoal(wrappedGoal);
Expand All @@ -39,8 +53,8 @@ public void onEntitySpawn(CreatureSpawnEvent event) {
.build(0, false));

//躲避太阳
goalSelector.addGoal(Goals.MOVE.FLEE_SUN.clear().writeEntityCreature(WrappedEntityCreature.getWrappedEntityCreature((Creature) event.getEntity())).writeDouble(1.0).build(3, false));
goalSelector.addGoal(Goals.RESTRICT_SUN.clear().writeEntityCreature(WrappedEntityCreature.getWrappedEntityCreature((Creature) event.getEntity())).build(2, false));
//goalSelector.addGoal(Goals.MOVE.FLEE_SUN.clear().writeEntityCreature(WrappedEntityCreature.getWrappedEntityCreature((Creature) event.getEntity())).writeDouble(1.0).build(3, false));
//goalSelector.addGoal(Goals.RESTRICT_SUN.clear().writeEntityCreature(WrappedEntityCreature.getWrappedEntityCreature((Creature) event.getEntity())).build(2, false));

//破坏方块
//goalSelector.addGoal(new FocessGoalItem(new ZombieBlockGoal((Zombie) event.getEntity())).build(1,false));
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/focess/betterai/navigation/PlayerPathPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.focess.betterai.navigation;

import org.bukkit.Location;
import org.bukkit.entity.Player;

import com.focess.pathfinder.core.navigation.focess.FocessPathPoint;

public class PlayerPathPoint extends FocessPathPoint{

private Player player;

public PlayerPathPoint(Player player,int index, Location loc) {
super(index, loc);
this.player=player;
}

public String getPlayerName() {
return player.getName().toLowerCase();
}

public Player getPlayer() {
return player;
}

public void setPlayer(Player player) {
this.player = player;
}

}
68 changes: 58 additions & 10 deletions src/main/java/com/focess/betterai/navigation/ZombieNavigation.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.focess.pathfinder.core.navigation.focess.FocessPathPoint;
import com.focess.pathfinder.navigation.BasicPath;
import com.focess.pathfinder.navigation.PathPoint;
import com.toolapi.handler.Handler;
import com.toolapi.handler.HandlerManager;
import com.toolapi.utils.MathUtil;
import com.toolapi.utils.TestUtil;

import pers.blockdurability.data.DurabilityManager;
Expand Down Expand Up @@ -54,8 +57,34 @@ public void gotoPathPoint(FocessPathPoint pathPoint) {
Location loc=getBlockLocation();
Location pLoc=pathPoint.getLocation();
Zombie zombie=aiZombie.getBukkitEntity();
if(Math.abs(loc.getY()-pLoc.getY())<1) {
zombie.getPathfinder().moveTo(pathPoint.getLocation());
if(Math.abs(loc.getY()-pLoc.getY())<1) {//y坐标相同
Location newLoc = null;
Location blockLoc;
int ran=MathUtil.getRandomNumber(1, 2);
if(loc.getBlockX()==pLoc.getBlockX()&&loc.getBlockZ()!=pLoc.getBlockZ()) {
ran=1;
}else if(loc.getBlockZ()==pLoc.getBlockZ()&&loc.getBlockX()!=pLoc.getBlockX()) {
ran=2;
}
if(ran==1) {
newLoc=new Location(loc.getWorld(),loc.getBlockX(),loc.getBlockY(),
loc.getBlockZ()<pLoc.getBlockZ()?loc.getBlockZ()+1:loc.getBlockZ()-1);
}else {
newLoc=new Location(loc.getWorld(),
loc.getBlockX()<pLoc.getBlockX()?loc.getBlockX()+1:loc.getBlockX()-1
,loc.getBlockY(),loc.getBlockZ());
}

blockLoc=newLoc.clone();
blockLoc.setY(blockLoc.getY()-1);
blockLoc.getBlock().setType(Material.STONE);

zombie.getPathfinder().moveTo(newLoc);





}else {//y坐标不同
//System.out.println("else");
if(loc.getY()>pLoc.getY()) {//向下走
Expand All @@ -64,11 +93,19 @@ public void gotoPathPoint(FocessPathPoint pathPoint) {
dur++;
if(dur==10){
unBlock.setType(Material.AIR);
this.aiZombie.getBukkitEntity().teleport(unBlock.getLocation());
DurabilityManager.INSTANCE.setBlockDurability(unBlock, -1);
}else
DurabilityManager.INSTANCE.setBlockDurability(unBlock, dur);
}else if(loc.getY()<pLoc.getY()) {//向上走
zombie.setVelocity(new Vector(0,1,0));
}else if(loc.getY()+0.8<pLoc.getY()) {//向上走
zombie.setVelocity(new Vector(0,0.2,0));
HandlerManager.INSTANCE.addHanlder(new Handler(){
@Override
public void handle() throws Exception {
//System.out.println("STONE");
loc.getBlock().setType(Material.STONE);
}
});
}
//DurabilityManager.INSTANCE.setBlockDurability(block, dur);
}
Expand Down Expand Up @@ -102,25 +139,36 @@ private Location getBlockLocation() {

@Override
public void recalculatePath() {
BasicPath path=this.getCurrentPath();
ZombiePath path=this.getCurrentPath();
List<FocessPathPoint> points=path.getPathPoints();
points.clear();
Player player=ZombieUtil.findPlayer(aiZombie);
if(player==null)return;
Location pLoc=player.getLocation();
points.add(new FocessPathPoint(1,pLoc));//编号为1
points.add(new PlayerPathPoint(player,1,pLoc));//编号为1
}


@Override
public void timer() {
if(!this.aiZombie.isAlive()) {
NavigationManager.INSTANCE.unregisterFocessNavigation(this);
return;
}
FocessPathPoint point=this.getCurrentPath().getNowPathPoint();
if(point!=null) {
TestUtil.test("id:"+aiZombie.getEntityID()+",GO:"+point.getLocation());
}else {
TestUtil.test("id:"+aiZombie.getEntityID()+",GO:null");
if(point==null){//无对象
//TODO 一段时间后移除
}
this.gotoPathPoint(point);
}


@Override
public boolean equals(Object ano) {
if(ano==null||!(ano instanceof ZombieNavigation))return false;
ZombieNavigation zb=(ZombieNavigation)ano;
if(zb.getAIZombie().equals(this.getAIZombie()))return true;
return false;
}

}
17 changes: 15 additions & 2 deletions src/main/java/com/focess/betterai/navigation/ZombiePath.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.focess.betterai.navigation;

import org.bukkit.Location;
import org.bukkit.World;

import com.focess.pathfinder.core.navigation.focess.FocessPath;
import com.focess.pathfinder.core.navigation.focess.FocessPathPoint;
import com.toolapi.utils.WorldUtil;

public class ZombiePath extends FocessPath{
private ZombieNavigation zombieNavigation;
Expand All @@ -11,8 +15,8 @@ public ZombiePath(ZombieNavigation zombieNavigation) {
}

@Override
public FocessPathPoint getNowPathPoint() {
return this.getPathPoint(1);
public PlayerPathPoint getNowPathPoint() {
return (PlayerPathPoint) this.getPathPoint(1);
}

public ZombieNavigation getZombieNavigation() {
Expand All @@ -22,6 +26,15 @@ public ZombieNavigation getZombieNavigation() {
public void setZombieNavigation(ZombieNavigation zombieNavigation) {
this.zombieNavigation = zombieNavigation;
}

@Override
public boolean isPathAlive() {
PlayerPathPoint playerPoint=getNowPathPoint();
if(playerPoint==null)return false;
Location loc=playerPoint.getLocation();
if(WorldUtil.hasPlayerInDistance(loc, 5, playerPoint.getPlayerName()))return true;
return false;
}


}
22 changes: 21 additions & 1 deletion src/main/java/com/focess/betterai/utils/ZombieUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.focess.betterai.utils;

import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
Expand Down Expand Up @@ -52,7 +56,23 @@ public static AIZombie spawnAIZombie(Location loc) {
* @return
*/
public static Player findPlayer(AIZombie zombie) {
Player player=EntityUtil.getNearestPlayerInRange(zombie.getBukkitEntity(), range);
Entity entity=zombie.getBukkitEntity();
if(entity==null)return null;
Location eLoc=entity.getLocation();
List<Entity> entities=entity.getNearbyEntities(range, range, range);
double pDis=Double.MAX_VALUE;
Player player=null;
for(Entity en:entities) {
if(en instanceof Player) {
Player p=(Player)en;
if(p.getGameMode()!=GameMode.SURVIVAL)continue;
double tmpDis=en.getLocation().distance(eLoc);
if(tmpDis<pDis) {
player=p;
pDis=tmpDis;
}
}
}
return player;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/focess/betterai/zombie/AIZombie.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@ public void setFindPlayerRange(double findPlayerRange) {
this.findPlayerRange = findPlayerRange;
}

public boolean equals(Object ano) {
if(ano==null||!(ano instanceof AIZombie))return false;
AIZombie ai=(AIZombie)ano;
if(ai.getBukkitEntity()==null) {
if(this.getBukkitEntity()==null)return true;
else return false;
}
if(ai.getBukkitEntity().equals(this.getBukkitEntity()))return true;
return false;
}

}

0 comments on commit 28dda12

Please sign in to comment.