1. 程式人生 > >Cocos2d-x裡面如何實現MVC(完)

Cocos2d-x裡面如何實現MVC(完)

    今天我們討論的主題是Model-View-Controller (MVC)設計模式,以及如何在cocos2d-x裡面實現它。來自波蘭的Bartek Wilczyński寫了一系列的文章來介紹這個模式,同時說明了為什麼要使用mvc,以及如何在cocos2d裡面使用mvc。

    這個波蘭人寫的文章已經被我全部翻譯過來了,請點選傳送門檢視。

    當我在讀他寫的這些文章的時候,我記得Jeremy Flores在github上面有一個cocos2d裡面實現mvc的版本庫。他把它取名為Cocos2D-MNC

,全名是Model-Node-Controller。並且程式碼是開源的,MIT許可。

    這個MVC模式和遊戲實體元件系統差不多,我在這篇文章裡面就有介紹過了。對於這兩個系統來說,它的思想都是統一的,那就是不要繼承CCSprite並把遊戲logic全部塞到sprite裡面去。CCSprite應該只負責渲染顯示。而且有時候,你可能需要建立很多sprite,我們最好是建立一個CCNode類,然後裡面聚合許多sprites。這樣CCNode成為了Controller,控制view。當view(比如sprite,effect,gL drawings等等)在螢幕上面移動的時候,controller結點會輪詢所有它包含的結點來查詢一些遊戲相關的狀態資訊,並且做一些遊戲邏輯,然後反過來再更新view。

    對於小遊戲來說,mvc模式確實可以執行地很好。它比起直接繼承CCSprite,並把一大堆處理邏輯放到CCSprite裡面要強多了。如果你發現,你還是不停地繼承ccsprite,然後把一大堆處理邏輯塞到一個ccsprite的子類裡面,那麼你就應該考慮一下mvc設計模式了。

     當我們在cocos2d論壇裡面提到“是否繼承CCSprite還是使用一些model類來構建你的遊戲物件結構?”這樣的問題的時候,我還是要再強調一點,多用組合,少用繼承!如果一味地使用繼承,那麼當遊戲世界裡面的物件種類變多,功能變複雜以後,會導致整個繼承樹“頭重腳輕”,嚴重破壞了良好的面向物件設計原則---我們設計的類層次結構應該是扁平結構的,而不是一個頭很大的樹。

    那麼,我們需要使用怎樣的架構來處理遊戲裡面的物件呢?答案就是使用組合。現在已經有一些非常好的引擎,比如TorqueXPushButton Engine、Unity3D等,它們都是基於組合的實體元件系統。

    你可以從PushButton的文件裡面得到有關實體元件系統的介紹。同時,可以讀一讀《Components in TorqueX and what the differences are to XNA Game Components》這篇文章來加深對實體元件系統的理解。

    Scott Bilas在2002的GDC大會上提出了一種基於資料驅動的遊戲物件系統,同時Dungeon Siege使用了這個新理念,它指出了為什麼繼承對於遊戲開發者來說非常不好,還說明了基於物件組合的元件系統的優點。事實上,在2002年,我開始與SpellForce一起工作的時候,我們已經有一個元件系統了,我們把它叫做Aspects、Abilities和Spells。它可以幫助我們把所有的遊戲資料都儲存到資料庫裡面,程式設計師只需要寫一些泛型程式碼來統一處理這些資料就行了。

    在2009年的GDC大會上面,Radical Entertainment’s Marcin Chady也做了一個類似的ppt,大家可以點此檢視

    Mick West還寫了一篇文章,《重構遊戲實體為遊戲元件》,在這篇文章裡面,它很好地描述了,為什麼要更改以前的繼承模型,轉而投向元件系統的懷抱。

    還有一些更高階的讀物,比如一些paper《Dynamic Game Object Component System for Mutable Behavior Characters》 ,它基於finite state machines討論了元件系統,而且引入了基於規則的NPC行為系統。

   在Game Architect 部落格裡面把它稱之為Anatomy of Despair,並且指出了基於繼承的類設計的一些缺點,同時展示了使用組合如何來解決這些問題。

  後記:《如何在cocos2d-x裡面實現mvc》這一系統的文章到此就全部結束了,非常感謝大家的耐心閱讀。mvc在各種開發中被廣泛使用,幾乎沒有哪一個開發者不知道mvc。但是,mvc不是銀彈,沒有銀彈!我個人覺得基於元件的實體系統和fsm更適合現在遊戲架構。由於本人水平和經驗有限,故只能翻譯這麼多。這篇博文提到了其它許多沒有翻譯過的文章,建議大家都讀一讀。我也不打算翻譯了。有興趣的同學可以翻譯出來,為開發者社群貢獻一點力量。

   全劇終!

