作者:Wei Ciao Jheng
在成为iOS开发者以前,我是一家制作MMO的公司的游戏程序员。我最擅长也最喜欢的编程语言是C++和Lua。作为游戏开发者,我曾经深入挖掘Lua的潜力,并用它制作了一个游戏框架。
以下是在我的游戏应用中使用到的三种编程语言:
C++:可跨平台使用的抽象框架。
Objective-C:处理针对iOS平台的细节和OpenGL ES执行。
Lua: 数据描述。用户界面。游戏逻辑等等。
使用Lua,我可以在组件的基础上轻易地描述出游戏对象。在上图的例子中,游戏对象Player是由3个组件构成的,即:
Transform: 负责游戏世界中的转化、旋转和缩放。
Quad: 引入2D平面的一个简单的四元组,代表这个对象。
BoundingBox: 盒状的碰撞检测。
一个对象可以由若干组件组成,然而,并不是所有组件都需要升级和/或执行。不像传统的基于对象的架构,在基于组件的架构中,我们分别升级和执行游戏对象。
我们所谓的游戏对象,其实不过是一个容器。游戏对象不必与特定的游戏组件如何行使功能有关。它只是以一种抽象的方式容纳和管理这些特定的游戏组件,并且所有任务都是赋给组件。游戏组件才具有效力。
例如,当游戏对象包含Interpolator这个游戏组件时,我们可以在初值和终值之间插入变量,然后将其赋给图象通道或按比例缩放。如果运用得当,Interpolator会让你的对象和界面看起来更活泼更生动。
至于内存管理,与Lua无关。所有动态内存分配的对象都是用C++写的,然后引用到Lua。Lua只得到指示器。Lua可以执行复杂的内存管理任务,但将C++和Lua清楚地分开,可以使内存管理的出错率降低。
Lua是一种轻型、强大的和跨平台的语言。用Lua编程可以节省大量编译资源文件的时间,并且编写代码的过程中有更高的转换率。转换率越高,可以迭代的次数就越多。在游戏开过程中,更多迭代有助于提高工作效率和提升游戏品质。(本文为游戏邦/gamerboom.com编译,拒绝任何不保留版权的转载,如需转载请联系:游戏邦)
Designing a Component-based Architecture in Lua for Game Apps
by Wei Ciao Jheng
Hi there, I’m Wei Ciao. Before becoming an iOS developer, I was a game programmer working for a company making MMO genre games. My primary skills and fondness in programming languages are C++ and Lua. As a game developer, I’ve dug deeply in Lua and developed a game framework based on it.
Here is the combination of programming languages utilized in my game apps:
C++: General abstract framework can be used in cross-platforms.
Objective-C: Handling iOS platform-specific details and OpenGL ES implementation.
Lua: Data description. User interfaces. Game logic. Anything else.
With Lua, I can easily describe a game object simply based on components. In the example above, the game object, Player, is composed of 3 components, which are:
Transform: In charge of translation, rotation and scaling in game world.
Quad: A simple quad drawn in 2D plane to represent the object.
BoundingBox: Collision detecting in box-shaped.
One object can comprise several components, however, not every component needs to be updated and/or rendered. Unlike traditional object-oriented architecture, in component-based architecture we can update and render the game objects accordingly.
What we called a game object, actually is nothing more than a container. Game object doesn’t need to be involved in how specific game components function. It merely contains and manages them in an abstract way and all tasks are assigned to its components. Powers are delegated to the game components.
For example, when a game object consists of Interpolator game component, we can interpolate a variable between the starting value and the ending value, and then apply it to image alpha or scaling. Interpolator will make your objects and interfaces look bouncing and vivid if used properly.
As for memory management, Lua takes no responsibility. All dynamically memory-allocated objects are created in C++ and passed-by-reference for Lua to utilize. Lua only gets pointers. Lua could perform complex memory management tasks, but a clear separation between C++ and Lua helps making memory management less error-prone.
Lua is lightweight, robust and cross-platform. Programming in Lua can save you a lot of compile-time for source files, and the coding process has a quick turnover rate. The higher turnover rate is, the more iterations you can get. In the process of game development, more iterations help increasing work efficiency and improving game quality.
If you are wondering how it works in game apps, you may check out my latest game Bonnie’s Brunch 2 and upcoming Jade Ninja.
Live well, happy coding and make great games!(source:gamasutra)