您的位置:首页 > 大数据 > 人工智能

NPC简单AI处理

2010-09-13 14:15 323 查看
以前曾做过一个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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: