正文

开发游戏引擎(6)

Windows移动游戏开发实战 作者:(美)Adam Dawes


 

7. 推进游戏

游戏一旦启动,就会进入一个循环,其中游戏状态将连续地进行更新,例如:所有飞船将移动,玩家的角色会改变位置;会创建新的对象,而一些已有的对象要被销毁。为了使所有这些事情都能够发生,我们将调用Advance函数(参见程序清单4-5)。

该函数不负责直接执行这些更新,而是触发这些更新的发生。我们既需要将游戏作为一个整体进行更新(这样可以检测游戏结束状态是否被触发),也需要对游戏中所有的单独对象进行更新(不论使用什么方法,都要允许每个对象可以按照要求的方式移动并且可以同其他对象进行交互)。

程序清单4-5  Advance 函数

/// <summary>

/// Virtual function to allow the game itself and all objects within

/// the game to be updated.

/// </summary>

public virtual void Advance()

{

// Update the game itself

Update();

// Update all objects that are within our object collection

foreach (CGameObjectGDIBase gameObj in _gameObjects)

{

// Ignore objects that have been flagged as terminated

if (!gameObj.Terminate)

{

// Update the object's last position

gameObj.UpdatePreviousPosition();

// Perform any update processing required upon the object

gameObj.Update();

}

}

// Now that everything is updated, render the scene

Render();

// Remove any objects that have been flagged for termination

RemoveTerminatedObjects();

// Update the Frames Per Second information

UpdateFPS();

}

这段代码为游戏本身调用了其Update函数(正如我们前面所讨论的),然后对游戏对象列表中的每个对象进行循环。对每个对象,它首先检测该对象是否已经终止;当对象从游戏中被删除,并等待被销毁时更新就会发生,所以我们不需要对这些对象做更进一步的处理。当所有对象都仍然为激活状态时,我们可以做两件事情:

● 将对象的位置复制到它上一个位置变量中。我们将在本章后文中介绍CgameObject-

Base类时对其原因进行解释。

● 调用每个对象的Update方法,使对象可以执行自己所需要的任何操作。


上一章目录下一章

Copyright © 读书网 www.dushu.com 2005-2020, All Rights Reserved.
鄂ICP备15019699号 鄂公网安备 42010302001612号