这一切都在 STEP 事件中。
我目前正在尝试为某些敌方 AI 创建我的第一个状态机。作为 gml 和 gamemaker studio 2 的新手,我的代码非常基础,因为我不知道如何实现内置函数。
在我的状态之一,匆忙状态,敌人应该追逐玩家。我通过创造一些条件来做到这一点,如果玩家在敌人的左边,敌人就会向左跑。如果玩家向右,敌人就会向右跑。这在理论上是我编写的代码,但是当我进入游戏时,有时它会起作用,但随后会痉挛并走另一条路。
如果我的角色在圆圈内,它不会跑向玩家而是跑开。如果我颠倒条件,这不会改变。
case behaviour.rush:
{
//radius in square
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
//direction to face player
if (behaviourState == behaviour.rush && playerObject.x >. warriorx) hsp = -4;
else if (behaviourState == behaviour.rush &&
playerObject.x <= warriorx) hsp = 4;
x = x + hsp;
}
if (!point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.idle;
}
}
我的完整代码:
image_speed = 1;
vsp = vsp+grv;
Print(behaviourState);
if (hsp > 0) image_xscale = 3; else if (hsp < 0) image_xscale = -3;
//animation
if (behaviourState == behaviour.idle) sprite_index = tikiAxeWarriorIdle;
if (hsp == 2 || hsp == -2)sprite_index = tikiAxeWarriorWalk;
if (hsp == 4 || hsp == -4) sprite_index = tikiAxeWarriorRush;
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
Print("in circle");
}
Print(hsp);
switch(behaviourState)
{
case behaviour.idle:
{
//stand still
if (alarm[1] <= 0) alarm[1] = room_speed * 4;
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.rush;
}
}
case behaviour.wander:
{
if (alarm[0] <= 0) alarm[0] = room_speed * 3;
//move
if (!place_meeting(x + hsp,y,wallObject))
{
x = x + hsp;
}
if (place_meeting(x + hsp,y,wallObject))//checking frame before.
{
hsp = -hsp;
}
//check for behaviour.rush.
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.rush;
}
}
case behaviour.rush:
{
//radius in square
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
//direction to face player
if (behaviourState == behaviour.rush && playerObject.x > warriorx) hsp = -4;
else if (behaviourState == behaviour.rush && playerObject.x <= warriorx) hsp = 4;
x = x + hsp;
}
if (!point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.idle;
}
}
case behaviour.attack:
{
//attack.
//if player is hit, he dies.
}
}
预期的结果是敌人“冲”向我的玩家位置,当超过时,面向另一个方向。如果我的玩家离开圈子,它应该回到空闲状态。