NPC简单AI处理

以前曾做过一个ARPG游戏,相应用到了NPC寻路与攻击多种状态等。
一般的NPC移动时就是通过不停检测与英雄是否产生了碰撞,否则就获得英雄的坐标,再进行分X或Y的方向行走。当然这只是用于地图没有任何障碍物上。
public boolean isRamWithHero() {// 碰撞检测
  if ((x + imWidth > hero.x + 10) & (y + (imHeight >> 1) > hero.y)
    & (hero.x + hero.imWidth > x + 10)
    & (hero.y + (hero.imHeight >> 1) > y)) {
   return true;
  }
  return false;
}
public boolean isMoveX() {
  if (x - hero.x >= steplength) {// left
   direction = MS_LEFT;  
   return true;
  } else if (hero.x - x >= steplength) {// right
   direction = MS_RIGHT;  
   return true;
  }
  return false;
}
public boolean isMoveY() {
  if (hero.y - y >= steplength) {// left
   direction = MS_DOWN; 
   return true;
  } else if (y - hero.y >= steplength) {// right
   direction = MS_UP;  
   return true;
  }
  return false;
}
public void seekWalk() {// 寻路行走
  if (isRamWithHero()) {
   if (hero.RoleState == RS_ATTACK || hero.RoleState == RS_SKILL) {// 检测主角是否攻击状态  
    RoleState = RS_BEATING;
   } else {  
    RoleState = RS_ATTACK;
   }
  } else {// 处理寻路行走
   if (isMoveY) {
    if (!isMoveY()) {
     isMoveX();
    }
   } else {
    if (!isMoveX()) {
     isMoveY();
    }
   }
  }
}
当一个NPC有了寻找英雄的功能后,就要有攻击英雄的技能,但是考虑一个问题的是,只按X或Y这两个方向做移动的话,那当NPC有一定数量后,就会产生重叠的现象,而为了做得太真实一点,所以就在NPC攻击英雄时产生一个机率,让NPC由英雄的正方向走到其左边或者右边,进行围攻。
public void dealAttack() {// 处理攻击走位
  counter = (byte) MainCanvas.getRandom(0, 2);// 各有一半的机会走不同方向
  switch (direction) {
  case MS_UP:
  case MS_DOWN:
   if (counter == 0) {
    MoveDirection = MS_LEFT;
   } else if (counter == 1) {
    MoveDirection = MS_RIGHT;
   }  
   RoleState = RS_DEALMOVE;
   break;
  case MS_LEFT:
  case MS_RIGHT:
   if (counter == 0) {
    MoveDirection = MS_UP;
   } else if (counter == 1) {
    MoveDirection = MS_DOWN;
   }  
   RoleState = RS_DEALMOVE;
   break;
  }
  counter = 0;
  dealMove();
}
public void dealMove() {// 处理位置移动
  if (isRamWithHero()
    & (hero.RoleState == RS_ATTACK || hero.RoleState == RS_SKILL)) {
   setBeatingFormat();
   RoleState = RS_BEATING;
  } else {
   switch (MoveDirection) {
   case MS_UP:
    dealMoveUp();
    break;
   case MS_DOWN:
    dealMoveDown();
    break;
   case MS_LEFT:
    dealMoveLeft();
    break;
   case MS_RIGHT:
    dealMoveRight();
    break;
   }
  }
}
public void dealMoveUp() {// 走到上面
  if (y + imHeight > hero.y & y > 35 && MapY <= 30) {
   direction = MS_UP;  
  } else if (counter < 16) {
   counter++;
   if (x - hero.x >= steplength) {
    direction = MS_LEFT;   
   } else if (hero.x - x >= steplength) {
    direction = MS_RIGHT;   
   } else {
    setMoveFormat();
    RoleState = RS_WALK;
   }
  } else {
   setMoveFormat();
   RoleState = RS_WALK;
  }
}
public void dealMoveDown() {// 走到下面
  if (y < hero.y + hero.imHeight) {
   direction = MS_DOWN;  
  } else if (counter < 16) {
   counter++;
   if (x - hero.x >= steplength) {
    direction = MS_LEFT;   
   } else if (hero.x - x >= steplength) {
    direction = MS_RIGHT;   
   } else {
    setMoveFormat();
    RoleState = RS_WALK;
   }
  } else {
   setMoveFormat();
   RoleState = RS_WALK;
  }
}
public void dealMoveLeft() {// 走到左边
  if (x + imWidth > hero.x) {
   direction = MS_LEFT;  
  } else if (counter < 16) {
   counter++;
   if (y - hero.y >= steplength & y > 30 && MapY <= 30) {
    direction = MS_UP;   
   } else if (hero.y - y >= steplength) {
    direction = MS_DOWN;   
   } else {
    setMoveFormat();
    RoleState = RS_WALK;
   }
  } else {
   setMoveFormat();
   RoleState = RS_WALK;
  }
}
public void dealMoveRight() {// 走到右边
  if (x < hero.x + hero.imWidth) {
   direction = MS_RIGHT;  
  } else if (counter < 16) {
   counter++;
   if (y - hero.y >= steplength & y > 30 && MapY <= 30) {
    direction = MS_UP;   
   } else if (hero.y - y >= steplength) {
    direction = MS_DOWN;   
   } else {
    setMoveFormat();
    RoleState = RS_WALK;
   }
  } else {
   setMoveFormat();
   RoleState = RS_WALK;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值