cocos2d里面如何实现MVC(完)

    今天我们讨论的主题是Model-View-Controller (MVC)设计模式,以及如何在cocos2d里面实现它。来自波兰的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》这篇文章来加深对实体组件系统的理解。

     你还可以从维基百科上获得更多的信息。实际上,objc语言本身就是被设计为一种可重用的软件组件。

    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里面实现mvc》这一系统的文章到此就全部结束了,非常感谢大家的耐心阅读。mvc在cocoa开发中被广泛使用,几乎没有哪一个ios开发者不知道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

posted on 2012-03-18 21:29  子龙山人  阅读(11626)  评论(19编辑  收藏  举报