Create Your First Entity

An entity, as it relates to the Entity-Component architecture, is any game object within your game. This can include things like the player, the monsters, the collectibles, and even the projectiles—but typically, not something like the background.

With GameplayKit, you can either use a generic GKEntity object or create a new GKEntity subclass. For Val’s Revenge, you’ll create a new subclass.

In keeping with your new project organization, create a new group (N) above the Components group and name this new group Entities.

Although the Player class isn’t technically an entity (it’s an SKSpriteNode), drag the Player.swift file into the newly created Entities group. By placing the Player.swift file into the Entities group, you are keeping up with your overall project organization.

Inside the Entities group, create a new file (N) using the iOS Swift File template. Name this new file MonsterEntity.swift and replace its contents with the following code:

 import​ ​SpriteKit
 import​ ​GameplayKit
 
 class​ ​MonsterEntity​: ​GKEntity​ {
 
 init​(monsterType: ​String​) {
 super​.​init​()
 
  }
 
 required​ ​init​?(coder: ​NSCoder​) {
 super​.​init​(coder:coder)
  }
 }

This code creates a new MonsterEntity class with an initialization method that takes a single string input parameter with a name of monsterType. You’ll use this parameter to specify the monster type, which as of now, is either a skeleton or a goblin.

Because a GameplayKit entity is not a SpriteKit sprite node, and therefore does not include a visual representation of itself, you need to create a new component to render the sprite.

Inside the Components group, create another new file (N) using the iOS Swift File template. Name this new file RenderComponent.swift and replace its contents with the following code:

 import​ ​SpriteKit
 import​ ​GameplayKit
 
 class​ ​RenderComponent​: ​GKComponent​ {
 
 lazy​ ​var​ spriteNode: ​SKSpriteNode​? = {
  entity?.​component​(ofType: ​GKSKNodeComponent​.​self​)?.node ​as?​ ​SKSpriteNode
  }()
 
 init​(node: ​SKSpriteNode​) {
 super​.​init​()
  spriteNode = node
  }
 
 init​(imageNamed: ​String​, scale: ​CGFloat​) {
 super​.​init​()
 
  spriteNode = ​SKSpriteNode​(imageNamed: imageNamed)
  spriteNode?.​setScale​(scale)
  }
 override​ ​func​ ​didAddToEntity​() {
  spriteNode?.entity = entity
  }
 
 required​ ​init​?(coder: ​NSCoder​) {
 super​.​init​(coder:coder)
  }
 
 override​ ​class​ ​var​ supportsSecureCoding: ​Bool​ {
 true
  }
 }

Once again, you’re grabbing the entity’s node, except this time, you’re returning that node as an SKSpriteNode object and storing it in the spriteNode property.

Now that you have a monster entity and a way to render a sprite for it, it’s time to create a monster generator. (Because what good is a dungeon if it’s not filled with monsters, right?)

For your monster generator, you’ll build another component.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset