Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 879
Layon Tavares Avatar asked Mar 06 '16 05:03

Layon Tavares


People also ask

What is entity in games?

A game entity is an object that interacts with the game and responds to player input or other entities. Other terms for entity are actor, or game object.

What is Gameplay kit?

GameplayKit is an object-oriented framework that provides foundational tools and technologies for building games. GameplayKit includes tools for designing games with functional, reusable architecture, as well as technologies for building and enhancing gameplay features such as character movement and opponent behavior.

What is SpriteKit Swift?

Overview. SpriteKit is a general-purpose framework for drawing shapes, particles, text, images, and video in two dimensions. It leverages Metal to achieve high-performance rendering, while offering a simple programming interface to make it easy to create games and other graphics-intensive apps.


1 Answers

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.

like image 191
crashoverride777 Avatar answered Oct 31 '22 17:10

crashoverride777