C# 在基于Unity的Roguelike中调整代码执行速度
[更新]我尝试使用下面的建议,但仍然存在一个问题,即敌人似乎同时移动,如果视线中已经有敌人,他们在第一次看到时会移动两次。我真正想做的就是在玩家视线内的敌人的行动之间有一个延迟,这样他们就不会同时移动,播放声音等等 更新后的代码为:C# 在基于Unity的Roguelike中调整代码执行速度,c#,unity3d,coroutine,C#,Unity3d,Coroutine,[更新]我尝试使用下面的建议,但仍然存在一个问题,即敌人似乎同时移动,如果视线中已经有敌人,他们在第一次看到时会移动两次。我真正想做的就是在玩家视线内的敌人的行动之间有一个延迟,这样他们就不会同时移动,播放声音等等 更新后的代码为: private IEnumerator takeAllEnemyActions() { // Cycle through all enemies and have them perform their actions if (Enemies
private IEnumerator takeAllEnemyActions()
{
// Cycle through all enemies and have them perform their actions
if (Enemies != null && Enemies.Length > 0)
{
bool foundEnemyInLoS = false;
Enemy lastEnemy = null;
foreach (GameObject enemy in Enemies)
{
// Cache the enemies script
Enemy thisEnemy = enemy.GetComponent<Enemy>();
// If it is not this enemies turn to move then move on to the next enemy
if (thisEnemy.NextMoveTick > GameManager.Instance.CurrentTick)
{
continue;
}
// Check to see is this enemy is the first enemy in the players line of sight (If there is one)
if (!foundEnemyInLoS && thisEnemy.isInLineOfSightOfPlayer())
{
// If so then we save it for last to ensure that if any enemy is called with a delay that the last one does not make the player wait to move
foundEnemyInLoS = true;
lastEnemy = thisEnemy;
continue;
}
// At this point only enemies whose turn it is and who are not being saved for later can act
thisEnemy.TakeEnemyActions();
// If the enemy was in the players line of sight wait 200ms before continuing
if (thisEnemy.isInLineOfSightOfPlayer())
{
yield return new WaitForSeconds(0.5f);
}
}
// if there were enemies in the players line of sight then one was saved for last
// Take its action without a delay so the player can go next
if (foundEnemyInLoS)
{
lastEnemy.TakeEnemyActions();
}
}
private IEnumerator takeAllEnemyActions()
{
//在所有敌人中循环并让他们执行他们的行动
if(敌人!=null&&friends.Length>0)
{
bool foundEnemyInLoS=false;
敌方=空;
foreach(敌人中的游戏对象敌人)
{
//缓存敌人脚本
敌方this敌军=敌方.GetComponent();
//如果不是这样,敌人转向移动,然后移动到下一个敌人
如果(ThisForeign.NextMoveTick>GameManager.Instance.CurrentTick)
{
继续;
}
//检查此敌人是否是玩家视线中的第一个敌人(如果有)
如果(!foundEnemyInLoS&&ThisForeign.isInLineOfSightOfPlayer())
{
//如果是这样,那么我们将其保存到最后,以确保如果任何敌人被延迟呼叫,最后一个不会让玩家等待移动
foundEnemyInLoS=true;
最后的敌人=这个敌人;
继续;
}
//在这一点上,只有轮到它的敌人和没有被拯救的敌人才能行动
这个敌人。采取我的行动();
//如果敌人在玩家的视线内,等待200毫秒后继续
if(thisTowner.isInLineOfSightOfPlayer())
{
收益率返回新的WaitForSeconds(0.5f);
}
}
//如果玩家的视线中有敌人,那么最后一个敌人就被救了
//立即采取行动,以便玩家可以下一步行动
if(foundEnemyInLoS)
{
最后的敌人。采取我的行动();
}
}
原始问题:
在Unity3d 5中,在一个自上而下、2d、基于回合的roguelike游戏中,我对游戏节奏有一个问题。我使用一个非常简单的状态机来控制执行,但我希望所有在同一回合中移动的敌人都能这样做,如果他们在玩家的视线范围内,每个回合之间都有一个暂停,这样动画和声音就有时间发生而不会重叠(尤其是声音)。我唯一的问题是在不影响动画或相机移动的情况下实现实际暂停(相机可能会随着玩家的移动而滑动,并且停止)。在移动到下一个敌人之前,我只需要大约100毫秒的暂停
敌人在EnemyManager脚本的foreach循环中采取行动
private void takeAllEnemyActions()
{
// Cycle through all enemies and have them perform their actions
if (Enemies != null && Enemies.Length > 0)
{
foreach (GameObject enemy in Enemies)
if (enemy.GetComponent<Enemy>().NextMoveTick <= GameManager.Instance.CurrentTick)
enemy.GetComponent<Enemy>().TakeEnemyActions();
}
GameManager.States.NextPlayState();
}
private void takeAllEnemyActions()
{
//在所有敌人中循环并让他们执行他们的行动
if(敌人!=null&&friends.Length>0)
{
foreach(敌人中的游戏对象敌人)
if(敌方.GetComponent().NextMoveTick使用一个共同例程,并对每条语句进行生成。下面是一些修改后的代码
private IEnumerator TakeAllEnemyActions() {
// Cycle through all enemies and have them perform their actions
if (enemies != null && enemies.Length > 0) {
foreach (var enemy in enemies) {
if (enemy.GetComponent<Enemy>().NextMoveTick <= GameManager.Instance.CurrentTick) {
enemy.GetComponent<Enemy>().TakeEnemyActions();
yield return new WaitForSeconds (0.1f);
}
}
}
GameManager.States.NextPlayState();
}
好的,这是更好的工作除了(这是一个大的除了)它正在进入下一个播放状态,所以你会陷入一个循环中,敌人在行动,但他们不再等待玩家轮到他们。今晚我将进一步研究这个问题。也许我需要重新思考一下这个问题。我一直在研究这个问题,但仍然有问题。我更新了主要问题仅供参考,有一个问题[游戏开发专用SO网站](gamedev.stackexchange.com)。对于未来的问题,您可能会在那里得到更好的答案。
StartCoroutine(TakeAllEnemyActions());