我正在尝试编写一些东西,其中有生物在舞台上来回奔跑,上下奔跑,而我作为玩家,必须尝试接近它们,并捡起它们。舞台上也有界限——
- 地图约束——一个大的矩形框很容易完成。我已经做到了。
- 地图内的边界也是矩形,但不是将玩家弹回矩形内,而是尝试做相反的事情——让玩家远离它。
我的代码现在看起来像这样:
//Conditions that check if player/monsters are hittesting the boxes (rocks
//and stuff), then if correct, bounce them away. Following code excludes
//the monsters for simplicity.
if((mcPlayer.x - aBounceBox[b].x) < 0 && mcPlayer.y <= (aBounceBox[b].y + aBounceBox[b].height/2) && mcPlayer.y >= (aBounceBox[b].y - aBounceBox[b].height/2))
{
mcPlayer.x = aBounceBox[b].x - aBounceBox[b].width/2 - mcPlayer.width/2;
}
//Duplicate above code for right side of box here
if((mcPlayer.y - (aBounceBox[b].y + aBounceBox[b].height/2)) < 0 && (mcPlayer.x + mcPlayer.width/2) > (aBounceBox[b].x - aBounceBox[b].width/2) && (mcPlayer.x - mcPlayer.width/2) < (aBounceBox[b].x + aBounceBox[b].width/2))
{
mcPlayer.y = aBounceBox[b].y + aBounceBox[b].height/2;
}
//Duplicate above code for Upper boundary of box here
上面的代码效果不太好,因为在盒子的左右两侧反弹的代码与我正在测试的盒子的上下部分冲突。任何想法如何顺利地做到这一点?
另外,我遇到的另一个问题是游戏中怪物的路径。我试图让他们做以下事情:
- “有机地”四处走动,或者随机地移动一点——移动一点,停止。如果他们遇到边界,他们会停下来移动到别处。不关心去哪里,只要他们停止移动到岩石和树木中,诸如此类的事情。
- 尽量不要重叠,尽量在舞台上走动。
- 如果它们重叠,则将彼此推开,尽管我想让它们稍微重叠。
我正在缓慢地构建该代码,但我想我只想问是否有人对如何做到这一点有任何想法。