做者:Wei Ciao Jheng php
在成爲iOS開發者之前,我是一家制做MMO的公司的遊戲程序員。我最擅長也最喜歡的編程語言是C++和Lua。做爲遊戲開發者,我曾經深刻挖掘Lua的潛力,並用它製做了一個遊戲框架。 程序員
如下是在個人遊戲應用中使用到的三種編程語言: 編程
C++:可跨平臺使用的抽象框架。 app
Objective-C:處理針對iOS平臺的細節和OpenGL ES執行。 框架
Lua: 數據描述。用戶界面。遊戲邏輯等等。 less
使用Lua,我能夠在組件的基礎上輕易地描述出遊戲對象。在上圖的例子中,遊戲對象Player是由3個組件構成的,即: ui
Transform: 負責遊戲世界中的轉化、旋轉和縮放。 spa
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)