5

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.

1 Answers1

5

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.

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • That might be, but the 3 characters have different sprites and animation for each action (like a party with a wizzard, knight and bowman), in case of optimizing the memory and processing management would be bad to remove an visual component and add another visual with different parameters for init **each time** the player clicks a button? Or just one visual component with 3 methods to change between the characters and add another in this same component in case we decide to go for 4+, would be a good solution? Thanks for your answer. Problaby on the answer i'll pick this as the solution =D – Layon Tavares Mar 06 '16 at 15:33
  • 1
    It's hard to pinpoint what's the best way, quite a few possibilities. You could use 3 Entities. Your Sprite component should pass an image name string in its init method, this way you can give the 3 entities the same Sprite component but with a different textures. Than just create the animation components etc for the entities. You could again just make 1 entity and than create 3 instances im your GameScene with different components. I prefer to use entity subclass that have very little code apart from the components so maybe creating 3 entities is the way to go – crashoverride777 Mar 06 '16 at 15:45