Game Layout: Inheritance-Based & Component-Based

Introduce

This article is an abstract of a chapter of iOS Swift Game Development Cookbook. The main content is about two ways to lay out the architecture of the game:

  • Inheritance-Based
  • Component-Based

It is important to find out a best layout for your own game project; because an appropriate architecture will make your works more reasonable and flexible.

Before determine which way should be used, it is better to insight into both Inheritance-Based and Component-Based Game Layout.

Inheritance-Based

In this mode, a class of game objects is needed. All the others classes of your game objects should inherit this base class and add extra functionality according to their needs.

example:

First, define a class called GameObject:

class GameObject:NSObject {
    func update(deltaTime: Float){
        //do some thing
    }
}

When you need to create a new kind of game object, just create a subclass of GameObject class, which inherits all of the behavior of its superclass and can be customized:

class Monster: GameObject {

    var hitPoints : Int = 10 // how much health we have
    var target : GameObject? // the game object we're attacking
    
    override func update(deltaTime: Float) {

        super.update(deltaTime)

        // Do some monster-specific updating
        }
} 
Inheritance-Based Layout.png

Component-Based

This is a mode that any game object class contains a series of component classes for implementing its features and functionalities.

example:

First, define a Component class. This class represents components that are attached to game objects. And it is a very simple class that, at least initially, just defines a update:method and an instance of GameObject.

class Component: NSObject {

    // The game object this component is attached to
    var gameObject : GameObject?

    func update(deltaTime : Float) {
        // Update this component
    }
}

Next, define a GameObject class, which represents game objects in your project:

class GameObject: NSObject {

    // The collection of Component objects attached to us
    var components : [Component] = []

    //and some methods to manipulate the components list
    func someMethods()
    
    //update this object by updating all components
    func update(deltaTime:Float){
        for component in self.components {
            component.update(deltaTime)
        }
    }

Advantages

Inheritance-Based

“The advantage of a hierarchy-based layout is that each object is able to stand alone: if you have a Dragon object, you know that all of its behavior is contained inside that single object, and it doesn’t rely on other objects to work.

Component-Based

“A component-based layout means you can be more flexible with your design and not worry about inheritance issues. For example, if you’ve got a bunch of monsters, and you want one specific monster to have some new behavior (such as, say, exploding every five seconds), you just write a new component and add it to that monster. If you later decide that you want other monsters to also have that behavior, you can add that behavior to them, too.”

Disadvantages

Inheritance-Based

"The downside is that you can often end up with a very deep hierarchy of different game object types, which can be tricky to keep in your head as you program.”

Component-Based

“The main problem with component-based architectures is that it’s more laborious to create multiple copies of an object, because you have to create and add the same set of components every time you want a new copy.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末怀读,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌措嵌,老刑警劉巖囤踩,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件债蓝,死亡現(xiàn)場離奇詭異,居然都是意外死亡京髓,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進(jìn)店門商架,熙熙樓的掌柜王于貴愁眉苦臉地迎上來堰怨,“玉大人,你說我怎么就攤上這事蛇摸”竿迹” “怎么了?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵皇型,是天一觀的道長诬烹。 經(jīng)常有香客問我,道長弃鸦,這世上最難降的妖魔是什么绞吁? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮唬格,結(jié)果婚禮上家破,老公的妹妹穿的比我還像新娘。我一直安慰自己购岗,他們只是感情好汰聋,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著喊积,像睡著了一般烹困。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上乾吻,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天髓梅,我揣著相機(jī)與錄音,去河邊找鬼绎签。 笑死枯饿,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的诡必。 我是一名探鬼主播奢方,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蟋字?” 一聲冷哼從身側(cè)響起稿蹲,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎愉老,沒想到半個(gè)月后场绿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡嫉入,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年焰盗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片咒林。...
    茶點(diǎn)故事閱讀 38,137評論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡熬拒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出垫竞,到底是詐尸還是另有隱情澎粟,我是刑警寧澤,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布欢瞪,位于F島的核電站活烙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏遣鼓。R本人自食惡果不足惜啸盏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望骑祟。 院中可真熱鬧回懦,春花似錦、人聲如沸次企。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽缸棵。三九已至舟茶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間堵第,已是汗流浹背稚晚。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留型诚,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓鸳劳,卻偏偏與公主長得像狰贯,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容