青瓷引擎 - 游戏主循环

原创 2016年08月31日 15:17:27

游戏主循环

参考:游戏主循环,讲得非常清楚。

青瓷引擎调度器

青瓷引擎启动了个循环定时器来跑游戏主循环:

window.requestAnimationFrame(mainLoop);

游戏主循环的逻辑如下图所示:

Main Loop

这里我们需要先解释下两个概念:

场景(Scene)

1个场景就是一个游戏世界,或者说1个场景=1个关卡。一个场景包含了所有的游戏对象,例如角色、武器、例子系统、声音、敌人等等。在引擎编辑器中,所有的实体对象(或者说node节点)都完整的呈现在Hierarchy面板中:

Scene

更新场景

主循环会递归访问场景所有节点,伪代码如下:

foreach (nodes) {
    node.update();
    foreach (node’s components) {
        component.update();
    }
}

preUpdatepostUpdate的调度,与update类似。下面举个实际例子来加深下理解。

调度例子

  • 创建一个空的工程,名称为:MainLoop;
  • 创建一个JavaScript脚本文件:Test.js,代码如下:
var Test = qc.defineBehaviour('qc.demo.Test', qc.Behaviour, function() {
    console.log('Init');
}, {});

Test.prototype.preUpdate = function() {
    console.log(this.gameObject.name, 'call preUpdate');
};

Test.prototype.update = function() {
    console.log(this.gameObject.name, 'call update');
};

Test.prototype.postUpdate = function() {
    console.log(this.gameObject.name, 'call postUpdate');
};
  • 创建两个空的node节点,重命名为:node1和node2;
  • 将脚本Test.js挂载到node1和node2节点;
  • 运行工程,在浏览器的开发控制台查看输出:
    Call

在场景中有2个节点:node1和node2。游戏主循环将调度这两个节点及其挂载的所有组件:

Call

首先,游戏主循环调度所有nodes的preUpdate函数,然后是update,最后是postUpdate。大部分情况下,我们将游戏逻辑代码置于update中。当用户输入时,onDownonClickonUp等函数将会被主循环调用(后续再详细解释)。

附:Game Main Loop

Video games are simply ordinary software - there’s nothing intrinsically special about them. However, they SEEM to behave in a very different way from your ordinary everyday applications - so much that many experienced programmers are at a complete loss when faced with the task of developing a game from scratch. This difference is mostly caused by game’s close ties to their main loop. In this chapter, we’ll examine what is the main loop, and how it works in QICI.

A simple model

How simply could we model a computer game? Let’s start by separating the game in two parts: the data inside the computer, and everything outside the computer. This might seem like a strange thing to say, but remember that games are, first and foremost, interactive software. So, outside the computer, we have (at the very least) the player. Inside the computer we have all sorts of data, but the most important is what we call the game state: it contains all the dynamic information of the game: the position of the player, monsters; the current time; the score - in essence, everything that changes with time.

A computer game can then be understood as a system in which those two entities - player and game state - interact with each other according to a specific pattern of rules. The player inputs data into the system, and the system users the game state to generate output to display to the player. This is not very different from ordinary applications, in which e.g. a click (input) might cause a dialog to show up (output). The difference is how that behaves in respect with time.

The Game Loop

Most applications are developed in a reactive way - your program just sits around waiting for the user to click something, and then it does something in response. There is a main loop behind your application as well, but it’s not important - it’s abstracted away. With games, your code is constantly being invoked, even in the absence of user input.

The pseudo-code for a video game will often look like this:

function main() {
    init();
    runMainLoop();
    deInit();
}

The main loop looks something like this:

while (running) {
    processInput();
    updateGameState();
    drawScreen();
    waitUntilItsTime();
}

First, it reads input from the user and stores it somewhere. Here, it will check the keyboard, gamepad, plastic guitar - basically, it will collect all information from the outside world.

After that, it will use that information to update the game state - depending on what conditions we have, they will have different results. For example, on the menu screen, detecting that the “down” arrow got pressed might increment the “currently selected menu item” variable, but the same input while on the game might instead trigger the “set player as moving down” flag. Note that the world update is invoked even if the user didn’t perform any input. This is a very important property of most video games.

