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
        }
} 
Game Layout: Inheritance-Based & Component-Based_第1张图片
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.

你可能感兴趣的:(Game Layout: Inheritance-Based & Component-Based)