參考文獻:

Game Programming Patterns / Behaving Patterns / Component

How to implement MVC pattern in cocos2d game | XPerienced Blog

T=Machine » Entity System 1: Java/Android

T=Machine » Entity Systems are the future of MMOG development – Part 1

How to implement MVC pattern in cocos2d game–part 2 | XPerienced Blog

Games from Within| Indie iPhone game development

Game Programming Patterns

Game development |Mobile development |PODD

Cowboy Programming » Evolve Your Hierarchy

Object-Oriented Game Design| GBGames - Thoughts on Indie Game Development

Why Use MVC for Games? A Q&A Session | DeadPanic

C Coroutines for Game Entity State Management | Will's Blog

10 Reasons the Age of Finite State Machines is Over — AiGameDev.com

Subclass CCSprite vs Model Class. Best Practice? « cocos2d for iPhone

Animating a CCSprite using MVC design « cocos2d for iPhone

Animating a CCSprite using MVC design « cocos2d for iPhone

Subclassing CCNode and using CCSpriteBatchNode properly « cocos2d for iPhone

Component based entity systems « cocos2d for iPhone

How do you organize your code? « cocos2d for iPhone

Game Object Structure: Inheritance vs. Aggregation

Game Architecture Best Patterns and Practices - App Hub Forums

Finite State Machine Part 1

c++ - What are some programming design patterns that are useful in game development? - Game Development - Stack Exchange

How to write solid Pure Aggregation (composition) Game Objects in Java? - Stack Overflow

Prefer Composition over Inheritance | Learn & Master Cocos2D Game Development

Components, Draw Calls and performance…. « Big Bad Robots Indie Game Studio

To Components with Cocos2D with love… « Big Bad Robots Indie Game Studio

PushButton Engine

PBEDemos.asArtemis

Entity System FrameworkT=Machine » Entity System 1: Objective-C

architecture - Component based game engine design - Stack Overflow

Component Based Entity System Design Part 1 | Purple Pwny Games

Component-based game object systems in practice - Game Development - Stack Exchange

What is (functional) reactive programming? - Stack Overflow

Why I switched from component-based game engine architecture to functional reactive programming – Lambdor Devblog by Gerold Meisinger

Quickstarting game development in Haskell and Ubuntu – Lambdor Devblog by Gerold Meisinger

Game Coding Complete - Component System Example Code (from GPG6)

objective c - Obj-C component-based game architecture and message forwarding - Stack Overflow

mikeash.com: Friday Q&A 2009-03-27: Objective-C Message Forwarding

Articles - Component Based Objects

Unseen-Academy - Component System

Componentbased entity systems - Game Development Lab Wiki

divotkey/cogaen3-java - GitHub

Open source component-based game or engines? - Game Development - Stack Exchange

Component Based Entity System Design Part 2 | Purple Pwny Games

一個基於元件的動態物件系統 - 遊戲創意和設計 - TechWeb-遊戲社群

遊戲物件的實現 (補)_thunder54007-ChinaUnix部落格

遊戲物件的實現 - 元件工廠 - C++部落格

thelinuxlich/artemis_CSharp - GitHub

thelinuxlich/starwarrior_CSharp - GitHub

Recommended Eclipse plugins to generate UML from Java code - Stack Overflow

Game Object Structure: Inheritance vs. Aggregation

Linkvent Calendar, Day 3: MVC with Cocos2D | Learn & Master Cocos2D Game Development

Artemis Entity Framework Ported to C# | Ploobs

Downloads - cistron - A component-based programming framework targeted at game development. - Google Project Hosting

C++ Port of Artemis Entity Component System Framework (In Progress) - GameDev.net