Lastly, it will collect the current game state data to generate the output - it will figure out where the player and everything else are, and generate an image to display on the screen. A more complicated game will also have to deal with other forms of output - audio and network, for example.

QICI Game Loop

QICI start a loop timer to run game loop:

window.requestAnimationFrame(mainLoop);

The main loop is shown as the following diagram:

Main Loop

There are two concepts must be explained in detail first.

What is a Scene?

A scene is a game world, or 1 scene=1 level. The scene contains all gameObjects, for example, characters, weapons, particle systems, sounds, enemies. In QICI Editor, all entities (or gameObjects, or nodes) in the scene are listed in the Hierarchy views:

Scene

How to Update a Scene?

QICI calls scene’s nodes recursively. The pseudo-code looks like this:

foreach (nodes) {
    node.update();
    foreach (node’s components) {
        component.update();
    }
}

Preupdate and Postupdate is the same to Update a scene. Let’s get to an example to understand it.

Example

  • Create a empty project ‘MainLoop’ first.
  • New a javascript file ‘Test.js’ and type codes:
var Test = qc.defineBehaviour('qc.demo.Test', qc.Behaviour, function() {
    console.log('Init');
}, {});

Test.prototype.preUpdate = function() {
    console.log(this.gameObject.name, 'call preUpdate');
};

Test.prototype.update = function() {
    console.log(this.gameObject.name, 'call update');
};

Test.prototype.postUpdate = function() {
    console.log(this.gameObject.name, 'call postUpdate');
};
  • Create two empty nodes in the scene, and rename them to ‘node1’ and ‘node2’.
  • Link ‘Test.js’ to node1 and node2.
  • Run the project, and see output in the console view:
    Call

What happened inside the engine?

There are 2 nodes in the scenes: node1 and node2. The main loop visits them and calls their components’ function.

Call

The main loop calls all nodes’ preUpdate function first, then update function, and postUpdate function last. Most of the time, we will put our codes in the update function. When the mouse is clicked (or touch), these function will be called by main loop just like update function: onDown, onClick and onUp (Explain later chapter).

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

青瓷引擎 - 游戏主循环

青瓷引擎游戏主循环的实现。

青瓷引擎-面向组件式编程

什么是面向组件式编程?有什么好处?青瓷引擎为什么使用组件式结构?

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

青瓷引擎-Hello World!

通过创建一个HelloWorld工程,说明青瓷引擎的加载流程与工作方式

游戏主循环

摘自:http://my.oschina.net/u/243648/blog/71814 引言 游戏主循环是每个游戏的心跳,输送着整个游戏需要的养分。不幸的是没有任何一篇好的文章来指导...

游戏主循环

原博客地址:http://www.cppblog.com/Charlib/archive/2009/08/25/gameLoop.html   游戏主循环 引言 游戏主循环是每个游戏的心跳,输...

JS开发HTML5游戏《神奇的六边形》(四)

近期出现一款魔性的消除类HTML5游戏《神奇的六边形》,今天我们一起来看看如何通过开源免费的青瓷引擎(www.zuoyouxi.com)来实现这款游戏。 (点击图片可进入游戏体验) ...

开源免费的HTML5游戏引擎——青瓷引擎(QICI Engine) 1.0正式版发布了!

青瓷引擎是一套开源免费的JavaScript游戏引擎类库,其基于开源免费的Phaser游戏引擎,并提供了一套完全基于浏览器的跨平台集成式HTML5游戏编辑器。

JS开发HTML5游戏《神奇的六边形》(一)

近期出现一款魔性的消除类HTML5游戏《神奇的六边形》,今天我们一起来看看如何通过开源免费的青瓷引擎(www.zuoyouxi.com)来实现这款游戏。

cocos2d-x 游戏引擎的处理流程 MainLoop主循环(下)

http://blog.163.com/shiyanchunyee@126/blog/static/12667909820131191434362/ 3.Android Android平台的游戏...

游戏引擎理论与实现系列02-主控线程和循环

介绍简单版本的游戏主控流程
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)