Layon Tavares
Layon Tavares

Reputation: 99

How can i model Entities and Components in a effective way in GameplayKit/SpriteKit

Starting with GameplayKit here, I'm modelling an endless runner where the player can swap between characters with a click on screen. The character first can attack in short range (component), the second jumps (component), and the third shoots (component).

I would like to know what would be the best way for modelling between those two:

  1. Create a class Player that has 3 different characters entities (array), which one with it's components

  2. Create an entity Player that has 3 different characters components and add all movement and actions components (shoot/jumping...)

It's a real big project so i'm worried about the the best way to keep the code mantainable and readable for a long life-time.

Ps:. The SpriteComponent will be the component responsible visual representation on .sks for all entities.

Ps2:. Examples in swift if possible.

Thank you for your time.

Upvotes: 5

Views: 1427

Answers (1)

crashoverride777
crashoverride777

Reputation: 10674

It depends on what you do and what each character does besides using the components. Using gameplayKit I would probably prefer to create entities class rather than just classes.

Did you consider just using 1 player entity and than when you change character you just remove/add components for that character.

Say you change from the attacking one to the jumping one, you could remove the attack component

 player.removeComponent....

and add the jumping component

 let jumpingComponent = ...
 player.addComponent(jumpingComponent)

In general you want to make sure your components are as flexible and generic as possible. For example your spriteComponent should take an image string/texture in its init method so you can add the same component to each entity but use a different image.

 class SpriteComponent: GKComponent {

     let node: SKSpriteNode

     init(texture: SKTexture) {
          node = SKSpriteNode(texture: texture, color: SKColor.clearColor(), size: texture.size())
     }
}

Now you can add the same component to each character while using a different image

    let character1Texture = SKTexture(imageNamed: "Character 1 image"
    let spriteComponent = SpriteComponent(texture: character1Texture)
    addComponent(spriteComponent)

Same goes for your animations and other components.

The way I treat it is like this.

Say I have a game where I have 10 types of enemies that are more or less the same. I would probably try to just have 1 enemy entity class will all the components used by all 10 enemies. Than I would add the relevant components that make each unique dynamically or based on a enumType that I would pass in the init method.

On the other hand say I have 3 main characters that all do very unique things than maybe its easier and cleaner to just create 3 entity classes. Also dont forget about subclassing, so you can create 1 entity class with all the shared components and than subclass the other 2 entities.

Apple does that in DemoBots, where they have a TaskBot entity and than a FlyingBoy entity and a GroundBot entity that are both subclasses of TaskBot.

Its really up to you.

Upvotes: 5

Related Questions